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;
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_purge( mlt_consumer parent );
79 static void consumer_close( mlt_consumer parent );
80 static void *consumer_thread( void * );
81 static int consumer_get_dimensions( int *width, int *height );
82 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
84 /** This is what will be called by the factory - anything can be passed in
85 via the argument, but keep it simple.
88 mlt_consumer consumer_sdl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
90 // Create the consumer object
91 consumer_sdl self = calloc( 1, sizeof( struct consumer_sdl_s ) );
93 // If no malloc'd and consumer init ok
94 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
97 self->queue = mlt_deque_init( );
99 // Get the parent consumer object
100 mlt_consumer parent = &self->parent;
102 // We have stuff to clean up, so override the close method
103 parent->close = consumer_close;
105 // get a handle on properties
106 mlt_service service = MLT_CONSUMER_SERVICE( parent );
107 self->properties = MLT_SERVICE_PROPERTIES( service );
109 // Set the default volume
110 mlt_properties_set_double( self->properties, "volume", 1.0 );
112 // This is the initialisation of the consumer
113 pthread_mutex_init( &self->audio_mutex, NULL );
114 pthread_cond_init( &self->audio_cond, NULL);
115 pthread_mutex_init( &self->video_mutex, NULL );
116 pthread_cond_init( &self->video_cond, NULL);
118 // Default scaler (for now we'll use nearest)
119 mlt_properties_set( self->properties, "rescale", "nearest" );
120 mlt_properties_set( self->properties, "deinterlace_method", "onefield" );
121 mlt_properties_set_int( self->properties, "top_field_first", -1 );
123 // Default buffer for low latency
124 mlt_properties_set_int( self->properties, "buffer", 1 );
126 // Default audio buffer
127 mlt_properties_set_int( self->properties, "audio_buffer", 2048 );
129 // Ensure we don't join on a non-running object
132 // process actual param
133 if ( arg && sscanf( arg, "%dx%d", &self->width, &self->height ) )
135 mlt_properties_set_int( self->properties, "_arg_size", 1 );
139 self->width = mlt_properties_get_int( self->properties, "width" );
140 self->height = mlt_properties_get_int( self->properties, "height" );
144 self->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF;
145 #if !defined(__DARWIN__)
146 self->sdl_flags |= SDL_RESIZABLE;
148 // Allow thread to be started/stopped
149 parent->start = consumer_start;
150 parent->stop = consumer_stop;
151 parent->is_stopped = consumer_is_stopped;
152 parent->purge = consumer_purge;
154 // Register specific events
155 mlt_events_register( self->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
157 // Return the consumer produced
161 // malloc or consumer init failed
168 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
170 if ( listener != NULL )
171 listener( owner, self, ( SDL_Event * )args[ 0 ] );
174 int consumer_start( mlt_consumer parent )
176 consumer_sdl self = parent->child;
178 if ( !self->running )
180 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
181 int video_off = mlt_properties_get_int( properties, "video_off" );
182 int preview_off = mlt_properties_get_int( properties, "preview_off" );
183 int display_off = video_off | preview_off;
184 int audio_off = mlt_properties_get_int( properties, "audio_off" );
185 int sdl_started = mlt_properties_get_int( properties, "sdl_started" );
186 char *output_display = mlt_properties_get( properties, "output_display" );
187 char *window_id = mlt_properties_get( properties, "window_id" );
188 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
189 char *video_driver = mlt_properties_get( properties, "video_driver" );
190 char *audio_device = mlt_properties_get( properties, "audio_device" );
192 consumer_stop( parent );
197 if ( output_display != NULL )
198 setenv( "DISPLAY", output_display, 1 );
200 if ( window_id != NULL )
201 setenv( "SDL_WINDOWID", window_id, 1 );
203 if ( video_driver != NULL )
204 setenv( "SDL_VIDEODRIVER", video_driver, 1 );
206 if ( audio_driver != NULL )
207 setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
209 if ( audio_device != NULL )
210 setenv( "AUDIODEV", audio_device, 1 );
212 if ( ! mlt_properties_get_int( self->properties, "_arg_size" ) )
214 if ( mlt_properties_get_int( self->properties, "width" ) > 0 )
215 self->width = mlt_properties_get_int( self->properties, "width" );
216 if ( mlt_properties_get_int( self->properties, "height" ) > 0 )
217 self->height = mlt_properties_get_int( self->properties, "height" );
220 self->bpp = mlt_properties_get_int( self->properties, "bpp" );
222 if ( sdl_started == 0 && display_off == 0 )
224 pthread_mutex_lock( &mlt_sdl_mutex );
225 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
226 pthread_mutex_unlock( &mlt_sdl_mutex );
229 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
233 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
234 SDL_EnableUNICODE( 1 );
237 if ( audio_off == 0 )
238 SDL_InitSubSystem( SDL_INIT_AUDIO );
240 // Default window size
241 if ( mlt_properties_get_int( self->properties, "_arg_size" ) )
243 self->window_width = self->width;
244 self->window_height = self->height;
248 double display_ratio = mlt_properties_get_double( self->properties, "display_ratio" );
249 self->window_width = ( double )self->height * display_ratio + 0.5;
250 self->window_height = self->height;
253 pthread_mutex_lock( &mlt_sdl_mutex );
254 if ( !SDL_GetVideoSurface() && display_off == 0 )
256 if ( mlt_properties_get_int( self->properties, "fullscreen" ) )
258 const SDL_VideoInfo *vi;
259 pthread_mutex_lock( &mlt_sdl_mutex );
260 vi = SDL_GetVideoInfo();
261 pthread_mutex_unlock( &mlt_sdl_mutex );
262 self->window_width = vi->current_w;
263 self->window_height = vi->current_h;
264 self->sdl_flags |= SDL_FULLSCREEN;
265 SDL_ShowCursor( SDL_DISABLE );
267 SDL_SetVideoMode( self->window_width, self->window_height, 0, self->sdl_flags );
269 pthread_mutex_unlock( &mlt_sdl_mutex );
271 pthread_create( &self->thread, NULL, consumer_thread, self );
277 int consumer_stop( mlt_consumer parent )
279 // Get the actual object
280 consumer_sdl self = parent->child;
282 if ( self->joined == 0 )
284 // Kill the thread and clean up
290 pthread_join( self->thread, NULL );
293 if ( self->sdl_overlay != NULL )
294 SDL_FreeYUVOverlay( self->sdl_overlay );
295 self->sdl_overlay = NULL;
297 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
299 pthread_mutex_lock( &self->audio_mutex );
300 pthread_cond_broadcast( &self->audio_cond );
301 pthread_mutex_unlock( &self->audio_mutex );
302 SDL_QuitSubSystem( SDL_INIT_AUDIO );
305 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
307 pthread_mutex_lock( &mlt_sdl_mutex );
309 pthread_mutex_unlock( &mlt_sdl_mutex );
316 int consumer_is_stopped( mlt_consumer parent )
318 consumer_sdl self = parent->child;
319 return !self->running;
322 void consumer_purge( mlt_consumer parent )
324 consumer_sdl self = parent->child;
327 pthread_mutex_lock( &self->video_mutex );
328 while ( mlt_deque_count( self->queue ) )
329 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
331 pthread_cond_broadcast( &self->video_cond );
332 pthread_mutex_unlock( &self->video_mutex );
336 static int sdl_lock_display( )
338 pthread_mutex_lock( &mlt_sdl_mutex );
339 SDL_Surface *screen = SDL_GetVideoSurface( );
340 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
341 pthread_mutex_unlock( &mlt_sdl_mutex );
345 static void sdl_unlock_display( )
347 pthread_mutex_lock( &mlt_sdl_mutex );
348 SDL_Surface *screen = SDL_GetVideoSurface( );
349 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
350 SDL_UnlockSurface( screen );
351 pthread_mutex_unlock( &mlt_sdl_mutex );
354 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
356 consumer_sdl self = udata;
359 double volume = mlt_properties_get_double( self->properties, "volume" );
361 pthread_mutex_lock( &self->audio_mutex );
363 // Block until audio received
364 while ( self->running && len > self->audio_avail )
365 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
367 if ( self->audio_avail >= len )
369 // Place in the audio buffer
371 SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
373 memcpy( stream, self->audio_buffer, len );
375 // Remove len from the audio available
376 self->audio_avail -= len;
378 // Remove the samples
379 memmove( self->audio_buffer, self->audio_buffer + len, self->audio_avail );
383 // Just to be safe, wipe the stream first
384 memset( stream, 0, len );
387 SDL_MixAudio( stream, self->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
390 self->audio_avail = 0;
393 // We're definitely playing now
396 pthread_cond_broadcast( &self->audio_cond );
397 pthread_mutex_unlock( &self->audio_mutex );
400 static int consumer_play_audio( consumer_sdl self, mlt_frame frame, int init_audio, int *duration )
402 // Get the properties of self consumer
403 mlt_properties properties = self->properties;
404 mlt_audio_format afmt = mlt_audio_s16;
406 // Set the preferred params of the test card signal
407 int channels = mlt_properties_get_int( properties, "channels" );
408 int dest_channels = channels;
409 int frequency = mlt_properties_get_int( properties, "frequency" );
410 static int counter = 0;
412 int samples = mlt_sample_calculator( mlt_properties_get_double( self->properties, "fps" ), frequency, counter++ );
417 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
418 *duration = ( ( samples * 1000 ) / frequency );
419 pcm += mlt_properties_get_int( properties, "audio_offset" );
421 if ( mlt_properties_get_int( properties, "audio_off" ) )
428 if ( init_audio == 1 )
430 SDL_AudioSpec request;
433 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
435 // specify audio format
436 memset( &request, 0, sizeof( SDL_AudioSpec ) );
438 request.freq = frequency;
439 request.format = AUDIO_S16SYS;
440 request.channels = dest_channels;
441 request.samples = audio_buffer;
442 request.callback = sdl_fill_audio;
443 request.userdata = (void *)self;
444 if ( SDL_OpenAudio( &request, &got ) != 0 )
446 mlt_log_error( MLT_CONSUMER_SERVICE( self ), "SDL failed to open audio: %s\n", SDL_GetError() );
449 else if ( got.size != 0 )
456 if ( init_audio == 0 )
458 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
460 bytes = samples * dest_channels * sizeof(*pcm);
461 pthread_mutex_lock( &self->audio_mutex );
462 while ( self->running && bytes > ( sizeof( self->audio_buffer) - self->audio_avail ) )
463 pthread_cond_wait( &self->audio_cond, &self->audio_mutex );
466 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
468 if ( channels == dest_channels )
470 memcpy( &self->audio_buffer[ self->audio_avail ], pcm, bytes );
474 int16_t *dest = (int16_t*) &self->audio_buffer[ self->audio_avail ];
479 memcpy( dest, pcm, dest_channels * sizeof(*pcm) );
481 dest += dest_channels;
487 memset( &self->audio_buffer[ self->audio_avail ], 0, bytes );
489 self->audio_avail += bytes;
491 pthread_cond_broadcast( &self->audio_cond );
492 pthread_mutex_unlock( &self->audio_mutex );
502 static int consumer_play_video( consumer_sdl self, mlt_frame frame )
504 // Get the properties of this consumer
505 mlt_properties properties = self->properties;
507 mlt_image_format vfmt = mlt_image_yuv422;
508 int width = self->width, height = self->height;
512 int video_off = mlt_properties_get_int( properties, "video_off" );
513 int preview_off = mlt_properties_get_int( properties, "preview_off" );
514 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
515 int display_off = video_off | preview_off;
517 if ( self->running && display_off == 0 )
519 // Get the image, width and height
520 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
522 void *pool = mlt_cocoa_autorelease_init();
525 if ( SDL_GetVideoSurface() )
530 pthread_mutex_lock( &mlt_sdl_mutex );
531 changed = consumer_get_dimensions( &self->window_width, &self->window_height );
532 pthread_mutex_unlock( &mlt_sdl_mutex );
533 sdl_unlock_display( );
535 while ( SDL_PollEvent( &event ) )
537 mlt_events_fire( self->properties, "consumer-sdl-event", &event, NULL );
541 case SDL_VIDEORESIZE:
542 self->window_width = event.resize.w;
543 self->window_height = event.resize.h;
551 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
552 char keyboard[ 2 ] = " ";
553 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
554 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
556 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
557 callback( producer, keyboard );
567 if ( width != self->width || height != self->height )
569 if ( self->sdl_overlay != NULL )
570 SDL_FreeYUVOverlay( self->sdl_overlay );
571 self->sdl_overlay = NULL;
574 if ( self->running && ( !SDL_GetVideoSurface() || changed ) )
576 // Force an overlay recreation
577 if ( self->sdl_overlay != NULL )
578 SDL_FreeYUVOverlay( self->sdl_overlay );
579 self->sdl_overlay = NULL;
581 // open SDL window with video overlay, if possible
582 pthread_mutex_lock( &mlt_sdl_mutex );
583 SDL_Surface *screen = SDL_SetVideoMode( self->window_width, self->window_height, self->bpp, self->sdl_flags );
584 if ( consumer_get_dimensions( &self->window_width, &self->window_height ) )
585 screen = SDL_SetVideoMode( self->window_width, self->window_height, self->bpp, self->sdl_flags );
586 pthread_mutex_unlock( &mlt_sdl_mutex );
590 uint32_t color = mlt_properties_get_int( self->properties, "window_background" );
591 SDL_FillRect( screen, NULL, color >> 8 );
598 // Determine window's new display aspect ratio
599 double this_aspect = ( double )self->window_width / self->window_height;
601 // Get the display aspect ratio
602 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
604 // Determine frame's display aspect ratio
605 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
607 // Store the width and height received
609 self->height = height;
611 // If using hardware scaler
612 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
613 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
615 // Use hardware scaler to normalise display aspect ratio
616 self->rect.w = frame_aspect / this_aspect * self->window_width;
617 self->rect.h = self->window_height;
618 if ( self->rect.w > self->window_width )
620 self->rect.w = self->window_width;
621 self->rect.h = this_aspect / frame_aspect * self->window_height;
624 // Special case optimisation to negate odd effect of sample aspect ratio
625 // not corresponding exactly with image resolution.
626 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
628 self->rect.w = self->window_width;
629 self->rect.h = self->window_height;
631 // Use hardware scaler to normalise sample aspect ratio
632 else if ( self->window_height * display_ratio > self->window_width )
634 self->rect.w = self->window_width;
635 self->rect.h = self->window_width / display_ratio;
639 self->rect.w = self->window_height * display_ratio;
640 self->rect.h = self->window_height;
643 self->rect.x = ( self->window_width - self->rect.w ) / 2;
644 self->rect.y = ( self->window_height - self->rect.h ) / 2;
645 self->rect.x -= self->rect.x % 2;
647 mlt_properties_set_int( self->properties, "rect_x", self->rect.x );
648 mlt_properties_set_int( self->properties, "rect_y", self->rect.y );
649 mlt_properties_set_int( self->properties, "rect_w", self->rect.w );
650 mlt_properties_set_int( self->properties, "rect_h", self->rect.h );
652 SDL_SetClipRect( SDL_GetVideoSurface(), &self->rect );
655 if ( self->running && SDL_GetVideoSurface() && self->sdl_overlay == NULL )
657 SDL_SetClipRect( SDL_GetVideoSurface(), &self->rect );
658 self->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, SDL_GetVideoSurface() );
661 if ( self->running && SDL_GetVideoSurface() && self->sdl_overlay != NULL )
663 self->buffer = self->sdl_overlay->pixels[ 0 ];
664 if ( SDL_LockYUVOverlay( self->sdl_overlay ) >= 0 )
667 memcpy( self->buffer, image, width * height * 2 );
668 SDL_UnlockYUVOverlay( self->sdl_overlay );
669 SDL_DisplayYUVOverlay( self->sdl_overlay, &SDL_GetVideoSurface()->clip_rect );
673 sdl_unlock_display();
674 mlt_cocoa_autorelease_close( pool );
675 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
677 else if ( self->running )
679 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
681 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
682 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
688 static void *video_thread( void *arg )
691 consumer_sdl self = arg;
693 // Obtain time of thread start
698 mlt_frame next = NULL;
699 mlt_properties properties = NULL;
702 // Get real time flag
703 int real_time = mlt_properties_get_int( self->properties, "real_time" );
705 // Get the current time
706 gettimeofday( &now, NULL );
708 // Determine start time
709 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
711 while ( self->running )
713 // Pop the next frame
714 pthread_mutex_lock( &self->video_mutex );
715 next = mlt_deque_pop_front( self->queue );
716 while ( next == NULL && self->running )
718 pthread_cond_wait( &self->video_cond, &self->video_mutex );
719 next = mlt_deque_pop_front( self->queue );
721 pthread_mutex_unlock( &self->video_mutex );
723 if ( !self->running || next == NULL ) break;
725 // Get the properties
726 properties = MLT_FRAME_PROPERTIES( next );
728 // Get the speed of the frame
729 speed = mlt_properties_get_double( properties, "_speed" );
731 // Get the current time
732 gettimeofday( &now, NULL );
734 // Get the elapsed time
735 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
737 // See if we have to delay the display of the current frame
738 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && self->running )
740 // Obtain the scheduled playout time
741 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
743 // Determine the difference between the elapsed time and the scheduled playout time
744 int64_t difference = scheduled - elapsed;
746 // Smooth playback a bit
747 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
749 tm.tv_sec = difference / 1000000;
750 tm.tv_nsec = ( difference % 1000000 ) * 500;
751 nanosleep( &tm, NULL );
754 // Show current frame if not too old
755 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( self->queue ) < 2 ) )
756 consumer_play_video( self, next );
758 // If the queue is empty, recalculate start to allow build up again
759 if ( real_time && ( mlt_deque_count( self->queue ) == 0 && speed == 1.0 ) )
761 gettimeofday( &now, NULL );
762 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
767 static int dropped = 0;
768 mlt_log_info( MLT_CONSUMER_SERVICE(&self->parent), "dropped video frame %d\n", ++dropped );
771 // This frame can now be closed
772 mlt_frame_close( next );
777 mlt_frame_close( next );
779 mlt_consumer_stopped( &self->parent );
784 /** Threaded wrapper for pipe.
787 static void *consumer_thread( void *arg )
790 consumer_sdl self = arg;
793 mlt_consumer consumer = &self->parent;
795 // Convenience functionality
796 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
802 // internal intialization
805 mlt_frame frame = NULL;
807 int64_t playtime = 0;
808 struct timespec tm = { 0, 100000 };
810 // Loop until told not to
811 while( self->running )
813 // Get a frame from the attached producer
814 frame = !terminated? mlt_consumer_rt_frame( consumer ) : NULL;
816 // Check for termination
817 if ( terminate_on_pause && frame )
818 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
820 // Ensure that we have a frame
824 init_audio = consumer_play_audio( self, frame, init_audio, &duration );
826 // Determine the start time now
827 if ( self->playing && init_video )
829 // Create the video thread
830 pthread_create( &thread, NULL, video_thread, self );
832 // Video doesn't need to be initialised any more
836 // Set playtime for this frame
837 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "playtime", playtime );
839 while ( self->running && mlt_deque_count( self->queue ) > 15 )
840 nanosleep( &tm, NULL );
842 // Push this frame to the back of the queue
843 pthread_mutex_lock( &self->video_mutex );
844 if ( self->is_purge )
846 mlt_frame_close( frame );
852 mlt_deque_push_back( self->queue, frame );
853 pthread_cond_broadcast( &self->video_cond );
855 pthread_mutex_unlock( &self->video_mutex );
857 // Calculate the next playtime
858 playtime += ( duration * 1000 );
860 else if ( terminated )
862 if ( init_video || mlt_deque_count( self->queue ) == 0 )
865 nanosleep( &tm, NULL );
871 // Unblock sdl_preview
872 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "put_mode" ) == 1 )
874 frame = mlt_consumer_get_frame( consumer );
876 mlt_frame_close( frame );
880 // Kill the video thread
881 if ( init_video == 0 )
883 pthread_mutex_lock( &self->video_mutex );
884 pthread_cond_broadcast( &self->video_cond );
885 pthread_mutex_unlock( &self->video_mutex );
886 pthread_join( thread, NULL );
889 while( mlt_deque_count( self->queue ) )
890 mlt_frame_close( mlt_deque_pop_back( self->queue ) );
892 self->audio_avail = 0;
897 static int consumer_get_dimensions( int *width, int *height )
901 // SDL windows manager structure
904 // Specify the SDL Version
905 SDL_VERSION( &wm.version );
908 //sdl_lock_display();
911 // Get the wm structure
912 if ( SDL_GetWMInfo( &wm ) == 1 )
915 // Check that we have the X11 wm
916 if ( wm.subsystem == SDL_SYSWM_X11 )
918 // Get the SDL window
919 Window window = wm.info.x11.window;
921 // Get the display session
922 Display *display = wm.info.x11.display;
924 // Get the window attributes
925 XWindowAttributes attr;
926 XGetWindowAttributes( display, window, &attr );
928 // Determine whether window has changed
929 changed = *width != attr.width || *height != attr.height;
931 // Return width and height
933 *height = attr.height;
939 // Unlock the display
940 //sdl_unlock_display();
945 /** Callback to allow override of the close method.
948 static void consumer_close( mlt_consumer parent )
950 // Get the actual object
951 consumer_sdl self = parent->child;
954 ///mlt_consumer_stop( parent );
956 // Now clean up the rest
957 mlt_consumer_close( parent );
960 mlt_deque_close( self->queue );
963 pthread_mutex_destroy( &self->audio_mutex );
964 pthread_cond_destroy( &self->audio_cond );
966 // Finally clean up this