]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_preview.c
e43ac24d5a45ffc4fc695d0e71a90e3687ac9a4c
[mlt] / 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 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.
10  *
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.
15  *
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
19  */
20
21 #include <framework/mlt_consumer.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 extern pthread_mutex_t mlt_sdl_mutex;
32
33 typedef struct consumer_sdl_s *consumer_sdl;
34
35 struct consumer_sdl_s
36 {
37         struct mlt_consumer_s parent;
38         mlt_consumer active;
39         int ignore_change;
40         mlt_consumer play;
41         mlt_consumer still;
42         pthread_t thread;
43         int joined;
44         int running;
45         int sdl_flags;
46         double last_speed;
47
48         pthread_cond_t refresh_cond;
49         pthread_mutex_t refresh_mutex;
50         int refresh_count;
51 };
52
53 /** Forward references to static functions.
54 */
55
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 );
64
65 mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
66 {
67         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
68         if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
69         {
70                 // Get the parent consumer object
71                 mlt_consumer parent = &this->parent;
72
73                 // Get the properties
74                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
75
76                 // Get the width and height
77                 int width = mlt_properties_get_int( properties, "width" );
78                 int height = mlt_properties_get_int( properties, "height" );
79
80                 // Process actual param
81                 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
82                 {
83                         mlt_properties_set_int( properties, "width", width );
84                         mlt_properties_set_int( properties, "height", height );
85                 }
86
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;
97                 this->joined = 1;
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 );
105                 return parent;
106         }
107         free( this );
108         return NULL;
109 }
110
111 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
112 {
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 );
116 }
117
118 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
119 {
120         mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
121 }
122
123 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
124 {
125         if ( !strcmp( name, "refresh" ) )
126         {
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 );
132         }
133 }
134
135 static int consumer_start( mlt_consumer parent )
136 {
137         consumer_sdl this = parent->child;
138
139         if ( !this->running )
140         {
141                 // properties
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 );
145
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" );
152
153                 consumer_stop( parent );
154
155                 this->running = 1;
156                 this->joined = 0;
157                 this->last_speed = 1;
158
159                 if ( output_display != NULL )
160                         setenv( "DISPLAY", output_display, 1 );
161
162                 if ( window_id != NULL )
163                         setenv( "SDL_WINDOWID", window_id, 1 );
164
165                 if ( video_driver != NULL )
166                         setenv( "SDL_VIDEODRIVER", video_driver, 1 );
167
168                 if ( audio_driver != NULL )
169                         setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
170
171                 if ( audio_device != NULL )
172                         setenv( "AUDIODEV", audio_device, 1 );
173
174                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
175                 {
176                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
177                         return -1;
178                 }
179
180                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
181                 SDL_EnableUNICODE( 1 );
182
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 );
188
189                 mlt_properties_set_int( play, "progressive", progressive );
190                 mlt_properties_set_int( still, "progressive", progressive );
191
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" );
198
199                 mlt_properties_pass( play, properties, "play." );
200                 mlt_properties_pass( still, properties, "still." );
201
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 );
206
207                 mlt_properties_set_int( play, "put_mode", 1 );
208                 mlt_properties_set_int( still, "put_mode", 1 );
209
210                 // Start the still producer just to initialise the gui
211                 mlt_consumer_start( this->still );
212                 this->active = this->still;
213
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 );
217
218                 pthread_create( &this->thread, NULL, consumer_thread, this );
219         }
220
221         return 0;
222 }
223
224 static int consumer_stop( mlt_consumer parent )
225 {
226         // Get the actual object
227         consumer_sdl this = parent->child;
228
229         if ( this->joined == 0 )
230         {
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 );
235
236                 if ( app_locked && unlock ) unlock( );
237
238                 // Kill the thread and clean up
239                 this->running = 0;
240
241                 pthread_mutex_lock( &this->refresh_mutex );
242                 pthread_cond_broadcast( &this->refresh_cond );
243                 pthread_mutex_unlock( &this->refresh_mutex );
244                 if ( this->thread )
245                         pthread_join( this->thread, NULL );
246                 this->joined = 1;
247
248                 if ( app_locked && lock ) lock( );
249                 
250                 pthread_mutex_lock( &mlt_sdl_mutex );
251                 SDL_Quit( );
252                 pthread_mutex_unlock( &mlt_sdl_mutex );
253         }
254
255         return 0;
256 }
257
258 static int consumer_is_stopped( mlt_consumer parent )
259 {
260         consumer_sdl this = parent->child;
261         return !this->running;
262 }
263
264 static void *consumer_thread( void *arg )
265 {
266         // Identify the arg
267         consumer_sdl this = arg;
268
269         // Get the consumer
270         mlt_consumer consumer = &this->parent;
271
272         // Get the properties
273         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
274
275         // internal intialization
276         int first = 1;
277         mlt_frame frame = NULL;
278         int last_position = -1;
279
280         // Determine if the application is dealing with the preview
281         int preview_off = mlt_properties_get_int( properties, "preview_off" );
282
283         this->refresh_count = 0;
284
285         // Loop until told not to
286         while( this->running )
287         {
288                 // Get a frame from the attached producer
289                 frame = mlt_consumer_get_frame( consumer );
290
291                 // Ensure that we have a frame
292                 if ( this->running && frame != NULL )
293                 {
294                         // Get the speed of the frame
295                         double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
296
297                         // Lock during the operation
298                         mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
299
300                         // Get refresh request for the current frame
301                         int refresh = mlt_properties_get_int( properties, "refresh" );
302
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 );
307
308                         // Unlock after the operation
309                         mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
310
311                         // Set the changed property on this frame for the benefit of still
312                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
313
314                         // Make sure the recipient knows that this frame isn't really rendered
315                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
316
317                         // Optimisation to reduce latency
318                         if ( speed == 1.0 )
319                         {
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 );
323                         }
324                         else
325                         {
326                                 //mlt_consumer_purge( this->play );
327                                 last_position = -1;
328                         }
329
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 ) )
332                         {
333                                 this->running = 0;
334                                 mlt_frame_close( frame );
335                         }
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 ) )
338                         {
339                                 mlt_consumer_put_frame( this->active, frame );
340                         }
341                         // If we aren't playing normally, then use the still
342                         else if ( speed != 1 )
343                         {
344                                 if ( !mlt_consumer_is_stopped( this->play ) )
345                                         mlt_consumer_stop( this->play );
346                                 if ( mlt_consumer_is_stopped( this->still ) )
347                                 {
348                                         this->last_speed = speed;
349                                         this->active = this->still;
350                                         this->ignore_change = 0;
351                                         mlt_consumer_start( this->still );
352                                 }
353                                 mlt_consumer_put_frame( this->still, frame );
354                         }
355                         // Otherwise use the normal player
356                         else
357                         {
358                                 if ( !mlt_consumer_is_stopped( this->still ) )
359                                         mlt_consumer_stop( this->still );
360                                 if ( mlt_consumer_is_stopped( this->play ) )
361                                 {
362                                         this->last_speed = speed;
363                                         this->active = this->play;
364                                         this->ignore_change = 25;
365                                         mlt_consumer_start( this->play );
366                                 }
367                                 mlt_consumer_put_frame( this->play, frame );
368                         }
369
370                         // Copy the rectangle info from the active consumer
371                         if ( this->running && preview_off == 0 )
372                         {
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 ) );
380                         }
381
382                         if ( this->active == this->still )
383                         {
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 );
389                         }
390
391                         // We are definitely not waiting on the first frame any more
392                         first = 0;
393                 }
394                 else
395                 {
396                         if ( frame ) mlt_frame_close( frame );
397                         mlt_consumer_put_frame( this->active, NULL );
398                         this->running = 0;
399                 }
400         }
401
402         if ( this->play ) mlt_consumer_stop( this->play );
403         if ( this->still ) mlt_consumer_stop( this->still );
404
405         return NULL;
406 }
407
408 /** Callback to allow override of the close method.
409 */
410
411 static void consumer_close( mlt_consumer parent )
412 {
413         // Get the actual object
414         consumer_sdl this = parent->child;
415
416         // Stop the consumer
417         mlt_consumer_stop( parent );
418
419         // Close the child consumers
420         mlt_consumer_close( this->play );
421         mlt_consumer_close( this->still );
422
423         // Now clean up the rest
424         mlt_consumer_close( parent );
425
426         // Finally clean up this
427         free( this );
428 }