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