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