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