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