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