| 1 |
774 |
jeremybenn |
/*gst_native_data_line.c - Implements the native methods of GstNativeDataLine
|
| 2 |
|
|
Copyright (C) 2007 Free Software Foundation, Inc.
|
| 3 |
|
|
|
| 4 |
|
|
This file is part of GNU Classpath.
|
| 5 |
|
|
|
| 6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
| 7 |
|
|
it under the terms of the GNU General Public License as published by
|
| 8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
| 9 |
|
|
any later version.
|
| 10 |
|
|
|
| 11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
| 12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| 14 |
|
|
General Public License for more details.
|
| 15 |
|
|
|
| 16 |
|
|
You should have received a copy of the GNU General Public License
|
| 17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
| 18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 USA.
|
| 20 |
|
|
|
| 21 |
|
|
Linking this library statically or dynamically with other modules is
|
| 22 |
|
|
making a combined work based on this library. Thus, the terms and
|
| 23 |
|
|
conditions of the GNU General Public License cover the whole
|
| 24 |
|
|
combination.
|
| 25 |
|
|
|
| 26 |
|
|
As a special exception, the copyright holders of this library give you
|
| 27 |
|
|
permission to link this library with independent modules to produce an
|
| 28 |
|
|
executable, regardless of the license terms of these independent
|
| 29 |
|
|
modules, and to copy and distribute the resulting executable under
|
| 30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
| 31 |
|
|
independent module, the terms and conditions of the license of that
|
| 32 |
|
|
module. An independent module is a module which is not derived from
|
| 33 |
|
|
or based on this library. If you modify this library, you may extend
|
| 34 |
|
|
this exception to your version of the library, but you are not
|
| 35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
| 36 |
|
|
exception statement from your version. */
|
| 37 |
|
|
|
| 38 |
|
|
#include <jni.h>
|
| 39 |
|
|
|
| 40 |
|
|
#include <gst/gst.h>
|
| 41 |
|
|
|
| 42 |
|
|
#include "jcl.h"
|
| 43 |
|
|
#include "gnu_javax_sound_sampled_gstreamer_lines_GstNativeDataLine.h"
|
| 44 |
|
|
|
| 45 |
|
|
#include "gst_peer.h"
|
| 46 |
|
|
#include "gst_classpath_src.h"
|
| 47 |
|
|
#include "gst_native_pipeline.h"
|
| 48 |
|
|
|
| 49 |
|
|
static jfieldID pointerDataFID = NULL;
|
| 50 |
|
|
|
| 51 |
|
|
/* ************************************************************************** */
|
| 52 |
|
|
|
| 53 |
|
|
static GstElement *setup_pipeline (GstNativePipeline *jpipeline, int fd);
|
| 54 |
|
|
static void
|
| 55 |
|
|
gst_newpad (GstElement *decodebin, GstPad *pad, gboolean last, gpointer data);
|
| 56 |
|
|
|
| 57 |
|
|
/* ************************************************************************** */
|
| 58 |
|
|
|
| 59 |
|
|
JNIEXPORT void JNICALL
|
| 60 |
|
|
Java_gnu_javax_sound_sampled_gstreamer_lines_GstNativeDataLine_init_1id_1cache
|
| 61 |
|
|
(JNIEnv *env __attribute__ ((unused)), jclass clazz __attribute__ ((unused)))
|
| 62 |
|
|
{
|
| 63 |
|
|
jclass pointerClass = NULL;
|
| 64 |
|
|
|
| 65 |
|
|
#if SIZEOF_VOID_P == 8
|
| 66 |
|
|
pointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
|
| 67 |
|
|
if (pointerClass != NULL)
|
| 68 |
|
|
{
|
| 69 |
|
|
pointerDataFID = (*env)->GetFieldID (env, pointerClass, "data", "J");
|
| 70 |
|
|
}
|
| 71 |
|
|
#else
|
| 72 |
|
|
# if SIZEOF_VOID_P == 4
|
| 73 |
|
|
pointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
|
| 74 |
|
|
if (pointerClass != NULL)
|
| 75 |
|
|
{
|
| 76 |
|
|
pointerDataFID = (*env)->GetFieldID(env, pointerClass, "data", "I");
|
| 77 |
|
|
}
|
| 78 |
|
|
# else
|
| 79 |
|
|
# error "Pointer size is not supported."
|
| 80 |
|
|
# endif /* SIZEOF_VOID_P == 4 */
|
| 81 |
|
|
#endif /* SIZEOF_VOID_P == 8 */
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
JNIEXPORT jboolean JNICALL
|
| 85 |
|
|
Java_gnu_javax_sound_sampled_gstreamer_lines_GstNativeDataLine_setup_1sink_1pipeline
|
| 86 |
|
|
(JNIEnv *env, jclass clazz __attribute__ ((unused)),
|
| 87 |
|
|
jobject pointer)
|
| 88 |
|
|
{
|
| 89 |
|
|
GstNativePipeline *jpipeline = NULL;
|
| 90 |
|
|
|
| 91 |
|
|
GstElement *pipeline = NULL;
|
| 92 |
|
|
GstElement *sink = NULL;
|
| 93 |
|
|
GstElement *audioconv= NULL;
|
| 94 |
|
|
GstElement *resample = NULL;
|
| 95 |
|
|
GstElement *audio = NULL;
|
| 96 |
|
|
GstElement *decodebin = NULL;
|
| 97 |
|
|
|
| 98 |
|
|
GstPad *audiopad = NULL;
|
| 99 |
|
|
|
| 100 |
|
|
gst_init (NULL, NULL);
|
| 101 |
|
|
|
| 102 |
|
|
/* get the pipeline from the pointer, then create it if needed */
|
| 103 |
|
|
jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
|
| 104 |
|
|
pointerDataFID);
|
| 105 |
|
|
if (jpipeline == NULL)
|
| 106 |
|
|
return JNI_FALSE;
|
| 107 |
|
|
|
| 108 |
|
|
pipeline = setup_pipeline (jpipeline,
|
| 109 |
|
|
gst_native_pipeline_get_pipeline_fd (jpipeline));
|
| 110 |
|
|
if (pipeline == NULL)
|
| 111 |
|
|
return JNI_FALSE;
|
| 112 |
|
|
|
| 113 |
|
|
/* add the audio sink to the pipeline */
|
| 114 |
|
|
/* TODO: hardcoded values */
|
| 115 |
|
|
sink = gst_element_factory_make ("autoaudiosink", "alsa-output");
|
| 116 |
|
|
if (sink == NULL)
|
| 117 |
|
|
{
|
| 118 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 119 |
|
|
gst_object_unref(GST_OBJECT(sink));
|
| 120 |
|
|
|
| 121 |
|
|
g_warning ("unable to create sink\n");
|
| 122 |
|
|
return JNI_FALSE;
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
audioconv = gst_element_factory_make ("audioconvert", "aconv");
|
| 126 |
|
|
if (audioconv == NULL)
|
| 127 |
|
|
{
|
| 128 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 129 |
|
|
gst_object_unref(GST_OBJECT(sink));
|
| 130 |
|
|
gst_object_unref(GST_OBJECT(decodebin));
|
| 131 |
|
|
|
| 132 |
|
|
g_warning ("unable to create audioconv\n");
|
| 133 |
|
|
return JNI_FALSE;
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
audio = gst_bin_new ("audiobin");
|
| 137 |
|
|
if (audio == NULL)
|
| 138 |
|
|
{
|
| 139 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 140 |
|
|
gst_object_unref(GST_OBJECT(sink));
|
| 141 |
|
|
gst_object_unref(GST_OBJECT(decodebin));
|
| 142 |
|
|
|
| 143 |
|
|
g_warning ("unable to create audioconv\n");
|
| 144 |
|
|
return JNI_FALSE;
|
| 145 |
|
|
}
|
| 146 |
|
|
|
| 147 |
|
|
resample = gst_element_factory_make ("audioresample", "audioresample");
|
| 148 |
|
|
if (audioconv == NULL)
|
| 149 |
|
|
{
|
| 150 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 151 |
|
|
gst_object_unref(GST_OBJECT(sink));
|
| 152 |
|
|
gst_object_unref(GST_OBJECT(decodebin));
|
| 153 |
|
|
gst_object_unref(GST_OBJECT(audio));
|
| 154 |
|
|
|
| 155 |
|
|
g_warning ("unable to create resample\n");
|
| 156 |
|
|
return JNI_FALSE;
|
| 157 |
|
|
}
|
| 158 |
|
|
|
| 159 |
|
|
audiopad = gst_element_get_pad (audioconv, "sink");
|
| 160 |
|
|
gst_bin_add_many (GST_BIN (audio), audioconv, resample, sink, NULL);
|
| 161 |
|
|
gst_element_link (audioconv, sink);
|
| 162 |
|
|
|
| 163 |
|
|
gst_element_add_pad (audio, gst_ghost_pad_new ("sink", audiopad));
|
| 164 |
|
|
|
| 165 |
|
|
gst_object_unref (audiopad);
|
| 166 |
|
|
gst_bin_add (GST_BIN (pipeline), audio);
|
| 167 |
|
|
|
| 168 |
|
|
decodebin = gst_bin_get_by_name (GST_BIN (pipeline), "decodebin");
|
| 169 |
|
|
g_signal_connect (decodebin, "new-decoded-pad", G_CALLBACK (gst_newpad),
|
| 170 |
|
|
audio);
|
| 171 |
|
|
|
| 172 |
|
|
gst_native_pipeline_set_pipeline (jpipeline, pipeline);
|
| 173 |
|
|
|
| 174 |
|
|
return JNI_TRUE;
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
/* ************************************************************************** */
|
| 178 |
|
|
|
| 179 |
|
|
static GstElement *setup_pipeline (GstNativePipeline *jpipeline, int fd)
|
| 180 |
|
|
{
|
| 181 |
|
|
GstElement *decodebin = NULL;
|
| 182 |
|
|
GstElement *source = NULL;
|
| 183 |
|
|
|
| 184 |
|
|
GstElement *pipeline = NULL;
|
| 185 |
|
|
|
| 186 |
|
|
if (fd < 0)
|
| 187 |
|
|
return NULL;
|
| 188 |
|
|
|
| 189 |
|
|
pipeline = gst_pipeline_new ("java sound pipeline");
|
| 190 |
|
|
if (pipeline == NULL)
|
| 191 |
|
|
return NULL;
|
| 192 |
|
|
|
| 193 |
|
|
decodebin = gst_element_factory_make ("decodebin", "decodebin");
|
| 194 |
|
|
if (decodebin == NULL)
|
| 195 |
|
|
{
|
| 196 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 197 |
|
|
gst_object_unref(GST_OBJECT(source));
|
| 198 |
|
|
|
| 199 |
|
|
g_warning ("unable to create decodebin\n");
|
| 200 |
|
|
return NULL;
|
| 201 |
|
|
}
|
| 202 |
|
|
|
| 203 |
|
|
source = gst_element_factory_make ("fdsrc", "source");
|
| 204 |
|
|
if (source == NULL)
|
| 205 |
|
|
{
|
| 206 |
|
|
gst_object_unref(GST_OBJECT(pipeline));
|
| 207 |
|
|
gst_object_unref(GST_OBJECT(source));
|
| 208 |
|
|
gst_object_unref(GST_OBJECT(decodebin));
|
| 209 |
|
|
|
| 210 |
|
|
g_warning ("unable to create a source");
|
| 211 |
|
|
return JNI_FALSE;
|
| 212 |
|
|
}
|
| 213 |
|
|
g_object_set (G_OBJECT (source), "fd", fd, NULL);
|
| 214 |
|
|
|
| 215 |
|
|
gst_bin_add_many (GST_BIN (pipeline), source, decodebin, NULL);
|
| 216 |
|
|
gst_element_link (source, decodebin);
|
| 217 |
|
|
|
| 218 |
|
|
return pipeline;
|
| 219 |
|
|
}
|
| 220 |
|
|
|
| 221 |
|
|
static void
|
| 222 |
|
|
gst_newpad (GstElement *decodebin, GstPad *pad, gboolean last, gpointer data)
|
| 223 |
|
|
{
|
| 224 |
|
|
GstCaps *caps;
|
| 225 |
|
|
GstStructure *str;
|
| 226 |
|
|
GstPad *audiopad;
|
| 227 |
|
|
|
| 228 |
|
|
GstElement *audio = (GstElement *) data;
|
| 229 |
|
|
|
| 230 |
|
|
/* only link once */
|
| 231 |
|
|
audiopad = gst_element_get_pad (audio, "sink");
|
| 232 |
|
|
if (GST_PAD_IS_LINKED (audiopad))
|
| 233 |
|
|
{
|
| 234 |
|
|
g_object_unref (audiopad);
|
| 235 |
|
|
return;
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
/* check media type */
|
| 239 |
|
|
caps = gst_pad_get_caps (pad);
|
| 240 |
|
|
str = gst_caps_get_structure (caps, 0);
|
| 241 |
|
|
if (!g_strrstr (gst_structure_get_name (str), "audio"))
|
| 242 |
|
|
{
|
| 243 |
|
|
gst_caps_unref (caps);
|
| 244 |
|
|
gst_object_unref (audiopad);
|
| 245 |
|
|
return;
|
| 246 |
|
|
}
|
| 247 |
|
|
gst_caps_unref (caps);
|
| 248 |
|
|
|
| 249 |
|
|
/* link'n'play */
|
| 250 |
|
|
gst_pad_link (pad, audiopad);
|
| 251 |
|
|
}
|