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