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