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