]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_preview.c
Support exif rotation with pixbuf producer
[mlt] / src / modules / sdl / consumer_sdl_preview.c
1 /*
2  * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4  * Author: Charles Yates
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_consumer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_producer.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30 #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                 return parent;
108         }
109         free( this );
110         return NULL;
111 }
112
113 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
114 {
115         consumer_sdl this = parent->child;
116         this->last_speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
117         this->last_position = mlt_frame_get_position( frame );
118         mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-frame-show", frame, NULL );
119 }
120
121 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
122 {
123         mlt_events_fire( MLT_CONSUMER_PROPERTIES( parent ), "consumer-sdl-event", event, NULL );
124 }
125
126 static void consumer_refresh_cb( mlt_consumer sdl, mlt_consumer parent, char *name )
127 {
128         if ( !strcmp( name, "refresh" ) )
129         {
130                 consumer_sdl this = parent->child;
131                 pthread_mutex_lock( &this->refresh_mutex );
132                 this->refresh_count = this->refresh_count <= 0 ? 1 : this->refresh_count ++;
133                 pthread_cond_broadcast( &this->refresh_cond );
134                 pthread_mutex_unlock( &this->refresh_mutex );
135         }
136 }
137
138 static int consumer_start( mlt_consumer parent )
139 {
140         consumer_sdl this = parent->child;
141
142         if ( !this->running )
143         {
144                 // properties
145                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
146                 mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
147                 mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
148
149                 char *window_id = mlt_properties_get( properties, "window_id" );
150                 char *audio_driver = mlt_properties_get( properties, "audio_driver" );
151                 char *video_driver = mlt_properties_get( properties, "video_driver" );
152                 char *audio_device = mlt_properties_get( properties, "audio_device" );
153                 char *output_display = mlt_properties_get( properties, "output_display" );
154                 int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
155
156                 consumer_stop( parent );
157
158                 this->running = 1;
159                 this->joined = 0;
160                 this->last_speed = 1;
161
162                 if ( output_display != NULL )
163                         setenv( "DISPLAY", output_display, 1 );
164
165                 if ( window_id != NULL )
166                         setenv( "SDL_WINDOWID", window_id, 1 );
167
168                 if ( video_driver != NULL )
169                         setenv( "SDL_VIDEODRIVER", video_driver, 1 );
170
171                 if ( audio_driver != NULL )
172                         setenv( "SDL_AUDIODRIVER", audio_driver, 1 );
173
174                 if ( audio_device != NULL )
175                         setenv( "AUDIODEV", audio_device, 1 );
176
177                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
178                 {
179                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
180                         return -1;
181                 }
182
183                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
184                 SDL_EnableUNICODE( 1 );
185
186                 // Pass properties down
187                 mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
188                 mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
189                 mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
190                 mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
191
192                 mlt_properties_set_int( play, "progressive", progressive );
193                 mlt_properties_set_int( still, "progressive", progressive );
194
195                 mlt_properties_pass_list( play, properties, "resize,rescale,width,height,aspect_ratio,display_ratio,volume" );
196                 mlt_properties_pass_list( still, properties, "resize,rescale,width,height,aspect_ratio,display_ratio" );
197                 mlt_properties_pass_list( play, properties, "deinterlace_method" );
198                 mlt_properties_pass_list( still, properties, "deinterlace_method" );
199                 mlt_properties_pass_list( play, properties, "preview_off,preview_format,window_background" );
200                 mlt_properties_pass_list( still, properties, "preview_off,preview_format,window_background" );
201
202                 mlt_properties_pass( play, properties, "play." );
203                 mlt_properties_pass( still, properties, "still." );
204
205                 mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
206                 mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
207                 mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
208                 mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
209
210                 mlt_properties_set_int( play, "put_mode", 1 );
211                 mlt_properties_set_int( still, "put_mode", 1 );
212                 mlt_properties_set_int( play, "terminate_on_pause", 1 );
213
214                 // Start the still producer just to initialise the gui
215                 mlt_consumer_start( this->still );
216                 this->active = this->still;
217
218                 // Inform child consumers that we control the sdl
219                 mlt_properties_set_int( play, "sdl_started", 1 );
220                 mlt_properties_set_int( still, "sdl_started", 1 );
221
222                 pthread_create( &this->thread, NULL, consumer_thread, this );
223         }
224
225         return 0;
226 }
227
228 static int consumer_stop( mlt_consumer parent )
229 {
230         // Get the actual object
231         consumer_sdl this = parent->child;
232
233         if ( this->joined == 0 )
234         {
235                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
236                 int app_locked = mlt_properties_get_int( properties, "app_locked" );
237                 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
238                 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
239
240                 if ( app_locked && unlock ) unlock( );
241
242                 // Kill the thread and clean up
243                 this->running = 0;
244
245                 pthread_mutex_lock( &this->refresh_mutex );
246                 pthread_cond_broadcast( &this->refresh_cond );
247                 pthread_mutex_unlock( &this->refresh_mutex );
248                 if ( this->thread )
249                         pthread_join( this->thread, NULL );
250                 this->joined = 1;
251
252                 if ( app_locked && lock ) lock( );
253                 
254                 pthread_mutex_lock( &mlt_sdl_mutex );
255                 SDL_Quit( );
256                 pthread_mutex_unlock( &mlt_sdl_mutex );
257         }
258
259         return 0;
260 }
261
262 static int consumer_is_stopped( mlt_consumer parent )
263 {
264         consumer_sdl this = parent->child;
265         return !this->running;
266 }
267
268 static void *consumer_thread( void *arg )
269 {
270         // Identify the arg
271         consumer_sdl this = arg;
272
273         // Get the consumer and producer
274         mlt_consumer consumer = &this->parent;
275         mlt_producer producer = MLT_PRODUCER( mlt_service_get_producer( MLT_CONSUMER_SERVICE( consumer ) ) );
276
277         // Get the properties
278         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
279
280         // internal intialization
281         int first = 1;
282         mlt_frame frame = NULL;
283         int last_position = -1;
284         int eof_threshold = 10 + mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this->play ), "buffer" );
285         int prefill = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this->play ), "prefill" );
286         int buffer = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this->play ), "buffer" );
287
288         // Determine if the application is dealing with the preview
289         int preview_off = mlt_properties_get_int( properties, "preview_off" );
290
291         this->refresh_count = 0;
292
293         // Loop until told not to
294         while( this->running )
295         {
296                 // Get a frame from the attached producer
297                 frame = mlt_consumer_get_frame( consumer );
298
299                 // Ensure that we have a frame
300                 if ( this->running && frame != NULL )
301                 {
302                         // Get the speed of the frame
303                         double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
304
305                         // Lock during the operation
306                         mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
307
308                         // Get refresh request for the current frame
309                         int refresh = mlt_properties_get_int( properties, "refresh" );
310
311                         // Decrement refresh and clear changed
312                         mlt_events_block( properties, properties );
313                         mlt_properties_set_int( properties, "refresh", 0 );
314                         mlt_events_unblock( properties, properties );
315
316                         // Unlock after the operation
317                         mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
318
319                         // Set the changed property on this frame for the benefit of still
320                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
321
322                         // Make sure the recipient knows that this frame isn't really rendered
323                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
324
325                         // Optimisation to reduce latency
326                         if ( speed == 1.0 )
327                         {
328                                 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
329                                         mlt_consumer_purge( this->play );
330                                 last_position = mlt_frame_get_position( frame );
331                         }
332                         else
333                         {
334                                 //mlt_consumer_purge( this->play );
335                                 last_position = -1;
336                         }
337
338                         // If we aren't playing normally, then use the still
339                         if ( speed != 1 )
340                         {
341                                 mlt_position duration = mlt_producer_get_playtime( producer );
342
343                                 // Do not interrupt the normal consumer near the end
344                                 if ( !mlt_consumer_is_stopped( this->play ) && ( duration - this->last_position ) > eof_threshold )
345                                         mlt_consumer_stop( this->play );
346
347                                 // Switch to the still consumer
348                                 if ( mlt_consumer_is_stopped( this->still ) )
349                                 {
350                                         // If near the end, wait for the normal consumer to play out its buffers
351                                         assert( this->active == this->play );
352                                         if ( ! mlt_consumer_is_stopped( this->play ) && speed == 0.0 )
353                                         {
354                                                 // This frame with speed=0 will tell normal consumer to terminate
355                                                 mlt_consumer_put_frame( this->play, frame );
356
357                                                 if ( ( duration - this->last_position ) > ( prefill > 0 ? prefill : buffer ) )
358                                                 {
359                                                         while ( ! mlt_consumer_is_stopped( this->play ) )
360                                                         {
361                                                                 struct timespec tm = { 0, 10000000L }; // 10 ms
362                                                                 nanosleep( &tm, NULL );
363                                                         }
364                                                 }
365                                                 mlt_consumer_stop( this->play );
366
367                                                 // We want to be positioned at the end
368                                                 if ( duration - this->last_position < 10 )
369                                                         this->last_position = duration - 1;
370                                         }
371                                         else
372                                         {
373                                                 // We will need a new frame at the correct position
374                                                 mlt_frame_close( frame );
375                                         }
376
377                                         // Get a new frame at the sought position
378                                         if ( producer )
379                                                 mlt_producer_seek( producer, this->last_position );
380                                         frame = mlt_consumer_get_frame( consumer );
381
382                                         // Start the still consumer
383                                         this->last_speed = speed;
384                                         this->active = this->still;
385                                         this->ignore_change = 0;
386                                         mlt_consumer_start( this->still );
387                                 }
388                                 // Use the still consumer
389                                 if ( frame )
390                                         mlt_consumer_put_frame( this->still, frame );
391                         }
392                         // Allow a little grace time before switching consumers on speed changes
393                         else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
394                         {
395                                 mlt_consumer_put_frame( this->active, frame );
396                         }
397                         // Otherwise use the normal player
398                         else
399                         {
400                                 if ( !mlt_consumer_is_stopped( this->still ) )
401                                         mlt_consumer_stop( this->still );
402                                 if ( mlt_consumer_is_stopped( this->play ) )
403                                 {
404                                         this->last_speed = speed;
405                                         this->active = this->play;
406                                         this->ignore_change = 0;
407                                         mlt_consumer_start( this->play );
408                                 }
409                                 mlt_consumer_put_frame( this->play, frame );
410                         }
411
412                         // Copy the rectangle info from the active consumer
413                         if ( this->running && preview_off == 0 )
414                         {
415                                 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
416                                 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
417                                 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
418                                 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
419                                 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
420                                 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
421                                 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
422                         }
423
424                         if ( this->active == this->still )
425                         {
426                                 pthread_mutex_lock( &this->refresh_mutex );
427                                 if ( this->running && speed == 0 && this->refresh_count <= 0 )
428                                         pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
429                                 this->refresh_count --;
430                                 pthread_mutex_unlock( &this->refresh_mutex );
431                         }
432
433                         // We are definitely not waiting on the first frame any more
434                         first = 0;
435                 }
436                 else
437                 {
438                         if ( frame ) mlt_frame_close( frame );
439                         mlt_consumer_put_frame( this->active, NULL );
440                         this->running = 0;
441                 }
442         }
443
444         if ( this->play ) mlt_consumer_stop( this->play );
445         if ( this->still ) mlt_consumer_stop( this->still );
446
447         return NULL;
448 }
449
450 /** Callback to allow override of the close method.
451 */
452
453 static void consumer_close( mlt_consumer parent )
454 {
455         // Get the actual object
456         consumer_sdl this = parent->child;
457
458         // Stop the consumer
459         mlt_consumer_stop( parent );
460
461         // Close the child consumers
462         mlt_consumer_close( this->play );
463         mlt_consumer_close( this->still );
464
465         // Now clean up the rest
466         mlt_consumer_close( parent );
467
468         // Finally clean up this
469         free( this );
470 }