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