2 * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004, 2010 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_consumer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
26 #include <framework/mlt_log.h>
32 #include <SDL_syswm.h>
34 #include "consumer_sdl_osx.h"
36 extern pthread_mutex_t mlt_sdl_mutex;
38 /** This classes definition.
41 typedef struct consumer_sdl_s *consumer_sdl;
45 struct mlt_consumer_s parent;
46 mlt_properties properties;
51 uint8_t audio_buffer[ 4096 * 10 ];
53 pthread_mutex_t audio_mutex;
54 pthread_cond_t audio_cond;
55 pthread_mutex_t video_mutex;
56 pthread_cond_t video_cond;
65 SDL_Surface *sdl_screen;
66 SDL_Overlay *sdl_overlay;
72 /** Forward references to static functions.
75 static int consumer_start( mlt_consumer parent );
76 static int consumer_stop( mlt_consumer parent );
77 static int consumer_is_stopped( mlt_consumer parent );
78 static void consumer_close( mlt_consumer parent );
79 static void *consumer_thread( void * );
80 static int consumer_get_dimensions( int *width, int *height );
81 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
83 /** This is what will be called by the factory - anything can be passed in
84 via the argument, but keep it simple.
87 mlt_consumer consumer_sdl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
89 // Create the consumer object
90 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
92 // If no malloc'd and consumer init ok
93 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
96 this->queue = mlt_deque_init( );
98 // Get the parent consumer object
99 mlt_consumer parent = &this->parent;
101 // We have stuff to clean up, so override the close method
102 parent->close = consumer_close;
104 // get a handle on properties
105 mlt_service service = MLT_CONSUMER_SERVICE( parent );
106 this->properties = MLT_SERVICE_PROPERTIES( service );
108 // Set the default volume
109 mlt_properties_set_double( this->properties, "volume", 1.0 );
111 // This is the initialisation of the consumer
112 pthread_mutex_init( &this->audio_mutex, NULL );
113 pthread_cond_init( &this->audio_cond, NULL);
114 pthread_mutex_init( &this->video_mutex, NULL );
115 pthread_cond_init( &this->video_cond, NULL);
117 // Default scaler (for now we'll use nearest)
118 mlt_properties_set( this->properties, "rescale", "nearest" );
119 mlt_properties_set( this->properties, "deinterlace_method", "onefield" );
121 // Default buffer for low latency
122 mlt_properties_set_int( this->properties, "buffer", 1 );
124 // Default audio buffer
125 mlt_properties_set_int( this->properties, "audio_buffer", 2048 );
127 // Ensure we don't join on a non-running object
130 // process actual param
131 if ( arg && sscanf( arg, "%dx%d", &this->width, &this->height ) )
133 mlt_properties_set_int( this->properties, "_arg_size", 1 );
137 this->width = mlt_properties_get_int( this->properties, "width" );
138 this->height = mlt_properties_get_int( this->properties, "height" );
142 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF;
143 #if !defined(__DARWIN__)
144 this->sdl_flags |= SDL_RESIZABLE;
146 // Allow thread to be started/stopped
147 parent->start = consumer_start;
148 parent->stop = consumer_stop;
149 parent->is_stopped = consumer_is_stopped;
151 // Register specific events
152 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
154 // Return the consumer produced
158 // malloc or consumer init failed
165 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
167 if ( listener != NULL )
168 listener( owner, this, ( SDL_Event * )args[ 0 ] );
171 int consumer_start( mlt_consumer parent )
173 consumer_sdl this = parent->child;
175 if ( !this->running )
177 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
178 int video_off = mlt_properties_get_int( properties, "video_off" );
179 int preview_off = mlt_properties_get_int( properties, "preview_off" );
180 int display_off = video_off | preview_off;
181 int audio_off = mlt_properties_get_int( properties, "audio_off" );
182 int sdl_started = mlt_properties_get_int( properties, "sdl_started" );
184 consumer_stop( parent );
189 if ( ! mlt_properties_get_int( this->properties, "_arg_size" ) )
191 if ( mlt_properties_get_int( this->properties, "width" ) > 0 )
192 this->width = mlt_properties_get_int( this->properties, "width" );
193 if ( mlt_properties_get_int( this->properties, "height" ) > 0 )
194 this->height = mlt_properties_get_int( this->properties, "height" );
197 this->bpp = mlt_properties_get_int( this->properties, "bpp" );
199 if ( sdl_started == 0 && display_off == 0 )
201 pthread_mutex_lock( &mlt_sdl_mutex );
202 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
203 pthread_mutex_unlock( &mlt_sdl_mutex );
206 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
210 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
211 SDL_EnableUNICODE( 1 );
213 else if ( display_off == 0 )
215 pthread_mutex_lock( &mlt_sdl_mutex );
216 this->sdl_screen = SDL_GetVideoSurface( );
217 pthread_mutex_unlock( &mlt_sdl_mutex );
220 if ( audio_off == 0 )
221 SDL_InitSubSystem( SDL_INIT_AUDIO );
223 // Default window size
224 if ( mlt_properties_get_int( this->properties, "_arg_size" ) )
226 this->window_width = this->width;
227 this->window_height = this->height;
231 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
232 this->window_width = ( double )this->height * display_ratio + 0.5;
233 this->window_height = this->height;
236 if ( this->sdl_screen == NULL && display_off == 0 )
238 if ( mlt_properties_get_int( this->properties, "fullscreen" ) )
240 const SDL_VideoInfo *vi;
241 pthread_mutex_lock( &mlt_sdl_mutex );
242 vi = SDL_GetVideoInfo();
243 pthread_mutex_unlock( &mlt_sdl_mutex );
244 this->window_width = vi->current_w;
245 this->window_height = vi->current_h;
246 this->sdl_flags |= SDL_FULLSCREEN;
247 SDL_ShowCursor( SDL_DISABLE );
249 pthread_mutex_lock( &mlt_sdl_mutex );
250 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
251 pthread_mutex_unlock( &mlt_sdl_mutex );
254 pthread_create( &this->thread, NULL, consumer_thread, this );
260 int consumer_stop( mlt_consumer parent )
262 // Get the actual object
263 consumer_sdl this = parent->child;
265 if ( this->joined == 0 )
267 // Kill the thread and clean up
273 pthread_join( this->thread, NULL );
276 if ( this->sdl_overlay != NULL )
277 SDL_FreeYUVOverlay( this->sdl_overlay );
278 this->sdl_overlay = NULL;
280 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
282 pthread_mutex_lock( &this->audio_mutex );
283 pthread_cond_broadcast( &this->audio_cond );
284 pthread_mutex_unlock( &this->audio_mutex );
285 SDL_QuitSubSystem( SDL_INIT_AUDIO );
288 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
290 pthread_mutex_lock( &mlt_sdl_mutex );
292 pthread_mutex_unlock( &mlt_sdl_mutex );
295 this->sdl_screen = NULL;
301 int consumer_is_stopped( mlt_consumer parent )
303 consumer_sdl this = parent->child;
304 return !this->running;
307 static int sdl_lock_display( )
309 pthread_mutex_lock( &mlt_sdl_mutex );
310 SDL_Surface *screen = SDL_GetVideoSurface( );
311 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
312 pthread_mutex_unlock( &mlt_sdl_mutex );
316 static void sdl_unlock_display( )
318 pthread_mutex_lock( &mlt_sdl_mutex );
319 SDL_Surface *screen = SDL_GetVideoSurface( );
320 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
321 SDL_UnlockSurface( screen );
322 pthread_mutex_unlock( &mlt_sdl_mutex );
325 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
327 consumer_sdl this = udata;
330 double volume = mlt_properties_get_double( this->properties, "volume" );
332 pthread_mutex_lock( &this->audio_mutex );
334 // Block until audio received
335 while ( this->running && len > this->audio_avail )
336 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
338 if ( this->audio_avail >= len )
340 // Place in the audio buffer
342 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
344 memcpy( stream, this->audio_buffer, len );
346 // Remove len from the audio available
347 this->audio_avail -= len;
349 // Remove the samples
350 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
354 // Just to be safe, wipe the stream first
355 memset( stream, 0, len );
358 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
361 this->audio_avail = 0;
364 // We're definitely playing now
367 pthread_cond_broadcast( &this->audio_cond );
368 pthread_mutex_unlock( &this->audio_mutex );
371 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
373 // Get the properties of this consumer
374 mlt_properties properties = this->properties;
375 mlt_audio_format afmt = mlt_audio_s16;
377 // Set the preferred params of the test card signal
378 int channels = mlt_properties_get_int( properties, "channels" );
379 int dest_channels = channels;
380 int frequency = mlt_properties_get_int( properties, "frequency" );
381 static int counter = 0;
383 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
388 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
389 *duration = ( ( samples * 1000 ) / frequency );
390 pcm += mlt_properties_get_int( properties, "audio_offset" );
392 if ( mlt_properties_get_int( properties, "audio_off" ) )
399 if ( init_audio == 1 )
401 SDL_AudioSpec request;
404 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
406 // specify audio format
407 memset( &request, 0, sizeof( SDL_AudioSpec ) );
409 request.freq = frequency;
410 request.format = AUDIO_S16SYS;
411 request.channels = dest_channels;
412 request.samples = audio_buffer;
413 request.callback = sdl_fill_audio;
414 request.userdata = (void *)this;
415 if ( SDL_OpenAudio( &request, &got ) != 0 )
417 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "SDL failed to open audio: %s\n", SDL_GetError() );
420 else if ( got.size != 0 )
427 if ( init_audio == 0 )
429 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
431 bytes = samples * dest_channels * sizeof(*pcm);
432 pthread_mutex_lock( &this->audio_mutex );
433 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
434 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
437 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
439 if ( channels == dest_channels )
441 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
445 int16_t *dest = (int16_t*) &this->audio_buffer[ this->audio_avail ];
450 memcpy( dest, pcm, dest_channels * sizeof(*pcm) );
452 dest += dest_channels;
458 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
460 this->audio_avail += bytes;
462 pthread_cond_broadcast( &this->audio_cond );
463 pthread_mutex_unlock( &this->audio_mutex );
473 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
475 // Get the properties of this consumer
476 mlt_properties properties = this->properties;
478 mlt_image_format vfmt = mlt_image_yuv422;
479 int width = this->width, height = this->height;
483 int video_off = mlt_properties_get_int( properties, "video_off" );
484 int preview_off = mlt_properties_get_int( properties, "preview_off" );
485 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
486 int display_off = video_off | preview_off;
488 if ( this->running && display_off == 0 )
490 // Get the image, width and height
491 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
493 void *pool = mlt_cocoa_autorelease_init();
496 if ( this->sdl_screen != NULL )
501 pthread_mutex_lock( &mlt_sdl_mutex );
502 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
503 pthread_mutex_unlock( &mlt_sdl_mutex );
504 sdl_unlock_display( );
506 while ( SDL_PollEvent( &event ) )
508 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
512 case SDL_VIDEORESIZE:
513 this->window_width = event.resize.w;
514 this->window_height = event.resize.h;
522 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
523 char keyboard[ 2 ] = " ";
524 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
525 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
527 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
528 callback( producer, keyboard );
538 if ( width != this->width || height != this->height )
540 if ( this->sdl_overlay != NULL )
541 SDL_FreeYUVOverlay( this->sdl_overlay );
542 this->sdl_overlay = NULL;
545 if ( this->running && ( this->sdl_screen == NULL || changed ) )
547 // Force an overlay recreation
548 if ( this->sdl_overlay != NULL )
549 SDL_FreeYUVOverlay( this->sdl_overlay );
550 this->sdl_overlay = NULL;
552 // open SDL window with video overlay, if possible
553 pthread_mutex_lock( &mlt_sdl_mutex );
554 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
555 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
556 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
557 pthread_mutex_unlock( &mlt_sdl_mutex );
559 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
560 SDL_FillRect( this->sdl_screen, NULL, color >> 8 );
561 SDL_Flip( this->sdl_screen );
566 // Determine window's new display aspect ratio
567 double this_aspect = ( double )this->window_width / this->window_height;
569 // Get the display aspect ratio
570 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
572 // Determine frame's display aspect ratio
573 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
575 // Store the width and height received
577 this->height = height;
579 // If using hardware scaler
580 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
581 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
583 // Use hardware scaler to normalise display aspect ratio
584 this->rect.w = frame_aspect / this_aspect * this->window_width;
585 this->rect.h = this->window_height;
586 if ( this->rect.w > this->window_width )
588 this->rect.w = this->window_width;
589 this->rect.h = this_aspect / frame_aspect * this->window_height;
592 // Special case optimisation to negate odd effect of sample aspect ratio
593 // not corresponding exactly with image resolution.
594 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
596 this->rect.w = this->window_width;
597 this->rect.h = this->window_height;
599 // Use hardware scaler to normalise sample aspect ratio
600 else if ( this->window_height * display_ratio > this->window_width )
602 this->rect.w = this->window_width;
603 this->rect.h = this->window_width / display_ratio;
607 this->rect.w = this->window_height * display_ratio;
608 this->rect.h = this->window_height;
611 this->rect.x = ( this->window_width - this->rect.w ) / 2;
612 this->rect.y = ( this->window_height - this->rect.h ) / 2;
613 this->rect.x -= this->rect.x % 2;
615 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
616 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
617 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
618 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
620 SDL_SetClipRect( this->sdl_screen, &this->rect );
623 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
625 SDL_SetClipRect( this->sdl_screen, &this->rect );
626 this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
629 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
631 this->buffer = this->sdl_overlay->pixels[ 0 ];
632 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
635 memcpy( this->buffer, image, width * height * 2 );
636 SDL_UnlockYUVOverlay( this->sdl_overlay );
637 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
641 sdl_unlock_display();
642 mlt_cocoa_autorelease_close( pool );
643 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
645 else if ( this->running )
647 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
649 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
650 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
656 static void *video_thread( void *arg )
659 consumer_sdl this = arg;
661 // Obtain time of thread start
666 mlt_frame next = NULL;
667 mlt_properties properties = NULL;
670 // Get real time flag
671 int real_time = mlt_properties_get_int( this->properties, "real_time" );
673 // Get the current time
674 gettimeofday( &now, NULL );
676 // Determine start time
677 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
679 while ( this->running )
681 // Pop the next frame
682 pthread_mutex_lock( &this->video_mutex );
683 next = mlt_deque_pop_front( this->queue );
684 while ( next == NULL && this->running )
686 pthread_cond_wait( &this->video_cond, &this->video_mutex );
687 next = mlt_deque_pop_front( this->queue );
689 pthread_mutex_unlock( &this->video_mutex );
691 if ( !this->running || next == NULL ) break;
693 // Get the properties
694 properties = MLT_FRAME_PROPERTIES( next );
696 // Get the speed of the frame
697 speed = mlt_properties_get_double( properties, "_speed" );
699 // Get the current time
700 gettimeofday( &now, NULL );
702 // Get the elapsed time
703 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
705 // See if we have to delay the display of the current frame
706 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
708 // Obtain the scheduled playout time
709 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
711 // Determine the difference between the elapsed time and the scheduled playout time
712 int64_t difference = scheduled - elapsed;
714 // Smooth playback a bit
715 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
717 tm.tv_sec = difference / 1000000;
718 tm.tv_nsec = ( difference % 1000000 ) * 500;
719 nanosleep( &tm, NULL );
722 // Show current frame if not too old
723 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
724 consumer_play_video( this, next );
726 // If the queue is empty, recalculate start to allow build up again
727 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
729 gettimeofday( &now, NULL );
730 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
735 static int dropped = 0;
736 mlt_log_info( MLT_CONSUMER_SERVICE(&this->parent), "dropped video frame %d\n", ++dropped );
739 // This frame can now be closed
740 mlt_frame_close( next );
745 mlt_frame_close( next );
747 mlt_consumer_stopped( &this->parent );
752 /** Threaded wrapper for pipe.
755 static void *consumer_thread( void *arg )
758 consumer_sdl this = arg;
761 mlt_consumer consumer = &this->parent;
763 // Convenience functionality
764 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
770 // internal intialization
773 mlt_frame frame = NULL;
774 mlt_properties properties = NULL;
776 int64_t playtime = 0;
777 struct timespec tm = { 0, 100000 };
779 // Loop until told not to
780 while( !terminated && this->running )
782 // Get a frame from the attached producer
783 frame = mlt_consumer_rt_frame( consumer );
785 // Check for termination
786 if ( terminate_on_pause && frame != NULL )
787 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
789 // Ensure that we have a frame
792 // Get the frame properties
793 properties = MLT_FRAME_PROPERTIES( frame );
796 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
798 // Determine the start time now
799 if ( this->playing && init_video )
801 // Create the video thread
802 pthread_create( &thread, NULL, video_thread, this );
804 // Video doesn't need to be initialised any more
808 // Set playtime for this frame
809 mlt_properties_set_int( properties, "playtime", playtime );
811 while ( this->running && mlt_deque_count( this->queue ) > 15 )
812 nanosleep( &tm, NULL );
814 // Push this frame to the back of the queue
815 pthread_mutex_lock( &this->video_mutex );
816 mlt_deque_push_back( this->queue, frame );
817 pthread_cond_broadcast( &this->video_cond );
818 pthread_mutex_unlock( &this->video_mutex );
820 // Calculate the next playtime
821 playtime += ( duration * 1000 );
827 // Kill the video thread
828 if ( init_video == 0 )
830 pthread_mutex_lock( &this->video_mutex );
831 pthread_cond_broadcast( &this->video_cond );
832 pthread_mutex_unlock( &this->video_mutex );
833 pthread_join( thread, NULL );
836 while( mlt_deque_count( this->queue ) )
837 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
839 this->sdl_screen = NULL;
840 this->audio_avail = 0;
845 static int consumer_get_dimensions( int *width, int *height )
849 // SDL windows manager structure
852 // Specify the SDL Version
853 SDL_VERSION( &wm.version );
856 //sdl_lock_display();
859 // Get the wm structure
860 if ( SDL_GetWMInfo( &wm ) == 1 )
863 // Check that we have the X11 wm
864 if ( wm.subsystem == SDL_SYSWM_X11 )
866 // Get the SDL window
867 Window window = wm.info.x11.window;
869 // Get the display session
870 Display *display = wm.info.x11.display;
872 // Get the window attributes
873 XWindowAttributes attr;
874 XGetWindowAttributes( display, window, &attr );
876 // Determine whether window has changed
877 changed = *width != attr.width || *height != attr.height;
879 // Return width and height
881 *height = attr.height;
887 // Unlock the display
888 //sdl_unlock_display();
893 /** Callback to allow override of the close method.
896 static void consumer_close( mlt_consumer parent )
898 // Get the actual object
899 consumer_sdl this = parent->child;
902 ///mlt_consumer_stop( parent );
904 // Now clean up the rest
905 mlt_consumer_close( parent );
908 mlt_deque_close( this->queue );
911 pthread_mutex_destroy( &this->audio_mutex );
912 pthread_cond_destroy( &this->audio_cond );
914 // Finally clean up this