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