gst_native_data_line.c 7.6 KB

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