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
)
139 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( parent
);
140 mlt_properties play
= MLT_CONSUMER_PROPERTIES( this->play
);
141 mlt_properties still
= MLT_CONSUMER_PROPERTIES( this->still
);
143 char *window_id
= mlt_properties_get( properties
, "window_id" );
144 char *audio_driver
= mlt_properties_get( properties
, "audio_driver" );
145 int progressive
= mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" );
147 consumer_stop( parent
);
151 this->last_speed
= 1;
153 if ( window_id
!= NULL
)
154 setenv( "SDL_WINDOWID", window_id
, 1 );
156 if ( audio_driver
!= NULL
)
157 setenv( "SDL_AUDIODRIVER", audio_driver
, 1 );
159 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
161 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
165 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
166 SDL_EnableUNICODE( 1 );
168 // Pass properties down
169 mlt_properties_set_data( play
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
170 mlt_properties_set_data( still
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
171 mlt_properties_set_data( play
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
172 mlt_properties_set_data( still
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
174 mlt_properties_set_int( play
, "progressive", progressive
);
175 mlt_properties_set_int( still
, "progressive", progressive
);
177 mlt_properties_pass_list( play
, properties
, "resize,rescale,width,height,aspect_ratio,display_ratio" );
178 mlt_properties_pass_list( still
, properties
, "resize,rescale,width,height,aspect_ratio,display_ratio" );
179 mlt_properties_pass_list( play
, properties
, "deinterlace_method" );
180 mlt_properties_pass_list( still
, properties
, "deinterlace_method" );
181 mlt_properties_pass_list( play
, properties
, "preview_off,preview_format" );
182 mlt_properties_pass_list( still
, properties
, "preview_off,preview_format" );
184 mlt_properties_pass( play
, properties
, "play." );
185 mlt_properties_pass( still
, properties
, "still." );
187 mlt_properties_set_data( play
, "app_lock", mlt_properties_get_data( properties
, "app_lock", NULL
), 0, NULL
, NULL
);
188 mlt_properties_set_data( still
, "app_lock", mlt_properties_get_data( properties
, "app_lock", NULL
), 0, NULL
, NULL
);
189 mlt_properties_set_data( play
, "app_unlock", mlt_properties_get_data( properties
, "app_unlock", NULL
), 0, NULL
, NULL
);
190 mlt_properties_set_data( still
, "app_unlock", mlt_properties_get_data( properties
, "app_unlock", NULL
), 0, NULL
, NULL
);
192 mlt_properties_set_int( play
, "put_mode", 1 );
193 mlt_properties_set_int( still
, "put_mode", 1 );
195 // Start the still producer just to initialise the gui
196 mlt_consumer_start( this->still
);
197 this->active
= this->still
;
199 // Inform child consumers that we control the sdl
200 mlt_properties_set_int( play
, "sdl_started", 1 );
201 mlt_properties_set_int( still
, "sdl_started", 1 );
203 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
209 static int consumer_stop( mlt_consumer parent
)
211 // Get the actual object
212 consumer_sdl
this = parent
->child
;
214 if ( this->joined
== 0 )
216 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( parent
);
217 int app_locked
= mlt_properties_get_int( properties
, "app_locked" );
218 void ( *lock
)( void ) = mlt_properties_get_data( properties
, "app_lock", NULL
);
219 void ( *unlock
)( void ) = mlt_properties_get_data( properties
, "app_unlock", NULL
);
221 if ( app_locked
&& unlock
) unlock( );
223 // Kill the thread and clean up
226 pthread_mutex_lock( &this->refresh_mutex
);
227 pthread_cond_broadcast( &this->refresh_cond
);
228 pthread_mutex_unlock( &this->refresh_mutex
);
230 pthread_join( this->thread
, NULL
);
233 if ( app_locked
&& lock
) lock( );
241 static int consumer_is_stopped( mlt_consumer parent
)
243 consumer_sdl
this = parent
->child
;
244 return !this->running
;
247 static void *consumer_thread( void *arg
)
250 consumer_sdl
this = arg
;
253 mlt_consumer consumer
= &this->parent
;
255 // Get the properties
256 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( consumer
);
258 // internal intialization
260 mlt_frame frame
= NULL
;
261 int last_position
= -1;
263 // Determine if the application is dealing with the preview
264 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
266 this->refresh_count
= 0;
268 // Loop until told not to
269 while( this->running
)
271 // Get a frame from the attached producer
272 frame
= mlt_consumer_get_frame( consumer
);
274 // Ensure that we have a frame
275 if ( this->running
&& frame
!= NULL
)
277 // Get the speed of the frame
278 double speed
= mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame
), "_speed" );
280 // Lock during the operation
281 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer
) );
283 // Get refresh request for the current frame
284 int refresh
= mlt_properties_get_int( properties
, "refresh" );
286 // Decrement refresh and clear changed
287 mlt_events_block( properties
, properties
);
288 mlt_properties_set_int( properties
, "refresh", 0 );
289 mlt_events_unblock( properties
, properties
);
291 // Unlock after the operation
292 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer
) );
294 // Set the changed property on this frame for the benefit of still
295 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "refresh", refresh
);
297 // Make sure the recipient knows that this frame isn't really rendered
298 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 0 );
300 // Optimisation to reduce latency
303 if ( last_position
!= -1 && last_position
+ 1 != mlt_frame_get_position( frame
) )
304 mlt_consumer_purge( this->play
);
305 last_position
= mlt_frame_get_position( frame
);
309 //mlt_consumer_purge( this->play );
313 // If we're not the first frame and both consumers are stopped, then stop ourselves
314 if ( !first
&& mlt_consumer_is_stopped( this->play
) && mlt_consumer_is_stopped( this->still
) )
317 mlt_frame_close( frame
);
319 // Allow a little grace time before switching consumers on speed changes
320 else if ( this->ignore_change
-- > 0 && this->active
!= NULL
&& !mlt_consumer_is_stopped( this->active
) )
322 mlt_consumer_put_frame( this->active
, frame
);
324 // If we aren't playing normally, then use the still
325 else if ( speed
!= 1 )
327 if ( !mlt_consumer_is_stopped( this->play
) )
328 mlt_consumer_stop( this->play
);
329 if ( mlt_consumer_is_stopped( this->still
) )
331 this->last_speed
= speed
;
332 this->active
= this->still
;
333 this->ignore_change
= 0;
334 mlt_consumer_start( this->still
);
336 mlt_consumer_put_frame( this->still
, frame
);
338 // Otherwise use the normal player
341 if ( !mlt_consumer_is_stopped( this->still
) )
342 mlt_consumer_stop( this->still
);
343 if ( mlt_consumer_is_stopped( this->play
) )
345 this->last_speed
= speed
;
346 this->active
= this->play
;
347 this->ignore_change
= 25;
348 mlt_consumer_start( this->play
);
350 mlt_consumer_put_frame( this->play
, frame
);
353 // Copy the rectangle info from the active consumer
354 if ( this->running
&& preview_off
== 0 )
356 mlt_properties active
= MLT_CONSUMER_PROPERTIES( this->active
);
357 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer
) );
358 mlt_properties_set_int( properties
, "rect_x", mlt_properties_get_int( active
, "rect_x" ) );
359 mlt_properties_set_int( properties
, "rect_y", mlt_properties_get_int( active
, "rect_y" ) );
360 mlt_properties_set_int( properties
, "rect_w", mlt_properties_get_int( active
, "rect_w" ) );
361 mlt_properties_set_int( properties
, "rect_h", mlt_properties_get_int( active
, "rect_h" ) );
362 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer
) );
365 if ( this->active
== this->still
)
367 pthread_mutex_lock( &this->refresh_mutex
);
368 if ( this->running
&& speed
== 0 && this->refresh_count
<= 0 )
369 pthread_cond_wait( &this->refresh_cond
, &this->refresh_mutex
);
370 this->refresh_count
--;
371 pthread_mutex_unlock( &this->refresh_mutex
);
374 // We are definitely not waiting on the first frame any more
379 if ( frame
) mlt_frame_close( frame
);
380 mlt_consumer_put_frame( this->active
, NULL
);
385 if ( this->play
) mlt_consumer_stop( this->play
);
386 if ( this->still
) mlt_consumer_stop( this->still
);
391 /** Callback to allow override of the close method.
394 static void consumer_close( mlt_consumer parent
)
396 // Get the actual object
397 consumer_sdl
this = parent
->child
;
400 mlt_consumer_stop( parent
);
402 // Close the child consumers
403 mlt_consumer_close( this->play
);
404 mlt_consumer_close( this->still
);
406 // Now clean up the rest
407 mlt_consumer_close( parent
);
409 // Finally clean up this