2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005 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>
29 #include <SDL/SDL_syswm.h>
31 extern pthread_mutex_t mlt_sdl_mutex;
33 typedef struct consumer_sdl_s *consumer_sdl;
37 struct mlt_consumer_s parent;
48 pthread_cond_t refresh_cond;
49 pthread_mutex_t refresh_mutex;
53 /** Forward references to static functions.
56 static int consumer_start( mlt_consumer parent );
57 static int consumer_stop( mlt_consumer parent );
58 static int consumer_is_stopped( mlt_consumer parent );
59 static void consumer_close( mlt_consumer parent );
60 static void *consumer_thread( void * );
61 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
62 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer this, SDL_Event *event );
63 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer this, char *name );
65 mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
67 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
68 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
70 // Get the parent consumer object
71 mlt_consumer parent = &this->parent;
74 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
76 // Get the width and height
77 int width = mlt_properties_get_int( properties, "width" );
78 int height = mlt_properties_get_int( properties, "height" );
80 // Process actual param
81 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
83 mlt_properties_set_int( properties, "width", width );
84 mlt_properties_set_int( properties, "height", height );
87 // Create child consumers
88 this->play = mlt_factory_consumer( profile, "sdl", arg );
89 this->still = mlt_factory_consumer( profile, "sdl_still", arg );
90 mlt_properties_set( properties, "real_time", "0" );
91 mlt_properties_set( properties, "rescale", "nearest" );
92 mlt_properties_set( properties, "deinterlace_method", "onefield" );
93 parent->close = consumer_close;
94 parent->start = consumer_start;
95 parent->stop = consumer_stop;
96 parent->is_stopped = consumer_is_stopped;
98 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
99 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
100 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
101 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
102 pthread_cond_init( &this->refresh_cond, NULL );
103 pthread_mutex_init( &this->refresh_mutex, NULL );
104 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
111 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
113 consumer_sdl this = parent->child;
114 this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
115 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
118 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
120 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
123 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
125 if ( !strcmp( name, "refresh" ) )
127 consumer_sdl this = parent->child;
128 pthread_mutex_lock( &this->refresh_mutex );
129 this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
130 pthread_cond_broadcast( &this->refresh_cond );
131 pthread_mutex_unlock( &this->refresh_mutex );
135 static int consumer_start( mlt_consumer parent )
137 consumer_sdl this = parent->child;
139 if ( !this->running )
142 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
143 mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
144 mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
146 char *window_id = mlt_properties_get( properties, "window_id" );
147 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
148 char *video_driver = mlt_properties_get( properties, "video_driver" );
149 char *audio_device = mlt_properties_get( properties, "audio_device" );
150 char *output_display = mlt_properties_get( properties, "output_display" );
151 int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
153 consumer_stop( parent );
157 this->last_speed = 1;
159 if ( output_display != NULL )
160 setenv( "DISPLAY", output_display, 1 );
162 if ( window_id != NULL )
163 setenv( "SDL_WINDOWID", window_id, 1 );
165 if ( video_driver != NULL )
166 setenv( "SDL_VIDEODRIVER", video_driver, 1 );
168 if ( audio_driver != NULL )
169 setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
171 if ( audio_device != NULL )
172 setenv( "AUDIODEV", audio_device, 1 );
174 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
176 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
180 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
181 SDL_EnableUNICODE( 1 );
183 // Pass properties down
184 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
185 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
186 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
187 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
189 mlt_properties_set_int( play, "progressive", progressive );
190 mlt_properties_set_int( still, "progressive", progressive );
192 mlt_properties_pass_list( play, properties, "resize,rescale,width,height,aspect_ratio,display_ratio,volume" );
193 mlt_properties_pass_list( still, properties, "resize,rescale,width,height,aspect_ratio,display_ratio" );
194 mlt_properties_pass_list( play, properties, "deinterlace_method" );
195 mlt_properties_pass_list( still, properties, "deinterlace_method" );
196 mlt_properties_pass_list( play, properties, "preview_off,preview_format,window_background" );
197 mlt_properties_pass_list( still, properties, "preview_off,preview_format,window_background" );
199 mlt_properties_pass( play, properties, "play." );
200 mlt_properties_pass( still, properties, "still." );
202 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
203 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
204 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
205 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
207 mlt_properties_set_int( play, "put_mode", 1 );
208 mlt_properties_set_int( still, "put_mode", 1 );
210 // Start the still producer just to initialise the gui
211 mlt_consumer_start( this->still );
212 this->active = this->still;
214 // Inform child consumers that we control the sdl
215 mlt_properties_set_int( play, "sdl_started", 1 );
216 mlt_properties_set_int( still, "sdl_started", 1 );
218 pthread_create( &this->thread, NULL, consumer_thread, this );
224 static int consumer_stop( mlt_consumer parent )
226 // Get the actual object
227 consumer_sdl this = parent->child;
229 if ( this->joined == 0 )
231 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
232 int app_locked = mlt_properties_get_int( properties, "app_locked" );
233 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
234 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
236 if ( app_locked && unlock ) unlock( );
238 // Kill the thread and clean up
241 pthread_mutex_lock( &this->refresh_mutex );
242 pthread_cond_broadcast( &this->refresh_cond );
243 pthread_mutex_unlock( &this->refresh_mutex );
245 pthread_join( this->thread, NULL );
248 if ( app_locked && lock ) lock( );
250 pthread_mutex_lock( &mlt_sdl_mutex );
252 pthread_mutex_unlock( &mlt_sdl_mutex );
258 static int consumer_is_stopped( mlt_consumer parent )
260 consumer_sdl this = parent->child;
261 return !this->running;
264 static void *consumer_thread( void *arg )
267 consumer_sdl this = arg;
270 mlt_consumer consumer = &this->parent;
272 // Get the properties
273 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
275 // internal intialization
277 mlt_frame frame = NULL;
278 int last_position = -1;
280 // Determine if the application is dealing with the preview
281 int preview_off = mlt_properties_get_int( properties, "preview_off" );
283 this->refresh_count = 0;
285 // Loop until told not to
286 while( this->running )
288 // Get a frame from the attached producer
289 frame = mlt_consumer_get_frame( consumer );
291 // Ensure that we have a frame
292 if ( this->running && frame != NULL )
294 // Get the speed of the frame
295 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
297 // Lock during the operation
298 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
300 // Get refresh request for the current frame
301 int refresh = mlt_properties_get_int( properties, "refresh" );
303 // Decrement refresh and clear changed
304 mlt_events_block( properties, properties );
305 mlt_properties_set_int( properties, "refresh", 0 );
306 mlt_events_unblock( properties, properties );
308 // Unlock after the operation
309 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
311 // Set the changed property on this frame for the benefit of still
312 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
314 // Make sure the recipient knows that this frame isn't really rendered
315 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
317 // Optimisation to reduce latency
320 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
321 mlt_consumer_purge( this->play );
322 last_position = mlt_frame_get_position( frame );
326 //mlt_consumer_purge( this->play );
330 // If we're not the first frame and both consumers are stopped, then stop ourselves
331 if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
334 mlt_frame_close( frame );
336 // Allow a little grace time before switching consumers on speed changes
337 else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
339 mlt_consumer_put_frame( this->active, frame );
341 // If we aren't playing normally, then use the still
342 else if ( speed != 1 )
344 if ( !mlt_consumer_is_stopped( this->play ) )
345 mlt_consumer_stop( this->play );
346 if ( mlt_consumer_is_stopped( this->still ) )
348 this->last_speed = speed;
349 this->active = this->still;
350 this->ignore_change = 0;
351 mlt_consumer_start( this->still );
353 mlt_consumer_put_frame( this->still, frame );
355 // Otherwise use the normal player
358 if ( !mlt_consumer_is_stopped( this->still ) )
359 mlt_consumer_stop( this->still );
360 if ( mlt_consumer_is_stopped( this->play ) )
362 this->last_speed = speed;
363 this->active = this->play;
364 this->ignore_change = 25;
365 mlt_consumer_start( this->play );
367 mlt_consumer_put_frame( this->play, frame );
370 // Copy the rectangle info from the active consumer
371 if ( this->running && preview_off == 0 )
373 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
374 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
375 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
376 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
377 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
378 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
379 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
382 if ( this->active == this->still )
384 pthread_mutex_lock( &this->refresh_mutex );
385 if ( this->running && speed == 0 && this->refresh_count <= 0 )
386 pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
387 this->refresh_count --;
388 pthread_mutex_unlock( &this->refresh_mutex );
391 // We are definitely not waiting on the first frame any more
396 if ( frame ) mlt_frame_close( frame );
397 mlt_consumer_put_frame( this->active, NULL );
402 if ( this->play ) mlt_consumer_stop( this->play );
403 if ( this->still ) mlt_consumer_stop( this->still );
408 /** Callback to allow override of the close method.
411 static void consumer_close( mlt_consumer parent )
413 // Get the actual object
414 consumer_sdl this = parent->child;
417 mlt_consumer_stop( parent );
419 // Close the child consumers
420 mlt_consumer_close( this->play );
421 mlt_consumer_close( this->still );
423 // Now clean up the rest
424 mlt_consumer_close( parent );
426 // Finally clean up this