2 * consumer_sdl_audio.c -- A Simple DirectMedia Layer audio-only consumer
3 * Copyright (C) 2009, 2010 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;
62 /** Forward references to static functions.
65 static int consumer_start( mlt_consumer parent );
66 static int consumer_stop( mlt_consumer parent );
67 static int consumer_is_stopped( mlt_consumer parent );
68 static void consumer_close( mlt_consumer parent );
69 static void *consumer_thread( void * );
70 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer this, char *name );
72 /** This is what will be called by the factory - anything can be passed in
73 via the argument, but keep it simple.
76 mlt_consumer consumer_sdl_audio_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
78 // Create the consumer object
79 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
81 // If no malloc'd and consumer init ok
82 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
85 this->queue = mlt_deque_init( );
87 // Get the parent consumer object
88 mlt_consumer parent = &this->parent;
90 // We have stuff to clean up, so override the close method
91 parent->close = consumer_close;
93 // get a handle on properties
94 mlt_service service = MLT_CONSUMER_SERVICE( parent );
95 this->properties = MLT_SERVICE_PROPERTIES( service );
97 // Set the default volume
98 mlt_properties_set_double( this->properties, "volume", 1.0 );
100 // This is the initialisation of the consumer
101 pthread_mutex_init( &this->audio_mutex, NULL );
102 pthread_cond_init( &this->audio_cond, NULL);
103 pthread_mutex_init( &this->video_mutex, NULL );
104 pthread_cond_init( &this->video_cond, NULL);
106 // Default scaler (for now we'll use nearest)
107 mlt_properties_set( this->properties, "rescale", "nearest" );
108 mlt_properties_set( this->properties, "deinterlace_method", "onefield" );
110 // Default buffer for low latency
111 mlt_properties_set_int( this->properties, "buffer", 1 );
113 // Default audio buffer
114 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
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( &this->refresh_cond, NULL );
126 pthread_mutex_init( &this->refresh_mutex, NULL );
127 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
129 // Return the consumer produced
133 // malloc or consumer init failed
140 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
142 if ( !strcmp( name, "refresh" ) )
144 consumer_sdl this = parent->child;
145 pthread_mutex_lock( &this->refresh_mutex );
146 this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count + 1;
147 pthread_cond_broadcast( &this->refresh_cond );
148 pthread_mutex_unlock( &this->refresh_mutex );
152 int consumer_start( mlt_consumer parent )
154 consumer_sdl this = parent->child;
156 if ( !this->running )
158 consumer_stop( parent );
164 pthread_mutex_lock( &mlt_sdl_mutex );
165 int ret = SDL_Init( SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE );
166 pthread_mutex_unlock( &mlt_sdl_mutex );
169 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
173 pthread_create( &this->thread, NULL, consumer_thread, this );
179 int consumer_stop( mlt_consumer parent )
181 // Get the actual object
182 consumer_sdl this = parent->child;
184 if ( this->joined == 0 )
186 // Kill the thread and clean up
190 // Unlatch the consumer thread
191 pthread_mutex_lock( &this->refresh_mutex );
192 pthread_cond_broadcast( &this->refresh_cond );
193 pthread_mutex_unlock( &this->refresh_mutex );
195 // Cleanup the main thread
197 pthread_join( this->thread, NULL );
199 // Unlatch the audio callback
200 pthread_mutex_lock( &this->audio_mutex );
201 pthread_cond_broadcast( &this->audio_cond );
202 pthread_mutex_unlock( &this->audio_mutex );
204 SDL_QuitSubSystem( SDL_INIT_AUDIO );
210 int consumer_is_stopped( mlt_consumer parent )
212 consumer_sdl this = parent->child;
213 return !this->running;
216 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
218 consumer_sdl this = udata;
221 double volume = mlt_properties_get_double( this->properties, "volume" );
223 pthread_mutex_lock( &this->audio_mutex );
225 // Block until audio received
226 while ( this->running && len > this->audio_avail )
227 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
229 if ( this->audio_avail >= len )
231 // Place in the audio buffer
233 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
235 memcpy( stream, this->audio_buffer, len );
237 // Remove len from the audio available
238 this->audio_avail -= len;
240 // Remove the samples
241 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
245 // Just to be safe, wipe the stream first
246 memset( stream, 0, len );
249 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
252 this->audio_avail = 0;
255 // We're definitely playing now
258 pthread_cond_broadcast( &this->audio_cond );
259 pthread_mutex_unlock( &this->audio_mutex );
262 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
264 // Get the properties of this consumer
265 mlt_properties properties = this->properties;
266 mlt_audio_format afmt = mlt_audio_s16;
268 // Set the preferred params of the test card signal
269 int channels = mlt_properties_get_int( properties, "channels" );
270 int frequency = mlt_properties_get_int( properties, "frequency" );
271 static int counter = 0;
273 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
278 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
279 *duration = ( ( samples * 1000 ) / frequency );
281 if ( mlt_properties_get_int( properties, "audio_off" ) )
288 if ( init_audio == 1 )
290 SDL_AudioSpec request;
293 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
295 // specify audio format
296 memset( &request, 0, sizeof( SDL_AudioSpec ) );
298 request.freq = frequency;
299 request.format = AUDIO_S16SYS;
300 request.channels = channels;
301 request.samples = audio_buffer;
302 request.callback = sdl_fill_audio;
303 request.userdata = (void *)this;
304 if ( SDL_OpenAudio( &request, &got ) != 0 )
306 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "SDL failed to open audio: %s\n", SDL_GetError() );
309 else if ( got.size != 0 )
316 if ( init_audio == 0 )
318 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
319 bytes = ( samples * channels * 2 );
320 pthread_mutex_lock( &this->audio_mutex );
321 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
322 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
325 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
326 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
328 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
329 this->audio_avail += bytes;
331 pthread_cond_broadcast( &this->audio_cond );
332 pthread_mutex_unlock( &this->audio_mutex );
342 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
344 // Get the properties of this consumer
345 mlt_properties properties = this->properties;
346 if ( this->running && !mlt_consumer_is_stopped( &this->parent ) )
347 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
352 static void *video_thread( void *arg )
355 consumer_sdl this = arg;
357 // Obtain time of thread start
362 mlt_frame next = NULL;
363 mlt_properties properties = NULL;
366 // Get real time flag
367 int real_time = mlt_properties_get_int( this->properties, "real_time" );
369 // Get the current time
370 gettimeofday( &now, NULL );
372 // Determine start time
373 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
375 while ( this->running )
377 // Pop the next frame
378 pthread_mutex_lock( &this->video_mutex );
379 next = mlt_deque_pop_front( this->queue );
380 while ( next == NULL && this->running )
382 pthread_cond_wait( &this->video_cond, &this->video_mutex );
383 next = mlt_deque_pop_front( this->queue );
385 pthread_mutex_unlock( &this->video_mutex );
387 if ( !this->running || next == NULL ) break;
389 // Get the properties
390 properties = MLT_FRAME_PROPERTIES( next );
392 // Get the speed of the frame
393 speed = mlt_properties_get_double( properties, "_speed" );
395 // Get the current time
396 gettimeofday( &now, NULL );
398 // Get the elapsed time
399 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
401 // See if we have to delay the display of the current frame
402 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
404 // Obtain the scheduled playout time
405 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
407 // Determine the difference between the elapsed time and the scheduled playout time
408 int64_t difference = scheduled - elapsed;
410 // Smooth playback a bit
411 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
413 tm.tv_sec = difference / 1000000;
414 tm.tv_nsec = ( difference % 1000000 ) * 500;
415 nanosleep( &tm, NULL );
418 // Show current frame if not too old
419 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
420 consumer_play_video( this, next );
422 // If the queue is empty, recalculate start to allow build up again
423 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
425 gettimeofday( &now, NULL );
426 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
430 // This frame can now be closed
431 mlt_frame_close( next );
436 mlt_frame_close( next );
438 mlt_consumer_stopped( &this->parent );
443 /** Threaded wrapper for pipe.
446 static void *consumer_thread( void *arg )
449 consumer_sdl this = arg;
452 mlt_consumer consumer = &this->parent;
454 // Get the properties
455 mlt_properties consumer_props = MLT_CONSUMER_PROPERTIES( consumer );
460 // internal intialization
463 mlt_frame frame = NULL;
464 mlt_properties properties = NULL;
466 int64_t playtime = 0;
467 struct timespec tm = { 0, 100000 };
468 int last_position = -1;
469 this->refresh_count = 0;
471 // Loop until told not to
472 while( this->running )
474 // Get a frame from the attached producer
475 frame = mlt_consumer_rt_frame( consumer );
477 // Ensure that we have a frame
480 // Get the frame properties
481 properties = MLT_FRAME_PROPERTIES( frame );
483 // Get the speed of the frame
484 double speed = mlt_properties_get_double( properties, "_speed" );
486 // Get refresh request for the current frame
487 int refresh = mlt_properties_get_int( consumer_props, "refresh" );
490 mlt_events_block( consumer_props, consumer_props );
491 mlt_properties_set_int( consumer_props, "refresh", 0 );
492 mlt_events_unblock( consumer_props, consumer_props );
495 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
497 // Determine the start time now
498 if ( this->playing && init_video )
500 // Create the video thread
501 pthread_create( &thread, NULL, video_thread, this );
503 // Video doesn't need to be initialised any more
507 // Set playtime for this frame
508 mlt_properties_set_int( properties, "playtime", playtime );
510 while ( this->running && speed != 0 && mlt_deque_count( this->queue ) > 15 )
511 nanosleep( &tm, NULL );
513 // Push this frame to the back of the queue
514 if ( this->running && speed )
516 pthread_mutex_lock( &this->video_mutex );
517 mlt_deque_push_back( this->queue, frame );
518 pthread_cond_broadcast( &this->video_cond );
519 pthread_mutex_unlock( &this->video_mutex );
521 // Calculate the next playtime
522 playtime += ( duration * 1000 );
524 else if ( this->running )
526 pthread_mutex_lock( &this->refresh_mutex );
527 if ( refresh == 0 && this->refresh_count <= 0 )
529 consumer_play_video( this, frame );
530 pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
532 mlt_frame_close( frame );
533 this->refresh_count --;
534 pthread_mutex_unlock( &this->refresh_mutex );
537 mlt_frame_close( frame );
539 // Optimisation to reduce latency
542 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
543 mlt_consumer_purge( consumer );
544 last_position = mlt_frame_get_position( frame );
548 mlt_consumer_purge( consumer );
554 // Kill the video thread
555 if ( init_video == 0 )
557 pthread_mutex_lock( &this->video_mutex );
558 pthread_cond_broadcast( &this->video_cond );
559 pthread_mutex_unlock( &this->video_mutex );
560 pthread_join( thread, NULL );
563 while( mlt_deque_count( this->queue ) )
564 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
566 this->audio_avail = 0;
571 /** Callback to allow override of the close method.
574 static void consumer_close( mlt_consumer parent )
576 // Get the actual object
577 consumer_sdl this = parent->child;
580 mlt_consumer_stop( parent );
582 // Now clean up the rest
583 mlt_consumer_close( parent );
586 mlt_deque_close( this->queue );
589 pthread_mutex_destroy( &this->audio_mutex );
590 pthread_cond_destroy( &this->audio_cond );
591 pthread_mutex_destroy( &this->video_mutex );
592 pthread_cond_destroy( &this->video_cond );
593 pthread_mutex_destroy( &this->refresh_mutex );
594 pthread_cond_destroy( &this->refresh_cond );
596 // Finally clean up this