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;
55 double last_frame_aspect;
60 SDL_Surface *sdl_screen;
61 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 );
76 /** This is what will be called by the factory - anything can be passed in
77 via the argument, but keep it simple.
80 mlt_consumer consumer_sdl_init( char *arg )
82 // Create the consumer object
83 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
85 // If no malloc'd and consumer init ok
86 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
89 this->queue = mlt_deque_init( );
91 // Get the parent consumer object
92 mlt_consumer parent = &this->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 this->properties = mlt_service_properties( service );
101 // Set the default volume
102 mlt_properties_set_double( this->properties, "volume", 1.0 );
104 // This is the initialisation of the consumer
105 pthread_mutex_init( &this->audio_mutex, NULL );
106 pthread_cond_init( &this->audio_cond, NULL);
107 pthread_mutex_init( &this->video_mutex, NULL );
108 pthread_cond_init( &this->video_cond, NULL);
110 // Default scaler (for now we'll use nearest)
111 mlt_properties_set( this->properties, "rescale", "nearest" );
113 // Default buffer for low latency
114 mlt_properties_set_int( this->properties, "buffer", 1 );
116 // Default progressive true
117 mlt_properties_set_int( this->properties, "progressive", 0 );
119 // Default audio buffer
120 mlt_properties_set_int( this->properties, "audio_buffer", 1024 );
122 // Get sample aspect ratio
123 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
125 // Default display aspect ratio
126 this->display_aspect = 4.0 / 3.0;
128 // process actual param
129 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
131 this->width = mlt_properties_get_int( this->properties, "width" );
132 this->height = mlt_properties_get_int( this->properties, "height" );
134 // Default window size
135 this->window_width = ( float )this->height * this->display_aspect;
136 this->window_height = this->height;
140 if ( (int)( ( float )this->width / this->height * 1000 ) !=
141 (int)( this->display_aspect * 1000 ) )
144 this->display_aspect = ( float )this->width / this->height;
145 this->aspect_ratio = 1.0;
146 mlt_properties_set_double( this->properties, "aspect_ratio", this->aspect_ratio );
150 this->window_width = this->width;
151 this->window_height = this->height;
155 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
157 // Allow thread to be started/stopped
158 parent->start = consumer_start;
159 parent->stop = consumer_stop;
160 parent->is_stopped = consumer_is_stopped;
162 // Return the consumer produced
166 // malloc or consumer init failed
173 int consumer_start( mlt_consumer parent )
175 consumer_sdl this = parent->child;
177 if ( !this->running )
179 pthread_attr_t thread_attributes;
184 // Allow the user to force resizing to window size
185 if ( mlt_properties_get_int( this->properties, "resize" ) )
187 mlt_properties_set_int( this->properties, "width", this->width );
188 mlt_properties_set_int( this->properties, "height", this->height );
191 // Inherit the scheduling priority
192 pthread_attr_init( &thread_attributes );
193 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
195 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
201 int consumer_stop( mlt_consumer parent )
203 // Get the actual object
204 consumer_sdl this = parent->child;
206 if ( this->joined == 0 )
208 // Kill the thread and clean up
211 pthread_mutex_lock( &this->audio_mutex );
212 pthread_cond_broadcast( &this->audio_cond );
213 pthread_mutex_unlock( &this->audio_mutex );
215 pthread_join( this->thread, NULL );
222 int consumer_is_stopped( mlt_consumer parent )
224 consumer_sdl this = parent->child;
225 return !this->running;
228 static int sdl_lock_display( )
230 SDL_Surface *screen = SDL_GetVideoSurface( );
231 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
234 static void sdl_unlock_display( )
236 SDL_Surface *screen = SDL_GetVideoSurface( );
237 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
238 SDL_UnlockSurface( screen );
241 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
243 consumer_sdl this = udata;
246 float volume = mlt_properties_get_double( this->properties, "volume" );
248 pthread_mutex_lock( &this->audio_mutex );
250 // Block until audio received
251 while ( this->running && len > this->audio_avail )
252 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
254 if ( this->audio_avail >= len )
256 // Place in the audio buffer
257 memcpy( stream, this->audio_buffer, len );
259 // Remove len from the audio available
260 this->audio_avail -= len;
262 // Remove the samples
263 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
267 // Just to be safe, wipe the stream first
268 memset( stream, 0, len );
270 // Copy what we have into the stream
271 memcpy( stream, this->audio_buffer, this->audio_avail );
274 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
277 this->audio_avail = 0;
280 // We're definitely playing now
283 pthread_cond_broadcast( &this->audio_cond );
284 pthread_mutex_unlock( &this->audio_mutex );
287 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
289 // Get the properties of this consumer
290 mlt_properties properties = this->properties;
291 mlt_audio_format afmt = mlt_audio_pcm;
293 // Set the preferred params of the test card signal
294 int channels = mlt_properties_get_int( properties, "channels" );
295 int frequency = mlt_properties_get_int( properties, "frequency" );
296 static int counter = 0;
298 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
303 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
304 *duration = ( ( samples * 1000 ) / frequency );
306 if ( mlt_properties_get_int( properties, "audio_off" ) )
313 if ( init_audio == 1 )
315 SDL_AudioSpec request;
318 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
320 // specify audio format
321 memset( &request, 0, sizeof( SDL_AudioSpec ) );
323 request.freq = frequency;
324 request.format = AUDIO_S16;
325 request.channels = channels;
326 request.samples = audio_buffer;
327 request.callback = sdl_fill_audio;
328 request.userdata = (void *)this;
329 if ( SDL_OpenAudio( &request, &got ) != 0 )
331 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
334 else if ( got.size != 0 )
341 if ( init_audio == 0 )
343 mlt_properties properties = mlt_frame_properties( frame );
344 bytes = ( samples * channels * 2 );
345 pthread_mutex_lock( &this->audio_mutex );
346 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
347 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
350 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
351 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
353 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
354 this->audio_avail += bytes;
356 pthread_cond_broadcast( &this->audio_cond );
357 pthread_mutex_unlock( &this->audio_mutex );
367 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
369 // Get the properties of this consumer
370 mlt_properties properties = this->properties;
372 mlt_image_format vfmt = mlt_image_yuv422;
373 int width = this->width, height = this->height;
377 if ( mlt_properties_get_int( properties, "video_off" ) == 0 )
379 // Get the image, width and height
380 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
383 if ( this->sdl_screen != NULL )
387 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
389 while ( SDL_PollEvent( &event ) )
393 case SDL_VIDEORESIZE:
394 this->window_width = event.resize.w;
395 this->window_height = event.resize.h;
403 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
404 char keyboard[ 2 ] = " ";
405 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
406 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
408 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
409 callback( producer, keyboard );
417 if ( width != this->width || height != this->height || this->last_frame_aspect != mlt_frame_get_aspect_ratio( frame ) )
420 this->height = height;
421 this->last_frame_aspect = mlt_frame_get_aspect_ratio( frame );
425 if ( this->sdl_screen == NULL || changed )
427 // Determine frame's display aspect ratio
428 float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
430 // Determine window's new display aspect ratio
431 float this_aspect = ( float )this->window_width / this->window_height;
433 // If using hardware scaler
434 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
435 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
437 // Special case optimisation to negate odd effect of sample aspect ratio
438 // not corresponding exactly with image resolution.
439 if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) &&
440 ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
442 this->rect.w = this->window_width;
443 this->rect.h = this->window_height;
447 // Use hardware scaler to normalise display aspect ratio
448 this->rect.w = frame_aspect / this_aspect * this->window_width;
449 this->rect.h = this->window_height;
450 if ( this->rect.w > this->window_width )
452 this->rect.w = this->window_width;
453 this->rect.h = this_aspect / frame_aspect * this->window_height;
457 // Special case optimisation to negate odd effect of sample aspect ratio
458 // not corresponding exactly with image resolution.
459 else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) )
461 this->rect.w = this->window_width;
462 this->rect.h = this->window_height;
464 // Use hardware scaler to normalise sample aspect ratio
465 else if ( this->window_height * this->display_aspect > this->window_width )
467 this->rect.w = this->window_width;
468 this->rect.h = this->window_width / this->display_aspect;
472 this->rect.w = this->window_height * this->display_aspect;
473 this->rect.h = this->window_height;
476 this->rect.x = ( this->window_width - this->rect.w ) / 2;
477 this->rect.y = ( this->window_height - this->rect.h ) / 2;
479 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
480 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
481 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
482 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
484 // Force an overlay recreation
485 if ( this->sdl_overlay != NULL )
486 SDL_FreeYUVOverlay( this->sdl_overlay );
488 // open SDL window with video overlay, if possible
490 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
491 sdl_unlock_display();
493 if ( this->sdl_screen != NULL )
495 SDL_SetClipRect( this->sdl_screen, &this->rect );
497 this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
498 sdl_unlock_display();
502 if ( mlt_properties_get_int( properties, "changed" ) )
505 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
506 SDL_SetClipRect( this->sdl_screen, &this->rect );
507 SDL_Flip( this->sdl_screen );
508 sdl_unlock_display();
509 mlt_properties_set_int( properties, "changed", 0 );
512 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
514 this->buffer = this->sdl_overlay->pixels[ 0 ];
516 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
519 memcpy( this->buffer, image, width * height * 2 );
520 SDL_UnlockYUVOverlay( this->sdl_overlay );
521 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
523 sdl_unlock_display();
530 static void *video_thread( void *arg )
533 consumer_sdl this = arg;
535 // Obtain time of thread start
540 mlt_frame next = NULL;
541 mlt_properties properties = NULL;
544 // Get real time flag
545 int real_time = mlt_properties_get_int( this->properties, "real_time" );
547 // Get the current time
548 gettimeofday( &now, NULL );
550 // Determine start time
551 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
553 while ( this->running )
555 // Pop the next frame
556 pthread_mutex_lock( &this->video_mutex );
557 while ( ( next = mlt_deque_pop_front( this->queue ) ) == NULL && this->running )
558 pthread_cond_wait( &this->video_cond, &this->video_mutex );
559 pthread_mutex_unlock( &this->video_mutex );
561 // Get the properties
562 properties = mlt_frame_properties( next );
564 // Get the speed of the frame
565 speed = mlt_properties_get_double( properties, "_speed" );
567 // Get the current time
568 gettimeofday( &now, NULL );
570 // Get the elapsed time
571 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
573 // See if we have to delay the display of the current frame
574 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
576 // Obtain the scheduled playout time
577 mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
579 // Determine the difference between the elapsed time and the scheduled playout time
580 mlt_position difference = scheduled - elapsed;
582 // Smooth playback a bit
583 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
585 tm.tv_sec = difference / 1000000;
586 tm.tv_nsec = ( difference % 1000000 ) * 500;
587 nanosleep( &tm, NULL );
590 // Show current frame if not too old
591 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
592 consumer_play_video( this, next );
594 // If the queue is empty, recalculate start to allow build up again
595 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
597 gettimeofday( &now, NULL );
598 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
602 // This frame can now be closed
603 mlt_frame_close( next );
606 mlt_consumer_stopped( &this->parent );
611 /** Threaded wrapper for pipe.
614 static void *consumer_thread( void *arg )
617 consumer_sdl this = arg;
620 mlt_consumer consumer = &this->parent;
625 // internal intialization
628 mlt_frame frame = NULL;
629 mlt_properties properties = NULL;
631 int64_t playtime = 0;
632 struct timespec tm = { 0, 100000 };
634 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
636 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
640 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
641 SDL_EnableUNICODE( 1 );
643 // Loop until told not to
644 while( this->running )
646 // Get a frame from the attached producer
647 frame = mlt_consumer_rt_frame( consumer );
649 // Ensure that we have a frame
652 // Get the frame properties
653 properties = mlt_frame_properties( frame );
656 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
658 // Determine the start time now
659 if ( this->playing && init_video )
661 // Create the video thread
662 pthread_create( &thread, NULL, video_thread, this );
664 // Video doesn't need to be initialised any more
668 // Set playtime for this frame
669 mlt_properties_set_position( properties, "playtime", playtime );
671 while ( this->running && mlt_deque_count( this->queue ) > 15 )
672 nanosleep( &tm, NULL );
674 // Push this frame to the back of the queue
675 pthread_mutex_lock( &this->video_mutex );
676 mlt_deque_push_back( this->queue, frame );
677 pthread_cond_broadcast( &this->video_cond );
678 pthread_mutex_unlock( &this->video_mutex );
680 // Calculate the next playtime
681 playtime += ( duration * 1000 );
685 // Kill the video thread
686 if ( init_video == 0 )
688 pthread_mutex_lock( &this->video_mutex );
689 pthread_cond_broadcast( &this->video_cond );
690 pthread_mutex_unlock( &this->video_mutex );
691 pthread_join( thread, NULL );
695 if ( this->sdl_overlay != NULL )
696 SDL_FreeYUVOverlay( this->sdl_overlay );
699 while( mlt_deque_count( this->queue ) )
700 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
702 this->sdl_screen = NULL;
703 this->sdl_overlay = NULL;
704 this->audio_avail = 0;
709 static int consumer_get_dimensions( int *width, int *height )
713 // SDL windows manager structure
716 // Specify the SDL Version
717 SDL_VERSION( &wm.version );
722 // Get the wm structure
723 if ( SDL_GetWMInfo( &wm ) == 1 )
725 // Check that we have the X11 wm
726 if ( wm.subsystem == SDL_SYSWM_X11 )
728 // Get the SDL window
729 Window window = wm.info.x11.window;
731 // Get the display session
732 Display *display = wm.info.x11.display;
734 // Get the window attributes
735 XWindowAttributes attr;
736 XGetWindowAttributes( display, window, &attr );
738 // Determine whether window has changed
739 changed = *width != attr.width || *height != attr.height;
741 // Return width and height
743 *height = attr.height;
747 // Unlock the display
753 /** Callback to allow override of the close method.
756 static void consumer_close( mlt_consumer parent )
758 // Get the actual object
759 consumer_sdl this = parent->child;
762 mlt_consumer_stop( parent );
765 mlt_deque_close( this->queue );
768 pthread_mutex_destroy( &this->audio_mutex );
769 pthread_cond_destroy( &this->audio_cond );
771 // Now clean up the rest
772 mlt_consumer_close( parent );
774 // Finally clean up this