]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
- Removed 'resize' property logic and width/height confusion
[mlt] / src / modules / sdl / consumer_sdl.c
1 /*
2  * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30 #include <sys/time.h>
31
32 #ifdef __DARWIN__
33 #       include "consumer_sdl_osx_hack.h"
34 #       import <AppKit/NSApplication.h>
35 #       import <Foundation/Foundation.h>
36 #endif
37
38
39 /** This classes definition.
40 */
41
42 typedef struct consumer_sdl_s *consumer_sdl;
43
44 struct consumer_sdl_s
45 {
46         struct mlt_consumer_s parent;
47         mlt_properties properties;
48         mlt_deque queue;
49         pthread_t thread;
50         int joined;
51         int running;
52         uint8_t audio_buffer[ 4096 * 10 ];
53         int audio_avail;
54         pthread_mutex_t audio_mutex;
55         pthread_cond_t audio_cond;
56         pthread_mutex_t video_mutex;
57         pthread_cond_t video_cond;
58         int window_width;
59         int window_height;
60         int previous_width;
61         int previous_height;
62         int width;
63         int height;
64         int playing;
65         int sdl_flags;
66         SDL_Surface *sdl_screen;
67         SDL_Overlay *sdl_overlay;
68         SDL_Rect rect;
69         uint8_t *buffer;
70         int bpp;
71 };
72
73 /** Forward references to static functions.
74 */
75
76 static int consumer_start( mlt_consumer parent );
77 static int consumer_stop( mlt_consumer parent );
78 static int consumer_is_stopped( mlt_consumer parent );
79 static void consumer_close( mlt_consumer parent );
80 static void *consumer_thread( void * );
81 static int consumer_get_dimensions( int *width, int *height );
82 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
83
84 /** This is what will be called by the factory - anything can be passed in
85         via the argument, but keep it simple.
86 */
87
88 mlt_consumer consumer_sdl_init( char *arg )
89 {
90 #ifdef __DARWIN__
91         // Initialize Cocoa
92         NSApplicationLoad();
93         [NSApplication sharedApplication];
94
95         // Spawn a fake thread so that cocoa knows to be multithreaded
96         DummyThread *dummy = [[DummyThread alloc] init];
97         [NSThread detachNewThreadSelector:@selector(startThread:) toTarget:dummy withObject:nil];
98 #endif
99
100         // Create the consumer object
101         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
102
103         // If no malloc'd and consumer init ok
104         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
105         {
106                 // Create the queue
107                 this->queue = mlt_deque_init( );
108
109                 // Get the parent consumer object
110                 mlt_consumer parent = &this->parent;
111
112                 // We have stuff to clean up, so override the close method
113                 parent->close = consumer_close;
114
115                 // get a handle on properties
116                 mlt_service service = MLT_CONSUMER_SERVICE( parent );
117                 this->properties = MLT_SERVICE_PROPERTIES( service );
118
119                 // Default display aspect ratio
120                 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
121                 
122                 // Set the default volume
123                 mlt_properties_set_double( this->properties, "volume", 1.0 );
124
125                 // This is the initialisation of the consumer
126                 pthread_mutex_init( &this->audio_mutex, NULL );
127                 pthread_cond_init( &this->audio_cond, NULL);
128                 pthread_mutex_init( &this->video_mutex, NULL );
129                 pthread_cond_init( &this->video_cond, NULL);
130                 
131                 // Default scaler (for now we'll use nearest)
132                 mlt_properties_set( this->properties, "rescale", "nearest" );
133
134                 // Default buffer for low latency
135                 mlt_properties_set_int( this->properties, "buffer", 1 );
136
137                 // Default progressive true
138                 mlt_properties_set_int( this->properties, "progressive", 0 );
139
140                 // Default audio buffer
141                 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
142
143                 // Ensure we don't join on a non-running object
144                 this->joined = 1;
145                 
146                 // process actual param
147                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
148                 {
149                         this->width = mlt_properties_get_int( this->properties, "width" );
150                         this->height = mlt_properties_get_int( this->properties, "height" );
151                 }
152
153                 // Default window size
154                 this->window_width = ( double )this->height * display_ratio;
155                 this->window_height = this->height;
156
157                 // Set the sdl flags
158                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
159
160                 // Allow thread to be started/stopped
161                 parent->start = consumer_start;
162                 parent->stop = consumer_stop;
163                 parent->is_stopped = consumer_is_stopped;
164
165                 // Register specific events
166                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
167
168                 // Return the consumer produced
169                 return parent;
170         }
171
172         // malloc or consumer init failed
173         free( this );
174
175         // Indicate failure
176         return NULL;
177 }
178
179 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
180 {
181         if ( listener != NULL )
182                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
183 }
184
185 int consumer_start( mlt_consumer parent )
186 {
187         consumer_sdl this = parent->child;
188
189         if ( !this->running )
190         {
191                 consumer_stop( parent );
192
193                 this->running = 1;
194                 this->joined = 0;
195
196                 this->width = mlt_properties_get_int( this->properties, "width" );
197                 this->height = mlt_properties_get_int( this->properties, "height" );
198
199                 pthread_create( &this->thread, NULL, consumer_thread, this );
200         }
201
202         return 0;
203 }
204
205 int consumer_stop( mlt_consumer parent )
206 {
207         // Get the actual object
208         consumer_sdl this = parent->child;
209
210         if ( this->joined == 0 )
211         {
212                 // Kill the thread and clean up
213                 this->joined = 1;
214                 this->running = 0;
215                 pthread_join( this->thread, NULL );
216         }
217
218         return 0;
219 }
220
221 int consumer_is_stopped( mlt_consumer parent )
222 {
223         consumer_sdl this = parent->child;
224         return !this->running;
225 }
226
227 static int sdl_lock_display( )
228 {
229         SDL_Surface *screen = SDL_GetVideoSurface( );
230         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
231 }
232
233 static void sdl_unlock_display( )
234 {
235         SDL_Surface *screen = SDL_GetVideoSurface( );
236         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
237                 SDL_UnlockSurface( screen );
238 }
239
240 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
241 {
242         consumer_sdl this = udata;
243
244         // Get the volume
245         double volume = mlt_properties_get_double( this->properties, "volume" );
246
247         pthread_mutex_lock( &this->audio_mutex );
248
249         // Block until audio received
250         while ( this->running && len > this->audio_avail )
251                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
252
253         if ( this->audio_avail >= len )
254         {
255                 // Place in the audio buffer
256                 if ( volume != 1.0 )
257                         SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
258                 else
259                         memcpy( stream, this->audio_buffer, len );
260
261                 // Remove len from the audio available
262                 this->audio_avail -= len;
263
264                 // Remove the samples
265                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
266         }
267         else
268         {
269                 // Just to be safe, wipe the stream first
270                 memset( stream, 0, len );
271
272                 // Copy what we have into the stream
273                 memcpy( stream, this->audio_buffer, this->audio_avail );
274
275                 // Mix the audio 
276                 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
277
278                 // No audio left
279                 this->audio_avail = 0;
280         }
281
282         // We're definitely playing now
283         this->playing = 1;
284
285         pthread_cond_broadcast( &this->audio_cond );
286         pthread_mutex_unlock( &this->audio_mutex );
287 }
288
289 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
290 {
291         // Get the properties of this consumer
292         mlt_properties properties = this->properties;
293         mlt_audio_format afmt = mlt_audio_pcm;
294
295         // Set the preferred params of the test card signal
296         int channels = mlt_properties_get_int( properties, "channels" );
297         int frequency = mlt_properties_get_int( properties, "frequency" );
298         static int counter = 0;
299
300         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
301         
302         int16_t *pcm;
303         int bytes;
304
305         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
306         *duration = ( ( samples * 1000 ) / frequency );
307
308         if ( mlt_properties_get_int( properties, "audio_off" ) )
309         {
310                 this->playing = 1;
311                 init_audio = 1;
312                 return init_audio;
313         }
314
315         if ( init_audio == 1 )
316         {
317                 SDL_AudioSpec request;
318                 SDL_AudioSpec got;
319
320                 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
321
322                 // specify audio format
323                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
324                 this->playing = 0;
325                 request.freq = frequency;
326                 request.format = AUDIO_S16;
327                 request.channels = channels;
328                 request.samples = audio_buffer;
329                 request.callback = sdl_fill_audio;
330                 request.userdata = (void *)this;
331                 if ( SDL_OpenAudio( &request, &got ) != 0 )
332                 {
333                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
334                         init_audio = 2;
335                 }
336                 else if ( got.size != 0 )
337                 {
338                         SDL_PauseAudio( 0 );
339                         init_audio = 0;
340                 }
341         }
342
343         if ( init_audio == 0 )
344         {
345                 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
346                 bytes = ( samples * channels * 2 );
347                 pthread_mutex_lock( &this->audio_mutex );
348                 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
349                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
350                 if ( this->running )
351                 {
352                         if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
353                                 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
354                         else
355                                 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
356                         this->audio_avail += bytes;
357                 }
358                 pthread_cond_broadcast( &this->audio_cond );
359                 pthread_mutex_unlock( &this->audio_mutex );
360         }
361         else
362         {
363                 this->playing = 1;
364         }
365
366         return init_audio;
367 }
368
369 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
370 {
371         // Get the properties of this consumer
372         mlt_properties properties = this->properties;
373
374         mlt_image_format vfmt = mlt_image_yuv422;
375         int width = this->width, height = this->height;
376         uint8_t *image;
377         int changed = 0;
378
379         if ( this->running && mlt_properties_get_int( properties, "video_off" ) == 0 )
380         {
381                 // Get the image, width and height
382                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
383                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
384                 
385                 // Handle events
386                 if ( this->sdl_screen != NULL )
387                 {
388                         SDL_Event event;
389         
390                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
391
392                         while ( SDL_PollEvent( &event ) )
393                         {
394                                 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
395
396                                 switch( event.type )
397                                 {
398                                         case SDL_VIDEORESIZE:
399                                                 this->window_width = event.resize.w;
400                                                 this->window_height = event.resize.h;
401                                                 changed = 1;
402                                                 break;
403                                         case SDL_QUIT:
404                                                 this->running = 0;
405                                                 break;
406                                         case SDL_KEYDOWN:
407                                                 {
408                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
409                                                         char keyboard[ 2 ] = " ";
410                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
411                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
412                                                         {
413                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
414                                                                 callback( producer, keyboard );
415                                                         }
416                                                 }
417                                                 break;
418                                 }
419                         }
420                 }
421         
422                 if ( width != this->width || height != this->height )
423                 {
424                         if ( this->sdl_overlay != NULL )
425                                 SDL_FreeYUVOverlay( this->sdl_overlay );
426                         this->sdl_overlay = NULL;
427                 }
428
429                 if ( this->running && ( this->sdl_screen == NULL || changed ) )
430                 {
431                         // Force an overlay recreation
432                         if ( this->sdl_overlay != NULL )
433                                 SDL_FreeYUVOverlay( this->sdl_overlay );
434                         this->sdl_overlay = NULL;
435
436                         // open SDL window with video overlay, if possible
437                         sdl_lock_display();
438                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
439                         sdl_unlock_display();
440                         if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
441                                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
442                         //SDL_Flip( this->sdl_screen );
443                         mlt_properties_set_int( properties, "changed", 0 );
444                 }
445                 else if ( mlt_properties_get_int( properties, "changed" ) )
446                 {
447                         sdl_lock_display();
448                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
449                         SDL_SetClipRect( this->sdl_screen, &this->rect );
450                         sdl_unlock_display();
451                         mlt_properties_set_int( properties, "changed", 0 );
452                 }
453
454                 if ( this->running )
455                 {
456                         // Determine window's new display aspect ratio
457                         double this_aspect = ( double )this->window_width / this->window_height;
458
459                         // Get the display aspect ratio
460                         double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
461
462                         // Determine frame's display aspect ratio
463                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
464
465                         // Store the width and height received
466                         this->width = width;
467                         this->height = height;
468
469                         // If using hardware scaler
470                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
471                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
472                         {
473                                 // Use hardware scaler to normalise display aspect ratio
474                                 this->rect.w = frame_aspect / this_aspect * this->window_width;
475                                 this->rect.h = this->window_height;
476                                 if ( this->rect.w > this->window_width )
477                                 {
478                                         this->rect.w = this->window_width;
479                                         this->rect.h = this_aspect / frame_aspect * this->window_height;
480                                 }
481                         }
482                         // Special case optimisation to negate odd effect of sample aspect ratio
483                         // not corresponding exactly with image resolution.
484                         else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) ) 
485                         {
486                                 this->rect.w = this->window_width;
487                                 this->rect.h = this->window_height;
488                         }
489                         // Use hardware scaler to normalise sample aspect ratio
490                         else if ( this->window_height * display_ratio > this->window_width )
491                         {
492                                 this->rect.w = this->window_width;
493                                 this->rect.h = this->window_width / display_ratio;
494                         }
495                         else
496                         {
497                                 this->rect.w = this->window_height * display_ratio;
498                                 this->rect.h = this->window_height;
499                         }
500                         
501                         this->rect.x = ( this->window_width - this->rect.w ) / 2;
502                         this->rect.y = ( this->window_height - this->rect.h ) / 2;
503
504                         mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
505                         mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
506                         mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
507                         mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
508
509                         SDL_SetClipRect( this->sdl_screen, &this->rect );
510                 }
511
512                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
513                 {
514                         SDL_SetClipRect( this->sdl_screen, &this->rect );
515                         SDL_Flip( this->sdl_screen );
516                         sdl_lock_display();
517                         this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
518                         sdl_unlock_display();
519                 }
520
521                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
522                 {
523                         this->buffer = this->sdl_overlay->pixels[ 0 ];
524                         sdl_lock_display();
525                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
526                         {
527                                 if ( image != NULL )
528                                         memcpy( this->buffer, image, width * height * 2 );
529                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
530                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
531                         }
532                         sdl_unlock_display();
533                 }
534         }
535
536         return 0;
537 }
538
539 static void *video_thread( void *arg )
540 {
541         // Identify the arg
542         consumer_sdl this = arg;
543
544 #ifdef __DARWIN__
545         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
546 #endif
547
548         // Obtain time of thread start
549         struct timeval now;
550         int64_t start = 0;
551         int64_t elapsed = 0;
552         struct timespec tm;
553         mlt_frame next = NULL;
554         mlt_properties properties = NULL;
555         double speed = 0;
556
557         // Get real time flag
558         int real_time = mlt_properties_get_int( this->properties, "real_time" );
559
560         // Get the current time
561         gettimeofday( &now, NULL );
562
563         // Determine start time
564         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
565
566         while ( this->running )
567         {
568                 // Pop the next frame
569                 pthread_mutex_lock( &this->video_mutex );
570                 next = mlt_deque_pop_front( this->queue );
571                 while ( next == NULL && this->running )
572                 {
573                         pthread_cond_wait( &this->video_cond, &this->video_mutex );
574                         next = mlt_deque_pop_front( this->queue );
575                 }
576                 pthread_mutex_unlock( &this->video_mutex );
577
578                 if ( !this->running || next == NULL ) break;
579
580                 // Get the properties
581                 properties =  MLT_FRAME_PROPERTIES( next );
582
583                 // Get the speed of the frame
584                 speed = mlt_properties_get_double( properties, "_speed" );
585
586                 // Get the current time
587                 gettimeofday( &now, NULL );
588
589                 // Get the elapsed time
590                 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
591
592                 // See if we have to delay the display of the current frame
593                 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
594                 {
595                         // Obtain the scheduled playout time
596                         mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
597
598                         // Determine the difference between the elapsed time and the scheduled playout time
599                         mlt_position difference = scheduled - elapsed;
600
601                         // Smooth playback a bit
602                         if ( real_time && ( difference > 20000 && speed == 1.0 ) )
603                         {
604                                 tm.tv_sec = difference / 1000000;
605                                 tm.tv_nsec = ( difference % 1000000 ) * 500;
606                                 nanosleep( &tm, NULL );
607                         }
608
609                         // Show current frame if not too old
610                         if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
611                                 consumer_play_video( this, next );
612
613                         // If the queue is empty, recalculate start to allow build up again
614                         if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
615                         {
616                                 gettimeofday( &now, NULL );
617                                 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
618                         }
619                 }
620
621                 // This frame can now be closed
622                 mlt_frame_close( next );
623                 next = NULL;
624         }
625
626         if ( next != NULL )
627                 mlt_frame_close( next );
628
629         mlt_consumer_stopped( &this->parent );
630
631 #ifdef __DARWIN__
632         [pool release];
633 #endif
634
635         return NULL;
636 }
637
638 /** Threaded wrapper for pipe.
639 */
640
641 static void *consumer_thread( void *arg )
642 {
643         // Identify the arg
644         consumer_sdl this = arg;
645
646 #ifdef __DARWIN__
647         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
648 #endif
649
650         // Get the consumer
651         mlt_consumer consumer = &this->parent;
652
653         // Video thread
654         pthread_t thread;
655
656         // internal intialization
657         int init_audio = 1;
658         int init_video = 1;
659         mlt_frame frame = NULL;
660         mlt_properties properties = NULL;
661         int duration = 0;
662         int64_t playtime = 0;
663         struct timespec tm = { 0, 100000 };
664
665         this->bpp = mlt_properties_get_int( this->properties, "bpp" );
666
667         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
668         {
669                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
670                 {
671                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
672                         return NULL;
673                 }
674
675                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
676                 SDL_EnableUNICODE( 1 );
677         }
678         else
679         {
680                 if ( SDL_GetVideoSurface( ) != NULL )
681                 {
682                         this->sdl_screen = SDL_GetVideoSurface( );
683                         consumer_get_dimensions( &this->window_width, &this->window_height );
684                         mlt_properties_set_int( this->properties, "changed", 0 );
685                 }
686         }
687
688         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
689                 SDL_InitSubSystem( SDL_INIT_AUDIO );
690
691         // Loop until told not to
692         while( this->running )
693         {
694                 // Get a frame from the attached producer
695                 frame = mlt_consumer_rt_frame( consumer );
696
697                 // Ensure that we have a frame
698                 if ( frame != NULL )
699                 {
700                         // Get the frame properties
701                         properties =  MLT_FRAME_PROPERTIES( frame );
702
703                         // Play audio
704                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
705
706                         // Determine the start time now
707                         if ( this->playing && init_video )
708                         {
709                                 // Create the video thread
710                                 pthread_create( &thread, NULL, video_thread, this );
711
712                                 // Video doesn't need to be initialised any more
713                                 init_video = 0;
714                         }
715
716                         // Set playtime for this frame
717                         mlt_properties_set_position( properties, "playtime", playtime );
718
719                         while ( this->running && mlt_deque_count( this->queue ) > 15 )
720                                 nanosleep( &tm, NULL );
721
722                         // Push this frame to the back of the queue
723                         pthread_mutex_lock( &this->video_mutex );
724                         mlt_deque_push_back( this->queue, frame );
725                         pthread_cond_broadcast( &this->video_cond );
726                         pthread_mutex_unlock( &this->video_mutex );
727
728                         // Calculate the next playtime
729                         playtime += ( duration * 1000 );
730                 }
731         }
732
733         // Kill the video thread
734         if ( init_video == 0 )
735         {
736                 pthread_mutex_lock( &this->video_mutex );
737                 pthread_cond_broadcast( &this->video_cond );
738                 pthread_mutex_unlock( &this->video_mutex );
739                 pthread_join( thread, NULL );
740         }
741
742         // internal cleanup
743         if ( this->sdl_overlay != NULL )
744                 SDL_FreeYUVOverlay( this->sdl_overlay );
745
746         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
747                 SDL_QuitSubSystem( SDL_INIT_AUDIO );
748
749         //if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
750                 //SDL_Quit( );
751
752         while( mlt_deque_count( this->queue ) )
753                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
754
755         this->sdl_screen = NULL;
756         this->sdl_overlay = NULL;
757         this->audio_avail = 0;
758
759 #ifdef __DARWIN__
760         [pool release];
761 #endif
762
763         return NULL;
764 }
765
766 static int consumer_get_dimensions( int *width, int *height )
767 {
768         int changed = 0;
769
770         // SDL windows manager structure
771         SDL_SysWMinfo wm;
772
773         // Specify the SDL Version
774         SDL_VERSION( &wm.version );
775
776         // Lock the display
777         sdl_lock_display();
778
779 #ifndef __DARWIN__
780         // Get the wm structure
781         if ( SDL_GetWMInfo( &wm ) == 1 )
782         {
783                 // Check that we have the X11 wm
784                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
785                 {
786                         // Get the SDL window
787                         Window window = wm.info.x11.window;
788
789                         // Get the display session
790                         Display *display = wm.info.x11.display;
791
792                         // Get the window attributes
793                         XWindowAttributes attr;
794                         XGetWindowAttributes( display, window, &attr );
795
796                         // Determine whether window has changed
797                         changed = *width != attr.width || *height != attr.height;
798
799                         // Return width and height
800                         *width = attr.width;
801                         *height = attr.height;
802                 }
803         }
804 #endif
805
806         // Unlock the display
807         sdl_lock_display();
808
809         return changed;
810 }
811
812 /** Callback to allow override of the close method.
813 */
814
815 static void consumer_close( mlt_consumer parent )
816 {
817         // Get the actual object
818         consumer_sdl this = parent->child;
819
820         // Stop the consumer
821         ///mlt_consumer_stop( parent );
822
823         // Now clean up the rest
824         mlt_consumer_close( parent );
825
826         // Close the queue
827         mlt_deque_close( this->queue );
828
829         // Destroy mutexes
830         pthread_mutex_destroy( &this->audio_mutex );
831         pthread_cond_destroy( &this->audio_cond );
832                 
833         // Finally clean up this
834         free( this );
835 }