summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/native/jni/gstreamer-peer/gst_native_pipeline.c
blob: 3e32568974676c6a5328f57285d0ba32a6cb846b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*gst_native_pipeline.c - Header file for the GstClasspathPlugin
 Copyright (C) 2007 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

#include <jni.h>
#include <jcl.h>

#include <string.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include <unistd.h>

#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */

#if defined(HAVE_SYS_IOCTL_H)
#define BSD_COMP /* Get FIONREAD on Solaris2 */
#include <sys/ioctl.h>
#endif
#if defined(HAVE_SYS_FILIO_H) /* Get FIONREAD on Solaris 2.5 */
#include <sys/filio.h>
#endif

#include <gdk/gdk.h>
#include <glib.h>

#include <gst/gst.h>

#include "cpio.h"
#include "gst_peer.h"

#include "gnu_javax_sound_sampled_gstreamer_lines_GstPipeline.h"
#include "gst_native_pipeline.h"

static jmethodID pointerConstructorMID = NULL;

static jfieldID pipelineFID = NULL;
static jfieldID pointerDataFID = NULL;
static jfieldID nameFID = NULL;
static jfieldID capacityFID = NULL;

/*
 * Needed to compute the size of the data still available for processing in the
 * pipeline. We give a default here but this will be overwritten by the
 * detection routines.
 */
static long GST_DETECTED_PIPE_CAPACITY = 65536;

/*
 * Note: the Java code uses enum classes, these are not mapped into constants
 * by the javah tool, changes to these values should be reflected in the Java
 * side.
 */
enum
{
  PLAY,
  PAUSE,
  STOP
};

/*
 * Defined as constants in the Java code, hence mapped by javah.
 */
enum
{
  READ = gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_READ,
  WRITE = gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_WRITE
};

struct _GstNativePipelinePrivate
{
  JavaVM *vm;
  jclass GstPipelineClass;
  jclass PointerClass;
  
  jobject jni_pipeline;

  char *name;
  int fd;
 
  GstElement *pipeline;
};

/* ************************************************************************** */
/*
static void gst_native_pipeline_clean (GstNativePipeline *self);*/
static char *create_name (void);
static void init_pointer_IDs (JNIEnv* env);
static jint get_free_space (int fd);
static void detect_pipe_max (void);

/* ************************************************************************** */

/* JNI Methods */

JNIEXPORT void JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_init_1id_1cache
  (JNIEnv *env, jclass clazz)
{
  pipelineFID = (*env)->GetFieldID (env, clazz, "pipeline",
                                    "Lgnu/classpath/Pointer;");
  nameFID = (*env)->GetFieldID (env, clazz, "name", "Ljava/lang/String;");
  capacityFID = (*env)->GetFieldID (env, clazz, "capacity", "J");

  init_pointer_IDs (env);
}

