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