]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
src/framework/mlt_geometry.c
[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                 // Allow the user to force resizing to window size
197                 if ( mlt_properties_get_int( this->properties, "resize" ) )
198                 {
199                         mlt_properties_set_int( this->properties, "width", this->width );
200                         mlt_properties_set_int( this->properties, "height", this->height );
201                 }
202                 else
203                 {
204                         this->width = mlt_properties_get_int( this->properties, "width" );
205                         this->height = mlt_properties_get_int( this->properties, "height" );
206                 }
207
208                 pthread_create( &this->thread, NULL, consumer_thread, this );
209         }
210
211         return 0;
212 }
213
214 int consumer_stop( mlt_consumer parent )
215 {
216         // Get the actual object
217         consumer_sdl this = parent->child;
218
219         if ( this->joined == 0 )
220         {
221                 // Kill the thread and clean up
222                 this->joined = 1;
223                 this->running = 0;
224                 pthread_join( this->thread, NULL );
225         }
226
227         return 0;
228 }
229
230 int consumer_is_stopped( mlt_consumer parent )
231 {
232         consumer_sdl this = parent->child;
233         return !this->running;
234 }
235
236 static int sdl_lock_display( )
237 {
238         SDL_Surface *screen = SDL_GetVideoSurface( );
239         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
240 }
241
242 static void sdl_unlock_display( )
243 {
244         SDL_Surface *screen = SDL_GetVideoSurface( );
245         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
246                 SDL_UnlockSurface( screen );
247 }
248
249 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
250 {
251         consumer_sdl this = udata;
252
253         // Get the volume
254         double volume = mlt_properties_get_double( this->properties, "volume" );
255
256         pthread_mutex_lock( &this->audio_mutex );
257
258         // Block until audio received
259         while ( this->running && len > this->audio_avail )
260                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
261
262         if ( this->audio_avail >= len )
263         {
264                 // Place in the audio buffer
265                 memcpy( stream, this->audio_buffer, len );
266
267                 // Remove len from the audio available
268                 this->audio_avail -= len;
269
270                 // Remove the samples
271                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
272         }
273         else
274         {
275                 // Just to be safe, wipe the stream first
276                 memset( stream, 0, len );
277
278                 // Copy what we have into the stream
279                 memcpy( stream, this->audio_buffer, this->audio_avail );
280
281                 // Mix the audio 
282                 SDL_MixAudio( stream, stream, len, ( int )( ( double )SDL_MIX_MAXVOLUME * volume ) );
283
284                 // No audio left
285                 this->audio_avail = 0;
286         }
287
288         // We're definitely playing now
289         this->playing = 1;
290
291         pthread_cond_broadcast( &this->audio_cond );
292         pthread_mutex_unlock( &this->audio_mutex );
293 }
294
295 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
296 {
297         // Get the properties of this consumer
298         mlt_properties properties = this->properties;
299         mlt_audio_format afmt = mlt_audio_pcm;
300
301         // Set the preferred params of the test card signal
302         int channels = mlt_properties_get_int( properties, "channels" );
303         int frequency = mlt_properties_get_int( properties, "frequency" );
304         static int counter = 0;
305
306         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
307         
308         int16_t *pcm;
309         int bytes;
310
311         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
312         *duration = ( ( samples * 1000 ) / frequency );
313
314         if ( mlt_properties_get_int( properties, "audio_off" ) )
315         {
316                 this->playing = 1;
317                 init_audio = 1;
318                 return init_audio;
319         }
320
321         if ( init_audio == 1 )
322         {
323                 SDL_AudioSpec request;
324                 SDL_AudioSpec got;
325
326                 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
327
328                 // specify audio format
329                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
330                 this->playing = 0;
331                 request.freq = frequency;
332                 request.format = AUDIO_S16;
333                 request.channels = channels;
334                 request.samples = audio_buffer;
335                 request.callback = sdl_fill_audio;
336                 request.userdata = (void *)this;
337                 if ( SDL_OpenAudio( &request, &got ) != 0 )
338                 {
339                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
340                         init_audio = 2;
341                 }
342                 else if ( got.size != 0 )
343                 {
344                         SDL_PauseAudio( 0 );
345                         init_audio = 0;
346                 }
347         }
348
349         if ( init_audio == 0 )
350         {
351                 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
352                 bytes = ( samples * channels * 2 );
353                 pthread_mutex_lock( &this->audio_mutex );
354                 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
355                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
356                 if ( this->running )
357                 {
358                         if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
359                                 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
360                         else
361                                 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
362                         this->audio_avail += bytes;
363                 }
364                 pthread_cond_broadcast( &this->audio_cond );
365                 pthread_mutex_unlock( &this->audio_mutex );
366         }
367         else
368         {
369                 this->playing = 1;
370         }
371
372         return init_audio;
373 }
374
375 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
376 {
377         // Get the properties of this consumer
378         mlt_properties properties = this->properties;
379
380         mlt_image_format vfmt = mlt_image_yuv422;
381         int width = this->width, height = this->height;
382         uint8_t *image;
383         int changed = 0;
384
385         if ( this->running && mlt_properties_get_int( properties, "video_off" ) == 0 )
386         {
387                 // Get the image, width and height
388                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
389                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
390                 
391                 // Handle events
392                 if ( this->sdl_screen != NULL )
393                 {
394                         SDL_Event event;
395         
396                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
397
398                         while ( SDL_PollEvent( &event ) )
399                         {
400                                 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
401
402                                 switch( event.type )
403                                 {
404                                         case SDL_VIDEORESIZE:
405                                                 this->window_width = event.resize.w;
406                                                 this->window_height = event.resize.h;
407                                                 changed = 1;
408                                                 break;
409                                         case SDL_QUIT:
410                                                 this->running = 0;
411                                                 break;
412                                         case SDL_KEYDOWN:
413                                                 {
414                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
415                                                         char keyboard[ 2 ] = " ";
416                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
417                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
418                                                         {
419                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
420                                                                 callback( producer, keyboard );
421                                                         }
422                                                 }
423                                                 break;
424                                 }
425                         }
426                 }
427         
428                 if ( width != this->width || height != this->height )
429                 {
430                         if ( this->sdl_overlay != NULL )
431                                 SDL_FreeYUVOverlay( this->sdl_overlay );
432                         this->sdl_overlay = NULL;
433                 }
434
435                 if ( this->running && ( this->sdl_screen == NULL || changed ) )
436                 {
437                         // Force an overlay recreation
438                         if ( this->sdl_overlay != NULL )
439                                 SDL_FreeYUVOverlay( this->sdl_overlay );
440                         this->sdl_overlay = NULL;
441
442                         // open SDL window with video overlay, if possible
443                         sdl_lock_display();
444                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
445                         sdl_unlock_display();
446                         if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
447                                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
448                         //SDL_Flip( this->sdl_screen );
449                         mlt_properties_set_int( properties, "changed", 0 );
450                 }
451                 else if ( mlt_properties_get_int( properties, "changed" ) )
452                 {
453                         sdl_lock_display();
454                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
455                         SDL_SetClipRect( this->sdl_screen, &this->rect );
456                         sdl_unlock_display();
457                         mlt_properties_set_int( properties, "changed", 0 );
458                 }
459
460                 if ( this->running )
461                 {
462                         // Determine window's new display aspect ratio
463                         double this_aspect = ( double )this->window_width / this->window_height;
464
465                         // Get the display aspect ratio
466                         double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
467
468                         // Determine frame's display aspect ratio
469                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
470
471                         // Store the width and height received
472                         this->width = width;
473                         this->height = height;
474
475                         // If using hardware scaler
476                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
477                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
478                         {
479                                 // Use hardware scaler to normalise display aspect ratio
480                                 this->rect.w = frame_aspect / this_aspect * this->window_width;
481                                 this->rect.h = this->window_height;
482                                 if ( this->rect.w > this->window_width )
483                                 {
484                                         this->rect.w = this->window_width;
485                                         this->rect.h = this_aspect / frame_aspect * this->window_height;
486                                 }
487                         }
488                         // Special case optimisation to negate odd effect of sample aspect ratio
489                         // not corresponding exactly with image resolution.
490                         else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) ) 
491                         {
492                                 this->rect.w = this->window_width;
493                                 this->rect.h = this->window_height;
494                         }
495                         // Use hardware scaler to normalise sample aspect ratio
496                         else if ( this->window_height * display_ratio > this->window_width )
497                         {
498                                 this->rect.w = this->window_width;
499                                 this->rect.h = this->window_width / display_ratio;
500                         }
501                         else
502                         {
503                                 this->rect.w = this->window_height * display_ratio;
504                                 this->rect.h = this->window_height;
505                         }
506                         
507                         this->rect.x = ( this->window_width - this->rect.w ) / 2;
508                         this->rect.y = ( this->window_height - this->rect.h ) / 2;
509
510                         mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
511                         mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
512                         mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
513                         mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
514
515                         SDL_SetClipRect( this->sdl_screen, &this->rect );
516                 }
517
518                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
519                 {
520                         SDL_SetClipRect( this->sdl_screen, &this->rect );
521                         SDL_Flip( this->sdl_screen );
522                         sdl_lock_display();
523                         this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
524                         sdl_unlock_display();
525                 }
526
527                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
528                 {
529                         this->buffer = this->sdl_overlay->pixels[ 0 ];
530                         sdl_lock_display();
531                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
532                         {
533                                 if ( image != NULL )
534                                         memcpy( this->buffer, image, width * height * 2 );
535                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
536                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
537                         }
538                         sdl_unlock_display();
539                 }
540         }
541
542         return 0;
543 }
544
545 static void *video_thread( void *arg )
546 {
547         // Identify the arg
548         consumer_sdl this = arg;
549
550 #ifdef __DARWIN__
551         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
552 #endif
553
554         // Obtain time of thread start
555         struct timeval now;
556         int64_t start = 0;
557         int64_t elapsed = 0;
558         struct timespec tm;
559         mlt_frame next = NULL;
560         mlt_properties properties = NULL;
561         double speed = 0;
562
563         // Get real time flag
564         int real_time = mlt_properties_get_int( this->properties, "real_time" );
565
566         // Get the current time
567         gettimeofday( &now, NULL );
568
569         // Determine start time
570         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
571
572         while ( this->running )
573         {
574                 // Pop the next frame
575                 pthread_mutex_lock( &this->video_mutex );
576                 next = mlt_deque_pop_front( this->queue );
577                 while ( next == NULL && this->running )
578                 {
579                         pthread_cond_wait( &this->video_cond, &this->video_mutex );
580                         next = mlt_deque_pop_front( this->queue );
581                 }
582                 pthread_mutex_unlock( &this->video_mutex );
583
584                 if ( !this->running || next == NULL ) break;
585
586                 // Get the properties
587                 properties =  MLT_FRAME_PROPERTIES( next );
588
589                 // Get the speed of the frame
590                 speed = mlt_properties_get_double( properties, "_speed" );
591
592                 // Get the current time
593                 gettimeofday( &now, NULL );
594
595                 // Get the elapsed time
596                 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
597
598                 // See if we have to delay the display of the current frame
599                 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
600                 {
601                         // Obtain the scheduled playout time
602                         mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
603
604                         // Determine the difference between the elapsed time and the scheduled playout time
605                         mlt_position difference = scheduled - elapsed;
606
607                         // Smooth playback a bit
608                         if ( real_time && ( difference > 20000 && speed == 1.0 ) )
609                         {
610                                 tm.tv_sec = difference / 1000000;
611                                 tm.tv_nsec = ( difference % 1000000 ) * 500;
612                                 nanosleep( &tm, NULL );
613                         }
614
615                         // Show current frame if not too old
616                         if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
617                                 consumer_play_video( this, next );
618
619                         // If the queue is empty, recalculate start to allow build up again
620                         if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
621                         {
622                                 gettimeofday( &now, NULL );
623                                 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
624                         }
625                 }
626
627                 // This frame can now be closed
628                 mlt_frame_close( next );
629                 next = NULL;
630         }
631
632         if ( next != NULL )
633                 mlt_frame_close( next );
634
635         mlt_consumer_stopped( &this->parent );
636
637 #ifdef __DARWIN__
638         [pool release];
639 #endif
640
641         return NULL;
642 }
643
644 /** Threaded wrapper for pipe.
645 */
646
647 static void *consumer_thread( void *arg )
648 {
649         // Identify the arg
650         consumer_sdl this = arg;
651
652 #ifdef __DARWIN__
653         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
654 #endif
655
656         // Get the consumer
657         mlt_consumer consumer = &this->parent;
658
659         // Video thread
660         pthread_t thread;
661
662         // internal intialization
663         int init_audio = 1;
664         int init_video = 1;
665         mlt_frame frame = NULL;
666         mlt_properties properties = NULL;
667         int duration = 0;
668         int64_t playtime = 0;
669         struct timespec tm = { 0, 100000 };
670
671         this->bpp = mlt_properties_get_int( this->properties, "bpp" );
672
673         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
674         {
675                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
676                 {
677                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
678                         return NULL;
679                 }
680
681                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
682                 SDL_EnableUNICODE( 1 );
683         }
684         else
685         {
686                 if ( SDL_GetVideoSurface( ) != NULL )
687                 {
688                         this->sdl_screen = SDL_GetVideoSurface( );
689                         consumer_get_dimensions( &this->window_width, &this->window_height );
690                         mlt_properties_set_int( this->properties, "changed", 0 );
691                 }
692         }
693
694         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
695                 SDL_InitSubSystem( SDL_INIT_AUDIO );
696
697         // Loop until told not to
698         while( this->running )
699         {
700                 // Get a frame from the attached producer
701                 frame = mlt_consumer_rt_frame( consumer );
702
703                 // Ensure that we have a frame
704                 if ( frame != NULL )
705                 {
706                         // Get the frame properties
707                         properties =  MLT_FRAME_PROPERTIES( frame );
708
709                         // Play audio
710                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
711
712                         // Determine the start time now
713                         if ( this->playing && init_video )
714                         {
715                                 // Create the video thread
716                                 pthread_create( &thread, NULL, video_thread, this );
717
718                                 // Video doesn't need to be initialised any more
719                                 init_video = 0;
720                         }
721
722                         // Set playtime for this frame
723                         mlt_properties_set_position( properties, "playtime", playtime );
724
725                         while ( this->running && mlt_deque_count( this->queue ) > 15 )
726                                 nanosleep( &tm, NULL );
727
728                         // Push this frame to the back of the queue
729                         pthread_mutex_lock( &this->video_mutex );
730                         mlt_deque_push_back( this->queue, frame );
731                         pthread_cond_broadcast( &this->video_cond );
732                         pthread_mutex_unlock( &this->video_mutex );
733
734                         // Calculate the next playtime
735                         playtime += ( duration * 1000 );
736                 }
737         }
738
739         // Kill the video thread
740         if ( init_video == 0 )
741         {
742                 pthread_mutex_lock( &this->video_mutex );
743                 pthread_cond_broadcast( &this->video_cond );
744                 pthread_mutex_unlock( &this->video_mutex );
745                 pthread_join( thread, NULL );
746         }
747
748         // internal cleanup
749         if ( this->sdl_overlay != NULL )
750                 SDL_FreeYUVOverlay( this->sdl_overlay );
751
752         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
753                 SDL_QuitSubSystem( SDL_INIT_AUDIO );
754
755         //if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
756                 //SDL_Quit( );
757
758         while( mlt_deque_count( this->queue ) )
759                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
760
761         this->sdl_screen = NULL;
762         this->sdl_overlay = NULL;
763         this->audio_avail = 0;
764
765 #ifdef __DARWIN__
766         [pool release];
767 #endif
768
769         return NULL;
770 }
771
772 static int consumer_get_dimensions( int *width, int *height )
773 {
774         int changed = 0;
775
776         // SDL windows manager structure
777         SDL_SysWMinfo wm;
778
779         // Specify the SDL Version
780         SDL_VERSION( &wm.version );
781
782         // Lock the display
783         sdl_lock_display();
784
785 #ifndef __DARWIN__
786         // Get the wm structure
787         if ( SDL_GetWMInfo( &wm ) == 1 )
788         {
789                 // Check that we have the X11 wm
790                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
791                 {
792                         // Get the SDL window
793                         Window window = wm.info.x11.window;
794
795                         // Get the display session
796                         Display *display = wm.info.x11.display;
797
798                         // Get the window attributes
799                         XWindowAttributes attr;
800                         XGetWindowAttributes( display, window, &attr );
801
802                         // Determine whether window has changed
803                         changed = *width != attr.width || *height != attr.height;
804
805                         // Return width and height
806                         *width = attr.width;
807                         *height = attr.height;
808                 }
809         }
810 #endif
811
812         // Unlock the display
813         sdl_lock_display();
814
815         return changed;
816 }
817
818 /** Callback to allow override of the close method.
819 */
820
821 static void consumer_close( mlt_consumer parent )
822 {
823         // Get the actual object
824         consumer_sdl this = parent->child;
825
826         // Stop the consumer
827         ///mlt_consumer_stop( parent );
828
829         // Now clean up the rest
830         mlt_consumer_close( parent );
831
832         // Close the queue
833         mlt_deque_close( this->queue );
834
835         // Destroy mutexes
836         pthread_mutex_destroy( &this->audio_mutex );
837         pthread_cond_destroy( &this->audio_cond );
838                 
839         // Finally clean up this
840         free( this );
841 }