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