JNIEXPORT void JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_init_1instance
  (JNIEnv *env, jobject pipeline)
{
  GstNativePipeline *_pipeline = NULL;
  
  jclass localGstPipelineClass = NULL;
  jclass localPointerClass = NULL;
  jobject _pointer = NULL;
  
  _pipeline =
    (GstNativePipeline *) JCL_malloc (env, sizeof (GstNativePipeline));
  if (_pipeline == NULL)
    return;
  
  _pipeline->priv = (GstNativePipelinePrivate *)
    JCL_malloc (env, sizeof (GstNativePipelinePrivate));
  if (_pipeline->priv == NULL)
    {
      JCL_free (env, _pipeline);
      return;
    }
  
#if SIZEOF_VOID_P == 8
  localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
#else
# if SIZEOF_VOID_P == 4
  localPointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32");
# else
#   error "Pointer size is not supported."
# endif /* SIZEOF_VOID_P == 4 */
#endif /* SIZEOF_VOID_P == 8 */

  localGstPipelineClass = (*env)->GetObjectClass(env, pipeline);
  if (localGstPipelineClass == NULL || localGstPipelineClass == NULL)
    {
      JCL_free (env, _pipeline->priv);
      JCL_free (env, _pipeline);
      JCL_ThrowException (env, "java/lang/InternalError",
                               "Class Initialization failed.");
      return;
    }

  GST_DETECTED_PIPE_CAPACITY = (long) (*env)->GetLongField(env, pipeline,
                                                           capacityFID);
  
  /* fill the object */
  (*env)->GetJavaVM(env, &_pipeline->priv->vm);
  _pipeline->priv->jni_pipeline = (*env)->NewGlobalRef(env, pipeline);
  _pipeline->priv->GstPipelineClass =
    (*env)->NewGlobalRef(env, localGstPipelineClass);
  _pipeline->priv->PointerClass = (*env)->NewGlobalRef(env, localPointerClass);
  _pipeline->priv->pipeline = NULL;
  
  _pointer = (*env)->GetObjectField(env, pipeline, pipelineFID);
  
  if (_pointer == NULL)
    {
#if SIZEOF_VOID_P == 8
      _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
                                   pointerConstructorMID, (jlong) _pipeline);
#else
      _pointer = (*env)->NewObject(env, _pipeline->priv->PointerClass,
                                   pointerConstructorMID, (jint) _pipeline);
#endif
    }
  else
    {
#if SIZEOF_VOID_P == 8
      (*env)->SetLongField(env, pipeline, pipelineFID, (jlong) _pipeline);
#else
      (*env)->SetIntField(env, pipeline, pipelineFID, (jint) _pipeline);
#endif
    }
      
  /* store back our pointer into the calling class */
  (*env)->SetObjectField(env, pipeline, pipelineFID, _pointer);
}

JNIEXPORT jboolean JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_set_1state
  (JNIEnv *env, jclass clazz, jobject pointer, jint state)
{
  GstNativePipeline *jpipeline = NULL;
  jboolean result = JNI_FALSE;
  
  if (pointer == NULL)
    {
      JCL_ThrowException (env, "javax/sound/sampled/LineUnavailableException",
                               "Can't change pipeline state: " \
                               "pipeline not initialized");
      return result;
    }
    
  jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
                                                             pointerDataFID);
  if (jpipeline == NULL)
    return JNI_FALSE;
                                                         
  switch (state)
    {
      case (PLAY):
        gst_element_set_state(GST_ELEMENT(jpipeline->priv->pipeline),
                              GST_STATE_PLAYING);
        result = JNI_TRUE;
        break;
        
      case (PAUSE):
        gst_element_set_state(GST_ELEMENT(jpipeline->priv->pipeline),
                              GST_STATE_PAUSED);
        result = JNI_TRUE;
        break;
        
      case (STOP):
#ifndef WITHOUT_FILESYSTEM
        /* clean the pipeline and kill named pipe */
        if (jpipeline->priv->name)
          {
            cpio_removeFile (jpipeline->priv->name);
            g_free (jpipeline->priv->name);
            jpipeline->priv->name = NULL;
          }
#endif /* WITHOUT_FILESYSTEM */
  
        if (jpipeline->priv->pipeline != NULL)
          gst_object_unref (GST_OBJECT(jpipeline->priv->pipeline));
        result = JNI_TRUE;
        break;
        
      default:
        /* nothing */
        result = JNI_FALSE;
        break; 
    }
    
  return result;
}

JNIEXPORT void JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_open_1native_1pipe
  (JNIEnv *env, jclass clazz, jobject pointer, jint mode)
{
  GstNativePipeline *jpipeline = NULL;
  
  jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
                                                             pointerDataFID);
  switch (mode)
    {
      case (READ):
        jpipeline->priv->fd =
            open (jpipeline->priv->name, O_RDONLY | O_NONBLOCK);
        break;
      
      case (WRITE):
        /* TODO: no-op currently */
        break;
    }
}

JNIEXPORT void JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_close_1native_1pipe
  (JNIEnv *env, jclass clazz, jobject pointer)
{
#ifndef WITHOUT_FILESYSTEM
  GstNativePipeline *jpipeline = NULL;
  jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
                                                             pointerDataFID);
  /* kill the named pipe */
  if (jpipeline->priv->name)
    {
      cpio_removeFile (jpipeline->priv->name);
      g_free (jpipeline->priv->name);
      jpipeline->priv->name = NULL;
    }
