]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
Remove local references to SDL_Surface. (SF-186)
[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 this, 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 this = calloc( 1, sizeof( struct consumer_sdl_s ) );
90
91         // If no malloc'd and consumer init ok
92         if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
93         {
94                 // Create the queue
95                 this->queue = mlt_deque_init( );
96
97                 // Get the parent consumer object
98                 mlt_consumer parent = &this->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                 this->properties = MLT_SERVICE_PROPERTIES( service );
106
107                 // Set the default volume
108                 mlt_properties_set_double( this->properties, "volume", 1.0 );
109
110                 // This is the initialisation of the consumer
111                 pthread_mutex_init( &this->audio_mutex, NULL );
112                 pthread_cond_init( &this->audio_cond, NULL);
113                 pthread_mutex_init( &this->video_mutex, NULL );
114                 pthread_cond_init( &this->video_cond, NULL);
115                 
116                 // Default scaler (for now we'll use nearest)
117                 mlt_properties_set( this->properties, "rescale", "nearest" );
118                 mlt_properties_set( this->properties, "deinterlace_method", "onefield" );
119                 mlt_properties_set_int( this->properties, "top_field_first", -1 );
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", 2048 );
126
127                 // Ensure we don't join on a non-running object
128                 this->joined = 1;
129                 
130                 // process actual param
131                 if ( arg && sscanf( arg, "%dx%d", &this->width, &this->height ) )
132                 {
133                         mlt_properties_set_int( this->properties, "_arg_size", 1 );
134                 }
135                 else
136                 {
137                         this->width = mlt_properties_get_int( this->properties, "width" );
138                         this->height = mlt_properties_get_int( this->properties, "height" );
139                 }
140         
141                 // Set the sdl flags
142                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF;
143 #if !defined(__DARWIN__)
144                 this->sdl_flags |= SDL_RESIZABLE;
145 #endif          
146                 // Allow thread to be started/stopped
147                 parent->start = consumer_start;
148                 parent->stop = consumer_stop;
149                 parent->is_stopped = consumer_is_stopped;
150
151                 // Register specific events
152                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
153
154                 // Return the consumer produced
155                 return parent;
156         }
157
158         // malloc or consumer init failed
159         free( this );
160
161         // Indicate failure
162         return NULL;
163 }
164
165 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
166 {
167         if ( listener != NULL )
168                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
169 }
170
171 int consumer_start( mlt_consumer parent )
172 {
173         consumer_sdl this = parent->child;
174
175         if ( !this->running )
176         {
177                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
178                 int video_off = mlt_properties_get_int( properties, "video_off" );
179                 int preview_off = mlt_properties_get_int( properties, "preview_off" );
180                 int display_off = video_off | preview_off;
181                 int audio_off = mlt_properties_get_int( properties, "audio_off" );
182                 int sdl_started = mlt_properties_get_int( properties, "sdl_started" );
183                 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                 this->running = 1;
192                 this->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( this->properties, "_arg_size" ) )
210                 {
211                         if ( mlt_properties_get_int( this->properties, "width" ) > 0 )
212                                 this->width = mlt_properties_get_int( this->properties, "width" );
213                         if ( mlt_properties_get_int( this->properties, "height" ) > 0 )
214                                 this->height = mlt_properties_get_int( this->properties, "height" );
215                 }
216
217                 this->bpp = mlt_properties_get_int( this->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( this->properties, "_arg_size" ) )
239                 {
240                         this->window_width = this->width;
241                         this->window_height = this->height;
242                 }
243                 else
244                 {
245                         double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
246                         this->window_width = ( double )this->height * display_ratio + 0.5;
247                         this->window_height = this->height;
248                 }
249
250                 pthread_mutex_lock( &mlt_sdl_mutex );
251                 if ( !SDL_GetVideoSurface() && display_off == 0 )
252                 {
253                         if ( mlt_properties_get_int( this->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                                 this->window_width = vi->current_w;
260                                 this->window_height = vi->current_h;
261                                 this->sdl_flags |= SDL_FULLSCREEN;
262                                 SDL_ShowCursor( SDL_DISABLE );
263                         }
264                         SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
265                 }
266                 pthread_mutex_unlock( &mlt_sdl_mutex );
267
268                 pthread_create( &this->thread, NULL, consumer_thread, this );
269         }
270
271         return 0;
272 }
273
274 int consumer_stop( mlt_consumer parent )
275 {
276         // Get the actual object
277         consumer_sdl this = parent->child;
278
279         if ( this->joined == 0 )
280         {
281                 // Kill the thread and clean up
282                 this->joined = 1;
283                 this->running = 0;
284 #ifndef WIN32
285                 if ( this->thread )
286 #endif
287                         pthread_join( this->thread, NULL );
288
289                 // internal cleanup
290                 if ( this->sdl_overlay != NULL )
291                         SDL_FreeYUVOverlay( this->sdl_overlay );
292                 this->sdl_overlay = NULL;
293
294                 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
295                 {
296                         pthread_mutex_lock( &this->audio_mutex );
297                         pthread_cond_broadcast( &this->audio_cond );
298                         pthread_mutex_unlock( &this->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 this = parent->child;
316         return !this->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 this = udata;
340
341         // Get the volume
342         double volume = mlt_properties_get_double( this->properties, "volume" );
343
344         pthread_mutex_lock( &this->audio_mutex );
345
346         // Block until audio received
347         while ( this->running && len > this->audio_avail )
348                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
349
350         if ( this->audio_avail >= len )
351         {
352                 // Place in the audio buffer
353                 if ( volume != 1.0 )
354                         SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
355                 else
356                         memcpy( stream, this->audio_buffer, len );
357
358                 // Remove len from the audio available
359                 this->audio_avail -= len;
360
361                 // Remove the samples
362                 memmove( this->audio_buffer, this->audio_buffer + len, this->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, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
371
372                 // No audio left
373                 this->audio_avail = 0;
374         }
375
376         // We're definitely playing now
377         this->playing = 1;
378
379         pthread_cond_broadcast( &this->audio_cond );
380         pthread_mutex_unlock( &this->audio_mutex );
381 }
382
383 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
384 {
385         // Get the properties of this consumer
386         mlt_properties properties = this->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( this->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                 this->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                 this->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 *)this;
427                 if ( SDL_OpenAudio( &request, &got ) != 0 )
428                 {
429                         mlt_log_error( MLT_CONSUMER_SERVICE( this ), "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( &this->audio_mutex );
445                 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
446                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
447                 if ( this->running )
448                 {
449                         if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
450                         {
451                                 if ( channels == dest_channels )
452                                 {
453                                         memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
454                                 }
455                                 else
456                                 {
457                                         int16_t *dest = (int16_t*) &this->audio_buffer[ this->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( &this->audio_buffer[ this->audio_avail ], 0, bytes );
471                         }
472                         this->audio_avail += bytes;
473                 }
474                 pthread_cond_broadcast( &this->audio_cond );
475                 pthread_mutex_unlock( &this->audio_mutex );
476         }
477         else
478         {
479                 this->playing = 1;
480         }
481
482         return init_audio;
483 }
484
485 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
486 {
487         // Get the properties of this consumer
488         mlt_properties properties = this->properties;
489
490         mlt_image_format vfmt = mlt_image_yuv422;
491         int width = this->width, height = this->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 ( this->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( &this->window_width, &this->window_height );
515                         pthread_mutex_unlock( &mlt_sdl_mutex );
516                         sdl_unlock_display( );
517
518                         while ( SDL_PollEvent( &event ) )
519                         {
520                                 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
521
522                                 switch( event.type )
523                                 {
524                                         case SDL_VIDEORESIZE:
525                                                 this->window_width = event.resize.w;
526                                                 this->window_height = event.resize.h;
527                                                 changed = 1;
528                                                 break;
529                                         case SDL_QUIT:
530                                                 this->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 != this->width || height != this->height )
551                 {
552                         if ( this->sdl_overlay != NULL )
553                                 SDL_FreeYUVOverlay( this->sdl_overlay );
554                         this->sdl_overlay = NULL;
555                 }
556
557                 if ( this->running && ( !SDL_GetVideoSurface() || changed ) )
558                 {
559                         // Force an overlay recreation
560                         if ( this->sdl_overlay != NULL )
561                                 SDL_FreeYUVOverlay( this->sdl_overlay );
562                         this->sdl_overlay = NULL;
563
564                         // open SDL window with video overlay, if possible
565                         pthread_mutex_lock( &mlt_sdl_mutex );
566                         consumer_get_dimensions( &this->window_width, &this->window_height );
567                         SDL_Surface *screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
568                         pthread_mutex_unlock( &mlt_sdl_mutex );
569
570                         if ( screen )
571                         {
572                                 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
573                                 SDL_FillRect( screen, NULL, color >> 8 );
574                                 SDL_Flip( screen );
575                         }
576                 }
577
578                 if ( this->running )
579                 {
580                         // Determine window's new display aspect ratio
581                         double this_aspect = ( double )this->window_width / this->window_height;
582
583                         // Get the display aspect ratio
584                         double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
585
586                         // Determine frame's display aspect ratio
587                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
588
589                         // Store the width and height received
590                         this->width = width;
591                         this->height = height;
592
593                         // If using hardware scaler
594                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
595                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
596                         {
597                                 // Use hardware scaler to normalise display aspect ratio
598                                 this->rect.w = frame_aspect / this_aspect * this->window_width;
599                                 this->rect.h = this->window_height;
600                                 if ( this->rect.w > this->window_width )
601                                 {
602                                         this->rect.w = this->window_width;
603                                         this->rect.h = this_aspect / frame_aspect * this->window_height;
604                                 }
605                         }
606                         // Special case optimisation to negate odd effect of sample aspect ratio
607                         // not corresponding exactly with image resolution.
608                         else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) ) 
609                         {
610                                 this->rect.w = this->window_width;
611                                 this->rect.h = this->window_height;
612                         }
613                         // Use hardware scaler to normalise sample aspect ratio
614                         else if ( this->window_height * display_ratio > this->window_width )
615                         {
616                                 this->rect.w = this->window_width;
617                                 this->rect.h = this->window_width / display_ratio;
618                         }
619                         else
620                         {
621                                 this->rect.w = this->window_height * display_ratio;
622                                 this->rect.h = this->window_height;
623                         }
624                         
625                         this->rect.x = ( this->window_width - this->rect.w ) / 2;
626                         this->rect.y = ( this->window_height - this->rect.h ) / 2;
627                         this->rect.x -= this->rect.x % 2;
628
629                         mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
630                         mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
631                         mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
632                         mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
633
634                         SDL_SetClipRect( SDL_GetVideoSurface(), &this->rect );
635                 }
636
637                 if ( this->running && SDL_GetVideoSurface() && this->sdl_overlay == NULL )
638                 {
639                         SDL_SetClipRect( SDL_GetVideoSurface(), &this->rect );
640                         this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, SDL_GetVideoSurface() );
641                 }
642
643                 if ( this->running && SDL_GetVideoSurface() && this->sdl_overlay != NULL )
644                 {
645                         this->buffer = this->sdl_overlay->pixels[ 0 ];
646                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
647                         {
648                                 if ( image != NULL )
649                                         memcpy( this->buffer, image, width * height * 2 );
650                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
651                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &SDL_GetVideoSurface()->clip_rect );
652                         }
653                 }
654
655                 sdl_unlock_display();
656                 mlt_cocoa_autorelease_close( pool );
657                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
658         }
659         else if ( this->running )
660         {
661                 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
662                 if ( !video_off )
663                         mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
664                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
665         }
666
667         return 0;
668 }
669
670 static void *video_thread( void *arg )
671 {
672         // Identify the arg
673         consumer_sdl this = arg;
674
675         // Obtain time of thread start
676         struct timeval now;
677         int64_t start = 0;
678         int64_t elapsed = 0;
679         struct timespec tm;
680         mlt_frame next = NULL;
681         mlt_properties properties = NULL;
682         double speed = 0;
683
684         // Get real time flag
685         int real_time = mlt_properties_get_int( this->properties, "real_time" );
686
687         // Get the current time
688         gettimeofday( &now, NULL );
689
690         // Determine start time
691         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
692
693         while ( this->running )
694         {
695                 // Pop the next frame
696                 pthread_mutex_lock( &this->video_mutex );
697                 next = mlt_deque_pop_front( this->queue );
698                 while ( next == NULL && this->running )
699                 {
700                         pthread_cond_wait( &this->video_cond, &this->video_mutex );
701                         next = mlt_deque_pop_front( this->queue );
702                 }
703                 pthread_mutex_unlock( &this->video_mutex );
704
705                 if ( !this->running || next == NULL ) break;
706
707                 // Get the properties
708                 properties =  MLT_FRAME_PROPERTIES( next );
709
710                 // Get the speed of the frame
711                 speed = mlt_properties_get_double( properties, "_speed" );
712
713                 // Get the current time
714                 gettimeofday( &now, NULL );
715
716                 // Get the elapsed time
717                 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
718
719                 // See if we have to delay the display of the current frame
720                 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
721                 {
722                         // Obtain the scheduled playout time
723                         int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
724
725                         // Determine the difference between the elapsed time and the scheduled playout time
726                         int64_t difference = scheduled - elapsed;
727
728                         // Smooth playback a bit
729                         if ( real_time && ( difference > 20000 && speed == 1.0 ) )
730                         {
731                                 tm.tv_sec = difference / 1000000;
732                                 tm.tv_nsec = ( difference % 1000000 ) * 500;
733                                 nanosleep( &tm, NULL );
734                         }
735
736                         // Show current frame if not too old
737                         if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
738                                 consumer_play_video( this, next );
739
740                         // If the queue is empty, recalculate start to allow build up again
741                         if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
742                         {
743                                 gettimeofday( &now, NULL );
744                                 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
745                         }
746                 }
747                 else
748                 {
749                         static int dropped = 0;
750                         mlt_log_info( MLT_CONSUMER_SERVICE(&this->parent), "dropped video frame %d\n", ++dropped );
751                 }
752
753                 // This frame can now be closed
754                 mlt_frame_close( next );
755                 next = NULL;
756         }
757
758         if ( next != NULL )
759                 mlt_frame_close( next );
760
761         mlt_consumer_stopped( &this->parent );
762
763         return NULL;
764 }
765
766 /** Threaded wrapper for pipe.
767 */
768
769 static void *consumer_thread( void *arg )
770 {
771         // Identify the arg
772         consumer_sdl this = arg;
773
774         // Get the consumer
775         mlt_consumer consumer = &this->parent;
776
777         // Convenience functionality
778         int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
779         int terminated = 0;
780
781         // Video thread
782         pthread_t thread;
783
784         // internal intialization
785         int init_audio = 1;
786         int init_video = 1;
787         mlt_frame frame = NULL;
788         int duration = 0;
789         int64_t playtime = 0;
790         struct timespec tm = { 0, 100000 };
791
792         // Loop until told not to
793         while( this->running )
794         {
795                 // Get a frame from the attached producer
796                 frame = !terminated? mlt_consumer_rt_frame( consumer ) : NULL;
797
798                 // Check for termination
799                 if ( terminate_on_pause && frame )
800                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
801
802                 // Ensure that we have a frame
803                 if ( frame )
804                 {
805                         // Play audio
806                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
807
808                         // Determine the start time now
809                         if ( this->playing && init_video )
810                         {
811                                 // Create the video thread
812                                 pthread_create( &thread, NULL, video_thread, this );
813
814                                 // Video doesn't need to be initialised any more
815                                 init_video = 0;
816                         }
817
818                         // Set playtime for this frame
819                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "playtime", playtime );
820
821                         while ( this->running && mlt_deque_count( this->queue ) > 15 )
822                                 nanosleep( &tm, NULL );
823
824                         // Push this frame to the back of the queue
825                         pthread_mutex_lock( &this->video_mutex );
826                         mlt_deque_push_back( this->queue, frame );
827                         pthread_cond_broadcast( &this->video_cond );
828                         pthread_mutex_unlock( &this->video_mutex );
829
830                         // Calculate the next playtime
831                         playtime += ( duration * 1000 );
832                 }
833                 else if ( terminated )
834                 {
835                         if ( init_video || mlt_deque_count( this->queue ) == 0 )
836                                 break;
837                         else
838                                 nanosleep( &tm, NULL );
839                 }
840         }
841
842         this->running = 0;
843         
844         // Unblock sdl_preview
845         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "put_mode" ) == 1 )
846         {
847                 frame = mlt_consumer_get_frame( consumer );
848                 if ( frame )
849                         mlt_frame_close( frame );
850                 frame = NULL;
851         }
852
853         // Kill the video thread
854         if ( init_video == 0 )
855         {
856                 pthread_mutex_lock( &this->video_mutex );
857                 pthread_cond_broadcast( &this->video_cond );
858                 pthread_mutex_unlock( &this->video_mutex );
859                 pthread_join( thread, NULL );
860         }
861
862         while( mlt_deque_count( this->queue ) )
863                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
864
865         this->audio_avail = 0;
866
867         return NULL;
868 }
869
870 static int consumer_get_dimensions( int *width, int *height )
871 {
872         int changed = 0;
873
874         // SDL windows manager structure
875         SDL_SysWMinfo wm;
876
877         // Specify the SDL Version
878         SDL_VERSION( &wm.version );
879
880         // Lock the display
881         //sdl_lock_display();
882
883 #ifndef __DARWIN__
884         // Get the wm structure
885         if ( SDL_GetWMInfo( &wm ) == 1 )
886         {
887 #ifndef WIN32
888                 // Check that we have the X11 wm
889                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
890                 {
891                         // Get the SDL window
892                         Window window = wm.info.x11.window;
893
894                         // Get the display session
895                         Display *display = wm.info.x11.display;
896
897                         // Get the window attributes
898                         XWindowAttributes attr;
899                         XGetWindowAttributes( display, window, &attr );
900
901                         // Determine whether window has changed
902                         changed = *width != attr.width || *height != attr.height;
903
904                         // Return width and height
905                         *width = attr.width;
906                         *height = attr.height;
907                 }
908 #endif
909         }
910 #endif
911
912         // Unlock the display
913         //sdl_unlock_display();
914
915         return changed;
916 }
917
918 /** Callback to allow override of the close method.
919 */
920
921 static void consumer_close( mlt_consumer parent )
922 {
923         // Get the actual object
924         consumer_sdl this = parent->child;
925
926         // Stop the consumer
927         ///mlt_consumer_stop( parent );
928
929         // Now clean up the rest
930         mlt_consumer_close( parent );
931
932         // Close the queue
933         mlt_deque_close( this->queue );
934
935         // Destroy mutexes
936         pthread_mutex_destroy( &this->audio_mutex );
937         pthread_cond_destroy( &this->audio_cond );
938                 
939         // Finally clean up this
940         free( this );
941 }