]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_preview.c
Fix minor regression on previous commit to sdl_preview.
[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         mlt_position last_position;
48
49         pthread_cond_t refresh_cond;
50         pthread_mutex_t refresh_mutex;
51         int refresh_count;
52 };
53
54 /** Forward references to static functions.
55 */
56
57 static int consumer_start( mlt_consumer parent );
58 static int consumer_stop( mlt_consumer parent );
59 static int consumer_is_stopped( mlt_consumer parent );
60 static void consumer_close( mlt_consumer parent );
61 static void *consumer_thread( void * );
62 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
63 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer this, SDL_Event *event );
64 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer this, char *name );
65
66 mlt_consumer consumer_sdl_preview_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
67 {
68         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
69         if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
70         {
71                 // Get the parent consumer object
72                 mlt_consumer parent = &this->parent;
73
74                 // Get the properties
75                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
76
77                 // Get the width and height
78                 int width = mlt_properties_get_int( properties, "width" );
79                 int height = mlt_properties_get_int( properties, "height" );
80
81                 // Process actual param
82                 if ( arg == NULL || sscanf( arg, "%dx%d", &width, &height ) == 2 )
83                 {
84                         mlt_properties_set_int( properties, "width", width );
85                         mlt_properties_set_int( properties, "height", height );
86                 }
87
88                 // Create child consumers
89                 this->play = mlt_factory_consumer( profile, "sdl", arg );
90                 this->still = mlt_factory_consumer( profile, "sdl_still", arg );
91                 mlt_properties_set( properties, "real_time", "0" );
92                 mlt_properties_set( properties, "rescale", "nearest" );
93                 mlt_properties_set( properties, "deinterlace_method", "onefield" );
94                 parent->close = consumer_close;
95                 parent->start = consumer_start;
96                 parent->stop = consumer_stop;
97                 parent->is_stopped = consumer_is_stopped;
98                 this->joined = 1;
99                 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
100                 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
101                 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->play ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
102                 mlt_events_listen( MLT_CONSUMER_PROPERTIES( this->still ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
103                 pthread_cond_init( &this->refresh_cond, NULL );
104                 pthread_mutex_init( &this->refresh_mutex, NULL );
105                 mlt_events_listen( MLT_CONSUMER_PROPERTIES( parent ), this, "property-changed", ( mlt_listener )consumer_refresh_cb );
106                 return parent;
107         }
108         free( this );
109         return NULL;
110 }
111
112 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
113 {
114         consumer_sdl this = parent->child;
115         this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
116         this->last_position = mlt_frame_get_position( frame );
117         mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
118 }
119
120 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
121 {
122         mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
123 }
124
125 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
126 {
127         if ( !strcmp( name, "refresh" ) )
128         {
129                 consumer_sdl this = parent->child;
130                 pthread_mutex_lock( &this->refresh_mutex );
131                 this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
132                 pthread_cond_broadcast( &this->refresh_cond );
133                 pthread_mutex_unlock( &this->refresh_mutex );
134         }
135 }
136
137 static int consumer_start( mlt_consumer parent )
138 {
139         consumer_sdl this = parent->child;
140
141         if ( !this->running )
142         {
143                 // properties
144                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
145                 mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
146                 mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
147
148                 char *window_id = mlt_properties_get( properties, "window_id" );
149                 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
150                 char *video_driver = mlt_properties_get( properties, "video_driver" );
151                 char *audio_device = mlt_properties_get( properties, "audio_device" );
152                 char *output_display = mlt_properties_get( properties, "output_display" );
153                 int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
154
155                 consumer_stop( parent );
156
157                 this->running = 1;
158                 this->joined = 0;
159                 this->last_speed = 1;
160
161                 if ( output_display != NULL )
162                         setenv( "DISPLAY", output_display, 1 );
163
164                 if ( window_id != NULL )
165                         setenv( "SDL_WINDOWID", window_id, 1 );
166
167                 if ( video_driver != NULL )
168                         setenv( "SDL_VIDEODRIVER", video_driver, 1 );
169
170                 if ( audio_driver != NULL )
171                         setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
172
173                 if ( audio_device != NULL )
174                         setenv( "AUDIODEV", audio_device, 1 );
175
176                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
177                 {
178                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
179                         return -1;
180                 }
181
182                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
183                 SDL_EnableUNICODE( 1 );
184
185                 // Pass properties down
186                 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
187                 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
188                 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
189                 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
190
191                 mlt_properties_set_int( play, "progressive", progressive );
192                 mlt_properties_set_int( still, "progressive", progressive );
193
194                 mlt_properties_pass_list( play, properties, "resize,rescale,width,height,aspect_ratio,display_ratio,volume" );
195                 mlt_properties_pass_list( still, properties, "resize,rescale,width,height,aspect_ratio,display_ratio" );
196                 mlt_properties_pass_list( play, properties, "deinterlace_method" );
197                 mlt_properties_pass_list( still, properties, "deinterlace_method" );
198                 mlt_properties_pass_list( play, properties, "preview_off,preview_format,window_background" );
199                 mlt_properties_pass_list( still, properties, "preview_off,preview_format,window_background" );
200
201                 mlt_properties_pass( play, properties, "play." );
202                 mlt_properties_pass( still, properties, "still." );
203
204                 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
205                 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
206                 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
207                 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
208
209                 mlt_properties_set_int( play, "put_mode", 1 );
210                 mlt_properties_set_int( still, "put_mode", 1 );
211                 mlt_properties_set_int( play, "terminate_on_pause", 1 );
212
213                 // Start the still producer just to initialise the gui
214                 mlt_consumer_start( this->still );
215                 this->active = this->still;
216
217                 // Inform child consumers that we control the sdl
218                 mlt_properties_set_int( play, "sdl_started", 1 );
219                 mlt_properties_set_int( still, "sdl_started", 1 );
220
221                 pthread_create( &this->thread, NULL, consumer_thread, this );
222         }
223
224         return 0;
225 }
226
227 static int consumer_stop( mlt_consumer parent )
228 {
229         // Get the actual object
230         consumer_sdl this = parent->child;
231
232         if ( this->joined == 0 )
233         {
234                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
235                 int app_locked = mlt_properties_get_int( properties, "app_locked" );
236                 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
237                 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
238
239                 if ( app_locked && unlock ) unlock( );
240
241                 // Kill the thread and clean up
242                 this->running = 0;
243
244                 pthread_mutex_lock( &this->refresh_mutex );
245                 pthread_cond_broadcast( &this->refresh_cond );
246                 pthread_mutex_unlock( &this->refresh_mutex );
247                 if ( this->thread )
248                         pthread_join( this->thread, NULL );
249                 this->joined = 1;
250
251                 if ( app_locked && lock ) lock( );
252                 
253                 pthread_mutex_lock( &mlt_sdl_mutex );
254                 SDL_Quit( );
255                 pthread_mutex_unlock( &mlt_sdl_mutex );
256         }
257
258         return 0;
259 }
260
261 static int consumer_is_stopped( mlt_consumer parent )
262 {
263         consumer_sdl this = parent->child;
264         return !this->running;
265 }
266
267 static void *consumer_thread( void *arg )
268 {
269         // Identify the arg
270         consumer_sdl this = arg;
271
272         // Get the consumer and producer
273         mlt_consumer consumer = &this->parent;
274         mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
275
276         // Get the properties
277         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
278
279         // internal intialization
280         int first = 1;
281         mlt_frame frame = NULL;
282         int last_position = -1;
283         int eof_threshold = 10 + mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this->play ), "buffer" );
284
285         // Determine if the application is dealing with the preview
286         int preview_off = mlt_properties_get_int( properties, "preview_off" );
287
288         this->refresh_count = 0;
289
290         // Loop until told not to
291         while( this->running )
292         {
293                 // Get a frame from the attached producer
294                 frame = mlt_consumer_get_frame( consumer );
295
296                 // Ensure that we have a frame
297                 if ( this->running && frame != NULL )
298                 {
299                         // Get the speed of the frame
300                         double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
301
302                         // Lock during the operation
303                         mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
304
305                         // Get refresh request for the current frame
306                         int refresh = mlt_properties_get_int( properties, "refresh" );
307
308                         // Decrement refresh and clear changed
309                         mlt_events_block( properties, properties );
310                         mlt_properties_set_int( properties, "refresh", 0 );
311                         mlt_events_unblock( properties, properties );
312
313                         // Unlock after the operation
314                         mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
315
316                         // Set the changed property on this frame for the benefit of still
317                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
318
319                         // Make sure the recipient knows that this frame isn't really rendered
320                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
321
322                         // Optimisation to reduce latency
323                         if ( speed == 1.0 )
324                         {
325                                 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
326                                         mlt_consumer_purge( this->play );
327                                 last_position = mlt_frame_get_position( frame );
328                         }
329                         else
330                         {
331                                 //mlt_consumer_purge( this->play );
332                                 last_position = -1;
333                         }
334
335                         // If we aren't playing normally, then use the still
336                         if ( speed != 1 )
337                         {
338                                 mlt_position duration = mlt_producer_get_playtime( producer );
339
340                                 // Do not interrupt the normal consumer near the end
341                                 if ( !mlt_consumer_is_stopped( this->play ) && ( duration - this->last_position ) > eof_threshold )
342                                         mlt_consumer_stop( this->play );
343
344                                 // Switch to the still consumer
345                                 if ( mlt_consumer_is_stopped( this->still ) )
346                                 {
347                                         // If near the end, wait for the normal consumer to play out its buffers
348                                         if ( ! mlt_consumer_is_stopped( this->play ) && speed == 0.0 )
349                                         {
350                                                 // This frame with speed=0 will tell normal consumer to terminate
351                                                 mlt_consumer_put_frame( this->play, frame );
352                                                 while ( ! mlt_consumer_is_stopped( this->play ) )
353                                                 {
354                                                         struct timespec tm = { 0, 10000000L }; // 10 ms
355                                                         nanosleep( &tm, NULL );
356                                                 }
357
358                                                 // We want to be positioned at the end
359                                                 if ( duration - this->last_position < 10 )
360                                                         this->last_position = duration - 1;
361                                         }
362                                         else
363                                         {
364                                                 // We will need a new frame at the correct position
365                                                 mlt_frame_close( frame );
366                                         }
367
368                                         // Get a new frame at the sought position
369                                         if ( producer )
370                                                 mlt_producer_seek( producer, this->last_position );
371                                         frame = mlt_consumer_get_frame( consumer );
372
373                                         // Start the still consumer
374                                         this->last_speed = speed;
375                                         this->active = this->still;
376                                         this->ignore_change = 0;
377                                         mlt_consumer_start( this->still );
378                                 }
379                                 // Use the still consumer
380                                 if ( frame )
381                                         mlt_consumer_put_frame( this->still, frame );
382                         }
383                         // Allow a little grace time before switching consumers on speed changes
384                         else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
385                         {
386                                 mlt_consumer_put_frame( this->active, frame );
387                         }
388                         // Otherwise use the normal player
389                         else
390                         {
391                                 if ( !mlt_consumer_is_stopped( this->still ) )
392                                         mlt_consumer_stop( this->still );
393                                 if ( mlt_consumer_is_stopped( this->play ) )
394                                 {
395                                         this->last_speed = speed;
396                                         this->active = this->play;
397                                         this->ignore_change = 0;
398                                         mlt_consumer_start( this->play );
399                                 }
400                                 mlt_consumer_put_frame( this->play, frame );
401                         }
402
403                         // Copy the rectangle info from the active consumer
404                         if ( this->running && preview_off == 0 )
405                         {
406                                 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
407                                 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
408                                 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
409                                 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
410                                 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
411                                 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
412                                 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
413                         }
414
415                         if ( this->active == this->still )
416                         {
417                                 pthread_mutex_lock( &this->refresh_mutex );
418                                 if ( this->running && speed == 0 && this->refresh_count <= 0 )
419                                         pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
420                                 this->refresh_count --;
421                                 pthread_mutex_unlock( &this->refresh_mutex );
422                         }
423
424                         // We are definitely not waiting on the first frame any more
425                         first = 0;
426                 }
427                 else
428                 {
429                         if ( frame ) mlt_frame_close( frame );
430                         mlt_consumer_put_frame( this->active, NULL );
431                         this->running = 0;
432                 }
433         }
434
435         if ( this->play ) mlt_consumer_stop( this->play );
436         if ( this->still ) mlt_consumer_stop( this->still );
437
438         return NULL;
439 }
440
441 /** Callback to allow override of the close method.
442 */
443
444 static void consumer_close( mlt_consumer parent )
445 {
446         // Get the actual object
447         consumer_sdl this = parent->child;
448
449         // Stop the consumer
450         mlt_consumer_stop( parent );
451
452         // Close the child consumers
453         mlt_consumer_close( this->play );
454         mlt_consumer_close( this->still );
455
456         // Now clean up the rest
457         mlt_consumer_close( parent );
458
459         // Finally clean up this
460         free( this );
461 }