#endif /* WITHOUT_FILESYSTEM */
}

JNIEXPORT jboolean JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_create_1named_1pipe
  (JNIEnv *env, jobject GstPipeline, jobject pointer)
{
#ifndef WITHOUT_FILESYSTEM
  /*
   * We get a temp name for the named pipe, create the named pipe and then
   * set the relative field in the java class.
   */
  GstNativePipeline *jpipeline = NULL;
  jstring *name = NULL;
  
  jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
                                                             pointerDataFID);
  if (jpipeline == NULL)
    return JNI_FALSE;                                                        
  
  jpipeline->priv->name = create_name ();
  if (jpipeline->priv->name == NULL)
    return JNI_FALSE;
   
  if (mkfifo (jpipeline->priv->name, 0600) < 0)
    {
      if (jpipeline->priv->name != NULL)
        free (jpipeline->priv->name);
      return JNI_FALSE;
    }
  
  /* now set the String field */
  name = (*env)->NewStringUTF(env, jpipeline->priv->name);
  if (name == NULL)
    {
      cpio_removeFile (jpipeline->priv->name);
      if (jpipeline->priv->name != NULL)
        free (jpipeline->priv->name);
      
      return JNI_FALSE;
    }
  
  (*env)->SetObjectField(env, GstPipeline, nameFID, name);
    
  return JNI_TRUE;
  
#else /* not WITHOUT_FILESYSTEM */
  return JNI_FALSE;
#endif /* not WITHOUT_FILESYSTEM */
}

JNIEXPORT jint JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_available
  (JNIEnv *env, jclass clazz, jobject pointer, jint mode)
{
  jint result = -1;
 
#ifndef WITHOUT_FILESYSTEM
  
  GstNativePipeline *jpipeline = NULL;
  jpipeline = (GstNativePipeline *) get_object_from_pointer (env, pointer,
                                                             pointerDataFID);
                                                                                                         
  if (mode == READ)
    {
      result = get_free_space (jpipeline->priv->fd);
    }
  else
    {
# if defined (FIONREAD)      
      if (ioctl (jpipeline->priv->fd, FIONREAD, &result) == -1)
        g_warning("IMPLEMENT ME: ioctl failed");
        
# else /* not defined (FIONREAD) */
      g_warning("IMPLEMENT ME: !defined (FIONREAD");
# endif /* defined (FIONREAD) */
    
    } /* if (mode == READ) */
    
#endif  /* not WITHOUT_FILESYSTEM */

  return result;
}

JNIEXPORT jlong JNICALL
Java_gnu_javax_sound_sampled_gstreamer_lines_GstPipeline_detect_1pipe_1size
  (JNIEnv *env, jobject GstPipeline)
{
  detect_pipe_max ();
  
  return GST_DETECTED_PIPE_CAPACITY;
}

/* exported library functions */
/*
static void gst_native_pipeline_clean (GstNativePipeline *self)
{
  JNIEnv *env = NULL;
  
  env = gst_get_jenv (self->priv->vm);
  
  (*env)->DeleteGlobalRef (env, self->priv->jni_pipeline);
  (*env)->DeleteGlobalRef (env, self->priv->GstPipelineClass);
  (*env)->DeleteGlobalRef (env, self->priv->PointerClass);
  
  if (self->priv->pipeline != NULL)
    gst_object_unref (GST_OBJECT (self->priv->pipeline));
  
  if (self->priv->name)
    {
      cpio_removeFile (self->priv->name);
      g_free (self->priv->name);
      self->priv->name = NULL;
    }
    
  JCL_free (env, self->priv);
  JCL_free (env, self);
}
*/
void gst_native_pipeline_set_pipeline (GstNativePipeline *self,
                                       GstElement *pipeline)
{
  if (self->priv->pipeline != NULL)
    gst_object_unref (GST_OBJECT (self->priv->pipeline));
    
  self->priv->pipeline = pipeline;
}

GstElement *gst_native_pipeline_get_pipeline (GstNativePipeline *self)
{
  return self->priv->pipeline;
}

