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