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_Overlay *sdl_overlay;
71 /** Forward references to static functions.
74 static int consumer_start( mlt_consumer parent );
75 static int consumer_stop( mlt_consumer parent );
76 static int consumer_is_stopped( mlt_consumer parent );
77 static void consumer_close( mlt_consumer parent );
78 static void *consumer_thread( void * );
79 static int consumer_get_dimensions( int *width, int *height );
80 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
82 /** This is what will be called by the factory - anything can be passed in
83 via the argument, but keep it simple.
86 mlt_consumer consumer_sdl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
88 // Create the consumer object
89 consumer_sdl self = calloc( 1, sizeof( struct consumer_sdl_s ) );
91 // If no malloc'd and consumer init ok
92 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
95 self->queue = mlt_deque_init( );
97 // Get the parent consumer object
98 mlt_consumer parent = &self->parent;
100 // We have stuff to clean up, so override the close method
101 parent->close = consumer_close;
103 // get a handle on properties
104 mlt_service service = MLT_CONSUMER_SERVICE( parent );
105 self->properties = MLT_SERVICE_PROPERTIES( service );
107 // Set the default volume
108 mlt_properties_set_double( self->properties, "volume", 1.0 );
110 // This is the initialisation of the consumer
111 pthread_mutex_init( &self->audio_mutex, NULL );
112 pthread_cond_init( &self->audio_cond, NULL);
113 pthread_mutex_init( &self->video_mutex, NULL );
114 pthread_cond_init( &self->video_cond, NULL);
116 // Default scaler (for now we'll use nearest)
117 mlt_properties_set( self->properties, "rescale", "nearest" );
118 mlt_properties_set( self->properties, "deinterlace_method", "onefield" );
119 mlt_properties_set_int( self->properties, "top_field_first", -1 );
121 // Default buffer for low latency
122 mlt_properties_set_int( self->properties, "buffer", 1 );
124 // Default audio buffer
125 mlt_properties_set_int( self->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", &self->width, &self->height ) )
133 mlt_properties_set_int( self->properties, "_arg_size", 1 );
137 self->width = mlt_properties_get_int( self->properties, "width" );
138 self->height = mlt_properties_get_int( self->properties, "height" );
142 self->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF;
143 #if !defined(__DARWIN__)
144 self->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( self->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 self, void **args )
167 if ( listener != NULL )
168 listener( owner, self, ( SDL_Event * )args[ 0 ] );
171 int consumer_start( mlt_consumer parent )
173 consumer_sdl self = parent->child;
175 if ( !self->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" );
183 char *output_display = mlt_properties_get( properties, "output_display" );
184 char *window_id = mlt_properties_get( properties, "window_id" );
185 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
186 char *video_driver = mlt_properties_get( properties, "video_driver" );
187 char *audio_device = mlt_properties_get( properties, "audio_device" );
189 consumer_stop( parent );
194 if ( output_display != NULL )
195 setenv( "DISPLAY", output_display, 1 );
197 if ( window_id != NULL )
198 setenv( "SDL_WINDOWID", window_id, 1 );
200 if ( video_driver != NULL )
201 setenv( "SDL_VIDEODRIVER", video_driver, 1 );
203 if ( audio_driver != NULL )
204 setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
206 if ( audio_device != NULL )
207 setenv( "AUDIODEV", audio_device, 1 );
209 if ( ! mlt_properties_get_int( self->properties, "_arg_size" ) )
211 if ( mlt_properties_get_int( self->properties, "width" ) > 0 )
212 self->width = mlt_properties_get_int( self->properties, "width" );
213 if ( mlt_properties_get_int( self->properties, "height" ) > 0 )
214 self->height = mlt_properties_get_int( self->properties, "height" );
217 self->bpp = mlt_properties_get_int( self->properties, "bpp" );
219 if ( sdl_started == 0 && display_off == 0 )
221 pthread_mutex_lock( &mlt_sdl_mutex );
222 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
223 pthread_mutex_unlock( &mlt_sdl_mutex );
226 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
230 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
231 SDL_EnableUNICODE( 1 );
234 if ( audio_off == 0 )
235 SDL_InitSubSystem( SDL_INIT_AUDIO );
237 // Default window size
238 if ( mlt_properties_get_int( self->properties, "_arg_size" ) )
240 self->window_width = self->width;
241 self->window_height = self->height;
245 double display_ratio = mlt_properties_get_double( self->properties, "display_ratio" );
246 self->window_width = ( double )self->height * display_ratio + 0.5;
247 self->window_height = self->height;
250 pthread_mutex_lock( &mlt_sdl_mutex );
251 if ( !SDL_GetVideoSurface() && display_off == 0 )
253 if ( mlt_properties_get_int( self->properties, "fullscreen" ) )
255 const SDL_VideoInfo *vi;
256 pthread_mutex_lock( &mlt_sdl_mutex );
257 vi = SDL_GetVideoInfo();
258 pthread_mutex_unlock( &mlt_sdl_mutex );
259 self->window_width = vi->current_w;
260 self->window_height = vi->current_h;
261 self->sdl_flags |= SDL_FULLSCREEN;
262 SDL_ShowCursor( SDL_DISABLE );
264 SDL_SetVideoMode( self->window_width, self->window_height, 0, self->sdl_flags );
266 pthread_mutex_unlock( &mlt_sdl_mutex );
268 pthread_create( &self->thread, NULL, consumer_thread, self );
274 int consumer_stop( mlt_consumer parent )
276 // Get the actual object
277 consumer_sdl self = parent->child;
279 if ( self->joined == 0 )
281 // Kill the thread and clean up
287 pthread_join( self->thread, NULL );
290 if ( self->sdl_overlay != NULL )
291 SDL_FreeYUVOverlay( self->sdl_overlay );
292 self->sdl_overlay = NULL;
294 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
296 pthread_mutex_lock( &self->audio_mutex );
297 pthread_cond_broadcast( &self->audio_cond );
298 pthread_mutex_unlock( &self->audio_mutex );
299 SDL_QuitSubSystem( SDL_INIT_AUDIO );
302 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
304 pthread_mutex_lock( &mlt_sdl_mutex );
306 pthread_mutex_unlock( &mlt_sdl_mutex );
313 int consumer_is_stopped( mlt_consumer parent )
315 consumer_sdl self = parent->child;
316 return !self->running;
319 static int sdl_lock_display( )
321 pthread_mutex_lock( &mlt_sdl_mutex );
322 SDL_Surface *screen = SDL_GetVideoSurface( );
323 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
324 pthread_mutex_unlock( &mlt_sdl_mutex );
328 static void sdl_unlock_display( )
330 pthread_mutex_lock( &mlt_sdl_mutex );
331 SDL_Surface *screen = SDL_GetVideoSurface( );
332 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
333 SDL_UnlockSurface( screen );
334 pthread_mutex_unlock( &mlt_sdl_mutex );
337 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
339 consumer_sdl self = udata;
342 double volume = mlt_properties_get_double( self->properties, "volume" );
344 pthread_mutex_lock( &self->audio_mutex );
346 // Block until audio received
347 while ( self->running && len > self->audio_avail )
348 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
350 if ( self->audio_avail >= len )
352 // Place in the audio buffer
354 SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
356 memcpy( stream, self->audio_buffer, len );
358 // Remove len from the audio available
359 self->audio_avail -= len;
361 // Remove the samples
362 memmove( self->audio_buffer, self->audio_buffer + len, self->audio_avail );
366 // Just to be safe, wipe the stream first
367 memset( stream, 0, len );
370 SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
373 self->audio_avail = 0;
376 // We're definitely playing now
379 pthread_cond_broadcast( &self->audio_cond );
380 pthread_mutex_unlock( &self->audio_mutex );
383 static int consumer_play_audio( consumer_sdl self, mlt_frame frame, int init_audio, int *duration )
385 // Get the properties of self consumer
386 mlt_properties properties = self->properties;
387 mlt_audio_format afmt = mlt_audio_s16;
389 // Set the preferred params of the test card signal
390 int channels = mlt_properties_get_int( properties, "channels" );
391 int dest_channels = channels;
392 int frequency = mlt_properties_get_int( properties, "frequency" );
393 static int counter = 0;
395 int samples = mlt_sample_calculator( mlt_properties_get_double( self->properties, "fps" ), frequency, counter++ );
400 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
401 *duration = ( ( samples * 1000 ) / frequency );
402 pcm += mlt_properties_get_int( properties, "audio_offset" );
404 if ( mlt_properties_get_int( properties, "audio_off" ) )
411 if ( init_audio == 1 )
413 SDL_AudioSpec request;
416 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
418 // specify audio format
419 memset( &request, 0, sizeof( SDL_AudioSpec ) );
421 request.freq = frequency;
422 request.format = AUDIO_S16SYS;
423 request.channels = dest_channels;
424 request.samples = audio_buffer;
425 request.callback = sdl_fill_audio;
426 request.userdata = (void *)self;
427 if ( SDL_OpenAudio( &request, &got ) != 0 )
429 mlt_log_error( MLT_CONSUMER_SERVICE( self ), "SDL failed to open audio: %s\n", SDL_GetError() );
432 else if ( got.size != 0 )
439 if ( init_audio == 0 )
441 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
443 bytes = samples * dest_channels * sizeof(*pcm);
444 pthread_mutex_lock( &self->audio_mutex );
445 while ( self->running && bytes > ( sizeof( self->audio_buffer) - self->audio_avail ) )
446 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
449 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
451 if ( channels == dest_channels )
453 memcpy( &self->audio_buffer[ self->audio_avail ], pcm, bytes );
457 int16_t *dest = (int16_t*) &self->audio_buffer[ self->audio_avail ];
462 memcpy( dest, pcm, dest_channels * sizeof(*pcm) );
464 dest += dest_channels;
470 memset( &self->audio_buffer[ self->audio_avail ], 0, bytes );
472 self->audio_avail += bytes;
474 pthread_cond_broadcast( &self->audio_cond );
475 pthread_mutex_unlock( &self->audio_mutex );
485 static int consumer_play_video( consumer_sdl self, mlt_frame frame )
487 // Get the properties of this consumer
488 mlt_properties properties = self->properties;
490 mlt_image_format vfmt = mlt_image_yuv422;
491 int width = self->width, height = self->height;
495 int video_off = mlt_properties_get_int( properties, "video_off" );
496 int preview_off = mlt_properties_get_int( properties, "preview_off" );
497 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
498 int display_off = video_off | preview_off;
500 if ( self->running && display_off == 0 )
502 // Get the image, width and height
503 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
505 void *pool = mlt_cocoa_autorelease_init();
508 if ( SDL_GetVideoSurface() )
513 pthread_mutex_lock( &mlt_sdl_mutex );
514 changed = consumer_get_dimensions( &self->window_width, &self->window_height );
515 pthread_mutex_unlock( &mlt_sdl_mutex );
516 sdl_unlock_display( );
518 while ( SDL_PollEvent( &event ) )
520 mlt_events_fire( self->properties, "consumer-sdl-event", &event, NULL );
524 case SDL_VIDEORESIZE:
525 self->window_width = event.resize.w;
526 self->window_height = event.resize.h;
534 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
535 char keyboard[ 2 ] = " ";
536 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
537 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
539 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
540 callback( producer, keyboard );
550 if ( width != self->width || height != self->height )
552 if ( self->sdl_overlay != NULL )
553 SDL_FreeYUVOverlay( self->sdl_overlay );
554 self->sdl_overlay = NULL;
557 if ( self->running && ( !SDL_GetVideoSurface() || changed ) )
559 // Force an overlay recreation
560 if ( self->sdl_overlay != NULL )
561 SDL_FreeYUVOverlay( self->sdl_overlay );
562 self->sdl_overlay = NULL;
564 // open SDL window with video overlay, if possible
565 pthread_mutex_lock( &mlt_sdl_mutex );
566 SDL_Surface *screen = SDL_SetVideoMode( self->window_width, self->window_height, self->bpp, self->sdl_flags );
567 if ( consumer_get_dimensions( &self->window_width, &self->window_height ) )
568 screen = SDL_SetVideoMode( self->window_width, self->window_height, self->bpp, self->sdl_flags );
569 pthread_mutex_unlock( &mlt_sdl_mutex );
573 uint32_t color = mlt_properties_get_int( self->properties, "window_background" );
574 SDL_FillRect( screen, NULL, color >> 8 );
581 // Determine window's new display aspect ratio
582 double this_aspect = ( double )self->window_width / self->window_height;
584 // Get the display aspect ratio
585 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
587 // Determine frame's display aspect ratio
588 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
590 // Store the width and height received
592 self->height = height;
594 // If using hardware scaler
595 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
596 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
598 // Use hardware scaler to normalise display aspect ratio
599 self->rect.w = frame_aspect / this_aspect * self->window_width;
600 self->rect.h = self->window_height;
601 if ( self->rect.w > self->window_width )
603 self->rect.w = self->window_width;
604 self->rect.h = this_aspect / frame_aspect * self->window_height;
607 // Special case optimisation to negate odd effect of sample aspect ratio
608 // not corresponding exactly with image resolution.
609 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
611 self->rect.w = self->window_width;
612 self->rect.h = self->window_height;
614 // Use hardware scaler to normalise sample aspect ratio
615 else if ( self->window_height * display_ratio > self->window_width )
617 self->rect.w = self->window_width;
618 self->rect.h = self->window_width / display_ratio;
622 self->rect.w = self->window_height * display_ratio;
623 self->rect.h = self->window_height;
626 self->rect.x = ( self->window_width - self->rect.w ) / 2;
627 self->rect.y = ( self->window_height - self->rect.h ) / 2;
628 self->rect.x -= self->rect.x % 2;
630 mlt_properties_set_int( self->properties, "rect_x", self->rect.x );
631 mlt_properties_set_int( self->properties, "rect_y", self->rect.y );
632 mlt_properties_set_int( self->properties, "rect_w", self->rect.w );
633 mlt_properties_set_int( self->properties, "rect_h", self->rect.h );
635 SDL_SetClipRect( SDL_GetVideoSurface(), &self->rect );
638 if ( self->running && SDL_GetVideoSurface() && self->sdl_overlay == NULL )
640 SDL_SetClipRect( SDL_GetVideoSurface(), &self->rect );
641 self->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, SDL_GetVideoSurface() );
644 if ( self->running && SDL_GetVideoSurface() && self->sdl_overlay != NULL )
646 self->buffer = self->sdl_overlay->pixels[ 0 ];
647 if ( SDL_LockYUVOverlay( self->sdl_overlay ) >= 0 )
650 memcpy( self->buffer, image, width * height * 2 );
651 SDL_UnlockYUVOverlay( self->sdl_overlay );
652 SDL_DisplayYUVOverlay( self->sdl_overlay, &SDL_GetVideoSurface()->clip_rect );
656 sdl_unlock_display();
657 mlt_cocoa_autorelease_close( pool );
658 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
660 else if ( self->running )
662 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
664 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
665 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
671 static void *video_thread( void *arg )
674 consumer_sdl self = arg;
676 // Obtain time of thread start
681 mlt_frame next = NULL;
682 mlt_properties properties = NULL;
685 // Get real time flag
686 int real_time = mlt_properties_get_int( self->properties, "real_time" );
688 // Get the current time
689 gettimeofday( &now, NULL );
691 // Determine start time
692 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
694 while ( self->running )
696 // Pop the next frame
697 pthread_mutex_lock( &self->video_mutex );
698 next = mlt_deque_pop_front( self->queue );
699 while ( next == NULL && self->running )
701 pthread_cond_wait( &self->video_cond, &self->video_mutex );
702 next = mlt_deque_pop_front( self->queue );
704 pthread_mutex_unlock( &self->video_mutex );
706 if ( !self->running || next == NULL ) break;
708 // Get the properties
709 properties = MLT_FRAME_PROPERTIES( next );
711 // Get the speed of the frame
712 speed = mlt_properties_get_double( properties, "_speed" );
714 // Get the current time
715 gettimeofday( &now, NULL );
717 // Get the elapsed time
718 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
720 // See if we have to delay the display of the current frame
721 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
723 // Obtain the scheduled playout time
724 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
726 // Determine the difference between the elapsed time and the scheduled playout time
727 int64_t difference = scheduled - elapsed;
729 // Smooth playback a bit
730 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
732 tm.tv_sec = difference / 1000000;
733 tm.tv_nsec = ( difference % 1000000 ) * 500;
734 nanosleep( &tm, NULL );
737 // Show current frame if not too old
738 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
739 consumer_play_video( self, next );
741 // If the queue is empty, recalculate start to allow build up again
742 if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
744 gettimeofday( &now, NULL );
745 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
750 static int dropped = 0;
751 mlt_log_info( MLT_CONSUMER_SERVICE(&self->parent), "dropped video frame %d\n", ++dropped );
754 // This frame can now be closed
755 mlt_frame_close( next );
760 mlt_frame_close( next );
762 mlt_consumer_stopped( &self->parent );
767 /** Threaded wrapper for pipe.
770 static void *consumer_thread( void *arg )
773 consumer_sdl self = arg;
776 mlt_consumer consumer = &self->parent;
778 // Convenience functionality
779 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
785 // internal intialization
788 mlt_frame frame = NULL;
790 int64_t playtime = 0;
791 struct timespec tm = { 0, 100000 };
793 // Loop until told not to
794 while( self->running )
796 // Get a frame from the attached producer
797 frame = !terminated? mlt_consumer_rt_frame( consumer ) : NULL;
799 // Check for termination
800 if ( terminate_on_pause && frame )
801 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
803 // Ensure that we have a frame
807 init_audio = consumer_play_audio( self, frame, init_audio, &duration );
809 // Determine the start time now
810 if ( self->playing && init_video )
812 // Create the video thread
813 pthread_create( &thread, NULL, video_thread, self );
815 // Video doesn't need to be initialised any more
819 // Set playtime for this frame
820 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "playtime", playtime );
822 while ( self->running && mlt_deque_count( self->queue ) > 15 )
823 nanosleep( &tm, NULL );
825 // Push this frame to the back of the queue
826 pthread_mutex_lock( &self->video_mutex );
827 mlt_deque_push_back( self->queue, frame );
828 pthread_cond_broadcast( &self->video_cond );
829 pthread_mutex_unlock( &self->video_mutex );
831 // Calculate the next playtime
832 playtime += ( duration * 1000 );
834 else if ( terminated )
836 if ( init_video || mlt_deque_count( self->queue ) == 0 )
839 nanosleep( &tm, NULL );
845 // Unblock sdl_preview
846 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "put_mode" ) == 1 )
848 frame = mlt_consumer_get_frame( consumer );
850 mlt_frame_close( frame );
854 // Kill the video thread
855 if ( init_video == 0 )
857 pthread_mutex_lock( &self->video_mutex );
858 pthread_cond_broadcast( &self->video_cond );
859 pthread_mutex_unlock( &self->video_mutex );
860 pthread_join( thread, NULL );
863 while( mlt_deque_count( self->queue ) )
864 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
866 self->audio_avail = 0;
871 static int consumer_get_dimensions( int *width, int *height )
875 // SDL windows manager structure
878 // Specify the SDL Version
879 SDL_VERSION( &wm.version );
882 //sdl_lock_display();
885 // Get the wm structure
886 if ( SDL_GetWMInfo( &wm ) == 1 )
889 // Check that we have the X11 wm
890 if ( wm.subsystem == SDL_SYSWM_X11 )
892 // Get the SDL window
893 Window window = wm.info.x11.window;
895 // Get the display session
896 Display *display = wm.info.x11.display;
898 // Get the window attributes
899 XWindowAttributes attr;
900 XGetWindowAttributes( display, window, &attr );
902 // Determine whether window has changed
903 changed = *width != attr.width || *height != attr.height;
905 // Return width and height
907 *height = attr.height;
913 // Unlock the display
914 //sdl_unlock_display();
919 /** Callback to allow override of the close method.
922 static void consumer_close( mlt_consumer parent )
924 // Get the actual object
925 consumer_sdl self = parent->child;
928 ///mlt_consumer_stop( parent );
930 // Now clean up the rest
931 mlt_consumer_close( parent );
934 mlt_deque_close( self->queue );
937 pthread_mutex_destroy( &self->audio_mutex );
938 pthread_cond_destroy( &self->audio_cond );
940 // Finally clean up this