2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Charles Yates
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "consumer_sdl.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 typedef struct consumer_sdl_s *consumer_sdl;
35 struct mlt_consumer_s parent;
46 pthread_cond_t refresh_cond;
47 pthread_mutex_t refresh_mutex;
51 /** Forward references to static functions.
54 static int consumer_start( mlt_consumer parent );
55 static int consumer_stop( mlt_consumer parent );
56 static int consumer_is_stopped( mlt_consumer parent );
57 static void consumer_close( mlt_consumer parent );
58 static void *consumer_thread( void * );
59 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
60 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer this, SDL_Event *event );
61 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer this, char *name );
63 mlt_consumer consumer_sdl_preview_init( char *arg )
65 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
66 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
68 // Get the parent consumer object
69 mlt_consumer parent = &this->parent;
72 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
74 // Get the width and height
75 int width = mlt_properties_get_int( properties, "width" );
76 int height = mlt_properties_get_int( properties, "height" );
78 // Process actual param
79 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
81 mlt_properties_set_int( properties, "width", width );
82 mlt_properties_set_int( properties, "height", height );
85 // Create child consumers
86 this->play = mlt_factory_consumer( "sdl", arg );
87 this->still = mlt_factory_consumer( "sdl_still", arg );
88 mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "real_time", "0" );
89 mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "rescale", "nearest" );
90 parent->close = consumer_close;
91 parent->start = consumer_start;
92 parent->stop = consumer_stop;
93 parent->is_stopped = consumer_is_stopped;
95 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
96 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
97 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
98 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
99 pthread_cond_init( &this->refresh_cond, NULL );
100 pthread_mutex_init( &this->refresh_mutex, NULL );
101 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
108 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
110 consumer_sdl this = parent->child;
111 this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
112 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
115 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
117 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
120 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
122 if ( !strcmp( name, "refresh" ) )
124 consumer_sdl this = parent->child;
125 pthread_mutex_lock( &this->refresh_mutex );
126 this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
127 pthread_cond_broadcast( &this->refresh_cond );
128 pthread_mutex_unlock( &this->refresh_mutex );
132 static int consumer_start( mlt_consumer parent )
134 consumer_sdl this = parent->child;
136 if ( !this->running )
138 pthread_attr_t thread_attributes;
140 consumer_stop( parent );
144 this->last_speed = 1;
146 // Inherit the scheduling priority
147 pthread_attr_init( &thread_attributes );
148 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
150 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
156 static int consumer_stop( mlt_consumer parent )
158 // Get the actual object
159 consumer_sdl this = parent->child;
161 if ( this->joined == 0 )
163 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
164 int app_locked = mlt_properties_get_int( properties, "app_locked" );
165 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
166 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
168 if ( app_locked && unlock ) unlock( );
170 // Kill the thread and clean up
173 pthread_mutex_lock( &this->refresh_mutex );
174 pthread_cond_broadcast( &this->refresh_cond );
175 pthread_mutex_unlock( &this->refresh_mutex );
177 if ( this->play ) mlt_consumer_stop( this->play );
178 if ( this->still ) mlt_consumer_stop( this->still );
180 pthread_join( this->thread, NULL );
183 if ( app_locked && lock ) lock( );
189 static int consumer_is_stopped( mlt_consumer parent )
191 consumer_sdl this = parent->child;
192 return !this->running;
195 static void *consumer_thread( void *arg )
198 consumer_sdl this = arg;
201 mlt_consumer consumer = &this->parent;
203 // internal intialization
205 mlt_frame frame = NULL;
206 int last_position = -1;
209 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
210 mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
211 mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
213 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
215 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
219 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
220 SDL_EnableUNICODE( 1 );
222 // Inform child consumers that we control the sdl
223 mlt_properties_set_int( play, "sdl_started", 1 );
224 mlt_properties_set_int( still, "sdl_started", 1 );
226 // Pass properties down
227 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
228 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
229 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
230 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
231 mlt_properties_set_int( play, "resize", mlt_properties_get_int( properties, "resize" ) );
232 mlt_properties_set_int( still, "resize", mlt_properties_get_int( properties, "resize" ) );
233 mlt_properties_set( play, "rescale", mlt_properties_get( properties, "rescale" ) );
234 mlt_properties_set( still, "rescale", mlt_properties_get( properties, "rescale" ) );
235 mlt_properties_set_int( play, "width", mlt_properties_get_int( properties, "width" ) );
236 mlt_properties_set_int( still, "width", mlt_properties_get_int( properties, "width" ) );
237 mlt_properties_set_int( play, "height", mlt_properties_get_int( properties, "height" ) );
238 mlt_properties_set_int( still, "height", mlt_properties_get_int( properties, "height" ) );
240 mlt_properties_set_int( play, "progressive", 1 );
241 mlt_properties_set_int( still, "progressive", 1 );
243 mlt_properties_pass( play, MLT_CONSUMER_PROPERTIES( consumer ), "play." );
244 mlt_properties_pass( still, MLT_CONSUMER_PROPERTIES( consumer ), "still." );
246 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
247 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
248 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
249 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
251 mlt_properties_set_int( play, "put_mode", 1 );
252 mlt_properties_set_int( still, "put_mode", 1 );
254 // Loop until told not to
255 while( this->running )
257 // Get a frame from the attached producer
258 frame = mlt_consumer_get_frame( consumer );
260 // Ensure that we have a frame
263 // Get the speed of the frame
264 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
266 // Determine which speed to use
267 double use_speed = speed;
269 // Lock during the operation
270 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
272 // Get refresh request for the current frame
273 int refresh = mlt_properties_get_int( properties, "refresh" );
275 // Decrement refresh and clear changed
276 mlt_events_block( properties, properties );
277 mlt_properties_set_int( properties, "refresh", 0 );
278 mlt_events_unblock( properties, properties );
280 // Unlock after the operation
281 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
283 // Set the changed property on this frame for the benefit of still
284 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
286 // Make sure the recipient knows that this frame isn't really rendered
287 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
289 // Optimisation to reduce latency
292 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
293 mlt_consumer_purge( this->play );
294 last_position = mlt_frame_get_position( frame );
298 //mlt_consumer_purge( this->play );
302 // If we're not the first frame and both consumers are stopped, then stop ourselves
303 if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
306 mlt_frame_close( frame );
308 // Allow a little grace time before switching consumers on speed changes
309 else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
311 mlt_consumer_put_frame( this->active, frame );
313 // If we aren't playing normally, then use the still
314 else if ( use_speed != 1 )
316 if ( !mlt_consumer_is_stopped( this->play ) )
317 mlt_consumer_stop( this->play );
318 if ( mlt_consumer_is_stopped( this->still ) )
320 this->last_speed = use_speed;
321 this->active = this->still;
322 this->ignore_change = 0;
323 mlt_consumer_start( this->still );
325 mlt_consumer_put_frame( this->still, frame );
327 // Otherwise use the normal player
330 if ( !mlt_consumer_is_stopped( this->still ) )
331 mlt_consumer_stop( this->still );
332 if ( mlt_consumer_is_stopped( this->play ) )
334 this->last_speed = use_speed;
335 this->active = this->play;
336 this->ignore_change = 25;
337 mlt_consumer_start( this->play );
339 mlt_consumer_put_frame( this->play, frame );
342 // Copy the rectangle info from the active consumer
345 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
346 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
347 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
348 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
349 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
350 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
351 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
354 if ( this->active == this->still )
356 pthread_mutex_lock( &this->refresh_mutex );
357 if ( speed == 0 && this->refresh_count <= 0 )
358 pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
359 this->refresh_count --;
360 pthread_mutex_unlock( &this->refresh_mutex );
363 // We are definitely not waiting on the first frame any more
372 //mlt_consumer_stop( this->play );
373 //mlt_consumer_stop( this->still );
380 /** Callback to allow override of the close method.
383 static void consumer_close( mlt_consumer parent )
385 // Get the actual object
386 consumer_sdl this = parent->child;
389 mlt_consumer_stop( parent );
391 // Close the child consumers
392 mlt_consumer_close( this->play );
393 mlt_consumer_close( this->still );
395 // Now clean up the rest
396 mlt_consumer_close( parent );
398 // Finally clean up this