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