gst_native_pipeline.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*gst_native_pipeline.c - Header file for the GstClasspathPlugin
  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 <jcl.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/wait.h>
  38. #include <unistd.h>
  39. #ifdef HAVE_FCNTL_H
  40. #include <fcntl.h>
  41. #endif /* HAVE_FCNTL_H */
  42. #if defined(HAVE_SYS_IOCTL_H)
  43. #define BSD_COMP /* Get FIONREAD on Solaris2 */
  44. #include <sys/ioctl.h>
  45. #endif
  46. #if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
  47. #include <sys/filio.h>
  48. #endif
  49. #include <gdk/gdk.h>
  50. #include <glib.h>
  51. #include <gst/gst.h>
  52. #include "cpio.h"
  53. #include "gst_peer.h"
  54. #include "gnu_javax_sound_sampled_gstreamer_lines_GstPipeline.h"
  55. #include "gst_native_pipeline.h"
  56. static jmethodID pointerConstructorMID = NULL;
  57. static jfieldID pipelineFID = NULL;
  58. static jfieldID pointerDataFID = NULL;
  59. static jfieldID nameFID = NULL;
  60. static jfieldID capacityFID = NULL;
  61. /*
  62. * Needed to compute the size of the data still available for processing in the
  63. * pipeline. We give a default here but this will be overwritten by the
  64. * detection routines.
  65. */
  66. static long GST_DETECTED_PIPE_CAPACITY = 65536;
  67. /*
  68. * Note: the Java code uses enum classes, these are not mapped into constants
  69. * by the javah tool, changes to these values should be reflected in the Java
  70. * side.
  71. */
  72. enum
  73. {
  74. PLAY,
  75. PAUSE,
  76. STOP
  77. };
  78. /*
  79. * Defined as constants in the Java code, hence mapped by javah.
  80. */
  81. enum
  82. {
  83. READ = gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_READ,
  84. WRITE = gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_WRITE
  85. };
  86. struct _GstNativePipelinePrivate
  87. {
  88. JavaVM *vm;
  89. jclass GstPipelineClass;
  90. jclass PointerClass;
  91. jobject jni_pipeline;
  92. char *name;
  93. int fd;
  94. GstElement *pipeline;
  95. };
  96. /* ************************************************************************** */
  97. /*
  98. static void gst_native_pipeline_clean (GstNativePipeline *self);*/
  99. static char *create_name (void);
  100. static void init_pointer_IDs (JNIEnv* env);
  101. static jint get_free_space (int fd);
  102. static void detect_pipe_max (void);
  103. /* ************************************************************************** */
  104. /* JNI Methods */
  105. JNIEXPORT void JNICALL
  106. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_init_1id_1cache
  107. (JNIEnv *env, jclass clazz)
  108. {
  109. pipelineFID = (*env)->GetFieldID (env, clazz, "pipeline",
  110. "Lgnu/classpath/Pointer;");
  111. nameFID = (*env)->GetFieldID (env, clazz, "name", "Ljava/lang/String;");
  112. capacityFID = (*env)->GetFieldID (env, clazz, "capacity", "J");
  113. init_pointer_IDs (env);
  114. }
  115. JNIEXPORT void JNICALL
  116. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_init_1instance
  117. (JNIEnv *env, jobject pipeline)
  118. {
  119. GstNativePipeline *_pipeline = NULL;
  120. jclass localGstPipelineClass = NULL;
  121. jclass localPointerClass = NULL;
  122. jobject _pointer = NULL;
  123. _pipeline =
  124. (GstNativePipeline *) JCL_malloc (env, sizeof (GstNativePipeline));
  125. if (_pipeline == NULL)
  126. return;
  127. _pipeline->priv = (GstNativePipelinePrivate *)
  128. JCL_malloc (env, sizeof (GstNativePipelinePrivate));
  129. if (_pipeline->priv == NULL)
  130. {
  131. JCL_free (env, _pipeline);
  132. return;
  133. }
  134. #if SIZEOF_VOID_P == 8
  135. localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
  136. #else
  137. # if SIZEOF_VOID_P == 4
  138. localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
  139. # else
  140. # error "Pointer size is not supported."
  141. # endif /* SIZEOF_VOID_P == 4 */
  142. #endif /* SIZEOF_VOID_P == 8 */
  143. localGstPipelineClass = (*env)->GetObjectClass(env, pipeline);
  144. if (localGstPipelineClass == NULL || localGstPipelineClass == NULL)
  145. {
  146. JCL_free (env, _pipeline->priv);
  147. JCL_free (env, _pipeline);
  148. JCL_ThrowException (env, "java/lang/InternalError",
  149. "Class Initialization failed.");
  150. return;
  151. }
  152. GST_DETECTED_PIPE_CAPACITY = (long) (*env)->GetLongField(env, pipeline,
  153. capacityFID);
  154. /* fill the object */
  155. (*env)->GetJavaVM(env, &_pipeline->priv->vm);
  156. _pipeline->priv->jni_pipeline = (*env)->NewGlobalRef(env, pipeline);
  157. _pipeline->priv->GstPipelineClass =
  158. (*env)->NewGlobalRef(env, localGstPipelineClass);
  159. _pipeline->priv->PointerClass = (*env)->NewGlobalRef(env, localPointerClass);
  160. _pipeline->priv->pipeline = NULL;
  161. _pointer = (*env)->GetObjectField(env, pipeline, pipelineFID);
  162. if (_pointer == NULL)
  163. {
  164. #if SIZEOF_VOID_P == 8
  165. _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
  166. pointerConstructorMID, (jlong) _pipeline);
  167. #else
  168. _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
  169. pointerConstructorMID, (jint) _pipeline);
  170. #endif
  171. }
  172. else
  173. {
  174. #if SIZEOF_VOID_P == 8
  175. (*env)->SetLongField(env, pipeline, pipelineFID, (jlong) _pipeline);
  176. #else
  177. (*env)->SetIntField(env, pipeline, pipelineFID, (jint) _pipeline);
  178. #endif
  179. }
  180. /* store back our pointer into the calling class */
  181. (*env)->SetObjectField(env, pipeline, pipelineFID, _pointer);
  182. }
  183. JNIEXPORT jboolean JNICALL
  184. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_set_1state
  185. (JNIEnv *env, jclass clazz, jobject pointer, jint state)
  186. {
  187. GstNativePipeline *jpipeline = NULL;
  188. jboolean result = JNI_FALSE;
  189. if (pointer == NULL)
  190. {
  191. JCL_ThrowException (env, "javax/sound/sampled/LineUnavailableException",
  192. "Can't change pipeline state: " \
  193. "pipeline not initialized");
  194. return result;
  195. }
  196. jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
  197. pointerDataFID);
  198. if (jpipeline == NULL)
  199. return JNI_FALSE;
  200. switch (state)
  201. {
  202. case (PLAY):
  203. gst_element_set_state(GST_ELEMENT(jpipeline->priv->pipeline),
  204. GST_STATE_PLAYING);
  205. result = JNI_TRUE;
  206. break;
  207. case (PAUSE):
  208. gst_element_set_state(GST_ELEMENT(jpipeline->priv->pipeline),
  209. GST_STATE_PAUSED);
  210. result = JNI_TRUE;
  211. break;
  212. case (STOP):
  213. #ifndef WITHOUT_FILESYSTEM
  214. /* clean the pipeline and kill named pipe */
  215. if (jpipeline->priv->name)
  216. {
  217. cpio_removeFile (jpipeline->priv->name);
  218. g_free (jpipeline->priv->name);
  219. jpipeline->priv->name = NULL;
  220. }
  221. #endif /* WITHOUT_FILESYSTEM */
  222. if (jpipeline->priv->pipeline != NULL)
  223. gst_object_unref (GST_OBJECT(jpipeline->priv->pipeline));
  224. result = JNI_TRUE;
  225. break;
  226. default:
  227. /* nothing */
  228. result = JNI_FALSE;
  229. break;
  230. }
  231. return result;
  232. }
  233. JNIEXPORT void JNICALL
  234. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_open_1native_1pipe
  235. (JNIEnv *env, jclass clazz, jobject pointer, jint mode)
  236. {
  237. GstNativePipeline *jpipeline = NULL;
  238. jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
  239. pointerDataFID);
  240. switch (mode)
  241. {
  242. case (READ):
  243. jpipeline->priv->fd =
  244. open (jpipeline->priv->name, O_RDONLY | O_NONBLOCK);
  245. break;
  246. case (WRITE):
  247. /* TODO: no-op currently */
  248. break;
  249. }
  250. }
  251. JNIEXPORT void JNICALL
  252. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_close_1native_1pipe
  253. (JNIEnv *env, jclass clazz, jobject pointer)
  254. {
  255. #ifndef WITHOUT_FILESYSTEM
  256. GstNativePipeline *jpipeline = NULL;
  257. jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
  258. pointerDataFID);
  259. /* kill the named pipe */
  260. if (jpipeline->priv->name)
  261. {
  262. cpio_removeFile (jpipeline->priv->name);
  263. g_free (jpipeline->priv->name);
  264. jpipeline->priv->name = NULL;
  265. }
  266. #endif /* WITHOUT_FILESYSTEM */
  267. }
  268. JNIEXPORT jboolean JNICALL
  269. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_create_1named_1pipe
  270. (JNIEnv *env, jobject GstPipeline, jobject pointer)
  271. {
  272. #ifndef WITHOUT_FILESYSTEM
  273. /*
  274. * We get a temp name for the named pipe, create the named pipe and then
  275. * set the relative field in the java class.
  276. */
  277. GstNativePipeline *jpipeline = NULL;
  278. jstring *name = NULL;
  279. jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
  280. pointerDataFID);
  281. if (jpipeline == NULL)
  282. return JNI_FALSE;
  283. jpipeline->priv->name = create_name ();
  284. if (jpipeline->priv->name == NULL)
  285. return JNI_FALSE;
  286. if (mkfifo (jpipeline->priv->name, 0600) < 0)
  287. {
  288. if (jpipeline->priv->name != NULL)
  289. free (jpipeline->priv->name);
  290. return JNI_FALSE;
  291. }
  292. /* now set the String field */
  293. name = (*env)->NewStringUTF(env, jpipeline->priv->name);
  294. if (name == NULL)
  295. {
  296. cpio_removeFile (jpipeline->priv->name);
  297. if (jpipeline->priv->name != NULL)
  298. free (jpipeline->priv->name);
  299. return JNI_FALSE;
  300. }
  301. (*env)->SetObjectField(env, GstPipeline, nameFID, name);
  302. return JNI_TRUE;
  303. #else /* not WITHOUT_FILESYSTEM */
  304. return JNI_FALSE;
  305. #endif /* not WITHOUT_FILESYSTEM */
  306. }
  307. JNIEXPORT jint JNICALL
  308. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_available
  309. (JNIEnv *env, jclass clazz, jobject pointer, jint mode)
  310. {
  311. jint result = -1;
  312. #ifndef WITHOUT_FILESYSTEM
  313. GstNativePipeline *jpipeline = NULL;
  314. jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
  315. pointerDataFID);
  316. if (mode == READ)
  317. {
  318. result = get_free_space (jpipeline->priv->fd);
  319. }
  320. else
  321. {
  322. # if defined (FIONREAD)
  323. if (ioctl (jpipeline->priv->fd, FIONREAD, &result) == -1)
  324. g_warning("IMPLEMENT ME: ioctl failed");
  325. # else /* not defined (FIONREAD) */
  326. g_warning("IMPLEMENT ME: !defined (FIONREAD");
  327. # endif /* defined (FIONREAD) */
  328. } /* if (mode == READ) */
  329. #endif /* not WITHOUT_FILESYSTEM */
  330. return result;
  331. }
  332. JNIEXPORT jlong JNICALL
  333. Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_detect_1pipe_1size
  334. (JNIEnv *env, jobject GstPipeline)
  335. {
  336. detect_pipe_max ();
  337. return GST_DETECTED_PIPE_CAPACITY;
  338. }
  339. /* exported library functions */
  340. /*
  341. static void gst_native_pipeline_clean (GstNativePipeline *self)
  342. {
  343. JNIEnv *env = NULL;
  344. env = gst_get_jenv (self->priv->vm);
  345. (*env)->DeleteGlobalRef (env, self->priv->jni_pipeline);
  346. (*env)->DeleteGlobalRef (env, self->priv->GstPipelineClass);
  347. (*env)->DeleteGlobalRef (env, self->priv->PointerClass);
  348. if (self->priv->pipeline != NULL)
  349. gst_object_unref (GST_OBJECT (self->priv->pipeline));
  350. if (self->priv->name)
  351. {
  352. cpio_removeFile (self->priv->name);
  353. g_free (self->priv->name);
  354. self->priv->name = NULL;
  355. }
  356. JCL_free (env, self->priv);
  357. JCL_free (env, self);
  358. }
  359. */
  360. void gst_native_pipeline_set_pipeline (GstNativePipeline *self,
  361. GstElement *pipeline)
  362. {
  363. if (self->priv->pipeline != NULL)
  364. gst_object_unref (GST_OBJECT (self->priv->pipeline));
  365. self->priv->pipeline = pipeline;
  366. }
  367. GstElement *gst_native_pipeline_get_pipeline (GstNativePipeline *self)
  368. {
  369. return self->priv->pipeline;
  370. }
  371. char *gst_native_pipeline_get_pipeline_name (GstNativePipeline *self)
  372. {
  373. return self->priv->name;
  374. }
  375. int gst_native_pipeline_get_pipeline_fd (GstNativePipeline *self)
  376. {
  377. return self->priv->fd;
  378. }
  379. /* private functions */
  380. static void init_pointer_IDs (JNIEnv* env)
  381. {
  382. jclass PointerClass = NULL;
  383. #if SIZEOF_VOID_P == 8
  384. PointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
  385. if (PointerClass != NULL)
  386. {
  387. pointerDataFID = (*env)->GetFieldID (env, PointerClass, "data", "J");
  388. pointerConstructorMID = (*env)->GetMethodID (env, PointerClass, "<init>",
  389. "(J)V");
  390. }
  391. #else
  392. # if SIZEOF_VOID_P == 4
  393. PointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
  394. if (PointerClass != NULL)
  395. {
  396. pointerDataFID = (*env)->GetFieldID(env, PointerClass, "data", "I");
  397. pointerConstructorMID = (*env)->GetMethodID(env, PointerClass,
  398. "<init>", "(I)V");
  399. }
  400. # else
  401. # error "Pointer size is not supported."
  402. # endif /* SIZEOF_VOID_P == 4 */
  403. #endif /* SIZEOF_VOID_P == 8 */
  404. }
  405. static jint get_free_space (int fd)
  406. {
  407. jint result = -1;
  408. #if defined (FIONSPACE)
  409. if (ioctl (fd, FIONSPACE, &result) == -1)
  410. {
  411. g_warning("IMPLEMENT ME: ioctl failed");
  412. }
  413. #elif defined (FIONREAD)
  414. if (ioctl (fd, FIONREAD, &result) == -1)
  415. {
  416. g_warning("IMPLEMENT ME: ioctl failed");
  417. }
  418. result = GST_DETECTED_PIPE_CAPACITY - result;
  419. #else
  420. g_warning("IMPLEMENT ME!!! - !defined (FIONSPACE), !defined (FIONREAD");
  421. #endif
  422. return result;
  423. }
  424. static char *create_name (void)
  425. {
  426. char *buffer = NULL;
  427. char *tmp = NULL;
  428. buffer = (char *) g_malloc0 (_GST_MALLOC_SIZE_);
  429. if (buffer == NULL)
  430. {
  431. /* huston, we have a problem... */
  432. return NULL;
  433. }
  434. tmp = tempnam (NULL, _GST_PIPELINE_PREFIX_);
  435. if (tmp == NULL)
  436. {
  437. g_free (buffer);
  438. return NULL;
  439. }
  440. g_snprintf (buffer, _GST_MALLOC_SIZE_, "%s%s", tmp, _GST_PIPELINE_SUFFIX_);
  441. g_free (tmp);
  442. return buffer;
  443. }
  444. static void detect_pipe_max (void)
  445. {
  446. int read_fd;
  447. int write_fd;
  448. /* can be anything! */
  449. char *character = "a";
  450. char *pipe = NULL;
  451. gboolean available = TRUE;
  452. int w = 0;
  453. long wrote = 0;
  454. pipe = create_name ();
  455. if (pipe == NULL)
  456. {
  457. g_warning ("can't create test pipe name");
  458. return;
  459. }
  460. if (mkfifo (pipe, 0600) < 0)
  461. {
  462. g_warning ("unable to create test pipe...");
  463. g_free (pipe);
  464. return;
  465. }
  466. /* open both end of the pipe */
  467. read_fd = open (pipe, O_RDONLY | O_NONBLOCK);
  468. if (read_fd < 0)
  469. {
  470. cpio_removeFile (pipe);
  471. g_free (pipe);
  472. return;
  473. }
  474. write_fd = open (pipe, O_WRONLY | O_NONBLOCK);
  475. if (write_fd < 0)
  476. {
  477. cpio_closeFile (write_fd);
  478. cpio_removeFile (pipe);
  479. g_free (pipe);
  480. return;
  481. }
  482. while (available)
  483. {
  484. w = 0;
  485. cpio_write (write_fd, character, 1, &w);
  486. if (w < 0)
  487. available = FALSE;
  488. else
  489. wrote += w;
  490. }
  491. GST_DETECTED_PIPE_CAPACITY = wrote;
  492. cpio_closeFile (write_fd);
  493. cpio_closeFile (read_fd);
  494. cpio_removeFile (pipe);
  495. g_free (pipe);
  496. }