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