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>
30 #include <sys/timeb.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 * 6 ];
46 pthread_mutex_t audio_mutex;
47 pthread_cond_t audio_cond;
55 SDL_Surface *sdl_screen;
56 SDL_Overlay *sdl_overlay;
60 /** Forward references to static functions.
63 static int consumer_start( mlt_consumer parent );
64 static int consumer_stop( mlt_consumer parent );
65 static int consumer_is_stopped( mlt_consumer parent );
66 static void consumer_close( mlt_consumer parent );
67 static void *consumer_thread( void * );
68 static int consumer_get_dimensions( int *width, int *height );
70 /** This is what will be called by the factory - anything can be passed in
71 via the argument, but keep it simple.
74 mlt_consumer consumer_sdl_init( char *arg )
76 // Create the consumer object
77 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
79 // If no malloc'd and consumer init ok
80 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
83 this->queue = mlt_deque_init( );
85 // Get the parent consumer object
86 mlt_consumer parent = &this->parent;
88 // We have stuff to clean up, so override the close method
89 parent->close = consumer_close;
91 // get a handle on properties
92 mlt_service service = mlt_consumer_service( parent );
93 this->properties = mlt_service_properties( service );
95 // Set the default volume
96 mlt_properties_set_double( this->properties, "volume", 1.0 );
98 // This is the initialisation of the consumer
99 pthread_mutex_init( &this->audio_mutex, NULL );
100 pthread_cond_init( &this->audio_cond, NULL);
102 // Default scaler (for now we'll use nearest)
103 mlt_properties_set( this->properties, "rescale", "nearest" );
105 // Default progressive true
106 mlt_properties_set_int( this->properties, "progressive", 1 );
109 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
111 // process actual param
112 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
114 this->width = mlt_properties_get_int( this->properties, "width" );
115 this->height = mlt_properties_get_int( this->properties, "height" );
118 // Default window size
119 this->window_width = (int)( (float)this->height * this->aspect_ratio ) + 1;
120 this->window_height = this->height;
123 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE;
125 // Allow thread to be started/stopped
126 parent->start = consumer_start;
127 parent->stop = consumer_stop;
128 parent->is_stopped = consumer_is_stopped;
130 // Return the consumer produced
134 // malloc or consumer init failed
141 int consumer_start( mlt_consumer parent )
143 consumer_sdl this = parent->child;
145 if ( !this->running )
148 pthread_create( &this->thread, NULL, consumer_thread, this );
154 int consumer_stop( mlt_consumer parent )
156 // Get the actual object
157 consumer_sdl this = parent->child;
161 // Kill the thread and clean up
164 pthread_mutex_lock( &this->audio_mutex );
165 pthread_cond_broadcast( &this->audio_cond );
166 pthread_mutex_unlock( &this->audio_mutex );
168 pthread_join( this->thread, NULL );
174 int consumer_is_stopped( mlt_consumer parent )
176 consumer_sdl this = parent->child;
177 return !this->running;
180 static int sdl_lock_display( )
182 SDL_Surface *screen = SDL_GetVideoSurface( );
183 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
186 static void sdl_unlock_display( )
188 SDL_Surface *screen = SDL_GetVideoSurface( );
189 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
190 SDL_UnlockSurface( screen );
193 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
195 consumer_sdl this = udata;
198 float volume = mlt_properties_get_double( this->properties, "volume" );
200 pthread_mutex_lock( &this->audio_mutex );
202 // Block until audio received
203 while ( this->running && len > this->audio_avail )
204 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
206 if ( this->audio_avail >= len )
208 // Place in the audio buffer
209 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
211 // Remove len from the audio available
212 this->audio_avail -= len;
214 // Remove the samples
215 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
219 // Just to be safe, wipe the stream first
220 memset( stream, 0, len );
222 // Copy what we have into the stream
223 memcpy( stream, this->audio_buffer, this->audio_avail );
226 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
229 this->audio_avail = 0;
232 // We're definitely playing now
235 pthread_cond_broadcast( &this->audio_cond );
236 pthread_mutex_unlock( &this->audio_mutex );
239 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
241 // Get the properties of this consumer
242 mlt_properties properties = this->properties;
243 mlt_audio_format afmt = mlt_audio_pcm;
245 // Set the preferred params of the test card signal
247 int frequency = 48000;
248 static int counter = 0;
249 if ( mlt_properties_get_int( properties, "frequency" ) != 0 )
250 frequency = mlt_properties_get_int( properties, "frequency" );
252 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
257 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
258 *duration = ( ( samples * 1000 ) / frequency );
260 if ( mlt_properties_get_int( properties, "audio_off" ) )
266 if ( init_audio == 1 )
268 SDL_AudioSpec request;
271 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
272 SDL_EnableUNICODE( 1 );
274 // specify audio format
275 memset( &request, 0, sizeof( SDL_AudioSpec ) );
277 request.freq = frequency;
278 request.format = AUDIO_S16;
279 request.channels = channels;
280 request.samples = 2048;
281 request.callback = sdl_fill_audio;
282 request.userdata = (void *)this;
283 if ( SDL_OpenAudio( &request, &got ) != 0 )
285 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
288 else if ( got.size != 0 )
295 if ( init_audio == 0 )
297 bytes = ( samples * channels * 2 );
298 pthread_mutex_lock( &this->audio_mutex );
299 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
300 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
301 mlt_properties properties = mlt_frame_properties( frame );
302 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
303 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
305 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
306 this->audio_avail += bytes;
307 pthread_cond_broadcast( &this->audio_cond );
308 pthread_mutex_unlock( &this->audio_mutex );
318 static int consumer_play_video( consumer_sdl this, mlt_frame frame, int64_t elapsed, int skip, int64_t playtime )
320 // Get the properties of this consumer
321 mlt_properties properties = this->properties;
323 mlt_image_format vfmt = mlt_image_yuv422;
324 int width = this->width, height = this->height;
329 if ( mlt_properties_get_int( properties, "video_off" ) )
331 mlt_frame_close( frame );
336 if ( this->sdl_screen != NULL )
340 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
342 while ( SDL_PollEvent( &event ) )
346 case SDL_VIDEORESIZE:
347 this->window_width = event.resize.w;
348 this->window_height = event.resize.h;
353 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
354 char keyboard[ 2 ] = " ";
355 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
356 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
358 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
359 callback( producer, keyboard );
368 mlt_properties_set_int( mlt_frame_properties( frame ), "skip", skip );
369 mlt_properties_set_position( mlt_frame_properties( frame ), "playtime", playtime );
371 // Push this frame to the back of the queue
372 mlt_deque_push_back( this->queue, frame );
377 // We might want to use an old frame if the current frame is skipped
378 mlt_frame candidate = NULL;
380 while ( frame == NULL && mlt_deque_count( this->queue ) )
382 frame = mlt_deque_peek_front( this->queue );
383 show = !mlt_properties_get_int( mlt_frame_properties( frame ), "skip" );
384 playtime = mlt_properties_get_position( mlt_frame_properties( frame ), "playtime" );
386 // Check if frame is in the future or past
389 frame = mlt_deque_pop_front( this->queue );
390 mlt_frame_close( frame );
393 else if ( playtime > elapsed )
395 // no frame to show or remove
399 else if ( playtime < elapsed - 40 )
401 if ( candidate != NULL )
402 mlt_frame_close( candidate );
403 candidate = mlt_deque_pop_front( this->queue );
408 // Get the frame at the front of the queue
409 frame = mlt_deque_pop_front( this->queue );
415 else if ( candidate != NULL )
416 mlt_frame_close( candidate );
419 if ( this->playing && frame != NULL )
421 // Get the image, width and height
422 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
424 if ( width != this->width || height != this->height )
427 this->height = height;
431 if ( this->sdl_screen == NULL || changed )
433 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
434 float display_aspect_ratio = (float)width / (float)height;
437 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
438 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
440 if ( aspect_ratio == 1 )
442 rect.w = this->window_width;
443 rect.h = this->window_height;
445 else if ( this->window_width < this->window_height * aspect_ratio )
447 rect.w = this->window_width;
448 rect.h = this->window_width / aspect_ratio;
452 rect.w = this->window_height * aspect_ratio;
453 rect.h = this->window_height;
456 if ( mlt_properties_get_int( properties, "scale_overlay" ) )
458 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
459 rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
460 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
461 rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
464 rect.x = ( this->window_width - rect.w ) / 2;
465 rect.y = ( this->window_height - rect.h ) / 2;
467 // Force an overlay recreation
468 if ( this->sdl_overlay != NULL )
469 SDL_FreeYUVOverlay( this->sdl_overlay );
471 // open SDL window with video overlay, if possible
472 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
474 if ( this->sdl_screen != NULL )
476 SDL_SetClipRect( this->sdl_screen, &rect );
479 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
480 sdl_unlock_display();
484 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
486 this->buffer = this->sdl_overlay->pixels[ 0 ];
487 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
489 memcpy( this->buffer, image, width * height * 2 );
490 SDL_UnlockYUVOverlay( this->sdl_overlay );
491 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
498 mlt_frame_close( frame );
500 if ( mlt_deque_count( this->queue ) && !skip )
502 // Tell the producers about our scale relative to the normalisation
503 frame = mlt_deque_peek_front( this->queue );
504 mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_scale",
505 ( double )height / mlt_properties_get_double( properties, "height" ) );
506 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
512 /** Threaded wrapper for pipe.
515 static void *consumer_thread( void *arg )
518 consumer_sdl this = arg;
521 mlt_consumer consumer = &this->parent;
523 // internal intialization
526 // Obtain time of thread start
531 int64_t playtime = 0;
534 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
536 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
540 // Loop until told not to
541 while( this->running )
543 // Get a frame from the attached producer
544 mlt_frame frame = mlt_consumer_get_frame( consumer );
546 // Ensure that we have a frame
551 // SDL adapts display aspect, but set this so pixel aspect can be normalised
552 // mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_aspect_ratio",
553 // mlt_frame_get_aspect_ratio( frame ) );
556 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
560 // Get the current time
563 // Determine elapsed time
565 start = ( int64_t )now.time * 1000 + now.millitm;
567 elapsed = ( ( int64_t )now.time * 1000 + now.millitm ) - start;
569 skip = playtime < elapsed;
572 start = start + ( elapsed - playtime );
575 consumer_play_video( this, frame, elapsed, skip, playtime );
577 playtime += duration;
582 if ( init_audio == 0 )
584 if ( this->sdl_overlay != NULL )
585 SDL_FreeYUVOverlay( this->sdl_overlay );
588 while( mlt_deque_count( this->queue ) )
589 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
591 this->sdl_screen = NULL;
592 this->sdl_overlay = NULL;
593 this->audio_avail = 0;
598 static int consumer_get_dimensions( int *width, int *height )
602 // SDL windows manager structure
605 // Specify the SDL Version
606 SDL_VERSION( &wm.version );
608 // Get the wm structure
609 if ( SDL_GetWMInfo( &wm ) == 1 )
611 // Check that we have the X11 wm
612 if ( wm.subsystem == SDL_SYSWM_X11 )
614 // Get the SDL window
615 Window window = wm.info.x11.window;
617 // Get the display session
618 Display *display = wm.info.x11.display;
620 // Get the window attributes
621 XWindowAttributes attr;
622 XGetWindowAttributes( display, window, &attr );
624 // Determine whether window has changed
625 changed = *width != attr.width || *height != attr.height;
627 // Return width and height
629 *height = attr.height;
636 /** Callback to allow override of the close method.
639 static void consumer_close( mlt_consumer parent )
641 // Get the actual object
642 consumer_sdl this = parent->child;
645 mlt_consumer_stop( parent );
648 pthread_mutex_destroy( &this->audio_mutex );
649 pthread_cond_destroy( &this->audio_cond );
651 // Now clean up the rest
652 mlt_consumer_close( parent );
654 // Finally clean up this