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