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