2 * consumer_jack.c -- a JACK audio consumer
3 * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
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.
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.
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
20 #include <framework/mlt.h>
27 #include <jack/jack.h>
28 #include <jack/ringbuffer.h>
30 #define BUFFER_LEN (204800 * 6)
32 pthread_mutex_t g_activate_mutex = PTHREAD_MUTEX_INITIALIZER;
34 /** This classes definition.
37 typedef struct consumer_jack_s *consumer_jack;
39 struct consumer_jack_s
41 struct mlt_consumer_s parent;
47 pthread_mutex_t video_mutex;
48 pthread_cond_t video_cond;
51 pthread_cond_t refresh_cond;
52 pthread_mutex_t refresh_mutex;
55 jack_ringbuffer_t **ringbuffers;
59 /** Forward references to static functions.
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 );
73 mlt_consumer consumer_jack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
75 // Create the consumer object
76 consumer_jack self = calloc( 1, sizeof( struct consumer_jack_s ) );
78 // If no malloc'd and consumer init ok
79 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
83 snprintf( name, sizeof( name ), "mlt%d", getpid() );
84 if (( self->jack = jack_client_open( name, JackNullOption, NULL ) ))
86 jack_set_process_callback( self->jack, jack_process, self );
89 self->queue = mlt_deque_init( );
91 // Get the parent consumer object
92 mlt_consumer parent = &self->parent;
94 // We have stuff to clean up, so override the close method
95 parent->close = consumer_close;
97 // get a handle on properties
98 mlt_service service = MLT_CONSUMER_SERVICE( parent );
99 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
101 // This is the initialisation of the consumer
102 pthread_mutex_init( &self->video_mutex, NULL );
103 pthread_cond_init( &self->video_cond, NULL);
105 // Default scaler (for now we'll use nearest)
106 mlt_properties_set( properties, "rescale", "nearest" );
107 mlt_properties_set( properties, "deinterlace_method", "onefield" );
109 // Default buffer for low latency
110 mlt_properties_set_int( properties, "buffer", 1 );
112 // Set frequency from JACK
113 mlt_properties_set_int( properties, "frequency", (int) jack_get_sample_rate( self->jack ) );
115 // Set default volume
116 mlt_properties_set_double( properties, "volume", 1.0 );
118 // Ensure we don't join on a non-running object
121 // Allow thread to be started/stopped
122 parent->start = consumer_start;
123 parent->stop = consumer_stop;
124 parent->is_stopped = consumer_is_stopped;
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 );
131 // Return the consumer produced
136 // malloc or consumer init failed
143 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
145 if ( !strcmp( name, "refresh" ) )
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 );
155 static int consumer_start( mlt_consumer parent )
157 consumer_jack self = parent->child;
159 if ( !self->running )
161 consumer_stop( parent );
164 pthread_create( &self->thread, NULL, consumer_thread, self );
170 static int consumer_stop( mlt_consumer parent )
172 // Get the actual object
173 consumer_jack self = parent->child;
175 if ( self->running && !self->joined )
177 // Kill the thread and clean up
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 );
186 // Cleanup the main thread
190 pthread_join( self->thread, NULL );
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 );
199 jack_deactivate( self->jack );
200 if ( self->ringbuffers )
202 int n = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "channels" );
205 jack_ringbuffer_free( self->ringbuffers[n] );
206 jack_port_unregister( self->jack, self->ports[n] );
208 mlt_pool_release( self->ringbuffers );
210 self->ringbuffers = NULL;
212 mlt_pool_release( self->ports );
219 static int consumer_is_stopped( mlt_consumer parent )
221 consumer_jack self = parent->child;
222 return !self->running;
225 static int jack_process( jack_nframes_t frames, void * data )
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" );
233 if ( !self->ringbuffers )
236 for ( i = 0; i < channels; i++ )
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 );
242 jack_ringbuffer_read( self->ringbuffers[i], dest, ring_size < jack_size ? ring_size : jack_size );
243 if ( ring_size < jack_size )
244 memset( dest + ring_size, 0, jack_size - ring_size );
250 static void initialise_jack_ports( consumer_jack self )
253 char mlt_name[20], con_name[30];
254 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
255 const char **ports = NULL;
257 // Propogate these for the Jack processing callback
258 int channels = mlt_properties_get_int( properties, "channels" );
260 // Allocate buffers and ports
261 self->ringbuffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
262 self->ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
264 // Start Jack processing - required before registering ports
265 pthread_mutex_lock( &g_activate_mutex );
266 jack_activate( self->jack );
267 pthread_mutex_unlock( &g_activate_mutex );
270 // Register Jack ports
271 for ( i = 0; i < channels; i++ )
273 self->ringbuffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
274 snprintf( mlt_name, sizeof( mlt_name ), "out_%d", i + 1 );
275 self->ports[i] = jack_port_register( self->jack, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
276 JackPortIsOutput | JackPortIsTerminal, 0 );
279 // Establish connections
280 for ( i = 0; i < channels; i++ )
282 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( self->ports[i] ) );
283 if ( mlt_properties_get( properties, con_name ) )
284 snprintf( con_name, sizeof( con_name ), "%s", mlt_properties_get( properties, con_name ) );
288 ports = jack_get_ports( self->jack, NULL, NULL, JackPortIsPhysical | JackPortIsInput );
290 strncpy( con_name, ports[i], sizeof( con_name ));
292 snprintf( con_name, sizeof( con_name ), "system:playback_%d", i + 1);
293 con_name[ sizeof( con_name ) - 1 ] = '\0';
295 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, con_name );
296 jack_connect( self->jack, mlt_name, con_name );
302 static int consumer_play_audio( consumer_jack self, mlt_frame frame, int init_audio, int *duration )
304 // Get the properties of this consumer
305 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
306 mlt_audio_format afmt = mlt_audio_float;
308 // Set the preferred params of the test card signal
309 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES(frame), "_speed" );
310 int channels = mlt_properties_get_int( properties, "channels" );
311 int frequency = mlt_properties_get_int( properties, "frequency" );
312 int scrub = mlt_properties_get_int( properties, "scrub_audio" );
313 int samples = mlt_sample_calculator( mlt_properties_get_double( properties, "fps" ), frequency, self->counter++ );
316 mlt_frame_get_audio( frame, (void**) &buffer, &afmt, &frequency, &channels, &samples );
317 *duration = ( ( samples * 1000 ) / frequency );
319 if ( mlt_properties_get_int( properties, "audio_off" ) )
325 if ( init_audio == 1 )
328 initialise_jack_ports( self );
332 if ( init_audio == 0 && ( speed == 1.0 || speed == 0.0 ) )
335 size_t mlt_size = samples * sizeof(float);
336 float volume = mlt_properties_get_double( properties, "volume" );
338 if ( !scrub && speed == 0.0 )
344 i = samples * channels + 1;
349 // Write into output ringbuffer
350 for ( i = 0; i < channels; i++ )
352 size_t ring_size = jack_ringbuffer_write_space( self->ringbuffers[i] );
353 if ( ring_size >= mlt_size )
354 jack_ringbuffer_write( self->ringbuffers[i], (char*)( buffer + i * samples ), mlt_size );
361 static int consumer_play_video( consumer_jack self, mlt_frame frame )
363 // Get the properties of this consumer
364 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
365 if ( self->running && !mlt_consumer_is_stopped( &self->parent ) )
366 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
371 static void *video_thread( void *arg )
374 consumer_jack self = arg;
376 // Obtain time of thread start
381 mlt_frame next = NULL;
382 mlt_properties properties = NULL;
385 // Get real time flag
386 int real_time = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( &self->parent ), "real_time" );
388 // Get the current time
389 gettimeofday( &now, NULL );
391 // Determine start time
392 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
394 while ( self->running )
396 // Pop the next frame
397 pthread_mutex_lock( &self->video_mutex );
398 next = mlt_deque_pop_front( self->queue );
399 while ( next == NULL && self->running )
401 pthread_cond_wait( &self->video_cond, &self->video_mutex );
402 next = mlt_deque_pop_front( self->queue );
404 pthread_mutex_unlock( &self->video_mutex );
406 if ( !self->running || next == NULL ) break;
408 // Get the properties
409 properties = MLT_FRAME_PROPERTIES( next );
411 // Get the speed of the frame
412 speed = mlt_properties_get_double( properties, "_speed" );
414 // Get the current time
415 gettimeofday( &now, NULL );
417 // Get the elapsed time
418 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
420 // See if we have to delay the display of the current frame
421 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
423 // Obtain the scheduled playout time
424 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
426 // Determine the difference between the elapsed time and the scheduled playout time
427 int64_t difference = scheduled - elapsed;
429 // Smooth playback a bit
430 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
432 tm.tv_sec = difference / 1000000;
433 tm.tv_nsec = ( difference % 1000000 ) * 500;
434 nanosleep( &tm, NULL );
437 // Show current frame if not too old
438 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
439 consumer_play_video( self, next );
441 // If the queue is empty, recalculate start to allow build up again
442 if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
444 gettimeofday( &now, NULL );
445 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
449 // This frame can now be closed
450 mlt_frame_close( next );
455 mlt_frame_close( next );
457 mlt_consumer_stopped( &self->parent );
462 /** Threaded wrapper for pipe.
465 static void *consumer_thread( void *arg )
468 consumer_jack self = arg;
471 mlt_consumer consumer = &self->parent;
473 // Get the properties
474 mlt_properties consumer_props = MLT_CONSUMER_PROPERTIES( consumer );
479 // internal intialization
482 mlt_frame frame = NULL;
483 mlt_properties properties = NULL;
485 int64_t playtime = 0;
486 struct timespec tm = { 0, 100000 };
487 // int last_position = -1;
489 pthread_mutex_lock( &self->refresh_mutex );
490 self->refresh_count = 0;
491 pthread_mutex_unlock( &self->refresh_mutex );
493 // Loop until told not to
494 while( self->running )
496 // Get a frame from the attached producer
497 frame = mlt_consumer_rt_frame( consumer );
499 // Ensure that we have a frame
502 // Get the frame properties
503 properties = MLT_FRAME_PROPERTIES( frame );
505 // Get the speed of the frame
506 double speed = mlt_properties_get_double( properties, "_speed" );
508 // Get refresh request for the current frame
509 int refresh = mlt_properties_get_int( consumer_props, "refresh" );
512 mlt_events_block( consumer_props, consumer_props );
513 mlt_properties_set_int( consumer_props, "refresh", 0 );
514 mlt_events_unblock( consumer_props, consumer_props );
517 init_audio = consumer_play_audio( self, frame, init_audio, &duration );
519 // Determine the start time now
520 if ( self->playing && init_video )
522 // Create the video thread
523 pthread_create( &thread, NULL, video_thread, self );
525 // Video doesn't need to be initialised any more
529 // Set playtime for this frame
530 mlt_properties_set_int( properties, "playtime", playtime );
532 while ( self->running && speed != 0 && mlt_deque_count( self->queue ) > 15 )
533 nanosleep( &tm, NULL );
535 // Push this frame to the back of the queue
536 if ( self->running && speed )
538 pthread_mutex_lock( &self->video_mutex );
539 mlt_deque_push_back( self->queue, frame );
540 pthread_cond_broadcast( &self->video_cond );
541 pthread_mutex_unlock( &self->video_mutex );
543 // Calculate the next playtime
544 playtime += ( duration * 1000 );
546 else if ( self->running )
548 pthread_mutex_lock( &self->refresh_mutex );
549 if ( refresh == 0 && self->refresh_count <= 0 )
551 consumer_play_video( self, frame );
552 pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
554 mlt_frame_close( frame );
555 self->refresh_count --;
556 pthread_mutex_unlock( &self->refresh_mutex );
560 mlt_frame_close( frame );
564 // Optimisation to reduce latency
565 if ( frame && speed == 1.0 )
567 // TODO: disabled due to misbehavior on parallel-consumer
568 // if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
569 // mlt_consumer_purge( consumer );
570 // last_position = mlt_frame_get_position( frame );
574 mlt_consumer_purge( consumer );
575 // last_position = -1;
580 // Kill the video thread
581 if ( init_video == 0 )
583 pthread_mutex_lock( &self->video_mutex );
584 pthread_cond_broadcast( &self->video_cond );
585 pthread_mutex_unlock( &self->video_mutex );
586 pthread_join( thread, NULL );
589 while( mlt_deque_count( self->queue ) )
590 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
595 /** Callback to allow override of the close method.
598 static void consumer_close( mlt_consumer parent )
600 // Get the actual object
601 consumer_jack self = parent->child;
604 mlt_consumer_stop( parent );
606 // Now clean up the rest
607 mlt_consumer_close( parent );
610 mlt_deque_close( self->queue );
613 pthread_mutex_destroy( &self->video_mutex );
614 pthread_cond_destroy( &self->video_cond );
615 pthread_mutex_destroy( &self->refresh_mutex );
616 pthread_cond_destroy( &self->refresh_cond );
618 // Disconnect from JACK
619 jack_client_close( self->jack );
621 // Finally deallocate self