2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
29 #include <SDL/SDL_syswm.h>
32 /** This classes definition.
35 typedef struct consumer_sdl_s *consumer_sdl;
39 struct mlt_consumer_s parent;
40 mlt_properties properties;
45 uint8_t audio_buffer[ 4096 * 10 ];
47 pthread_mutex_t audio_mutex;
48 pthread_cond_t audio_cond;
49 pthread_mutex_t video_mutex;
50 pthread_cond_t video_cond;
59 SDL_Surface *sdl_screen;
60 SDL_Overlay *sdl_overlay;
66 /** Forward references to static functions.
69 static int consumer_start( mlt_consumer parent );
70 static int consumer_stop( mlt_consumer parent );
71 static int consumer_is_stopped( mlt_consumer parent );
72 static void consumer_close( mlt_consumer parent );
73 static void *consumer_thread( void * );
74 static int consumer_get_dimensions( int *width, int *height );
75 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
77 /** This is what will be called by the factory - anything can be passed in
78 via the argument, but keep it simple.
81 mlt_consumer consumer_sdl_init( char *arg )
83 // Create the consumer object
84 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
86 // If no malloc'd and consumer init ok
87 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
90 this->queue = mlt_deque_init( );
92 // Get the parent consumer object
93 mlt_consumer parent = &this->parent;
95 // We have stuff to clean up, so override the close method
96 parent->close = consumer_close;
98 // get a handle on properties
99 mlt_service service = MLT_CONSUMER_SERVICE( parent );
100 this->properties = MLT_SERVICE_PROPERTIES( service );
102 // Default display aspect ratio
103 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
105 // Set the default volume
106 mlt_properties_set_double( this->properties, "volume", 1.0 );
108 // This is the initialisation of the consumer
109 pthread_mutex_init( &this->audio_mutex, NULL );
110 pthread_cond_init( &this->audio_cond, NULL);
111 pthread_mutex_init( &this->video_mutex, NULL );
112 pthread_cond_init( &this->video_cond, NULL);
114 // Default scaler (for now we'll use nearest)
115 mlt_properties_set( this->properties, "rescale", "nearest" );
117 // Default buffer for low latency
118 mlt_properties_set_int( this->properties, "buffer", 1 );
120 // Default progressive true
121 mlt_properties_set_int( this->properties, "progressive", 0 );
123 // Default audio buffer
124 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
126 // Ensure we don't join on a non-running object
129 // process actual param
130 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
132 this->width = mlt_properties_get_int( this->properties, "width" );
133 this->height = mlt_properties_get_int( this->properties, "height" );
136 // Default window size
137 this->window_width = ( double )this->height * display_ratio;
138 this->window_height = this->height;
141 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
143 // Allow thread to be started/stopped
144 parent->start = consumer_start;
145 parent->stop = consumer_stop;
146 parent->is_stopped = consumer_is_stopped;
148 // Register specific events
149 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
151 // Return the consumer produced
155 // malloc or consumer init failed
162 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
164 if ( listener != NULL )
165 listener( owner, this, ( SDL_Event * )args[ 0 ] );
168 int consumer_start( mlt_consumer parent )
170 consumer_sdl this = parent->child;
172 if ( !this->running )
174 consumer_stop( parent );
179 this->width = mlt_properties_get_int( this->properties, "width" );
180 this->height = mlt_properties_get_int( this->properties, "height" );
182 this->bpp = mlt_properties_get_int( this->properties, "bpp" );
184 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
186 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
188 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
192 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
193 SDL_EnableUNICODE( 1 );
197 if ( SDL_GetVideoSurface( ) != NULL )
198 this->sdl_screen = SDL_GetVideoSurface( );
201 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
202 SDL_InitSubSystem( SDL_INIT_AUDIO );
204 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
206 pthread_create( &this->thread, NULL, consumer_thread, this );
212 int consumer_stop( mlt_consumer parent )
214 // Get the actual object
215 consumer_sdl this = parent->child;
217 if ( this->joined == 0 )
219 // Kill the thread and clean up
222 pthread_join( this->thread, NULL );
225 if ( this->sdl_overlay != NULL )
226 SDL_FreeYUVOverlay( this->sdl_overlay );
227 this->sdl_overlay = NULL;
229 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
230 SDL_QuitSubSystem( SDL_INIT_AUDIO );
232 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
235 this->sdl_screen = NULL;
241 int consumer_is_stopped( mlt_consumer parent )
243 consumer_sdl this = parent->child;
244 return !this->running;
247 static int sdl_lock_display( )
249 SDL_Surface *screen = SDL_GetVideoSurface( );
250 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
253 static void sdl_unlock_display( )
255 SDL_Surface *screen = SDL_GetVideoSurface( );
256 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
257 SDL_UnlockSurface( screen );
260 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
262 consumer_sdl this = udata;
265 double volume = mlt_properties_get_double( this->properties, "volume" );
267 pthread_mutex_lock( &this->audio_mutex );
269 // Block until audio received
270 while ( this->running && len > this->audio_avail )
271 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
273 if ( this->audio_avail >= len )
275 // Place in the audio buffer
277 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
279 memcpy( stream, this->audio_buffer, len );
281 // Remove len from the audio available
282 this->audio_avail -= len;
284 // Remove the samples
285 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
289 // Just to be safe, wipe the stream first
290 memset( stream, 0, len );
292 // Copy what we have into the stream
293 memcpy( stream, this->audio_buffer, this->audio_avail );
296 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
299 this->audio_avail = 0;
302 // We're definitely playing now
305 pthread_cond_broadcast( &this->audio_cond );
306 pthread_mutex_unlock( &this->audio_mutex );
309 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
311 // Get the properties of this consumer
312 mlt_properties properties = this->properties;
313 mlt_audio_format afmt = mlt_audio_pcm;
315 // Set the preferred params of the test card signal
316 int channels = mlt_properties_get_int( properties, "channels" );
317 int frequency = mlt_properties_get_int( properties, "frequency" );
318 static int counter = 0;
320 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
325 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
326 *duration = ( ( samples * 1000 ) / frequency );
328 if ( mlt_properties_get_int( properties, "audio_off" ) )
335 if ( init_audio == 1 )
337 SDL_AudioSpec request;
340 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
342 // specify audio format
343 memset( &request, 0, sizeof( SDL_AudioSpec ) );
345 request.freq = frequency;
346 request.format = AUDIO_S16;
347 request.channels = channels;
348 request.samples = audio_buffer;
349 request.callback = sdl_fill_audio;
350 request.userdata = (void *)this;
351 if ( SDL_OpenAudio( &request, &got ) != 0 )
353 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
356 else if ( got.size != 0 )
363 if ( init_audio == 0 )
365 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
366 bytes = ( samples * channels * 2 );
367 pthread_mutex_lock( &this->audio_mutex );
368 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
369 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
372 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
373 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
375 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
376 this->audio_avail += bytes;
378 pthread_cond_broadcast( &this->audio_cond );
379 pthread_mutex_unlock( &this->audio_mutex );
389 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
391 // Get the properties of this consumer
392 mlt_properties properties = this->properties;
394 mlt_image_format vfmt = mlt_image_yuv422;
395 int width = this->width, height = this->height;
399 if ( this->running && mlt_properties_get_int( properties, "video_off" ) == 0 )
401 // Get the image, width and height
402 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
403 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
406 if ( this->sdl_screen != NULL )
411 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
412 sdl_unlock_display( );
414 while ( SDL_PollEvent( &event ) )
416 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
420 case SDL_VIDEORESIZE:
421 this->window_width = event.resize.w;
422 this->window_height = event.resize.h;
430 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
431 char keyboard[ 2 ] = " ";
432 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
433 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
435 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
436 callback( producer, keyboard );
446 if ( width != this->width || height != this->height )
448 if ( this->sdl_overlay != NULL )
449 SDL_FreeYUVOverlay( this->sdl_overlay );
450 this->sdl_overlay = NULL;
453 if ( this->running && ( this->sdl_screen == NULL || changed ) )
455 // Force an overlay recreation
456 if ( this->sdl_overlay != NULL )
457 SDL_FreeYUVOverlay( this->sdl_overlay );
458 this->sdl_overlay = NULL;
460 // open SDL window with video overlay, if possible
461 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
462 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
463 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
468 // Determine window's new display aspect ratio
469 double this_aspect = ( double )this->window_width / this->window_height;
471 // Get the display aspect ratio
472 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
474 // Determine frame's display aspect ratio
475 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
477 // Store the width and height received
479 this->height = height;
481 // If using hardware scaler
482 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
483 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
485 // Use hardware scaler to normalise display aspect ratio
486 this->rect.w = frame_aspect / this_aspect * this->window_width;
487 this->rect.h = this->window_height;
488 if ( this->rect.w > this->window_width )
490 this->rect.w = this->window_width;
491 this->rect.h = this_aspect / frame_aspect * this->window_height;
494 // Special case optimisation to negate odd effect of sample aspect ratio
495 // not corresponding exactly with image resolution.
496 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
498 this->rect.w = this->window_width;
499 this->rect.h = this->window_height;
501 // Use hardware scaler to normalise sample aspect ratio
502 else if ( this->window_height * display_ratio > this->window_width )
504 this->rect.w = this->window_width;
505 this->rect.h = this->window_width / display_ratio;
509 this->rect.w = this->window_height * display_ratio;
510 this->rect.h = this->window_height;
513 this->rect.x = ( this->window_width - this->rect.w ) / 2;
514 this->rect.y = ( this->window_height - this->rect.h ) / 2;
516 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
517 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
518 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
519 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
521 SDL_SetClipRect( this->sdl_screen, &this->rect );
524 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
526 SDL_SetClipRect( this->sdl_screen, &this->rect );
527 this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
530 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
532 this->buffer = this->sdl_overlay->pixels[ 0 ];
533 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
536 memcpy( this->buffer, image, width * height * 2 );
537 SDL_UnlockYUVOverlay( this->sdl_overlay );
538 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
542 sdl_unlock_display();
548 static void *video_thread( void *arg )
551 consumer_sdl this = arg;
553 // Obtain time of thread start
558 mlt_frame next = NULL;
559 mlt_properties properties = NULL;
562 // Get real time flag
563 int real_time = mlt_properties_get_int( this->properties, "real_time" );
565 // Get the current time
566 gettimeofday( &now, NULL );
568 // Determine start time
569 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
571 while ( this->running )
573 // Pop the next frame
574 pthread_mutex_lock( &this->video_mutex );
575 next = mlt_deque_pop_front( this->queue );
576 while ( next == NULL && this->running )
578 pthread_cond_wait( &this->video_cond, &this->video_mutex );
579 next = mlt_deque_pop_front( this->queue );
581 pthread_mutex_unlock( &this->video_mutex );
583 if ( !this->running || next == NULL ) break;
585 // Get the properties
586 properties = MLT_FRAME_PROPERTIES( next );
588 // Get the speed of the frame
589 speed = mlt_properties_get_double( properties, "_speed" );
591 // Get the current time
592 gettimeofday( &now, NULL );
594 // Get the elapsed time
595 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
597 // See if we have to delay the display of the current frame
598 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
600 // Obtain the scheduled playout time
601 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
603 // Determine the difference between the elapsed time and the scheduled playout time
604 int64_t difference = scheduled - elapsed;
606 // Smooth playback a bit
607 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
609 tm.tv_sec = difference / 1000000;
610 tm.tv_nsec = ( difference % 1000000 ) * 500;
611 nanosleep( &tm, NULL );
614 // Show current frame if not too old
615 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
616 consumer_play_video( this, next );
618 // If the queue is empty, recalculate start to allow build up again
619 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
621 gettimeofday( &now, NULL );
622 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
626 // This frame can now be closed
627 mlt_frame_close( next );
632 mlt_frame_close( next );
634 mlt_consumer_stopped( &this->parent );
639 /** Threaded wrapper for pipe.
642 static void *consumer_thread( void *arg )
645 consumer_sdl this = arg;
648 mlt_consumer consumer = &this->parent;
653 // internal intialization
656 mlt_frame frame = NULL;
657 mlt_properties properties = NULL;
659 int64_t playtime = 0;
660 struct timespec tm = { 0, 100000 };
662 // Loop until told not to
663 while( this->running )
665 // Get a frame from the attached producer
666 frame = mlt_consumer_rt_frame( consumer );
668 // Ensure that we have a frame
671 // Get the frame properties
672 properties = MLT_FRAME_PROPERTIES( frame );
675 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
677 // Determine the start time now
678 if ( this->playing && init_video )
680 // Create the video thread
681 pthread_create( &thread, NULL, video_thread, this );
683 // Video doesn't need to be initialised any more
687 // Set playtime for this frame
688 mlt_properties_set_int( properties, "playtime", playtime );
690 while ( this->running && mlt_deque_count( this->queue ) > 15 )
691 nanosleep( &tm, NULL );
693 // Push this frame to the back of the queue
694 pthread_mutex_lock( &this->video_mutex );
695 mlt_deque_push_back( this->queue, frame );
696 pthread_cond_broadcast( &this->video_cond );
697 pthread_mutex_unlock( &this->video_mutex );
699 // Calculate the next playtime
700 playtime += ( duration * 1000 );
704 // Kill the video thread
705 if ( init_video == 0 )
707 pthread_mutex_lock( &this->video_mutex );
708 pthread_cond_broadcast( &this->video_cond );
709 pthread_mutex_unlock( &this->video_mutex );
710 pthread_join( thread, NULL );
713 while( mlt_deque_count( this->queue ) )
714 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
716 this->sdl_screen = NULL;
717 this->audio_avail = 0;
722 static int consumer_get_dimensions( int *width, int *height )
726 // SDL windows manager structure
729 // Specify the SDL Version
730 SDL_VERSION( &wm.version );
733 //sdl_lock_display();
736 // Get the wm structure
737 if ( SDL_GetWMInfo( &wm ) == 1 )
739 // Check that we have the X11 wm
740 if ( wm.subsystem == SDL_SYSWM_X11 )
742 // Get the SDL window
743 Window window = wm.info.x11.window;
745 // Get the display session
746 Display *display = wm.info.x11.display;
748 // Get the window attributes
749 XWindowAttributes attr;
750 XGetWindowAttributes( display, window, &attr );
752 // Determine whether window has changed
753 changed = *width != attr.width || *height != attr.height;
755 // Return width and height
757 *height = attr.height;
762 // Unlock the display
763 //sdl_unlock_display();
768 /** Callback to allow override of the close method.
771 static void consumer_close( mlt_consumer parent )
773 // Get the actual object
774 consumer_sdl this = parent->child;
777 ///mlt_consumer_stop( parent );
779 // Now clean up the rest
780 mlt_consumer_close( parent );
783 mlt_deque_close( this->queue );
786 pthread_mutex_destroy( &this->audio_mutex );
787 pthread_cond_destroy( &this->audio_cond );
789 // Finally clean up this