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>
28 #include <SDL/SDL_syswm.h>
30 /** This classes definition.
33 typedef struct consumer_sdl_s *consumer_sdl;
37 struct mlt_consumer_s parent;
38 mlt_properties properties;
43 uint8_t audio_buffer[ 4096 * 6 ];
45 pthread_mutex_t audio_mutex;
46 pthread_cond_t audio_cond;
57 /** Forward references to static functions.
60 static void consumer_close( mlt_consumer parent );
61 static void *consumer_thread( void * );
62 static int consumer_get_dimensions( int *width, int *height );
64 /** This is what will be called by the factory - anything can be passed in
65 via the argument, but keep it simple.
68 mlt_consumer consumer_sdl_init( char *arg )
70 // Create the consumer object
71 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
73 // If no malloc'd and consumer init ok
74 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
76 // Get the parent consumer object
77 mlt_consumer parent = &this->parent;
79 // We have stuff to clean up, so override the close method
80 parent->close = consumer_close;
82 // get a handle on properties
83 mlt_service service = mlt_consumer_service( parent );
84 this->properties = mlt_service_properties( service );
86 // Set the default volume
87 mlt_properties_set_double( this->properties, "volume", 1.0 );
89 // This is the initialisation of the consumer
91 pthread_mutex_init( &this->audio_mutex, NULL );
92 pthread_cond_init( &this->audio_cond, NULL);
94 // process actual param
95 if ( arg == NULL || !strcmp( arg, "PAL" ) )
100 else if ( !strcmp( arg, "NTSC" ) )
105 else if ( sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
111 // Create the the thread
112 pthread_create( &this->thread, NULL, consumer_thread, this );
114 // Return the consumer produced
118 // malloc or consumer init failed
125 static int sdl_lock_display( )
127 SDL_Surface *screen = SDL_GetVideoSurface( );
128 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
131 static void sdl_unlock_display( )
133 SDL_Surface *screen = SDL_GetVideoSurface( );
134 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
135 SDL_UnlockSurface( screen );
138 void sdl_fill_audio( void *udata, uint8_t *stream, int len )
140 consumer_sdl this = udata;
143 float volume = mlt_properties_get_double( this->properties, "volume" );
145 pthread_mutex_lock( &this->audio_mutex );
147 // Block until audio received
148 while ( this->running && len > this->audio_avail )
149 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
151 if ( this->audio_avail >= len )
153 // Place in the audio buffer
154 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
156 // Remove len from the audio available
157 this->audio_avail -= len;
159 // Remove the samples
160 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
164 // Just to be safe, wipe the stream first
165 memset( stream, 0, len );
167 // Copy what we have into the stream
168 memcpy( stream, this->audio_buffer, this->audio_avail );
171 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
174 this->audio_avail = 0;
177 // We're definitely playing now
180 pthread_cond_broadcast( &this->audio_cond );
181 pthread_mutex_unlock( &this->audio_mutex );
184 /** Threaded wrapper for pipe.
187 static void *consumer_thread( void *arg )
190 consumer_sdl this = arg;
193 mlt_consumer consumer = &this->parent;
195 // Get the service assoicated to the consumer
196 mlt_service service = mlt_consumer_service( consumer );
198 // Get the properties of this consumer
199 mlt_properties properties = this->properties;
201 // Define a frame pointer
204 // internal intialization
205 int sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
206 SDL_Surface *sdl_screen = NULL;
207 SDL_Overlay *sdl_overlay = NULL;
208 uint8_t *buffer = NULL;
212 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
214 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
218 // Loop until told not to
219 while( this->running )
221 // Get a frame from the service (should never return anything other than 0)
222 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
224 mlt_image_format vfmt = mlt_image_yuv422;
225 int width = this->width, height = this->height;
228 mlt_audio_format afmt = mlt_audio_pcm;
235 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
236 if ( init_audio == 1 )
238 SDL_AudioSpec request;
240 // specify audio format
241 request.freq = frequency;
242 request.format = AUDIO_S16;
243 request.channels = channels;
244 request.samples = 2048;
245 request.callback = sdl_fill_audio;
246 request.userdata = (void *)this;
247 if ( SDL_OpenAudio( &request, NULL ) < 0 )
249 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
255 bytes = ( samples * channels * 2 );
256 pthread_mutex_lock( &this->audio_mutex );
257 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
258 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
259 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
260 this->audio_avail += bytes;
261 pthread_cond_broadcast( &this->audio_cond );
262 pthread_mutex_unlock( &this->audio_mutex );
264 if ( this->count == this->size )
267 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
269 this->queue[ this->count ++ ] = frame;
273 // We're working on the oldest frame now
274 frame = this->queue[ 0 ];
276 // Shunt the frames in the queue down
278 for ( i = 1; i < this->count; i ++ )
279 this->queue[ i - 1 ] = this->queue[ i ];
282 // Get the image, width and height
283 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
285 if ( sdl_screen != NULL )
289 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
291 while ( SDL_PollEvent( &event ) )
295 case SDL_VIDEORESIZE:
296 this->window_width = event.resize.w;
297 this->window_height = event.resize.h;
302 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
303 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
304 if ( callback != NULL && producer != NULL )
305 callback( producer, SDL_GetKeyName(event.key.keysym.sym) );
311 if ( width != this->width || height != this->height )
314 this->height = height;
319 if ( sdl_screen == NULL || changed )
321 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
323 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
324 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
326 if ( this->window_width == 0 || this->window_height == 0 )
328 this->window_width = width;
329 this->window_height = height;
332 // open SDL window with video overlay, if possible
333 sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, sdl_flags );
335 if ( sdl_screen != NULL )
338 if ( this->window_width < this->window_height * aspect_ratio )
340 rect.w = this->window_width;
341 rect.h = this->window_width / aspect_ratio;
345 rect.w = this->window_height * aspect_ratio;
346 rect.h = this->window_height;
349 rect.x = ( this->window_width - rect.w ) / 2;
350 rect.y = ( this->window_height - rect.h ) / 2;
352 SDL_SetClipRect( sdl_screen, &rect );
354 // Force an overlay recreation
355 if ( sdl_overlay != NULL )
356 SDL_FreeYUVOverlay( sdl_overlay );
358 sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, sdl_screen );
359 sdl_unlock_display();
363 if ( sdl_screen != NULL && sdl_overlay != NULL )
365 buffer = sdl_overlay->pixels[ 0 ];
366 if ( sdl_lock_display() )
368 if ( SDL_LockYUVOverlay( sdl_overlay ) >= 0 )
370 mlt_resize_yuv422( buffer, this->width, this->height, image, width, height );
371 SDL_UnlockYUVOverlay( sdl_overlay );
372 SDL_DisplayYUVOverlay( sdl_overlay, &sdl_screen->clip_rect );
374 sdl_unlock_display();
385 mlt_frame_close( frame );
388 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
397 if ( init_audio == 0 )
399 if ( sdl_overlay != NULL )
400 SDL_FreeYUVOverlay( sdl_overlay );
406 static int consumer_get_dimensions( int *width, int *height )
410 // SDL windows manager structure
413 // Specify the SDL Version
414 SDL_VERSION( &wm.version );
416 // Get the wm structure
417 if ( SDL_GetWMInfo( &wm ) == 1 )
419 // Check that we have the X11 wm
420 if ( wm.subsystem == SDL_SYSWM_X11 )
422 // Get the SDL window
423 Window window = wm.info.x11.window;
425 // Get the display session
426 Display *display = wm.info.x11.display;
428 // Get the window attributes
429 XWindowAttributes attr;
430 XGetWindowAttributes( display, window, &attr );
432 // Determine whether window has changed
433 changed = *width != attr.width || *height != attr.height;
435 // Return width and height
437 *height = attr.height;
444 /** Callback to allow override of the close method.
447 static void consumer_close( mlt_consumer parent )
449 // Get the actual object
450 consumer_sdl this = parent->child;
452 // Kill the thread and clean up
455 pthread_mutex_lock( &this->audio_mutex );
456 pthread_cond_broadcast( &this->audio_cond );
457 pthread_mutex_unlock( &this->audio_mutex );
459 pthread_join( this->thread, NULL );
460 pthread_mutex_destroy( &this->audio_mutex );
461 pthread_cond_destroy( &this->audio_cond );
463 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
464 parent->close = NULL;
465 mlt_consumer_close( parent );
467 // Finally clean up this