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