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;
44 uint8_t audio_buffer[ 4096 * 10 ];
46 pthread_mutex_t audio_mutex;
47 pthread_cond_t audio_cond;
48 pthread_mutex_t video_mutex;
49 pthread_cond_t video_cond;
54 double last_frame_aspect;
59 SDL_Surface *sdl_screen;
60 SDL_Overlay *sdl_overlay;
65 /** Forward references to static functions.
68 static int consumer_start( mlt_consumer parent );
69 static int consumer_stop( mlt_consumer parent );
70 static int consumer_is_stopped( mlt_consumer parent );
71 static void consumer_close( mlt_consumer parent );
72 static void *consumer_thread( void * );
73 static int consumer_get_dimensions( int *width, int *height );
75 /** This is what will be called by the factory - anything can be passed in
76 via the argument, but keep it simple.
79 mlt_consumer consumer_sdl_init( char *arg )
81 // Create the consumer object
82 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
84 // If no malloc'd and consumer init ok
85 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
88 this->queue = mlt_deque_init( );
90 // Get the parent consumer object
91 mlt_consumer parent = &this->parent;
93 // We have stuff to clean up, so override the close method
94 parent->close = consumer_close;
96 // get a handle on properties
97 mlt_service service = mlt_consumer_service( parent );
98 this->properties = mlt_service_properties( service );
100 // Set the default volume
101 mlt_properties_set_double( this->properties, "volume", 1.0 );
103 // This is the initialisation of the consumer
104 pthread_mutex_init( &this->audio_mutex, NULL );
105 pthread_cond_init( &this->audio_cond, NULL);
106 pthread_mutex_init( &this->video_mutex, NULL );
107 pthread_cond_init( &this->video_cond, NULL);
109 // Default scaler (for now we'll use nearest)
110 mlt_properties_set( this->properties, "rescale", "nearest" );
112 // Default buffer for low latency
113 mlt_properties_set_int( this->properties, "buffer", 1 );
115 // Default progressive true
116 mlt_properties_set_int( this->properties, "progressive", 0 );
118 // Default audio buffer
119 mlt_properties_set_int( this->properties, "audio_buffer", 1024 );
121 // Get sample aspect ratio
122 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
124 // Default display aspect ratio
125 this->display_aspect = 4.0 / 3.0;
127 // process actual param
128 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
130 this->width = mlt_properties_get_int( this->properties, "width" );
131 this->height = mlt_properties_get_int( this->properties, "height" );
133 // Default window size
134 this->window_width = ( float )this->height * this->display_aspect;
135 this->window_height = this->height;
139 if ( (int)( ( float )this->width / this->height * 1000 ) !=
140 (int)( this->display_aspect * 1000 ) )
143 this->display_aspect = ( float )this->width / this->height;
144 this->aspect_ratio = 1.0;
145 mlt_properties_set_double( this->properties, "aspect_ratio", this->aspect_ratio );
149 this->window_width = this->width;
150 this->window_height = this->height;
154 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
156 // Allow thread to be started/stopped
157 parent->start = consumer_start;
158 parent->stop = consumer_stop;
159 parent->is_stopped = consumer_is_stopped;
161 // Return the consumer produced
165 // malloc or consumer init failed
172 int consumer_start( mlt_consumer parent )
174 consumer_sdl this = parent->child;
176 if ( !this->running )
178 pthread_attr_t thread_attributes;
182 // Allow the user to force resizing to window size
183 if ( mlt_properties_get_int( this->properties, "resize" ) )
185 mlt_properties_set_int( this->properties, "width", this->width );
186 mlt_properties_set_int( this->properties, "height", this->height );
189 // Inherit the scheduling priority
190 pthread_attr_init( &thread_attributes );
191 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
193 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
199 int consumer_stop( mlt_consumer parent )
201 // Get the actual object
202 consumer_sdl this = parent->child;
206 // Kill the thread and clean up
209 pthread_mutex_lock( &this->audio_mutex );
210 pthread_cond_broadcast( &this->audio_cond );
211 pthread_mutex_unlock( &this->audio_mutex );
213 pthread_join( this->thread, NULL );
219 int consumer_is_stopped( mlt_consumer parent )
221 consumer_sdl this = parent->child;
222 return !this->running;
225 static int sdl_lock_display( )
227 SDL_Surface *screen = SDL_GetVideoSurface( );
228 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
231 static void sdl_unlock_display( )
233 SDL_Surface *screen = SDL_GetVideoSurface( );
234 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
235 SDL_UnlockSurface( screen );
238 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
240 consumer_sdl this = udata;
243 float volume = mlt_properties_get_double( this->properties, "volume" );
245 pthread_mutex_lock( &this->audio_mutex );
247 // Block until audio received
248 while ( this->running && len > this->audio_avail )
249 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
251 if ( this->audio_avail >= len )
253 // Place in the audio buffer
254 memcpy( stream, this->audio_buffer, len );
256 // Remove len from the audio available
257 this->audio_avail -= len;
259 // Remove the samples
260 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
264 // Just to be safe, wipe the stream first
265 memset( stream, 0, len );
267 // Copy what we have into the stream
268 memcpy( stream, this->audio_buffer, this->audio_avail );
271 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
274 this->audio_avail = 0;
277 // We're definitely playing now
280 pthread_cond_broadcast( &this->audio_cond );
281 pthread_mutex_unlock( &this->audio_mutex );
284 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
286 // Get the properties of this consumer
287 mlt_properties properties = this->properties;
288 mlt_audio_format afmt = mlt_audio_pcm;
290 // Set the preferred params of the test card signal
291 int channels = mlt_properties_get_int( properties, "channels" );
292 int frequency = mlt_properties_get_int( properties, "frequency" );
293 static int counter = 0;
295 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
300 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
301 *duration = ( ( samples * 1000 ) / frequency );
303 if ( mlt_properties_get_int( properties, "audio_off" ) )
310 if ( init_audio == 1 )
312 SDL_AudioSpec request;
315 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
317 // specify audio format
318 memset( &request, 0, sizeof( SDL_AudioSpec ) );
320 request.freq = frequency;
321 request.format = AUDIO_S16;
322 request.channels = channels;
323 request.samples = audio_buffer;
324 request.callback = sdl_fill_audio;
325 request.userdata = (void *)this;
326 if ( SDL_OpenAudio( &request, &got ) != 0 )
328 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
331 else if ( got.size != 0 )
338 if ( init_audio == 0 )
340 mlt_properties properties = mlt_frame_properties( frame );
341 bytes = ( samples * channels * 2 );
342 pthread_mutex_lock( &this->audio_mutex );
343 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
344 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
347 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
348 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
350 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
351 this->audio_avail += bytes;
353 pthread_cond_broadcast( &this->audio_cond );
354 pthread_mutex_unlock( &this->audio_mutex );
364 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
366 // Get the properties of this consumer
367 mlt_properties properties = this->properties;
369 mlt_image_format vfmt = mlt_image_yuv422;
370 int width = this->width, height = this->height;
374 if ( mlt_properties_get_int( properties, "video_off" ) == 0 )
376 // Get the image, width and height
377 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
380 if ( this->sdl_screen != NULL )
384 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
386 while ( SDL_PollEvent( &event ) )
390 case SDL_VIDEORESIZE:
391 this->window_width = event.resize.w;
392 this->window_height = event.resize.h;
400 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
401 char keyboard[ 2 ] = " ";
402 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
403 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
405 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
406 callback( producer, keyboard );
414 if ( width != this->width || height != this->height || this->last_frame_aspect != mlt_frame_get_aspect_ratio( frame ) )
417 this->height = height;
418 this->last_frame_aspect = mlt_frame_get_aspect_ratio( frame );
422 if ( this->sdl_screen == NULL || changed )
424 // Determine frame's display aspect ratio
425 float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
427 // Determine window's new display aspect ratio
428 float this_aspect = ( float )this->window_width / this->window_height;
430 // If using hardware scaler
431 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
432 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
434 // Special case optimisation to negate odd effect of sample aspect ratio
435 // not corresponding exactly with image resolution.
436 if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) &&
437 ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
439 this->rect.w = this->window_width;
440 this->rect.h = this->window_height;
444 // Use hardware scaler to normalise display aspect ratio
445 this->rect.w = frame_aspect / this_aspect * this->window_width;
446 this->rect.h = this->window_height;
447 if ( this->rect.w > this->window_width )
449 this->rect.w = this->window_width;
450 this->rect.h = this_aspect / frame_aspect * this->window_height;
454 // Special case optimisation to negate odd effect of sample aspect ratio
455 // not corresponding exactly with image resolution.
456 else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) )
458 this->rect.w = this->window_width;
459 this->rect.h = this->window_height;
461 // Use hardware scaler to normalise sample aspect ratio
462 else if ( this->window_height * this->display_aspect > this->window_width )
464 this->rect.w = this->window_width;
465 this->rect.h = this->window_width / this->display_aspect;
469 this->rect.w = this->window_height * this->display_aspect;
470 this->rect.h = this->window_height;
473 this->rect.x = ( this->window_width - this->rect.w ) / 2;
474 this->rect.y = ( this->window_height - this->rect.h ) / 2;
476 // Force an overlay recreation
477 if ( this->sdl_overlay != NULL )
478 SDL_FreeYUVOverlay( this->sdl_overlay );
480 // open SDL window with video overlay, if possible
482 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
483 sdl_unlock_display();
485 if ( this->sdl_screen != NULL )
487 SDL_SetClipRect( this->sdl_screen, &this->rect );
489 this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
490 sdl_unlock_display();
494 if ( mlt_properties_get_int( properties, "changed" ) )
497 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
498 SDL_SetClipRect( this->sdl_screen, &this->rect );
499 SDL_Flip( this->sdl_screen );
500 sdl_unlock_display();
501 mlt_properties_set_int( properties, "changed", 0 );
504 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
506 this->buffer = this->sdl_overlay->pixels[ 0 ];
508 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
511 memcpy( this->buffer, image, width * height * 2 );
512 SDL_UnlockYUVOverlay( this->sdl_overlay );
513 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
515 sdl_unlock_display();
522 static void *video_thread( void *arg )
525 consumer_sdl this = arg;
527 // Obtain time of thread start
532 mlt_frame next = NULL;
533 mlt_properties properties = NULL;
536 // Get real time flag
537 int real_time = mlt_properties_get_int( this->properties, "real_time" );
539 // Get the current time
540 gettimeofday( &now, NULL );
542 // Determine start time
543 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
545 while ( this->running )
547 // Pop the next frame
548 pthread_mutex_lock( &this->video_mutex );
549 while ( ( next = mlt_deque_pop_front( this->queue ) ) == NULL && this->running )
550 pthread_cond_wait( &this->video_cond, &this->video_mutex );
551 pthread_mutex_unlock( &this->video_mutex );
553 // Get the properties
554 properties = mlt_frame_properties( next );
556 // Get the speed of the frame
557 speed = mlt_properties_get_double( properties, "_speed" );
559 // Get the current time
560 gettimeofday( &now, NULL );
562 // Get the elapsed time
563 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
565 // See if we have to delay the display of the current frame
566 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
568 // Obtain the scheduled playout time
569 mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
571 // Determine the difference between the elapsed time and the scheduled playout time
572 mlt_position difference = scheduled - elapsed;
574 // Smooth playback a bit
575 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
577 tm.tv_sec = difference / 1000000;
578 tm.tv_nsec = ( difference % 1000000 ) * 500;
579 nanosleep( &tm, NULL );
582 // Show current frame if not too old
583 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
584 consumer_play_video( this, next );
586 // If the queue is empty, recalculate start to allow build up again
587 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
589 gettimeofday( &now, NULL );
590 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
594 // This frame can now be closed
595 mlt_frame_close( next );
601 /** Threaded wrapper for pipe.
604 static void *consumer_thread( void *arg )
607 consumer_sdl this = arg;
610 mlt_consumer consumer = &this->parent;
615 // internal intialization
618 mlt_frame frame = NULL;
619 mlt_properties properties = NULL;
621 int64_t playtime = 0;
622 struct timespec tm = { 0, 100000 };
624 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
626 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
630 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
631 SDL_EnableUNICODE( 1 );
633 // Loop until told not to
634 while( this->running )
636 // Get a frame from the attached producer
637 frame = mlt_consumer_rt_frame( consumer );
639 // Ensure that we have a frame
642 // Get the frame properties
643 properties = mlt_frame_properties( frame );
646 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
648 // Determine the start time now
649 if ( this->playing && init_video )
651 // Create the video thread
652 pthread_create( &thread, NULL, video_thread, this );
654 // Video doesn't need to be initialised any more
658 // Set playtime for this frame
659 mlt_properties_set_position( properties, "playtime", playtime );
661 while ( this->running && mlt_deque_count( this->queue ) > 15 )
662 nanosleep( &tm, NULL );
664 // Push this frame to the back of the queue
665 pthread_mutex_lock( &this->video_mutex );
666 mlt_deque_push_back( this->queue, frame );
667 pthread_cond_broadcast( &this->video_cond );
668 pthread_mutex_unlock( &this->video_mutex );
670 // Calculate the next playtime
671 playtime += ( duration * 1000 );
675 // Kill the video thread
676 if ( init_video == 0 )
678 pthread_mutex_lock( &this->video_mutex );
679 pthread_cond_broadcast( &this->video_cond );
680 pthread_mutex_unlock( &this->video_mutex );
681 pthread_join( thread, NULL );
685 if ( this->sdl_overlay != NULL )
686 SDL_FreeYUVOverlay( this->sdl_overlay );
689 while( mlt_deque_count( this->queue ) )
690 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
692 this->sdl_screen = NULL;
693 this->sdl_overlay = NULL;
694 this->audio_avail = 0;
699 static int consumer_get_dimensions( int *width, int *height )
703 // SDL windows manager structure
706 // Specify the SDL Version
707 SDL_VERSION( &wm.version );
712 // Get the wm structure
713 if ( SDL_GetWMInfo( &wm ) == 1 )
715 // Check that we have the X11 wm
716 if ( wm.subsystem == SDL_SYSWM_X11 )
718 // Get the SDL window
719 Window window = wm.info.x11.window;
721 // Get the display session
722 Display *display = wm.info.x11.display;
724 // Get the window attributes
725 XWindowAttributes attr;
726 XGetWindowAttributes( display, window, &attr );
728 // Determine whether window has changed
729 changed = *width != attr.width || *height != attr.height;
731 // Return width and height
733 *height = attr.height;
737 // Unlock the display
743 /** Callback to allow override of the close method.
746 static void consumer_close( mlt_consumer parent )
748 // Get the actual object
749 consumer_sdl this = parent->child;
752 mlt_consumer_stop( parent );
755 mlt_deque_close( this->queue );
758 pthread_mutex_destroy( &this->audio_mutex );
759 pthread_cond_destroy( &this->audio_cond );
761 // Now clean up the rest
762 mlt_consumer_close( parent );
764 // Finally clean up this