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