char *gst_native_pipeline_get_pipeline_name (GstNativePipeline *self)
{
  return self->priv->name;
}

int gst_native_pipeline_get_pipeline_fd (GstNativePipeline *self)
{
  return self->priv->fd;
}

/* private functions */

static void init_pointer_IDs (JNIEnv* env)
{
  jclass PointerClass = NULL;
  
#if SIZEOF_VOID_P == 8
  PointerClass = JCL_FindClass (env, "gnu/classpath/Pointer64");
  if (PointerClass != NULL)
    {
      pointerDataFID = (*env)->GetFieldID (env, PointerClass, "data", "J");
      pointerConstructorMID = (*env)->GetMethodID (env, PointerClass, "<init>",
                                                   "(J)V");
    }
#else
# if SIZEOF_VOID_P == 4
  PointerClass = JCL_FindClass (env, "gnu/classpath/Pointer32"); 
  if (PointerClass != NULL)
    { 
      pointerDataFID = (*env)->GetFieldID(env, PointerClass, "data", "I");
      pointerConstructorMID = (*env)->GetMethodID(env, PointerClass,
                                                  "<init>", "(I)V");
    }
# else
#   error "Pointer size is not supported."
# endif /* SIZEOF_VOID_P == 4 */
#endif /* SIZEOF_VOID_P == 8 */
}

static jint get_free_space (int fd)
{
  jint result = -1;
  
#if defined (FIONSPACE)

  if (ioctl (fd, FIONSPACE, &result) == -1)
    {
      g_warning("IMPLEMENT ME: ioctl failed");
    }
    
#elif defined (FIONREAD)

  if (ioctl (fd, FIONREAD, &result) == -1)
    {
      g_warning("IMPLEMENT ME: ioctl failed");
    }

  result = GST_DETECTED_PIPE_CAPACITY - result;
  
#elif
   g_warning("IMPLEMENT ME!!! - !defined (FIONSPACE), !defined (FIONREAD");
 
#endif

  return result;
}

static char *create_name (void)
{
  char *buffer = NULL;
  char *tmp = NULL;
  
  buffer = (char *) g_malloc0 (_GST_MALLOC_SIZE_);
  if (buffer == NULL)
    {
      /* huston, we have a problem... */
      return NULL;
    }
    
  tmp = tempnam (NULL, _GST_PIPELINE_PREFIX_);
  if (tmp == NULL)
    {
      g_free (buffer);
      return NULL;
    }
  
  g_snprintf (buffer, _GST_MALLOC_SIZE_, "%s%s", tmp, _GST_PIPELINE_SUFFIX_);
  g_free (tmp);
  
  return buffer;
}

static void detect_pipe_max (void)
{
  int read_fd;
  int write_fd;
  
  /* can be anything! */
  char *character = "a";
  char *pipe = NULL;
  
  gboolean available = TRUE;
  int w = 0;
  long wrote = 0;
  
  pipe = create_name ();
  if (pipe == NULL)
    {
      g_warning ("can't create test pipe name");
      return;
    }
 
  if (mkfifo (pipe, 0600) < 0)
    {
      g_warning ("unable to create test pipe...");
      g_free (pipe);
      
      return;
    }
 
  /* open both end of the pipe */
  read_fd = open (pipe, O_RDONLY | O_NONBLOCK);
  if (read_fd < 0)
    {
      cpio_removeFile (pipe);
      g_free (pipe);
      
      return;
    }
    
  write_fd = open (pipe, O_WRONLY | O_NONBLOCK);
  if (write_fd < 0)
    {
      cpio_closeFile (write_fd);
      cpio_removeFile (pipe);
      g_free (pipe);
      
      return;
    }

  while (available)
    {
      w = 0;
          
      cpio_write (write_fd, character, 1, &w);
      if (w < 0)
        available = FALSE;
      else
        wrote += w;
    }
    
  GST_DETECTED_PIPE_CAPACITY = wrote;
    
  cpio_closeFile (write_fd);    
  cpio_closeFile (read_fd);
  cpio_removeFile (pipe);
      
  g_free (pipe);  
}
OpenPOWER on IntegriCloud