2 * consumer_jack.c -- a JACK audio consumer
3 * Copyright (C) 2011 Dan Dennedy <dan@dennedy.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * 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 /** This classes definition.
35 typedef struct consumer_jack_s *consumer_jack;
37 struct consumer_jack_s
39 struct mlt_consumer_s parent;
45 pthread_mutex_t video_mutex;
46 pthread_cond_t video_cond;
49 pthread_cond_t refresh_cond;
50 pthread_mutex_t refresh_mutex;
53 jack_ringbuffer_t **ringbuffers;
57 /** Forward references to static functions.
60 static int consumer_start( mlt_consumer parent );
61 static int consumer_stop( mlt_consumer parent );
62 static int consumer_is_stopped( mlt_consumer parent );
63 static void consumer_close( mlt_consumer parent );
64 static void *consumer_thread( void * );
65 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name );
66 static int jack_process( jack_nframes_t frames, void * data );
71 mlt_consumer consumer_jack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
73 // Create the consumer object
74 consumer_jack self = calloc( 1, sizeof( struct consumer_jack_s ) );
76 // If no malloc'd and consumer init ok
77 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
81 snprintf( name, sizeof( name ), "mlt%d", getpid() );
82 if (( self->jack = jack_client_open( name, JackNullOption, NULL ) ))
84 jack_set_process_callback( self->jack, jack_process, self );
87 self->queue = mlt_deque_init( );
89 // Get the parent consumer object
90 mlt_consumer parent = &self->parent;
92 // We have stuff to clean up, so override the close method
93 parent->close = consumer_close;
95 // get a handle on properties
96 mlt_service service = MLT_CONSUMER_SERVICE( parent );
97 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
99 // This is the initialisation of the consumer
100 pthread_mutex_init( &self->video_mutex, NULL );
101 pthread_cond_init( &self->video_cond, NULL);
103 // Default scaler (for now we'll use nearest)
104 mlt_properties_set( properties, "rescale", "nearest" );
105 mlt_properties_set( properties, "deinterlace_method", "onefield" );
107 // Default buffer for low latency
108 mlt_properties_set_int( properties, "buffer", 1 );
110 // Set frequency from JACK
111 mlt_properties_set_int( properties, "frequency", (int) jack_get_sample_rate( self->jack ) );
113 // Set default volume
114 mlt_properties_set_double( properties, "volume", 1.0 );
116 // Ensure we don't join on a non-running object
119 // Allow thread to be started/stopped
120 parent->start = consumer_start;
121 parent->stop = consumer_stop;
122 parent->is_stopped = consumer_is_stopped;
124 // Initialize the refresh handler
125 pthread_cond_init( &self->refresh_cond, NULL );
126 pthread_mutex_init( &self->refresh_mutex, NULL );
127 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), self, "property-changed", ( mlt_listener )consumer_refresh_cb );
129 // Return the consumer produced
134 // malloc or consumer init failed
141 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
143 if ( !strcmp( name, "refresh" ) )
145 consumer_jack self = parent->child;
146 pthread_mutex_lock( &self->refresh_mutex );
147 self->refresh_count = self->refresh_count <= 0 ? 1 : self->refresh_count + 1;
148 pthread_cond_broadcast( &self->refresh_cond );
149 pthread_mutex_unlock( &self->refresh_mutex );
153 static int consumer_start( mlt_consumer parent )
155 consumer_jack self = parent->child;
157 if ( !self->running )
159 consumer_stop( parent );
162 pthread_create( &self->thread, NULL, consumer_thread, self );
168 static int consumer_stop( mlt_consumer parent )
170 // Get the actual object
171 consumer_jack self = parent->child;
173 if ( self->running && !self->joined )
175 // Kill the thread and clean up
179 // Unlatch the consumer thread
180 pthread_mutex_lock( &self->refresh_mutex );
181 pthread_cond_broadcast( &self->refresh_cond );
182 pthread_mutex_unlock( &self->refresh_mutex );
184 // Cleanup the main thread
188 pthread_join( self->thread, NULL );
190 // Unlatch the video thread
191 pthread_mutex_lock( &self->video_mutex );
192 pthread_cond_broadcast( &self->video_cond );
193 pthread_mutex_unlock( &self->video_mutex );
197 jack_deactivate( self->jack );
198 if ( self->ringbuffers )
200 int n = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "channels" );
203 jack_ringbuffer_free( self->ringbuffers[n] );
204 jack_port_unregister( self->jack, self->ports[n] );
206 mlt_pool_release( self->ringbuffers );
208 self->ringbuffers = NULL;
210 mlt_pool_release( self->ports );
217 static int consumer_is_stopped( mlt_consumer parent )
219 consumer_jack self = parent->child;
220 return !self->running;
223 static int jack_process( jack_nframes_t frames, void * data )
226 consumer_jack self = (consumer_jack) data;
227 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
228 int channels = mlt_properties_get_int( properties, "channels" );
231 if ( !self->ringbuffers )
234 for ( i = 0; i < channels; i++ )
236 size_t jack_size = ( frames * sizeof(float) );
237 size_t ring_size = jack_ringbuffer_read_space( self->ringbuffers[i] );
238 char *dest = jack_port_get_buffer( self->ports[i], frames );
240 jack_ringbuffer_read( self->ringbuffers[i], dest, ring_size < jack_size ? ring_size : jack_size );
246 static void initialise_jack_ports( consumer_jack self )
249 char mlt_name[20], con_name[30];
250 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
252 // Propogate these for the Jack processing callback
253 int channels = mlt_properties_get_int( properties, "channels" );
255 // Allocate buffers and ports
256 self->ringbuffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
257 self->ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
259 // Start Jack processing - required before registering ports
260 jack_activate( self->jack );
263 // Register Jack ports
264 for ( i = 0; i < channels; i++ )
266 self->ringbuffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
267 snprintf( mlt_name, sizeof( mlt_name ), "out_%d", i + 1 );
268 self->ports[i] = jack_port_register( self->jack, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
269 JackPortIsOutput | JackPortIsTerminal, 0 );
272 // Establish connections
273 for ( i = 0; i < channels; i++ )
275 snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( self->ports[i] ) );
276 if ( mlt_properties_get( properties, con_name ) )
277 snprintf( con_name, sizeof( con_name ), "%s", mlt_properties_get( properties, con_name ) );
279 snprintf( con_name, sizeof( con_name ), "system:playback_%d", i + 1);
281 mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, con_name );
282 jack_connect( self->jack, mlt_name, con_name );
286 static int consumer_play_audio( consumer_jack self, mlt_frame frame, int init_audio, int *duration )
288 // Get the properties of this consumer
289 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
290 mlt_audio_format afmt = mlt_audio_float;
292 // Set the preferred params of the test card signal
293 int channels = mlt_properties_get_int( properties, "channels" );
294 int frequency = mlt_properties_get_int( properties, "frequency" );
295 int samples = mlt_sample_calculator( mlt_properties_get_double( properties, "fps" ), frequency, self->counter++ );
298 mlt_frame_get_audio( frame, (void**) &buffer, &afmt, &frequency, &channels, &samples );
299 *duration = ( ( samples * 1000 ) / frequency );
301 if ( mlt_properties_get_int( properties, "audio_off" ) )
307 if ( init_audio == 1 )
310 initialise_jack_ports( self );
314 if ( init_audio == 0 )
317 size_t mlt_size = samples * sizeof(float);
318 float volume = mlt_properties_get_double( properties, "volume" );
323 i = samples * channels + 1;
328 // Write into output ringbuffer
329 for ( i = 0; i < channels; i++ )
331 size_t ring_size = jack_ringbuffer_write_space( self->ringbuffers[i] );
332 if ( ring_size >= mlt_size )
333 jack_ringbuffer_write( self->ringbuffers[i], (char*)( buffer + i * samples ), mlt_size );
340 static int consumer_play_video( consumer_jack self, mlt_frame frame )
342 // Get the properties of this consumer
343 mlt_properties properties = MLT_CONSUMER_PROPERTIES( &self->parent );
344 if ( self->running && !mlt_consumer_is_stopped( &self->parent ) )
345 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
350 static void *video_thread( void *arg )
353 consumer_jack self = arg;
355 // Obtain time of thread start
360 mlt_frame next = NULL;
361 mlt_properties properties = NULL;
364 // Get real time flag
365 int real_time = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( &self->parent ), "real_time" );
367 // Get the current time
368 gettimeofday( &now, NULL );
370 // Determine start time
371 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
373 while ( self->running )
375 // Pop the next frame
376 pthread_mutex_lock( &self->video_mutex );
377 next = mlt_deque_pop_front( self->queue );
378 while ( next == NULL && self->running )
380 pthread_cond_wait( &self->video_cond, &self->video_mutex );
381 next = mlt_deque_pop_front( self->queue );
383 pthread_mutex_unlock( &self->video_mutex );
385 if ( !self->running || next == NULL ) break;
387 // Get the properties
388 properties = MLT_FRAME_PROPERTIES( next );
390 // Get the speed of the frame
391 speed = mlt_properties_get_double( properties, "_speed" );
393 // Get the current time
394 gettimeofday( &now, NULL );
396 // Get the elapsed time
397 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
399 // See if we have to delay the display of the current frame
400 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
402 // Obtain the scheduled playout time
403 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
405 // Determine the difference between the elapsed time and the scheduled playout time
406 int64_t difference = scheduled - elapsed;
408 // Smooth playback a bit
409 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
411 tm.tv_sec = difference / 1000000;
412 tm.tv_nsec = ( difference % 1000000 ) * 500;
413 nanosleep( &tm, NULL );
416 // Show current frame if not too old
417 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
418 consumer_play_video( self, next );
420 // If the queue is empty, recalculate start to allow build up again
421 if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
423 gettimeofday( &now, NULL );
424 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
428 // This frame can now be closed
429 mlt_frame_close( next );
434 mlt_frame_close( next );
436 mlt_consumer_stopped( &self->parent );
441 /** Threaded wrapper for pipe.
444 static void *consumer_thread( void *arg )
447 consumer_jack self = arg;
450 mlt_consumer consumer = &self->parent;
452 // Get the properties
453 mlt_properties consumer_props = MLT_CONSUMER_PROPERTIES( consumer );
458 // internal intialization
461 mlt_frame frame = NULL;
462 mlt_properties properties = NULL;
464 int64_t playtime = 0;
465 struct timespec tm = { 0, 100000 };
466 // int last_position = -1;
467 self->refresh_count = 0;
469 // Loop until told not to
470 while( self->running )
472 // Get a frame from the attached producer
473 frame = mlt_consumer_rt_frame( consumer );
475 // Ensure that we have a frame
478 // Get the frame properties
479 properties = MLT_FRAME_PROPERTIES( frame );
481 // Get the speed of the frame
482 double speed = mlt_properties_get_double( properties, "_speed" );
484 // Get refresh request for the current frame
485 int refresh = mlt_properties_get_int( consumer_props, "refresh" );
488 mlt_events_block( consumer_props, consumer_props );
489 mlt_properties_set_int( consumer_props, "refresh", 0 );
490 mlt_events_unblock( consumer_props, consumer_props );
493 init_audio = consumer_play_audio( self, frame, init_audio, &duration );
495 // Determine the start time now
496 if ( self->playing && init_video )
498 // Create the video thread
499 pthread_create( &thread, NULL, video_thread, self );
501 // Video doesn't need to be initialised any more
505 // Set playtime for this frame
506 mlt_properties_set_int( properties, "playtime", playtime );
508 while ( self->running && speed != 0 && mlt_deque_count( self->queue ) > 15 )
509 nanosleep( &tm, NULL );
511 // Push this frame to the back of the queue
512 if ( self->running && speed )
514 pthread_mutex_lock( &self->video_mutex );
515 mlt_deque_push_back( self->queue, frame );
516 pthread_cond_broadcast( &self->video_cond );
517 pthread_mutex_unlock( &self->video_mutex );
519 // Calculate the next playtime
520 playtime += ( duration * 1000 );
522 else if ( self->running )
524 pthread_mutex_lock( &self->refresh_mutex );
525 if ( refresh == 0 && self->refresh_count <= 0 )
527 consumer_play_video( self, frame );
528 pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
530 mlt_frame_close( frame );
531 self->refresh_count --;
532 pthread_mutex_unlock( &self->refresh_mutex );
536 mlt_frame_close( frame );
540 // Optimisation to reduce latency
541 if ( frame && speed == 1.0 )
543 // TODO: disabled due to misbehavior on parallel-consumer
544 // if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
545 // mlt_consumer_purge( consumer );
546 // last_position = mlt_frame_get_position( frame );
550 mlt_consumer_purge( consumer );
551 // last_position = -1;
556 // Kill the video thread
557 if ( init_video == 0 )
559 pthread_mutex_lock( &self->video_mutex );
560 pthread_cond_broadcast( &self->video_cond );
561 pthread_mutex_unlock( &self->video_mutex );
562 pthread_join( thread, NULL );
565 while( mlt_deque_count( self->queue ) )
566 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
571 /** Callback to allow override of the close method.
574 static void consumer_close( mlt_consumer parent )
576 // Get the actual object
577 consumer_jack self = parent->child;
580 mlt_consumer_stop( parent );
582 // Now clean up the rest
583 mlt_consumer_close( parent );
586 mlt_deque_close( self->queue );
589 pthread_mutex_destroy( &self->video_mutex );
590 pthread_cond_destroy( &self->video_cond );
591 pthread_mutex_destroy( &self->refresh_mutex );
592 pthread_cond_destroy( &self->refresh_cond );
594 // Disconnect from JACK
595 jack_client_close( self->jack );
597 // Finally deallocate self