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