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