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( 1, sizeof( struct consumer_sdl_s ) );
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" );
120 mlt_properties_set_int( this->properties, "top_field_first", -1 );
122 // Default buffer for low latency
123 mlt_properties_set_int( this->properties, "buffer", 1 );
125 // Default audio buffer
126 mlt_properties_set_int( this->properties, "audio_buffer", 2048 );
128 // Ensure we don't join on a non-running object
131 // process actual param
132 if ( arg && sscanf( arg, "%dx%d", &this->width, &this->height ) )
134 mlt_properties_set_int( this->properties, "_arg_size", 1 );
138 this->width = mlt_properties_get_int( this->properties, "width" );
139 this->height = mlt_properties_get_int( this->properties, "height" );
143 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_DOUBLEBUF;
144 #if !defined(__DARWIN__)
145 this->sdl_flags |= SDL_RESIZABLE;
147 // Allow thread to be started/stopped
148 parent->start = consumer_start;
149 parent->stop = consumer_stop;
150 parent->is_stopped = consumer_is_stopped;
152 // Register specific events
153 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
155 // Return the consumer produced
159 // malloc or consumer init failed
166 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
168 if ( listener != NULL )
169 listener( owner, this, ( SDL_Event * )args[ 0 ] );
172 int consumer_start( mlt_consumer parent )
174 consumer_sdl this = parent->child;
176 if ( !this->running )
178 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
179 int video_off = mlt_properties_get_int( properties, "video_off" );
180 int preview_off = mlt_properties_get_int( properties, "preview_off" );
181 int display_off = video_off | preview_off;
182 int audio_off = mlt_properties_get_int( properties, "audio_off" );
183 int sdl_started = mlt_properties_get_int( properties, "sdl_started" );
184 char *output_display = mlt_properties_get( properties, "output_display" );
185 char *window_id = mlt_properties_get( properties, "window_id" );
186 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
187 char *video_driver = mlt_properties_get( properties, "video_driver" );
188 char *audio_device = mlt_properties_get( properties, "audio_device" );
190 consumer_stop( parent );
195 if ( output_display != NULL )
196 setenv( "DISPLAY", output_display, 1 );
198 if ( window_id != NULL )
199 setenv( "SDL_WINDOWID", window_id, 1 );
201 if ( video_driver != NULL )
202 setenv( "SDL_VIDEODRIVER", video_driver, 1 );
204 if ( audio_driver != NULL )
205 setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
207 if ( audio_device != NULL )
208 setenv( "AUDIODEV", audio_device, 1 );
210 if ( ! mlt_properties_get_int( this->properties, "_arg_size" ) )
212 if ( mlt_properties_get_int( this->properties, "width" ) > 0 )
213 this->width = mlt_properties_get_int( this->properties, "width" );
214 if ( mlt_properties_get_int( this->properties, "height" ) > 0 )
215 this->height = mlt_properties_get_int( this->properties, "height" );
218 this->bpp = mlt_properties_get_int( this->properties, "bpp" );
220 if ( sdl_started == 0 && display_off == 0 )
222 pthread_mutex_lock( &mlt_sdl_mutex );
223 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
224 pthread_mutex_unlock( &mlt_sdl_mutex );
227 mlt_log_error( MLT_CONSUMER_SERVICE(parent), "Failed to initialize SDL: %s\n", SDL_GetError() );
231 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
232 SDL_EnableUNICODE( 1 );
234 else if ( display_off == 0 )
236 pthread_mutex_lock( &mlt_sdl_mutex );
237 this->sdl_screen = SDL_GetVideoSurface( );
238 pthread_mutex_unlock( &mlt_sdl_mutex );
241 if ( audio_off == 0 )
242 SDL_InitSubSystem( SDL_INIT_AUDIO );
244 // Default window size
245 if ( mlt_properties_get_int( this->properties, "_arg_size" ) )
247 this->window_width = this->width;
248 this->window_height = this->height;
252 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
253 this->window_width = ( double )this->height * display_ratio + 0.5;
254 this->window_height = this->height;
257 if ( this->sdl_screen == NULL && display_off == 0 )
259 if ( mlt_properties_get_int( this->properties, "fullscreen" ) )
261 const SDL_VideoInfo *vi;
262 pthread_mutex_lock( &mlt_sdl_mutex );
263 vi = SDL_GetVideoInfo();
264 pthread_mutex_unlock( &mlt_sdl_mutex );
265 this->window_width = vi->current_w;
266 this->window_height = vi->current_h;
267 this->sdl_flags |= SDL_FULLSCREEN;
268 SDL_ShowCursor( SDL_DISABLE );
270 pthread_mutex_lock( &mlt_sdl_mutex );
271 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
272 pthread_mutex_unlock( &mlt_sdl_mutex );
275 pthread_create( &this->thread, NULL, consumer_thread, this );
281 int consumer_stop( mlt_consumer parent )
283 // Get the actual object
284 consumer_sdl this = parent->child;
286 if ( this->joined == 0 )
288 // Kill the thread and clean up
294 pthread_join( this->thread, NULL );
297 if ( this->sdl_overlay != NULL )
298 SDL_FreeYUVOverlay( this->sdl_overlay );
299 this->sdl_overlay = NULL;
301 if ( !mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "audio_off" ) )
303 pthread_mutex_lock( &this->audio_mutex );
304 pthread_cond_broadcast( &this->audio_cond );
305 pthread_mutex_unlock( &this->audio_mutex );
306 SDL_QuitSubSystem( SDL_INIT_AUDIO );
309 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
311 pthread_mutex_lock( &mlt_sdl_mutex );
313 pthread_mutex_unlock( &mlt_sdl_mutex );
316 pthread_mutex_lock( &mlt_sdl_mutex );
317 this->sdl_screen = NULL;
318 pthread_mutex_unlock( &mlt_sdl_mutex );
324 int consumer_is_stopped( mlt_consumer parent )
326 consumer_sdl this = parent->child;
327 return !this->running;
330 static int sdl_lock_display( )
332 pthread_mutex_lock( &mlt_sdl_mutex );
333 SDL_Surface *screen = SDL_GetVideoSurface( );
334 int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
335 pthread_mutex_unlock( &mlt_sdl_mutex );
339 static void sdl_unlock_display( )
341 pthread_mutex_lock( &mlt_sdl_mutex );
342 SDL_Surface *screen = SDL_GetVideoSurface( );
343 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
344 SDL_UnlockSurface( screen );
345 pthread_mutex_unlock( &mlt_sdl_mutex );
348 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
350 consumer_sdl this = udata;
353 double volume = mlt_properties_get_double( this->properties, "volume" );
355 pthread_mutex_lock( &this->audio_mutex );
357 // Block until audio received
358 while ( this->running && len > this->audio_avail )
359 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
361 if ( this->audio_avail >= len )
363 // Place in the audio buffer
365 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
367 memcpy( stream, this->audio_buffer, len );
369 // Remove len from the audio available
370 this->audio_avail -= len;
372 // Remove the samples
373 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
377 // Just to be safe, wipe the stream first
378 memset( stream, 0, len );
381 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
384 this->audio_avail = 0;
387 // We're definitely playing now
390 pthread_cond_broadcast( &this->audio_cond );
391 pthread_mutex_unlock( &this->audio_mutex );
394 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
396 // Get the properties of this consumer
397 mlt_properties properties = this->properties;
398 mlt_audio_format afmt = mlt_audio_s16;
400 // Set the preferred params of the test card signal
401 int channels = mlt_properties_get_int( properties, "channels" );
402 int dest_channels = channels;
403 int frequency = mlt_properties_get_int( properties, "frequency" );
404 static int counter = 0;
406 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
411 mlt_frame_get_audio( frame, (void**) &pcm, &afmt, &frequency, &channels, &samples );
412 *duration = ( ( samples * 1000 ) / frequency );
413 pcm += mlt_properties_get_int( properties, "audio_offset" );
415 if ( mlt_properties_get_int( properties, "audio_off" ) )
422 if ( init_audio == 1 )
424 SDL_AudioSpec request;
427 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
429 // specify audio format
430 memset( &request, 0, sizeof( SDL_AudioSpec ) );
432 request.freq = frequency;
433 request.format = AUDIO_S16SYS;
434 request.channels = dest_channels;
435 request.samples = audio_buffer;
436 request.callback = sdl_fill_audio;
437 request.userdata = (void *)this;
438 if ( SDL_OpenAudio( &request, &got ) != 0 )
440 mlt_log_error( MLT_CONSUMER_SERVICE( this ), "SDL failed to open audio: %s\n", SDL_GetError() );
443 else if ( got.size != 0 )
450 if ( init_audio == 0 )
452 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
454 bytes = samples * dest_channels * sizeof(*pcm);
455 pthread_mutex_lock( &this->audio_mutex );
456 while ( this->running && bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
457 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
460 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
462 if ( channels == dest_channels )
464 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
468 int16_t *dest = (int16_t*) &this->audio_buffer[ this->audio_avail ];
473 memcpy( dest, pcm, dest_channels * sizeof(*pcm) );
475 dest += dest_channels;
481 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
483 this->audio_avail += bytes;
485 pthread_cond_broadcast( &this->audio_cond );
486 pthread_mutex_unlock( &this->audio_mutex );
496 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
498 // Get the properties of this consumer
499 mlt_properties properties = this->properties;
501 mlt_image_format vfmt = mlt_image_yuv422;
502 int width = this->width, height = this->height;
506 int video_off = mlt_properties_get_int( properties, "video_off" );
507 int preview_off = mlt_properties_get_int( properties, "preview_off" );
508 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
509 int display_off = video_off | preview_off;
511 if ( this->running && display_off == 0 )
513 // Get the image, width and height
514 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
516 void *pool = mlt_cocoa_autorelease_init();
519 if ( this->sdl_screen != NULL )
524 pthread_mutex_lock( &mlt_sdl_mutex );
525 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
526 pthread_mutex_unlock( &mlt_sdl_mutex );
527 sdl_unlock_display( );
529 while ( SDL_PollEvent( &event ) )
531 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
535 case SDL_VIDEORESIZE:
536 this->window_width = event.resize.w;
537 this->window_height = event.resize.h;
545 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
546 char keyboard[ 2 ] = " ";
547 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
548 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
550 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
551 callback( producer, keyboard );
561 if ( width != this->width || height != this->height )
563 if ( this->sdl_overlay != NULL )
564 SDL_FreeYUVOverlay( this->sdl_overlay );
565 this->sdl_overlay = NULL;
568 if ( this->running && ( this->sdl_screen == NULL || changed ) )
570 // Force an overlay recreation
571 if ( this->sdl_overlay != NULL )
572 SDL_FreeYUVOverlay( this->sdl_overlay );
573 this->sdl_overlay = NULL;
575 // open SDL window with video overlay, if possible
576 pthread_mutex_lock( &mlt_sdl_mutex );
577 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
578 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
579 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, this->bpp, this->sdl_flags );
580 pthread_mutex_unlock( &mlt_sdl_mutex );
582 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
583 SDL_FillRect( this->sdl_screen, NULL, color >> 8 );
584 SDL_Flip( this->sdl_screen );
589 // Determine window's new display aspect ratio
590 double this_aspect = ( double )this->window_width / this->window_height;
592 // Get the display aspect ratio
593 double display_ratio = mlt_properties_get_double( properties, "display_ratio" );
595 // Determine frame's display aspect ratio
596 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
598 // Store the width and height received
600 this->height = height;
602 // If using hardware scaler
603 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
604 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
606 // Use hardware scaler to normalise display aspect ratio
607 this->rect.w = frame_aspect / this_aspect * this->window_width;
608 this->rect.h = this->window_height;
609 if ( this->rect.w > this->window_width )
611 this->rect.w = this->window_width;
612 this->rect.h = this_aspect / frame_aspect * this->window_height;
615 // Special case optimisation to negate odd effect of sample aspect ratio
616 // not corresponding exactly with image resolution.
617 else if ( (int)( this_aspect * 1000 ) == (int)( display_ratio * 1000 ) )
619 this->rect.w = this->window_width;
620 this->rect.h = this->window_height;
622 // Use hardware scaler to normalise sample aspect ratio
623 else if ( this->window_height * display_ratio > this->window_width )
625 this->rect.w = this->window_width;
626 this->rect.h = this->window_width / display_ratio;
630 this->rect.w = this->window_height * display_ratio;
631 this->rect.h = this->window_height;
634 this->rect.x = ( this->window_width - this->rect.w ) / 2;
635 this->rect.y = ( this->window_height - this->rect.h ) / 2;
636 this->rect.x -= this->rect.x % 2;
638 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
639 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
640 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
641 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
643 SDL_SetClipRect( this->sdl_screen, &this->rect );
646 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay == NULL )
648 SDL_SetClipRect( this->sdl_screen, &this->rect );
649 this->sdl_overlay = SDL_CreateYUVOverlay( width, height, SDL_YUY2_OVERLAY, this->sdl_screen );
652 if ( this->running && this->sdl_screen != NULL && this->sdl_overlay != NULL )
654 this->buffer = this->sdl_overlay->pixels[ 0 ];
655 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
658 memcpy( this->buffer, image, width * height * 2 );
659 SDL_UnlockYUVOverlay( this->sdl_overlay );
660 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
664 sdl_unlock_display();
665 mlt_cocoa_autorelease_close( pool );
666 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
668 else if ( this->running )
670 vfmt = preview_format == mlt_image_none ? mlt_image_rgb24a : preview_format;
672 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
673 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
679 static void *video_thread( void *arg )
682 consumer_sdl this = arg;
684 // Obtain time of thread start
689 mlt_frame next = NULL;
690 mlt_properties properties = NULL;
693 // Get real time flag
694 int real_time = mlt_properties_get_int( this->properties, "real_time" );
696 // Get the current time
697 gettimeofday( &now, NULL );
699 // Determine start time
700 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
702 while ( this->running )
704 // Pop the next frame
705 pthread_mutex_lock( &this->video_mutex );
706 next = mlt_deque_pop_front( this->queue );
707 while ( next == NULL && this->running )
709 pthread_cond_wait( &this->video_cond, &this->video_mutex );
710 next = mlt_deque_pop_front( this->queue );
712 pthread_mutex_unlock( &this->video_mutex );
714 if ( !this->running || next == NULL ) break;
716 // Get the properties
717 properties = MLT_FRAME_PROPERTIES( next );
719 // Get the speed of the frame
720 speed = mlt_properties_get_double( properties, "_speed" );
722 // Get the current time
723 gettimeofday( &now, NULL );
725 // Get the elapsed time
726 elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
728 // See if we have to delay the display of the current frame
729 if ( mlt_properties_get_int( properties, "rendered" ) == 1 && this->running )
731 // Obtain the scheduled playout time
732 int64_t scheduled = mlt_properties_get_int( properties, "playtime" );
734 // Determine the difference between the elapsed time and the scheduled playout time
735 int64_t difference = scheduled - elapsed;
737 // Smooth playback a bit
738 if ( real_time && ( difference > 20000 && speed == 1.0 ) )
740 tm.tv_sec = difference / 1000000;
741 tm.tv_nsec = ( difference % 1000000 ) * 500;
742 nanosleep( &tm, NULL );
745 // Show current frame if not too old
746 if ( !real_time || ( difference > -10000 || speed != 1.0 || mlt_deque_count( this->queue ) < 2 ) )
747 consumer_play_video( this, next );
749 // If the queue is empty, recalculate start to allow build up again
750 if ( real_time && ( mlt_deque_count( this->queue ) == 0 && speed == 1.0 ) )
752 gettimeofday( &now, NULL );
753 start = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - scheduled + 20000;
758 static int dropped = 0;
759 mlt_log_info( MLT_CONSUMER_SERVICE(&this->parent), "dropped video frame %d\n", ++dropped );
762 // This frame can now be closed
763 mlt_frame_close( next );
768 mlt_frame_close( next );
770 mlt_consumer_stopped( &this->parent );
775 /** Threaded wrapper for pipe.
778 static void *consumer_thread( void *arg )
781 consumer_sdl this = arg;
784 mlt_consumer consumer = &this->parent;
786 // Convenience functionality
787 int terminate_on_pause = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "terminate_on_pause" );
793 // internal intialization
796 mlt_frame frame = NULL;
798 int64_t playtime = 0;
799 struct timespec tm = { 0, 100000 };
801 // Loop until told not to
802 while( this->running )
804 // Get a frame from the attached producer
805 frame = !terminated? mlt_consumer_rt_frame( consumer ) : NULL;
807 // Check for termination
808 if ( terminate_on_pause && frame )
809 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
811 // Ensure that we have a frame
815 init_audio = consumer_play_audio( this, frame, init_audio, &duration );
817 // Determine the start time now
818 if ( this->playing && init_video )
820 // Create the video thread
821 pthread_create( &thread, NULL, video_thread, this );
823 // Video doesn't need to be initialised any more
827 // Set playtime for this frame
828 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "playtime", playtime );
830 while ( this->running && mlt_deque_count( this->queue ) > 15 )
831 nanosleep( &tm, NULL );
833 // Push this frame to the back of the queue
834 pthread_mutex_lock( &this->video_mutex );
835 mlt_deque_push_back( this->queue, frame );
836 pthread_cond_broadcast( &this->video_cond );
837 pthread_mutex_unlock( &this->video_mutex );
839 // Calculate the next playtime
840 playtime += ( duration * 1000 );
842 else if ( terminated )
844 if ( init_video || mlt_deque_count( this->queue ) == 0 )
847 nanosleep( &tm, NULL );
853 // Unblock sdl_preview
854 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "put_mode" ) == 1 )
856 frame = mlt_consumer_get_frame( consumer );
858 mlt_frame_close( frame );
862 // Kill the video thread
863 if ( init_video == 0 )
865 pthread_mutex_lock( &this->video_mutex );
866 pthread_cond_broadcast( &this->video_cond );
867 pthread_mutex_unlock( &this->video_mutex );
868 pthread_join( thread, NULL );
871 while( mlt_deque_count( this->queue ) )
872 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
874 pthread_mutex_lock( &mlt_sdl_mutex );
875 this->sdl_screen = NULL;
876 pthread_mutex_unlock( &mlt_sdl_mutex );
877 this->audio_avail = 0;
882 static int consumer_get_dimensions( int *width, int *height )
886 // SDL windows manager structure
889 // Specify the SDL Version
890 SDL_VERSION( &wm.version );
893 //sdl_lock_display();
896 // Get the wm structure
897 if ( SDL_GetWMInfo( &wm ) == 1 )
900 // Check that we have the X11 wm
901 if ( wm.subsystem == SDL_SYSWM_X11 )
903 // Get the SDL window
904 Window window = wm.info.x11.window;
906 // Get the display session
907 Display *display = wm.info.x11.display;
909 // Get the window attributes
910 XWindowAttributes attr;
911 XGetWindowAttributes( display, window, &attr );
913 // Determine whether window has changed
914 changed = *width != attr.width || *height != attr.height;
916 // Return width and height
918 *height = attr.height;
924 // Unlock the display
925 //sdl_unlock_display();
930 /** Callback to allow override of the close method.
933 static void consumer_close( mlt_consumer parent )
935 // Get the actual object
936 consumer_sdl this = parent->child;
939 ///mlt_consumer_stop( parent );
941 // Now clean up the rest
942 mlt_consumer_close( parent );
945 mlt_deque_close( this->queue );
948 pthread_mutex_destroy( &this->audio_mutex );
949 pthread_cond_destroy( &this->audio_cond );
951 // Finally clean up this