]> git.sesse.net Git - mlt/blob - src/modules/jackrack/consumer_jack.c
fix attempt to construct LADSPA plugin with no _init.
[mlt] / src / modules / jackrack / consumer_jack.c
1 /*
2  * consumer_jack.c -- a JACK audio consumer
3  * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <pthread.h>
25 #include <sys/time.h>
26 #include <unistd.h>
27 #include <jack/jack.h>
28 #include <jack/ringbuffer.h>
29
30 #define BUFFER_LEN (204800 * 6)
31
32 pthread_mutex_t g_activate_mutex = PTHREAD_MUTEX_INITIALIZER;
33
34 /** This classes definition.
35 */
36
37 typedef struct consumer_jack_s *consumer_jack;
38
39 struct consumer_jack_s
40 {
41         struct mlt_consumer_s parent;
42         jack_client_t *jack;
43         mlt_deque queue;
44         pthread_t thread;
45         int joined;
46         int running;
47         pthread_mutex_t video_mutex;
48         pthread_cond_t video_cond;
49         int playing;
50
51         pthread_cond_t refresh_cond;
52         pthread_mutex_t refresh_mutex;
53         int refresh_count;
54         int counter;
55         jack_ringbuffer_t **ringbuffers;
56         jack_port_t **ports;
57 };
58
59 /** Forward references to static functions.
60 */
61
62 static int consumer_start( mlt_consumer parent );
63 static int consumer_stop( mlt_consumer parent );
64 static int consumer_is_stopped( mlt_consumer parent );
65 static void consumer_close( mlt_consumer parent );
66 static void *consumer_thread( void * );
67 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name );
68 static int jack_process( jack_nframes_t frames, void * data );
69
70 /** Constructor
71 */
72
73 mlt_consumer consumer_jack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
74 {
75         // Create the consumer object
76         consumer_jack self = calloc( 1, sizeof( struct consumer_jack_s ) );
77
78         // If no malloc'd and consumer init ok
79         if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
80         {
81                 char name[14];
82
83                 snprintf( name, sizeof( name ), "mlt%d", getpid() );
84                 if (( self->jack = jack_client_open( name, JackNullOption, NULL ) ))
85                 {
86                         jack_set_process_callback( self->jack, jack_process, self );
87
88                         // Create the queue
89                         self->queue = mlt_deque_init( );
90
91                         // Get the parent consumer object
92                         mlt_consumer parent = &self->parent;
93
94                         // We have stuff to clean up, so override the close method
95                         parent->close = consumer_close;
96
97                         // get a handle on properties
98                         mlt_service service = MLT_CONSUMER_SERVICE( parent );
99                         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
100
101                         // This is the initialisation of the consumer
102                         pthread_mutex_init( &self->video_mutex, NULL );
103                         pthread_cond_init( &self->video_cond, NULL);
104
105                         // Default scaler (for now we'll use nearest)
106                         mlt_properties_set( properties, "rescale", "nearest" );
107                         mlt_properties_set( properties, "deinterlace_method", "onefield" );
108
109                         // Default buffer for low latency
110                         mlt_properties_set_int( properties, "buffer", 1 );
111
112                         // Set frequency from JACK
113                         mlt_properties_set_int( properties, "frequency", (int) jack_get_sample_rate( self->jack ) );
114
115                         // Set default volume
116                         mlt_properties_set_double( properties, "volume", 1.0 );
117
118                         // Ensure we don't join on a non-running object
119                         self->joined = 1;
120
121                         // Allow thread to be started/stopped
122                         parent->start = consumer_start;
123                         parent->stop = consumer_stop;
124                         parent->is_stopped = consumer_is_stopped;
125
126                         // Initialize the refresh handler
127                         pthread_cond_init( &self->refresh_cond, NULL );
128                         pthread_mutex_init( &self->refresh_mutex, NULL );
129                         mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), self, "property-changed", ( mlt_listener )consumer_refresh_cb );
130
131                         // Return the consumer produced
132                         return parent;
133                 }
134         }
135
136         // malloc or consumer init failed
137         free( self );
138
139         // Indicate failure
140         return NULL;
141 }
142
143 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
144 {
145         if ( !strcmp( name, "refresh" ) )
146         {
147                 consumer_jack self = parent->child;
148                 pthread_mutex_lock( &self->refresh_mutex );
149                 self->refresh_count = self->refresh_count <= 0 ? 1 : self->refresh_count + 1;
150                 pthread_cond_broadcast( &self->refresh_cond );
151                 pthread_mutex_unlock( &self->refresh_mutex );
152         }
153 }
154
155 static int consumer_start( mlt_consumer parent )
156 {
157         consumer_jack self = parent->child;
158
159         if ( !self->running )
160         {
161                 consumer_stop( parent );
162                 self->running = 1;
163                 self->joined = 0;
164                 pthread_create( &self->thread, NULL, consumer_thread, self );
165         }
166
167         return 0;
168 }
169
170 static int consumer_stop( mlt_consumer parent )
171 {
172         // Get the actual object
173         consumer_jack self = parent->child;
174
175         if ( self->running && !self->joined )
176         {
177                 // Kill the thread and clean up
178                 self->joined = 1;
179                 self->running = 0;
180
181                 // Unlatch the consumer thread
182                 pthread_mutex_lock( &self->refresh_mutex );
183                 pthread_cond_broadcast( &self->refresh_cond );
184                 pthread_mutex_unlock( &self->refresh_mutex );
185
186                 // Cleanup the main thread
187 #ifndef WIN32
188                 if ( self->thread )
189 #endif
190                         pthread_join( self->thread, NULL );
191
192                 // Unlatch the video thread
193                 pthread_mutex_lock( &self->video_mutex );
194                 pthread_cond_broadcast( &self->video_cond );
195                 pthread_mutex_unlock( &self->video_mutex );
196
197                 // Cleanup JACK
198                 if ( self->playing )
199                         jack_deactivate( self->jack );
200                 if ( self->ringbuffers )
201                 {
202                         int n = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "channels" );
203                         while ( n-- )
204                         {
205                                 jack_ringbuffer_free( self->ringbuffers[n] );
206                                 jack_port_unregister( self->jack, self->ports[n] );
207                         }
208                         mlt_pool_release( self->ringbuffers );
209                 }
210                 self->ringbuffers = NULL;
211                 if ( self->ports )
212                         mlt_pool_release( self->ports );
213                 self->ports = NULL;
214         }
215
216         return 0;
217 }
218
219 static int consumer_is_stopped( mlt_consumer parent )
220 {
221         consumer_jack self = parent->child;
222         return !self->running;
223 }
224
225 static int jack_process( jack_nframes_t frames, void * data )
226 {
227         int error = 0;
228         consumer_jack self = (consumer_jack) data;
229         mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
230         int channels = mlt_properties_get_int( properties, "channels" );
231         int i;
232
233         if ( !self->ringbuffers )
234                 return 1;
235
236         for ( i = 0; i < channels; i++ )
237         {
238                 size_t jack_size = ( frames * sizeof(float) );
239                 size_t ring_size = jack_ringbuffer_read_space( self->ringbuffers[i] );
240                 char *dest = jack_port_get_buffer( self->ports[i], frames );
241
242                 jack_ringbuffer_read( self->ringbuffers[i], dest, ring_size < jack_size ? ring_size : jack_size );
243         }
244
245         return error;
246 }
247
248 static void initialise_jack_ports( consumer_jack self )
249 {
250         int i;
251         char mlt_name[20], con_name[30];
252         mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
253         const char **ports = NULL;
254
255         // Propogate these for the Jack processing callback
256         int channels = mlt_properties_get_int( properties, "channels" );
257
258         // Allocate buffers and ports
259         self->ringbuffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
260         self->ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
261
262         // Start Jack processing - required before registering ports
263         pthread_mutex_lock( &g_activate_mutex );
264         jack_activate( self->jack );
265         pthread_mutex_unlock( &g_activate_mutex );
266         self->playing = 1;
267
268         // Register Jack ports
269         for ( i = 0; i < channels; i++ )
270         {
271                 self->ringbuffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
272                 snprintf( mlt_name, sizeof( mlt_name ), "out_%d", i + 1 );
273                 self->ports[i] = jack_port_register( self->jack, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
274                                 JackPortIsOutput | JackPortIsTerminal, 0 );
275         }
276
277         // Establish connections
278         for ( i = 0; i < channels; i++ )
279         {
280                 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( self->ports[i] ) );
281                 if ( mlt_properties_get( properties, con_name ) )
282                         snprintf( con_name, sizeof( con_name ), "%s", mlt_properties_get( properties, con_name ) );
283                 else
284                 {
285                         if ( !ports )
286                                 ports = jack_get_ports( self->jack, NULL, NULL, JackPortIsPhysical | JackPortIsInput );
287                         if ( ports )
288                                 strcpy( con_name, ports[i] );
289                         else
290                                 snprintf( con_name, sizeof( con_name ), "system:playback_%d", i + 1);
291                 }
292                 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, con_name );
293                 jack_connect( self->jack, mlt_name, con_name );
294         }
295         if ( ports )
296                 jack_free( ports );
297 }
298
299 static int consumer_play_audio( consumer_jack self, mlt_frame frame, int init_audio, int *duration )
300 {
301         // Get the properties of this consumer
302         mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
303         mlt_audio_format afmt = mlt_audio_float;
304
305         // Set the preferred params of the test card signal
306         int channels = mlt_properties_get_int( properties, "channels" );
307         int frequency = mlt_properties_get_int( properties, "frequency" );
308         int samples = mlt_sample_calculator( mlt_properties_get_double( properties, "fps" ), frequency, self->counter++ );
309         float *buffer;
310
311         mlt_frame_get_audio( frame, (void**) &buffer, &afmt, &frequency, &channels, &samples );
312         *duration = ( ( samples * 1000 ) / frequency );
313
314         if ( mlt_properties_get_int( properties, "audio_off" ) )
315         {
316                 init_audio = 1;
317                 return init_audio;
318         }
319
320         if ( init_audio == 1 )
321         {
322                 self->playing = 0;
323                 initialise_jack_ports( self );
324                 init_audio = 0;
325         }
326
327         if ( init_audio == 0 )
328         {
329                 int i;
330                 size_t mlt_size = samples * sizeof(float);
331                 float volume = mlt_properties_get_double( properties, "volume" );
332
333                 if ( volume != 1.0 )
334                 {
335                         float *p = buffer;
336                         i = samples * channels + 1;
337                         while (--i)
338                                 *p++ *= volume;
339                 }
340
341                 // Write into output ringbuffer
342                 for ( i = 0; i < channels; i++ )
343                 {
344                         size_t ring_size = jack_ringbuffer_write_space( self->ringbuffers[i] );
345                         if ( ring_size >= mlt_size )
346                                 jack_ringbuffer_write( self->ringbuffers[i], (char*)( buffer + i * samples ), mlt_size );
347                 }
348         }
349
350         return init_audio;
351 }
352
353 static int consumer_play_video( consumer_jack self, mlt_frame frame )
354 {
355         // Get the properties of this consumer
356         mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
357         if ( self->running && !mlt_consumer_is_stopped( &self->parent ) )
358                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
359
360         return 0;
361 }
362
363 static void *video_thread( void *arg )
364 {
365         // Identify the arg
366         consumer_jack self = arg;
367
368         // Obtain time of thread start
369         struct timeval now;
370         int64_t start = 0;
371         int64_t elapsed = 0;
372         struct timespec tm;
373         mlt_frame next = NULL;
374         mlt_properties properties = NULL;
375         double speed = 0;
376
377         // Get real time flag
378         int real_time = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( &self->parent ), "real_time" );
379
380         // Get the current time
381         gettimeofday( &now, NULL );
382
383         // Determine start time
384         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
385
386         while ( self->running )
387         {
388                 // Pop the next frame
389                 pthread_mutex_lock( &self->video_mutex );
390                 next = mlt_deque_pop_front( self->queue );
391                 while ( next == NULL && self->running )
392                 {
393                         pthread_cond_wait( &self->video_cond, &self->video_mutex );
394                         next = mlt_deque_pop_front( self->queue );
395                 }
396                 pthread_mutex_unlock( &self->video_mutex );
397
398                 if ( !self->running || next == NULL ) break;
399
400                 // Get the properties
401                 properties =  MLT_FRAME_PROPERTIES( next );
402
403                 // Get the speed of the frame
404                 speed = mlt_properties_get_double( properties, "_speed" );
405
406                 // Get the current time
407                 gettimeofday( &now, NULL );
408
409                 // Get the elapsed time
410                 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
411
412                 // See if we have to delay the display of the current frame
413                 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
414                 {
415                         // Obtain the scheduled playout time
416                         int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
417
418                         // Determine the difference between the elapsed time and the scheduled playout time
419                         int64_t difference = scheduled - elapsed;
420
421                         // Smooth playback a bit
422                         if ( real_time && ( difference > 20000 && speed == 1.0 ) )
423                         {
424                                 tm.tv_sec = difference / 1000000;
425                                 tm.tv_nsec = ( difference % 1000000 ) * 500;
426                                 nanosleep( &tm, NULL );
427                         }
428
429                         // Show current frame if not too old
430                         if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
431                                 consumer_play_video( self, next );
432
433                         // If the queue is empty, recalculate start to allow build up again
434                         if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
435                         {
436                                 gettimeofday( &now, NULL );
437                                 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
438                         }
439                 }
440
441                 // This frame can now be closed
442                 mlt_frame_close( next );
443                 next = NULL;
444         }
445
446         if ( next != NULL )
447                 mlt_frame_close( next );
448
449         mlt_consumer_stopped( &self->parent );
450
451         return NULL;
452 }
453
454 /** Threaded wrapper for pipe.
455 */
456
457 static void *consumer_thread( void *arg )
458 {
459         // Identify the arg
460         consumer_jack self = arg;
461
462         // Get the consumer
463         mlt_consumer consumer = &self->parent;
464
465         // Get the properties
466         mlt_properties consumer_props = MLT_CONSUMER_PROPERTIES( consumer );
467
468         // Video thread
469         pthread_t thread;
470
471         // internal intialization
472         int init_audio = 1;
473         int init_video = 1;
474         mlt_frame frame = NULL;
475         mlt_properties properties = NULL;
476         int duration = 0;
477         int64_t playtime = 0;
478         struct timespec tm = { 0, 100000 };
479 //      int last_position = -1;
480         self->refresh_count = 0;
481
482         // Loop until told not to
483         while( self->running )
484         {
485                 // Get a frame from the attached producer
486                 frame = mlt_consumer_rt_frame( consumer );
487
488                 // Ensure that we have a frame
489                 if ( frame )
490                 {
491                         // Get the frame properties
492                         properties =  MLT_FRAME_PROPERTIES( frame );
493
494                         // Get the speed of the frame
495                         double speed = mlt_properties_get_double( properties, "_speed" );
496
497                         // Get refresh request for the current frame
498                         int refresh = mlt_properties_get_int( consumer_props, "refresh" );
499
500                         // Clear refresh
501                         mlt_events_block( consumer_props, consumer_props );
502                         mlt_properties_set_int( consumer_props, "refresh", 0 );
503                         mlt_events_unblock( consumer_props, consumer_props );
504
505                         // Play audio
506                         init_audio = consumer_play_audio( self, frame, init_audio, &duration );
507
508                         // Determine the start time now
509                         if ( self->playing && init_video )
510                         {
511                                 // Create the video thread
512                                 pthread_create( &thread, NULL, video_thread, self );
513
514                                 // Video doesn't need to be initialised any more
515                                 init_video = 0;
516                         }
517
518                         // Set playtime for this frame
519                         mlt_properties_set_int( properties, "playtime", playtime );
520
521                         while ( self->running && speed != 0 && mlt_deque_count( self->queue ) > 15 )
522                                 nanosleep( &tm, NULL );
523
524                         // Push this frame to the back of the queue
525                         if ( self->running && speed )
526                         {
527                                 pthread_mutex_lock( &self->video_mutex );
528                                 mlt_deque_push_back( self->queue, frame );
529                                 pthread_cond_broadcast( &self->video_cond );
530                                 pthread_mutex_unlock( &self->video_mutex );
531
532                                 // Calculate the next playtime
533                                 playtime += ( duration * 1000 );
534                         }
535                         else if ( self->running )
536                         {
537                                 pthread_mutex_lock( &self->refresh_mutex );
538                                 if ( refresh == 0 && self->refresh_count <= 0 )
539                                 {
540                                         consumer_play_video( self, frame );
541                                         pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
542                                 }
543                                 mlt_frame_close( frame );
544                                 self->refresh_count --;
545                                 pthread_mutex_unlock( &self->refresh_mutex );
546                         }
547                         else
548                         {
549                                 mlt_frame_close( frame );
550                                 frame = NULL;
551                         }
552
553                         // Optimisation to reduce latency
554                         if ( frame && speed == 1.0 )
555                         {
556                                 // TODO: disabled due to misbehavior on parallel-consumer
557 //                              if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
558 //                                      mlt_consumer_purge( consumer );
559 //                              last_position = mlt_frame_get_position( frame );
560                         }
561                         else
562                         {
563                                 mlt_consumer_purge( consumer );
564 //                              last_position = -1;
565                         }
566                 }
567         }
568
569         // Kill the video thread
570         if ( init_video == 0 )
571         {
572                 pthread_mutex_lock( &self->video_mutex );
573                 pthread_cond_broadcast( &self->video_cond );
574                 pthread_mutex_unlock( &self->video_mutex );
575                 pthread_join( thread, NULL );
576         }
577
578         while( mlt_deque_count( self->queue ) )
579                 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
580
581         return NULL;
582 }
583
584 /** Callback to allow override of the close method.
585 */
586
587 static void consumer_close( mlt_consumer parent )
588 {
589         // Get the actual object
590         consumer_jack self = parent->child;
591
592         // Stop the consumer
593         mlt_consumer_stop( parent );
594
595         // Now clean up the rest
596         mlt_consumer_close( parent );
597
598         // Close the queue
599         mlt_deque_close( self->queue );
600
601         // Destroy mutexes
602         pthread_mutex_destroy( &self->video_mutex );
603         pthread_cond_destroy( &self->video_cond );
604         pthread_mutex_destroy( &self->refresh_mutex );
605         pthread_cond_destroy( &self->refresh_cond );
606
607         // Disconnect from JACK
608         jack_client_close( self->jack );
609
610         // Finally deallocate self
611         free( self );
612 }