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