Consumer reworked
[melted] / src / modules / sdl / consumer_sdl_preview.c
1 /*
2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Charles Yates
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_producer.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30
31 typedef struct consumer_sdl_s *consumer_sdl;
32
33 struct consumer_sdl_s
34 {
35 struct mlt_consumer_s parent;
36 mlt_consumer active;
37 int ignore_change;
38 mlt_consumer play;
39 mlt_consumer still;
40 pthread_t thread;
41 int joined;
42 int running;
43 int sdl_flags;
44 double last_speed;
45
46 pthread_cond_t refresh_cond;
47 pthread_mutex_t refresh_mutex;
48 };
49
50 /** Forward references to static functions.
51 */
52
53 static int consumer_start( mlt_consumer parent );
54 static int consumer_stop( mlt_consumer parent );
55 static int consumer_is_stopped( mlt_consumer parent );
56 static void consumer_close( mlt_consumer parent );
57 static void *consumer_thread( void * );
58 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
59 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer this, SDL_Event *event );
60 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer this, char *name );
61
62 mlt_consumer consumer_sdl_preview_init( char *arg )
63 {
64 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
65 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
66 {
67 // Get the parent consumer object
68 mlt_consumer parent = &this->parent;
69
70 // Get the properties
71 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
72
73 // Get the width and height
74 int width = mlt_properties_get_int( properties, "width" );
75 int height = mlt_properties_get_int( properties, "height" );
76
77 // Process actual param
78 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
79 {
80 mlt_properties_set_int( properties, "width", width );
81 mlt_properties_set_int( properties, "height", height );
82 }
83
84 // Create child consumers
85 this->play = mlt_factory_consumer( "sdl", arg );
86 this->still = mlt_factory_consumer( "sdl_still", arg );
87 mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "real_time", "0" );
88 mlt_properties_set( MLT_CONSUMER_PROPERTIES( parent ), "rescale", "nearest" );
89 parent->close = consumer_close;
90 parent->start = consumer_start;
91 parent->stop = consumer_stop;
92 parent->is_stopped = consumer_is_stopped;
93 this->joined = 1;
94 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
95 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
96 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
97 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
98 pthread_cond_init( &this->refresh_cond, NULL );
99 pthread_mutex_init( &this->refresh_mutex, NULL );
100 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
101 return parent;
102 }
103 free( this );
104 return NULL;
105 }
106
107 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
108 {
109 consumer_sdl this = parent->child;
110 this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
111 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
112 }
113
114 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
115 {
116 mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
117 }
118
119 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
120 {
121 if ( !strcmp( name, "refresh" ) )
122 {
123 consumer_sdl this = parent->child;
124 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "refresh" ) )
125 {
126 pthread_mutex_lock( &this->refresh_mutex );
127 pthread_cond_broadcast( &this->refresh_cond );
128 pthread_mutex_unlock( &this->refresh_mutex );
129 }
130 }
131 }
132
133 static int consumer_start( mlt_consumer parent )
134 {
135 consumer_sdl this = parent->child;
136
137 if ( !this->running )
138 {
139 pthread_attr_t thread_attributes;
140
141 consumer_stop( parent );
142
143 this->running = 1;
144 this->joined = 0;
145 this->last_speed = 1;
146
147 // Inherit the scheduling priority
148 pthread_attr_init( &thread_attributes );
149 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
150
151 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
152 }
153
154 return 0;
155 }
156
157 static int consumer_stop( mlt_consumer parent )
158 {
159 // Get the actual object
160 consumer_sdl this = parent->child;
161
162 if ( this->joined == 0 )
163 {
164 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
165 int app_locked = mlt_properties_get_int( properties, "app_locked" );
166 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
167 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
168
169 if ( app_locked && unlock ) unlock( );
170
171 // Kill the thread and clean up
172 this->running = 0;
173
174 pthread_mutex_lock( &this->refresh_mutex );
175 pthread_cond_broadcast( &this->refresh_cond );
176 pthread_mutex_unlock( &this->refresh_mutex );
177
178 if ( this->play ) mlt_consumer_stop( this->play );
179 if ( this->still ) mlt_consumer_stop( this->still );
180
181 pthread_join( this->thread, NULL );
182 this->joined = 1;
183
184 if ( app_locked && lock ) lock( );
185 }
186
187 return 0;
188 }
189
190 static int consumer_is_stopped( mlt_consumer parent )
191 {
192 consumer_sdl this = parent->child;
193 return !this->running;
194 }
195
196 static void *consumer_thread( void *arg )
197 {
198 // Identify the arg
199 consumer_sdl this = arg;
200
201 // Get the consumer
202 mlt_consumer consumer = &this->parent;
203
204 // internal intialization
205 int first = 1;
206 mlt_frame frame = NULL;
207 int last_position = -1;
208
209 // properties
210 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
211 mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
212 mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
213
214 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
215 {
216 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
217 return NULL;
218 }
219
220 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
221 SDL_EnableUNICODE( 1 );
222
223 // Inform child consumers that we control the sdl
224 mlt_properties_set_int( play, "sdl_started", 1 );
225 mlt_properties_set_int( still, "sdl_started", 1 );
226
227 // Pass properties down
228 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
229 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
230 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
231 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
232 mlt_properties_set_int( play, "resize", mlt_properties_get_int( properties, "resize" ) );
233 mlt_properties_set_int( still, "resize", mlt_properties_get_int( properties, "resize" ) );
234 mlt_properties_set( play, "rescale", mlt_properties_get( properties, "rescale" ) );
235 mlt_properties_set( still, "rescale", mlt_properties_get( properties, "rescale" ) );
236 mlt_properties_set_int( play, "width", mlt_properties_get_int( properties, "width" ) );
237 mlt_properties_set_int( still, "width", mlt_properties_get_int( properties, "width" ) );
238 mlt_properties_set_int( play, "height", mlt_properties_get_int( properties, "height" ) );
239 mlt_properties_set_int( still, "height", mlt_properties_get_int( properties, "height" ) );
240
241 mlt_properties_set_int( play, "progressive", 1 );
242 mlt_properties_set_int( still, "progressive", 1 );
243
244 mlt_properties_pass( play, MLT_CONSUMER_PROPERTIES( consumer ), "play." );
245 mlt_properties_pass( still, MLT_CONSUMER_PROPERTIES( consumer ), "still." );
246
247 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
248 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
249 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
250 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
251
252 mlt_properties_set_int( play, "put_mode", 1 );
253 mlt_properties_set_int( still, "put_mode", 1 );
254
255 // Loop until told not to
256 while( this->running )
257 {
258 // Get a frame from the attached producer
259 frame = mlt_consumer_get_frame( consumer );
260
261 // Ensure that we have a frame
262 if ( frame != NULL )
263 {
264 // Get the speed of the frame
265 double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
266
267 // Determine which speed to use
268 double use_speed = speed;
269
270 // Get refresh request for the current frame (effect changes in still mode)
271 int refresh = mlt_properties_get_int( properties, "refresh" );
272
273 // Decrement refresh and clear changed
274 mlt_properties_set_int( properties, "refresh", refresh > 0 ? refresh - 1 : 0 );
275
276 // Set the changed property on this frame for the benefit of still
277 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
278
279 // Make sure the recipient knows that this frame isn't really rendered
280 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
281
282 // Optimisation to reduce latency
283 if ( speed == 1.0 )
284 {
285 //if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
286 //mlt_consumer_purge( this->play );
287 last_position = mlt_frame_get_position( frame );
288 }
289 else
290 {
291 last_position = -1;
292 }
293
294 // If we're not the first frame and both consumers are stopped, then stop ourselves
295 if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
296 {
297 this->running = 0;
298 mlt_frame_close( frame );
299 }
300 // Allow a little grace time before switching consumers on speed changes
301 else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
302 {
303 mlt_consumer_put_frame( this->active, frame );
304 }
305 // If we aren't playing normally, then use the still
306 else if ( use_speed != 1 )
307 {
308 if ( !mlt_consumer_is_stopped( this->play ) )
309 mlt_consumer_stop( this->play );
310 if ( mlt_consumer_is_stopped( this->still ) )
311 {
312 this->last_speed = use_speed;
313 this->active = this->still;
314 this->ignore_change = 0;
315 mlt_consumer_start( this->still );
316 }
317 mlt_consumer_put_frame( this->still, frame );
318 }
319 // Otherwise use the normal player
320 else
321 {
322 if ( !mlt_consumer_is_stopped( this->still ) )
323 mlt_consumer_stop( this->still );
324 if ( mlt_consumer_is_stopped( this->play ) )
325 {
326 this->last_speed = use_speed;
327 this->active = this->play;
328 this->ignore_change = 25;
329 mlt_consumer_start( this->play );
330 }
331 mlt_consumer_put_frame( this->play, frame );
332 }
333
334 // Copy the rectangle info from the active consumer
335 if ( this->running )
336 {
337 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
338 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
339 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
340 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
341 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
342 }
343
344 if ( this->active == this->still )
345 {
346 pthread_mutex_lock( &this->refresh_mutex );
347 if ( speed == 0 && mlt_properties_get_int( properties, "refresh" ) == 0 )
348 pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
349 pthread_mutex_unlock( &this->refresh_mutex );
350 }
351
352 // We are definitely not waiting on the first frame any more
353 first = 0;
354 }
355 }
356
357 mlt_consumer_stop( this->play );
358 mlt_consumer_stop( this->still );
359
360 SDL_Quit( );
361
362 return NULL;
363 }
364
365 /** Callback to allow override of the close method.
366 */
367
368 static void consumer_close( mlt_consumer parent )
369 {
370 // Get the actual object
371 consumer_sdl this = parent->child;
372
373 // Stop the consumer
374 mlt_consumer_stop( parent );
375
376 // Now clean up the rest
377 mlt_consumer_close( parent );
378
379 // Close the child consumers
380 mlt_consumer_close( this->play );
381 mlt_consumer_close( this->still );
382
383 // Finally clean up this
384 free( this );
385 }