]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
realtime scheduling updates; suppress libdv errors; add frame property deinterlace_me...
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30 #include <sys/time.h>
31
32 /** This classes definition.
33 */
34
35 typedef struct consumer_sdl_s *consumer_sdl;
36
37 struct consumer_sdl_s
38 {
39         struct mlt_consumer_s parent;
40         mlt_properties properties;
41         mlt_deque queue;
42         pthread_t thread;
43         int joined;
44         int running;
45         uint8_t audio_buffer[ 4096 * 10 ];
46         int audio_avail;
47         pthread_mutex_t audio_mutex;
48         pthread_cond_t audio_cond;
49         pthread_mutex_t video_mutex;
50         pthread_cond_t video_cond;
51         int window_width;
52         int window_height;
53         float aspect_ratio;
54         float display_aspect;
55         double last_frame_aspect;
56         int width;
57         int height;
58         int playing;
59         int sdl_flags;
60         SDL_Surface *sdl_screen;
61         SDL_Overlay *sdl_overlay;
62         SDL_Rect rect;
63         uint8_t *buffer;
64         int bpp;
65 };
66
67 /** Forward references to static functions.
68 */
69
70 static int consumer_start( mlt_consumer parent );
71 static int consumer_stop( mlt_consumer parent );
72 static int consumer_is_stopped( mlt_consumer parent );
73 static void consumer_close( mlt_consumer parent );
74 static void *consumer_thread( void * );
75 static int consumer_get_dimensions( int *width, int *height );
76 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
77
78 /** This is what will be called by the factory - anything can be passed in
79         via the argument, but keep it simple.
80 */
81
82 mlt_consumer consumer_sdl_init( char *arg )
83 {
84         // Create the consumer object
85         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
86
87         // If no malloc'd and consumer init ok
88         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
89         {
90                 // Create the queue
91                 this->queue = mlt_deque_init( );
92
93                 // Get the parent consumer object
94                 mlt_consumer parent = &this->parent;
95
96                 // We have stuff to clean up, so override the close method
97                 parent->close = consumer_close;
98
99                 // get a handle on properties
100                 mlt_service service = MLT_CONSUMER_SERVICE( parent );
101                 this->properties = MLT_SERVICE_PROPERTIES( service );
102
103                 // Set the default volume
104                 mlt_properties_set_double( this->properties, "volume", 1.0 );
105
106                 // This is the initialisation of the consumer
107                 pthread_mutex_init( &this->audio_mutex, NULL );
108                 pthread_cond_init( &this->audio_cond, NULL);
109                 pthread_mutex_init( &this->video_mutex, NULL );
110                 pthread_cond_init( &this->video_cond, NULL);
111                 
112                 // Default scaler (for now we'll use nearest)
113                 mlt_properties_set( this->properties, "rescale", "nearest" );
114
115                 // Default buffer for low latency
116                 mlt_properties_set_int( this->properties, "buffer", 1 );
117
118                 // Default progressive true
119                 mlt_properties_set_int( this->properties, "progressive", 0 );
120
121                 // Default audio buffer
122                 mlt_properties_set_int( this->properties, "audio_buffer", 512 );
123
124                 // Get sample aspect ratio
125                 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
126
127                 // Ensure we don't join on a non-running object
128                 this->joined = 1;
129                 
130                 // Default display aspect ratio
131                 this->display_aspect = 4.0 / 3.0;
132                 
133                 // process actual param
134                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
135                 {
136                         this->width = mlt_properties_get_int( this->properties, "width" );
137                         this->height = mlt_properties_get_int( this->properties, "height" );
138                 }
139
140                 // Default window size
141                 this->window_width = ( float )this->height * this->display_aspect;
142                 this->window_height = this->height;
143
144                 // Set the sdl flags
145                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
146
147                 // Allow thread to be started/stopped
148                 parent->start = consumer_start;
149                 parent->stop = consumer_stop;
150                 parent->is_stopped = consumer_is_stopped;
151
152                 // Register specific events
153                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
154
155                 // Return the consumer produced
156                 return parent;
157         }
158
159         // malloc or consumer init failed
160         free( this );
161
162         // Indicate failure
163         return NULL;
164 }
165
166 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
167 {
168         if ( listener != NULL )
169                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
170 }
171
172 int consumer_start( mlt_consumer parent )
173 {
174         consumer_sdl this = parent->child;
175
176         if ( !this->running )
177         {
178                 consumer_stop( parent );
179
180                 this->running = 1;
181                 this->joined = 0;
182
183                 // Allow the user to force resizing to window size
184                 if ( mlt_properties_get_int( this->properties, "resize" ) )
185                 {
186                         mlt_properties_set_int( this->properties, "width", this->width );
187                         mlt_properties_set_int( this->properties, "height", this->height );
188                 }
189
190                 pthread_create( &this->thread, NULL, consumer_thread, this );
191         }
192
193         return 0;
194 }
195
196 int consumer_stop( mlt_consumer parent )
197 {
198         // Get the actual object
199         consumer_sdl this = parent->child;
200
201         if ( this->joined == 0 )
202         {
203                 // Kill the thread and clean up
204                 this->joined = 1;
205                 this->running = 0;
206                 pthread_join( this->thread, NULL );
207         }
208
209         return 0;
210 }
211
212 int consumer_is_stopped( mlt_consumer parent )
213 {
214         consumer_sdl this = parent->child;
215         return !this->running;
216 }
217
218 static int sdl_lock_display( )
219 {
220         SDL_Surface *screen = SDL_GetVideoSurface( );
221         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
222 }
223
224 static void sdl_unlock_display( )
225 {
226         SDL_Surface *screen = SDL_GetVideoSurface( );
227         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
228                 SDL_UnlockSurface( screen );
229 }
230
231 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
232 {
233         consumer_sdl this = udata;
234
235         // Get the volume
236         float volume = mlt_properties_get_double( this->properties, "volume" );
237
238         pthread_mutex_lock( &this->audio_mutex );
239
240         // Block until audio received
241         while ( this->running && len > this->audio_avail )
242                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
243
244         if ( this->audio_avail >= len )
245         {
246                 // Place in the audio buffer
247                 memcpy( stream, this->audio_buffer, len );
248
249                 // Remove len from the audio available
250                 this->audio_avail -= len;
251
252                 // Remove the samples
253                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
254         }
255         else
256         {
257                 // Just to be safe, wipe the stream first
258                 memset( stream, 0, len );
259
260                 // Copy what we have into the stream
261                 memcpy( stream, this->audio_buffer, this->audio_avail );
262
263                 // Mix the audio 
264                 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
265
266                 // No audio left
267                 this->audio_avail = 0;
268         }
269
270         // We're definitely playing now
271         this->playing = 1;
272
273         pthread_cond_broadcast( &this->audio_cond );
274         pthread_mutex_unlock( &this->audio_mutex );
275 }
276
277 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
278 {
279         // Get the properties of this consumer
280         mlt_properties properties = this->properties;
281         mlt_audio_format afmt = mlt_audio_pcm;
282
283         // Set the preferred params of the test card signal
284         int channels = mlt_properties_get_int( properties, "channels" );
285         int frequency = mlt_properties_get_int( properties, "frequency" );
286         static int counter = 0;
287
288         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
289         
290         int16_t *pcm;
291         int bytes;
292
293         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
294         *duration = ( ( samples * 1000 ) / frequency );
295
296         if ( mlt_properties_get_int( properties, "audio_off" ) )
297         {
298                 this->playing = 1;
299                 init_audio = 1;
300                 return init_audio;
301         }
302
303         if ( init_audio == 1 )
304         {
305                 SDL_AudioSpec request;
306                 SDL_AudioSpec got;
307
308                 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
309
310                 // specify audio format
311                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
312                 this->playing = 0;
313                 request.freq = frequency;
314                 request.format = AUDIO_S16;
315                 request.channels = channels;
316                 request.samples = audio_buffer;
317                 request.callback = sdl_fill_audio;
318                 request.userdata = (void *)this;
319                 if ( SDL_OpenAudio( &request, &got ) != 0 )
320                 {
321                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
322                         init_audio = 2;
323                 }
324                 else if ( got.size != 0 )
325                 {
326                         SDL_PauseAudio( 0 );
327                         init_audio = 0;
328                 }
329         }
330
331         if ( init_audio == 0 )
332         {
333                 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
334                 bytes = ( samples * channels * 2 );
335                 pthread_mutex_lock( &this->audio_mutex );
336                 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
337                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
338                 if ( this->running )
339                 {
340                         if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
341                                 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
342                         else
343                                 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
344                         this->audio_avail += bytes;
345                 }
346                 pthread_cond_broadcast( &this->audio_cond );
347                 pthread_mutex_unlock( &this->audio_mutex );
348         }
349         else
350         {
351                 this->playing = 1;
352         }
353
354         return init_audio;
355 }
356
357 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
358 {
359         // Get the properties of this consumer
360         mlt_properties properties = this->properties;
361
362         mlt_image_format vfmt = mlt_image_yuv422;
363         int width = this->width, height = this->height;
364         uint8_t *image;
365         int changed = 0;
366
367         if ( this->running && mlt_properties_get_int( properties, "video_off" ) == 0 )
368         {
369                 // Get the image, width and height
370                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
371                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
372                 
373                 // Handle events
374                 if ( this->sdl_screen != NULL )
375                 {
376                         SDL_Event event;
377         
378                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
379
380                         while ( SDL_PollEvent( &event ) )
381                         {
382                                 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
383
384                                 switch( event.type )
385                                 {
386                                         case SDL_VIDEORESIZE:
387                                                 this->window_width = event.resize.w;
388                                                 this->window_height = event.resize.h;
389                                                 changed = 1;
390                                                 break;
391                                         case SDL_QUIT:
392                                                 this->running = 0;
393                                                 break;
394                                         case SDL_KEYDOWN:
395                                                 {
396                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
397                                                         char keyboard[ 2 ] = " ";
398                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
399                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
400                                                         {
401                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
402                                                                 callback( producer, keyboard );
403                                                         }
404                                                 }
405                                                 break;
406                                 }
407                         }
408                 }
409         
410                 if ( width != this->width || height != this->height )
411                 {
412                         if ( this->sdl_overlay != NULL )
413                                 SDL_FreeYUVOverlay( this->sdl_overlay );
414                         this->sdl_overlay = NULL;
415                 }
416
417                 if ( this->running && ( this->sdl_screen == NULL || changed ) )
418                 {
419                         // Force an overlay recreation
420                         if ( this->sdl_overlay != NULL )
421                                 SDL_FreeYUVOverlay( this->sdl_overlay );
422                         this->sdl_overlay = NULL;
423
424                         // open SDL window with video overlay, if possible
425                         sdl_lock_display();
426                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
427                         sdl_unlock_display();
428                         if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
429                                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
430                         //SDL_Flip( this->sdl_screen );
431                         mlt_properties_set_int( properties, "changed", 0 );
432                 }
433                 else if ( mlt_properties_get_int( properties, "changed" ) )
434                 {
435                         sdl_lock_display();
436                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
437                         SDL_SetClipRect( this->sdl_screen, &this->rect );
438                         sdl_unlock_display();
439                         mlt_properties_set_int( properties, "changed", 0 );
440                 }
441
442                 if ( this->running )
443                 {
444                         // Determine window's new display aspect ratio
445                         float this_aspect = ( float )this->window_width / this->window_height;
446
447                         // Determine frame's display aspect ratio
448                         float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
449                         this->width = width;
450                         this->height = height;
451
452                         // If using hardware scaler
453                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
454                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
455                         {
456                                 // Special case optimisation to negate odd effect of sample aspect ratio
457                                 // not corresponding exactly with image resolution.
458                                 if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) && 
459                                          ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
460                                 {
461                                         this->rect.w = this->window_width;
462                                         this->rect.h = this->window_height;
463                                 }
464                                 else
465                                 {
466                                         // Use hardware scaler to normalise display aspect ratio
467                                         this->rect.w = frame_aspect / this_aspect * this->window_width;
468                                         this->rect.h = this->window_height;
469                                         if ( this->rect.w > this->window_width )
470                                         {
471                                                 this->rect.w = this->window_width;
472                                                 this->rect.h = this_aspect / frame_aspect * this->window_height;
473                                         }
474                                 }
475                         }
476                         // Special case optimisation to negate odd effect of sample aspect ratio
477                         // not corresponding exactly with image resolution.
478                         else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) 
479                         {
480                                 this->rect.w = this->window_width;
481                                 this->rect.h = this->window_height;
482                         }
483                         // Use hardware scaler to normalise sample aspect ratio
484                         else if ( this->window_height * this->display_aspect > this->window_width )
485                         {
486                                 this->rect.w = this->window_width;
487                                 this->rect.h = this->window_width / this->display_aspect;
488                         }
489                         else
490                         {
491                                 this->rect.w = this->window_height * this->display_aspect;
492                                 this->rect.h = this->window_height;
493                         }
494                         
495                         this->rect.x = ( this->window_width - this->rect.w ) / 2;
496                         this->rect.y = ( this->window_height - this->rect.h ) / 2;
497
498                         mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
499                         mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
500                         mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
501                         mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
502
503                         SDL_SetClipRect( this->sdl_screen, &this->rect );
504                 }
505
506                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
507                 {
508                         SDL_SetClipRect( this->sdl_screen, &this->rect );
509                         SDL_Flip( this->sdl_screen );
510                         sdl_lock_display();
511                         this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
512                         sdl_unlock_display();
513                 }
514
515                 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
516                 {
517                         this->buffer = this->sdl_overlay->pixels[ 0 ];
518                         sdl_lock_display();
519                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
520                         {
521                                 if ( image != NULL )
522                                         memcpy( this->buffer, image, width * height * 2 );
523                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
524                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
525                         }
526                         sdl_unlock_display();
527                 }
528         }
529
530         return 0;
531 }
532
533 static void *video_thread( void *arg )
534 {
535         // Identify the arg
536         consumer_sdl this = arg;
537
538         // Obtain time of thread start
539         struct timeval now;
540         int64_t start = 0;
541         int64_t elapsed = 0;
542         struct timespec tm;
543         mlt_frame next = NULL;
544         mlt_properties properties = NULL;
545         double speed = 0;
546
547         // Get real time flag
548         int real_time = mlt_properties_get_int( this->properties, "real_time" );
549
550         // Get the current time
551         gettimeofday( &now, NULL );
552
553         // Determine start time
554         start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
555
556         while ( this->running )
557         {
558                 // Pop the next frame
559                 pthread_mutex_lock( &this->video_mutex );
560                 next = mlt_deque_pop_front( this->queue );
561                 while ( next == NULL && this->running )
562                 {
563                         pthread_cond_wait( &this->video_cond, &this->video_mutex );
564                         next = mlt_deque_pop_front( this->queue );
565                 }
566                 pthread_mutex_unlock( &this->video_mutex );
567
568                 if ( !this->running || next == NULL ) break;
569
570                 // Get the properties
571                 properties =  MLT_FRAME_PROPERTIES( next );
572
573                 // Get the speed of the frame
574                 speed = mlt_properties_get_double( properties, "_speed" );
575
576                 // Get the current time
577                 gettimeofday( &now, NULL );
578
579                 // Get the elapsed time
580                 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
581
582                 // See if we have to delay the display of the current frame
583                 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
584                 {
585                         // Obtain the scheduled playout time
586                         mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
587
588                         // Determine the difference between the elapsed time and the scheduled playout time
589                         mlt_position difference = scheduled - elapsed;
590
591                         // Smooth playback a bit
592                         if ( real_time && ( difference > 20000 && speed == 1.0 ) )
593                         {
594                                 tm.tv_sec = difference / 1000000;
595                                 tm.tv_nsec = ( difference % 1000000 ) * 500;
596                                 nanosleep( &tm, NULL );
597                         }
598
599                         // Show current frame if not too old
600                         if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
601                                 consumer_play_video( this, next );
602
603                         // If the queue is empty, recalculate start to allow build up again
604                         if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
605                         {
606                                 gettimeofday( &now, NULL );
607                                 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
608                         }
609                 }
610
611                 // This frame can now be closed
612                 mlt_frame_close( next );
613                 next = NULL;
614         }
615
616         if ( next != NULL )
617                 mlt_frame_close( next );
618
619         mlt_consumer_stopped( &this->parent );
620
621         return NULL;
622 }
623
624 /** Threaded wrapper for pipe.
625 */
626
627 static void *consumer_thread( void *arg )
628 {
629         // Identify the arg
630         consumer_sdl this = arg;
631
632         // Get the consumer
633         mlt_consumer consumer = &this->parent;
634
635         // Video thread
636         pthread_t thread;
637
638         // internal intialization
639         int init_audio = 1;
640         int init_video = 1;
641         mlt_frame frame = NULL;
642         mlt_properties properties = NULL;
643         int duration = 0;
644         int64_t playtime = 0;
645         struct timespec tm = { 0, 100000 };
646
647         this->bpp = mlt_properties_get_int( this->properties, "bpp" );
648
649         if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
650         {
651                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
652                 {
653                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
654                         return NULL;
655                 }
656
657                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
658                 SDL_EnableUNICODE( 1 );
659         }
660         else
661         {
662                 if ( SDL_GetVideoSurface( ) != NULL )
663                 {
664                         this->sdl_screen = SDL_GetVideoSurface( );
665                         consumer_get_dimensions( &this->window_width, &this->window_height );
666                         mlt_properties_set_int( this->properties, "changed", 0 );
667                 }
668         }
669
670         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
671                 SDL_InitSubSystem( SDL_INIT_AUDIO );
672
673         // Loop until told not to
674         while( this->running )
675         {
676                 // Get a frame from the attached producer
677                 frame = mlt_consumer_rt_frame( consumer );
678
679                 // Ensure that we have a frame
680                 if ( frame != NULL )
681                 {
682                         // Get the frame properties
683                         properties =  MLT_FRAME_PROPERTIES( frame );
684
685                         // Play audio
686                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
687
688                         // Determine the start time now
689                         if ( this->playing && init_video )
690                         {
691                                 // Create the video thread
692                                 pthread_create( &thread, NULL, video_thread, this );
693
694                                 // Video doesn't need to be initialised any more
695                                 init_video = 0;
696                         }
697
698                         // Set playtime for this frame
699                         mlt_properties_set_position( properties, "playtime", playtime );
700
701                         while ( this->running && mlt_deque_count( this->queue ) > 15 )
702                                 nanosleep( &tm, NULL );
703
704                         // Push this frame to the back of the queue
705                         pthread_mutex_lock( &this->video_mutex );
706                         mlt_deque_push_back( this->queue, frame );
707                         pthread_cond_broadcast( &this->video_cond );
708                         pthread_mutex_unlock( &this->video_mutex );
709
710                         // Calculate the next playtime
711                         playtime += ( duration * 1000 );
712                 }
713         }
714
715         // Kill the video thread
716         if ( init_video == 0 )
717         {
718                 pthread_mutex_lock( &this->video_mutex );
719                 pthread_cond_broadcast( &this->video_cond );
720                 pthread_mutex_unlock( &this->video_mutex );
721                 pthread_join( thread, NULL );
722         }
723
724         // internal cleanup
725         if ( this->sdl_overlay != NULL )
726                 SDL_FreeYUVOverlay( this->sdl_overlay );
727
728         if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "audio_off" ) )
729                 SDL_QuitSubSystem( SDL_INIT_AUDIO );
730
731         //if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
732                 //SDL_Quit( );
733
734         while( mlt_deque_count( this->queue ) )
735                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
736
737         this->sdl_screen = NULL;
738         this->sdl_overlay = NULL;
739         this->audio_avail = 0;
740
741         return NULL;
742 }
743
744 static int consumer_get_dimensions( int *width, int *height )
745 {
746         int changed = 0;
747
748         // SDL windows manager structure
749         SDL_SysWMinfo wm;
750
751         // Specify the SDL Version
752         SDL_VERSION( &wm.version );
753
754         // Lock the display
755         sdl_lock_display();
756
757         // Get the wm structure
758         if ( SDL_GetWMInfo( &wm ) == 1 )
759         {
760                 // Check that we have the X11 wm
761                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
762                 {
763                         // Get the SDL window
764                         Window window = wm.info.x11.window;
765
766                         // Get the display session
767                         Display *display = wm.info.x11.display;
768
769                         // Get the window attributes
770                         XWindowAttributes attr;
771                         XGetWindowAttributes( display, window, &attr );
772
773                         // Determine whether window has changed
774                         changed = *width != attr.width || *height != attr.height;
775
776                         // Return width and height
777                         *width = attr.width;
778                         *height = attr.height;
779                 }
780         }
781
782         // Unlock the display
783         sdl_lock_display();
784
785         return changed;
786 }
787
788 /** Callback to allow override of the close method.
789 */
790
791 static void consumer_close( mlt_consumer parent )
792 {
793         // Get the actual object
794         consumer_sdl this = parent->child;
795
796         // Stop the consumer
797         ///mlt_consumer_stop( parent );
798
799         // Now clean up the rest
800         mlt_consumer_close( parent );
801
802         // Close the queue
803         mlt_deque_close( this->queue );
804
805         // Destroy mutexes
806         pthread_mutex_destroy( &this->audio_mutex );
807         pthread_cond_destroy( &this->audio_cond );
808                 
809         // Finally clean up this
810         free( this );
811 }