2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005, 2010 Ushodaya Enterprises Limited
4 * Author: Charles Yates
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_factory.h>
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_log.h>
30 #include <SDL_syswm.h>
33 extern pthread_mutex_t mlt_sdl_mutex;
35 typedef struct consumer_sdl_s *consumer_sdl;
39 struct mlt_consumer_s parent;
49 mlt_position last_position;
51 pthread_cond_t refresh_cond;
52 pthread_mutex_t refresh_mutex;
56 /** Forward references to static functions.
59 static int consumer_start( mlt_consumer parent );
60 static int consumer_stop( mlt_consumer parent );
61 static int consumer_is_stopped( mlt_consumer parent );
62 static void consumer_close( mlt_consumer parent );
63 static void *consumer_thread( void * );
64 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer self, mlt_frame frame );
65 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer self, SDL_Event *event );
66 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer self, char *name );
68 mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
70 consumer_sdl self = calloc( 1, sizeof( struct consumer_sdl_s ) );
71 if ( self != NULL && mlt_consumer_init( &self->parent, self, profile ) == 0 )
73 // Get the parent consumer object
74 mlt_consumer parent = &self->parent;
77 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
79 // Get the width and height
80 int width = mlt_properties_get_int( properties, "width" );
81 int height = mlt_properties_get_int( properties, "height" );
83 // Process actual param
84 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
86 mlt_properties_set_int( properties, "width", width );
87 mlt_properties_set_int( properties, "height", height );
90 // Create child consumers
91 self->play = mlt_factory_consumer( profile, "sdl", arg );
92 self->still = mlt_factory_consumer( profile, "sdl_still", arg );
93 mlt_properties_set( properties, "rescale", "nearest" );
94 mlt_properties_set( properties, "deinterlace_method", "onefield" );
95 mlt_properties_set_int( properties, "prefill", 1 );
96 mlt_properties_set_int( properties, "top_field_first", -1 );
98 parent->close = consumer_close;
99 parent->start = consumer_start;
100 parent->stop = consumer_stop;
101 parent->is_stopped = consumer_is_stopped;
103 mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->play ), self, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
104 mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->still ), self, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
105 mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->play ), self, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
106 mlt_events_listen( MLT_CONSUMER_PROPERTIES( self->still ), self, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
107 pthread_cond_init( &self->refresh_cond, NULL );
108 pthread_mutex_init( &self->refresh_mutex, NULL );
109 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), self, "property-changed", ( mlt_listener )consumer_refresh_cb );
110 mlt_events_register( properties, "consumer-sdl-paused", NULL );
117 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
119 consumer_sdl self = parent->child;
120 self->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
121 self->last_position = mlt_frame_get_position( frame );
122 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
125 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
127 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
130 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
132 if ( !strcmp( name, "refresh" ) )
134 consumer_sdl self = parent->child;
135 pthread_mutex_lock( &self->refresh_mutex );
136 self->refresh_count = self->refresh_count <= 0 ? 1 : self->refresh_count + 1;
137 pthread_cond_broadcast( &self->refresh_cond );
138 pthread_mutex_unlock( &self->refresh_mutex );
142 static int consumer_start( mlt_consumer parent )
144 consumer_sdl self = parent->child;
146 if ( !self->running )
149 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
150 mlt_properties play = MLT_CONSUMER_PROPERTIES( self->play );
151 mlt_properties still = MLT_CONSUMER_PROPERTIES( self->still );
153 char *window_id = mlt_properties_get( properties, "window_id" );
154 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
155 char *video_driver = mlt_properties_get( properties, "video_driver" );
156 char *audio_device = mlt_properties_get( properties, "audio_device" );
157 char *output_display = mlt_properties_get( properties, "output_display" );
158 int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
160 consumer_stop( parent );
164 self->last_speed = 1;
166 if ( output_display != NULL )
167 setenv( "DISPLAY", output_display, 1 );
169 if ( window_id != NULL )
170 setenv( "SDL_WINDOWID", window_id, 1 );
172 if ( video_driver != NULL )
173 setenv( "SDL_VIDEODRIVER", video_driver, 1 );
175 if ( audio_driver != NULL )
176 setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
178 if ( audio_device != NULL )
179 setenv( "AUDIODEV", audio_device, 1 );
181 pthread_mutex_lock( &mlt_sdl_mutex );
182 int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
183 pthread_mutex_unlock( &mlt_sdl_mutex );
186 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
190 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
191 SDL_EnableUNICODE( 1 );
193 // Pass properties down
194 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
195 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
196 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
197 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
199 mlt_properties_set_int( play, "progressive", progressive );
200 mlt_properties_set_int( still, "progressive", progressive );
202 mlt_properties_pass_list( play, properties,
203 "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
204 ",top_field_first,volume,real_time,buffer,prefill,audio_off,frequency,drop_max" );
205 mlt_properties_pass_list( still, properties,
206 "deinterlace_method,resize,rescale,width,height,aspect_ratio,display_ratio,preview_off,preview_format,window_background"
209 mlt_properties_pass( play, properties, "play." );
210 mlt_properties_pass( still, properties, "still." );
212 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
213 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
214 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
215 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
217 mlt_properties_set_int( play, "put_mode", 1 );
218 mlt_properties_set_int( still, "put_mode", 1 );
219 mlt_properties_set_int( play, "terminate_on_pause", 1 );
221 // Start the still producer just to initialise the gui
222 mlt_consumer_start( self->still );
223 self->active = self->still;
225 // Inform child consumers that we control the sdl
226 mlt_properties_set_int( play, "sdl_started", 1 );
227 mlt_properties_set_int( still, "sdl_started", 1 );
229 pthread_create( &self->thread, NULL, consumer_thread, self );
235 static int consumer_stop( mlt_consumer parent )
237 // Get the actual object
238 consumer_sdl self = parent->child;
240 if ( self->joined == 0 )
242 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
243 int app_locked = mlt_properties_get_int( properties, "app_locked" );
244 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
245 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
247 if ( app_locked && unlock ) unlock( );
249 // Kill the thread and clean up
252 pthread_mutex_lock( &self->refresh_mutex );
253 pthread_cond_broadcast( &self->refresh_cond );
254 pthread_mutex_unlock( &self->refresh_mutex );
258 pthread_join( self->thread, NULL );
261 if ( app_locked && lock ) lock( );
263 pthread_mutex_lock( &mlt_sdl_mutex );
265 pthread_mutex_unlock( &mlt_sdl_mutex );
271 static int consumer_is_stopped( mlt_consumer parent )
273 consumer_sdl self = parent->child;
274 return !self->running;
277 static void *consumer_thread( void *arg )
280 consumer_sdl self = arg;
283 mlt_consumer consumer = &self->parent;
285 // Get the properties
286 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
288 // internal intialization
289 mlt_frame frame = NULL;
290 int last_position = -1;
292 int eos_threshold = 20;
294 eos_threshold = eos_threshold + mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self->play ), "buffer" );
296 // Determine if the application is dealing with the preview
297 int preview_off = mlt_properties_get_int( properties, "preview_off" );
299 pthread_mutex_lock( &self->refresh_mutex );
300 self->refresh_count = 0;
301 pthread_mutex_unlock( &self->refresh_mutex );
303 // Loop until told not to
304 while( self->running )
306 // Get a frame from the attached producer
307 frame = mlt_consumer_get_frame( consumer );
309 // Ensure that we have a frame
310 if ( self->running && frame != NULL )
312 // Get the speed of the frame
313 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
315 // Lock during the operation
316 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
318 // Get refresh request for the current frame
319 int refresh = mlt_properties_get_int( properties, "refresh" );
321 // Decrement refresh and clear changed
322 mlt_events_block( properties, properties );
323 mlt_properties_set_int( properties, "refresh", 0 );
324 mlt_events_unblock( properties, properties );
326 // Unlock after the operation
327 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
329 // Set the changed property on this frame for the benefit of still
330 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
332 // Make sure the recipient knows that this frame isn't really rendered
333 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
335 // Optimisation to reduce latency
338 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
339 mlt_consumer_purge( self->play );
340 last_position = mlt_frame_get_position( frame );
344 //mlt_consumer_purge( self->play );
348 // If we aren't playing normally, then use the still
351 mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
352 mlt_position duration = producer? mlt_producer_get_playtime( producer ) : -1;
355 #ifndef SKIP_WAIT_EOS
356 if ( self->active == self->play )
358 // Do not interrupt the play consumer near the end
359 if ( duration - self->last_position > eos_threshold )
361 // Get a new frame at the sought position
362 mlt_frame_close( frame );
364 mlt_producer_seek( producer, self->last_position );
365 frame = mlt_consumer_get_frame( consumer );
370 // Send frame with speed 0 to stop it
371 if ( frame && !mlt_consumer_is_stopped( self->play ) )
373 mlt_consumer_put_frame( self->play, frame );
378 // Check for end of stream
379 if ( mlt_consumer_is_stopped( self->play ) )
382 mlt_log_verbose( MLT_CONSUMER_SERVICE( consumer ), "END OF STREAM\n" );
384 eos = 0; // reset eos indicator
388 // Prevent a tight busy loop
389 struct timespec tm = { 0, 100000L }; // 100 usec
390 nanosleep( &tm, NULL );
395 pause = self->active == self->play;
399 // Start the still consumer
400 mlt_consumer_stop( self->play );
401 self->last_speed = speed;
402 self->active = self->still;
403 self->ignore_change = 0;
404 mlt_consumer_start( self->still );
406 // Send the frame to the active child
409 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", 1 );
410 mlt_consumer_put_frame( self->active, frame );
412 if ( pause && speed == 0.0 )
414 mlt_events_fire( properties, "consumer-sdl-paused", NULL );
417 // Allow a little grace time before switching consumers on speed changes
418 else if ( self->ignore_change -- > 0 && self->active != NULL && !mlt_consumer_is_stopped( self->active ) )
420 mlt_consumer_put_frame( self->active, frame );
422 // Otherwise use the normal player
425 if ( !mlt_consumer_is_stopped( self->still ) )
426 mlt_consumer_stop( self->still );
427 if ( mlt_consumer_is_stopped( self->play ) )
429 self->last_speed = speed;
430 self->active = self->play;
431 self->ignore_change = 0;
432 mlt_consumer_start( self->play );
435 mlt_consumer_put_frame( self->play, frame );
438 // Copy the rectangle info from the active consumer
439 if ( self->running && preview_off == 0 )
441 mlt_properties active = MLT_CONSUMER_PROPERTIES( self->active );
442 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
443 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
444 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
445 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
446 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
447 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
450 if ( self->active == self->still )
452 pthread_mutex_lock( &self->refresh_mutex );
453 if ( self->running && speed == 0 && self->refresh_count <= 0 )
455 mlt_events_fire( properties, "consumer-sdl-paused", NULL );
456 pthread_cond_wait( &self->refresh_cond, &self->refresh_mutex );
458 self->refresh_count --;
459 pthread_mutex_unlock( &self->refresh_mutex );
464 if ( frame ) mlt_frame_close( frame );
465 mlt_consumer_put_frame( self->active, NULL );
470 if ( self->play ) mlt_consumer_stop( self->play );
471 if ( self->still ) mlt_consumer_stop( self->still );
476 /** Callback to allow override of the close method.
479 static void consumer_close( mlt_consumer parent )
481 // Get the actual object
482 consumer_sdl self = parent->child;
485 mlt_consumer_stop( parent );
487 // Close the child consumers
488 mlt_consumer_close( self->play );
489 mlt_consumer_close( self->still );
491 // Now clean up the rest
492 mlt_consumer_close( parent );
494 // Finally clean up self