2 * consumer_sdl_audio.c -- A Simple DirectMedia Layer audio-only consumer
3 * Copyright (C) 2009-2012 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_consumer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
26 #include <framework/mlt_log.h>
34 extern pthread_mutex_t mlt_sdl_mutex;
36 /** This classes definition.
39 typedef struct consumer_sdl_s *consumer_sdl;
43 struct mlt_consumer_s parent;
44 mlt_properties properties;
49 uint8_t audio_buffer[ 4096 * 10 ];
51 pthread_mutex_t audio_mutex;
52 pthread_cond_t audio_cond;
53 pthread_mutex_t video_mutex;
54 pthread_cond_t video_cond;
57 pthread_cond_t refresh_cond;
58 pthread_mutex_t refresh_mutex;
63 /** Forward references to static functions.
66 static int consumer_start( mlt_consumer parent );
67 static int consumer_stop( mlt_consumer parent );
68 static int consumer_is_stopped( mlt_consumer parent );
69 static void consumer_purge( mlt_consumer parent );
70 static void consumer_close( mlt_consumer parent );
71 static void *consumer_thread( void * );
72 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer self, char *name );
74 /** This is what will be called by the factory - anything can be passed in
75 via the argument, but keep it simple.
78 mlt_consumer consumer_sdl_audio_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
80 // Create the consumer object
81 consumer_sdl self = calloc( 1, sizeof( struct consumer_sdl_s ) );
83 // If no malloc'd and consumer init ok
84 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
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 self->properties = MLT_SERVICE_PROPERTIES( service );
99 // Set the default volume
100 mlt_properties_set_double( self->properties, "volume", 1.0 );
102 // This is the initialisation of the consumer
103 pthread_mutex_init( &self->audio_mutex, NULL );
104 pthread_cond_init( &self->audio_cond, NULL);
105 pthread_mutex_init( &self->video_mutex, NULL );
106 pthread_cond_init( &self->video_cond, NULL);
108 // Default scaler (for now we'll use nearest)
109 mlt_properties_set( self->properties, "rescale", "nearest" );
110 mlt_properties_set( self->properties, "deinterlace_method", "onefield" );
111 mlt_properties_set_int( self->properties, "top_field_first", -1 );
113 // Default buffer for low latency
114 mlt_properties_set_int( self->properties, "buffer", 1 );
116 // Default audio buffer
117 mlt_properties_set_int( self->properties, "audio_buffer", 2048 );
119 // Ensure we don't join on a non-running object
122 // Allow thread to be started/stopped
123 parent->start = consumer_start;
124 parent->stop = consumer_stop;
125 parent->is_stopped = consumer_is_stopped;
126 parent->purge = consumer_purge;
128 // Initialize the refresh handler
129 pthread_cond_init( &self->refresh_cond, NULL );
130 pthread_mutex_init( &self->refresh_mutex, NULL );
131 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), self, "property-changed", ( mlt_listener )consumer_refresh_cb );
133 // Return the consumer produced
137 // malloc or consumer init failed
144 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
146 if ( !strcmp( name, "refresh" ) )
148 consumer_sdl self = parent->child;
149 pthread_mutex_lock( &self->refresh_mutex );
150 if ( self->refresh_count < 2 )
151 self->refresh_count = self->refresh_count <= 0 ? 1 : self->refresh_count + 1;
152 pthread_cond_broadcast( &self->refresh_cond );
153 pthread_mutex_unlock( &self->refresh_mutex );
157 int consumer_start( mlt_consumer parent )
159 consumer_sdl self = parent->child;
161 if ( !self->running )
163 consumer_stop( parent );
165 pthread_mutex_lock( &mlt_sdl_mutex );
166 int ret = SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE );
167 pthread_mutex_unlock( &mlt_sdl_mutex );
170 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
176 pthread_create( &self->thread, NULL, consumer_thread, self );
182 int consumer_stop( mlt_consumer parent )
184 // Get the actual object
185 consumer_sdl self = parent->child;
187 if ( self->running && !self->joined )
189 // Kill the thread and clean up
193 // Unlatch the consumer thread
194 pthread_mutex_lock( &self->refresh_mutex );
195 pthread_cond_broadcast( &self->refresh_cond );
196 pthread_mutex_unlock( &self->refresh_mutex );
198 // Cleanup the main thread
202 pthread_join( self->thread, NULL );
204 // Unlatch the video thread
205 pthread_mutex_lock( &self->video_mutex );
206 pthread_cond_broadcast( &self->video_cond );
207 pthread_mutex_unlock( &self->video_mutex );
209 // Unlatch the audio callback
210 pthread_mutex_lock( &self->audio_mutex );
211 pthread_cond_broadcast( &self->audio_cond );
212 pthread_mutex_unlock( &self->audio_mutex );
214 SDL_QuitSubSystem( SDL_INIT_AUDIO );
220 int consumer_is_stopped( mlt_consumer parent )
222 consumer_sdl self = parent->child;
223 return !self->running;
226 void consumer_purge( mlt_consumer parent )
228 consumer_sdl self = parent->child;
231 pthread_mutex_lock( &self->video_mutex );
232 while ( mlt_deque_count( self->queue ) )
233 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
235 pthread_cond_broadcast( &self->video_cond );
236 pthread_mutex_unlock( &self->video_mutex );
240 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
242 consumer_sdl self = udata;
245 double volume = mlt_properties_get_double( self->properties, "volume" );
247 pthread_mutex_lock( &self->audio_mutex );
249 // Block until audio received
251 while ( self->running && len > self->audio_avail )
252 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
255 if ( self->audio_avail >= len )
257 // Place in the audio buffer
259 SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
261 memcpy( stream, self->audio_buffer, len );
263 // Remove len from the audio available
264 self->audio_avail -= len;
266 // Remove the samples
267 memmove( self->audio_buffer, self->audio_buffer + len, self->audio_avail );
271 // Just to be safe, wipe the stream first
272 memset( stream, 0, len );
275 SDL_MixAudio( stream, self->audio_buffer, self->audio_avail,
276 ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
279 self->audio_avail = 0;
282 // We're definitely playing now
285 pthread_cond_broadcast( &self->audio_cond );
286 pthread_mutex_unlock( &self->audio_mutex );
289 static int consumer_play_audio( consumer_sdl self, mlt_frame frame, int init_audio, int *duration )
291 // Get the properties of this consumer
292 mlt_properties properties = self->properties;
293 mlt_audio_format afmt = mlt_audio_s16;
295 // Set the preferred params of the test card signal
296 int channels = mlt_properties_get_int( properties, "channels" );
297 int frequency = mlt_properties_get_int( properties, "frequency" );
298 int scrub = mlt_properties_get_int( properties, "scrub_audio" );
299 static int counter = 0;
301 int samples = mlt_sample_calculator( mlt_properties_get_double( self->properties, "fps" ), frequency, counter++ );
306 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
307 *duration = ( ( samples * 1000 ) / frequency );
309 if ( mlt_properties_get_int( properties, "audio_off" ) )
316 if ( init_audio == 1 )
318 SDL_AudioSpec request;
321 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
323 // specify audio format
324 memset( &request, 0, sizeof( SDL_AudioSpec ) );
326 request.freq = frequency;
327 request.format = AUDIO_S16SYS;
328 request.channels = channels;
329 request.samples = audio_buffer;
330 request.callback = sdl_fill_audio;
331 request.userdata = (void *)self;
332 if ( SDL_OpenAudio( &request, &got ) != 0 )
334 mlt_log_error( MLT_CONSUMER_SERVICE( self ), "SDL failed to open audio: %s\n", SDL_GetError() );
337 else if ( got.size != 0 )
344 if ( init_audio == 0 )
346 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
347 bytes = ( samples * channels * 2 );
348 pthread_mutex_lock( &self->audio_mutex );
349 while ( self->running && bytes > ( sizeof( self->audio_buffer) - self->audio_avail ) )
350 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
353 if ( scrub || mlt_properties_get_double( properties, "_speed" ) == 1 )
354 memcpy( &self->audio_buffer[ self->audio_avail ], pcm, bytes );
356 memset( &self->audio_buffer[ self->audio_avail ], 0, bytes );
357 self->audio_avail += bytes;
359 pthread_cond_broadcast( &self->audio_cond );
360 pthread_mutex_unlock( &self->audio_mutex );
370 static int consumer_play_video( consumer_sdl self, mlt_frame frame )
372 // Get the properties of this consumer
373 mlt_properties properties = self->properties;
374 if ( self->running && !mlt_consumer_is_stopped( &self->parent ) )
375 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
379 static void *video_thread( void *arg )
382 consumer_sdl self = arg;
384 // Obtain time of thread start
389 mlt_frame next = NULL;
390 mlt_properties properties = NULL;
393 // Get real time flag
394 int real_time = mlt_properties_get_int( self->properties, "real_time" );
396 // Get the current time
397 gettimeofday( &now, NULL );
399 // Determine start time
400 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
402 while ( self->running )
404 // Pop the next frame
405 pthread_mutex_lock( &self->video_mutex );
406 next = mlt_deque_pop_front( self->queue );
407 while ( next == NULL && self->running )
409 pthread_cond_wait( &self->video_cond, &self->video_mutex );
410 next = mlt_deque_pop_front( self->queue );
412 pthread_mutex_unlock( &self->video_mutex );
414 if ( !self->running || next == NULL ) break;
416 // Get the properties
417 properties = MLT_FRAME_PROPERTIES( next );
419 // Get the speed of the frame
420 speed = mlt_properties_get_double( properties, "_speed" );
422 // Get the current time
423 gettimeofday( &now, NULL );
425 // Get the elapsed time
426 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
428 // See if we have to delay the display of the current frame
429 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
431 // Obtain the scheduled playout time
432 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
434 // Determine the difference between the elapsed time and the scheduled playout time
435 int64_t difference = scheduled - elapsed;
437 // Smooth playback a bit
438 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
440 tm.tv_sec = difference / 1000000;
441 tm.tv_nsec = ( difference % 1000000 ) * 500;
442 nanosleep( &tm, NULL );
445 // Show current frame if not too old
446 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
447 consumer_play_video( self, next );
449 // If the queue is empty, recalculate start to allow build up again
450 if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
452 gettimeofday( &now, NULL );
453 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
457 // This frame can now be closed
458 mlt_frame_close( next );
463 mlt_frame_close( next );
465 mlt_consumer_stopped( &self->parent );
470 /** Threaded wrapper for pipe.
473 static void *consumer_thread( void *arg )
476 consumer_sdl self = arg;
479 mlt_consumer consumer = &self->parent;
481 // Get the properties
482 mlt_properties consumer_props = MLT_CONSUMER_PROPERTIES( consumer );
487 // internal intialization
490 mlt_frame frame = NULL;
491 mlt_properties properties = NULL;
493 int64_t playtime = 0;
494 struct timespec tm = { 0, 100000 };
495 // int last_position = -1;
497 pthread_mutex_lock( &self->refresh_mutex );
498 self->refresh_count = 0;
499 pthread_mutex_unlock( &self->refresh_mutex );
501 // Loop until told not to
502 while( self->running )
504 // Get a frame from the attached producer
505 frame = mlt_consumer_rt_frame( consumer );
507 // Ensure that we have a frame
510 // Get the frame properties
511 properties = MLT_FRAME_PROPERTIES( frame );
513 // Get the speed of the frame
514 double speed = mlt_properties_get_double( properties, "_speed" );
516 // Get refresh request for the current frame
517 int refresh = mlt_properties_get_int( consumer_props, "refresh" );
520 mlt_events_block( consumer_props, consumer_props );
521 mlt_properties_set_int( consumer_props, "refresh", 0 );
522 mlt_events_unblock( consumer_props, consumer_props );
525 init_audio = consumer_play_audio( self, frame, init_audio, &duration );
527 // Determine the start time now
528 if ( self->playing && init_video )
530 // Create the video thread
531 pthread_create( &thread, NULL, video_thread, self );
533 // Video doesn't need to be initialised any more
537 // Set playtime for this frame
538 mlt_properties_set_int( properties, "playtime", playtime );
540 while ( self->running && speed != 0 && mlt_deque_count( self->queue ) > 15 )
541 nanosleep( &tm, NULL );
543 // Push this frame to the back of the queue
544 if ( self->running && speed )
546 pthread_mutex_lock( &self->video_mutex );
547 if ( self->is_purge )
549 mlt_frame_close( frame );
554 mlt_deque_push_back( self->queue, frame );
555 pthread_cond_broadcast( &self->video_cond );
557 pthread_mutex_unlock( &self->video_mutex );
559 // Calculate the next playtime
560 playtime += ( duration * 1000 );
562 else if ( self->running )
564 pthread_mutex_lock( &self->refresh_mutex );
565 if ( ( refresh == 0 && self->refresh_count <= 0 ) || self->refresh_count > 1 )
567 consumer_play_video( self, frame );
568 pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
570 mlt_frame_close( frame );
571 self->refresh_count --;
572 pthread_mutex_unlock( &self->refresh_mutex );
576 mlt_frame_close( frame );
580 // Optimisation to reduce latency
581 if ( frame && speed == 1.0 )
583 // TODO: disabled due to misbehavior on parallel-consumer
584 // if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
585 // mlt_consumer_purge( consumer );
586 // last_position = mlt_frame_get_position( frame );
590 mlt_consumer_purge( consumer );
591 // last_position = -1;
596 // Kill the video thread
597 if ( init_video == 0 )
599 pthread_mutex_lock( &self->video_mutex );
600 pthread_cond_broadcast( &self->video_cond );
601 pthread_mutex_unlock( &self->video_mutex );
602 pthread_join( thread, NULL );
605 while( mlt_deque_count( self->queue ) )
606 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
608 self->audio_avail = 0;
613 /** Callback to allow override of the close method.
616 static void consumer_close( mlt_consumer parent )
618 // Get the actual object
619 consumer_sdl self = parent->child;
622 mlt_consumer_stop( parent );
624 // Now clean up the rest
625 mlt_consumer_close( parent );
628 mlt_deque_close( self->queue );
631 pthread_mutex_destroy( &self->audio_mutex );
632 pthread_cond_destroy( &self->audio_cond );
633 pthread_mutex_destroy( &self->video_mutex );
634 pthread_cond_destroy( &self->video_cond );
635 pthread_mutex_destroy( &self->refresh_mutex );
636 pthread_cond_destroy( &self->refresh_cond );
638 // Finally clean up this