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