]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_preview.c
src/framework/mlt_consumer.c src/framework/mlt_consumer.h
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * 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                 consumer_stop( parent );
139
140                 this->running = 1;
141                 this->joined = 0;
142                 this->last_speed = 1;
143
144                 pthread_create( &this->thread, NULL, consumer_thread, this );
145         }
146
147         return 0;
148 }
149
150 static int consumer_stop( mlt_consumer parent )
151 {
152         // Get the actual object
153         consumer_sdl this = parent->child;
154
155         if ( this->joined == 0 )
156         {
157                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( parent );
158                 int app_locked = mlt_properties_get_int( properties, "app_locked" );
159                 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
160                 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
161
162                 if ( app_locked && unlock ) unlock( );
163
164                 // Kill the thread and clean up
165                 this->running = 0;
166
167                 pthread_mutex_lock( &this->refresh_mutex );
168                 pthread_cond_broadcast( &this->refresh_cond );
169                 pthread_mutex_unlock( &this->refresh_mutex );
170
171                 if ( this->play ) mlt_consumer_stop( this->play );
172                 if ( this->still ) mlt_consumer_stop( this->still );
173
174                 pthread_join( this->thread, NULL );
175                 this->joined = 1;
176
177                 if ( app_locked && lock ) lock( );
178         }
179
180         return 0;
181 }
182
183 static int consumer_is_stopped( mlt_consumer parent )
184 {
185         consumer_sdl this = parent->child;
186         return !this->running;
187 }
188
189 static void *consumer_thread( void *arg )
190 {
191         // Identify the arg
192         consumer_sdl this = arg;
193
194         // Get the consumer
195         mlt_consumer consumer = &this->parent;
196
197         // internal intialization
198         int first = 1;
199         mlt_frame frame = NULL;
200         int last_position = -1;
201
202         // properties
203         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
204         mlt_properties play = MLT_CONSUMER_PROPERTIES( this->play );
205         mlt_properties still = MLT_CONSUMER_PROPERTIES( this->still );
206
207         int progressive = mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" );
208
209         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
210         {
211                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
212                 return NULL;
213         }
214
215         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
216         SDL_EnableUNICODE( 1 );
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         // Pass properties down
223         mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
224         mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
225         mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
226         mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
227         mlt_properties_set_int( play, "resize", mlt_properties_get_int( properties, "resize" ) );
228         mlt_properties_set_int( still, "resize", mlt_properties_get_int( properties, "resize" ) );
229         mlt_properties_set( play, "rescale", mlt_properties_get( properties, "rescale" ) );
230         mlt_properties_set( still, "rescale", mlt_properties_get( properties, "rescale" ) );
231         mlt_properties_set_int( play, "width", mlt_properties_get_int( properties, "width" ) );
232         mlt_properties_set_int( still, "width", mlt_properties_get_int( properties, "width" ) );
233         mlt_properties_set_int( play, "height", mlt_properties_get_int( properties, "height" ) );
234         mlt_properties_set_int( still, "height", mlt_properties_get_int( properties, "height" ) );
235         mlt_properties_set_double( play, "aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
236         mlt_properties_set_double( still, "aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
237         mlt_properties_set_double( play, "display_ratio", mlt_properties_get_double( properties, "display_ratio" ) );
238         mlt_properties_set_double( still, "display_ratio", mlt_properties_get_double( properties, "display_ratio" ) );
239
240         mlt_properties_set_int( play, "progressive", progressive );
241         mlt_properties_set_int( still, "progressive", progressive );
242         mlt_properties_set( play, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
243         mlt_properties_set( still, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
244
245         mlt_properties_pass( play, MLT_CONSUMER_PROPERTIES( consumer ), "play." );
246         mlt_properties_pass( still, MLT_CONSUMER_PROPERTIES( consumer ), "still." );
247
248         mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
249         mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
250         mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
251         mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
252
253         mlt_properties_set_int( play, "put_mode", 1 );
254         mlt_properties_set_int( still, "put_mode", 1 );
255
256         // Loop until told not to
257         while( this->running )
258         {
259                 // Get a frame from the attached producer
260                 frame = mlt_consumer_get_frame( consumer );
261
262                 // Ensure that we have a frame
263                 if ( frame != NULL )
264                 {
265                         // Get the speed of the frame
266                         double speed = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" );
267
268                         // Lock during the operation
269                         mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
270
271                         // Get refresh request for the current frame
272                         int refresh = mlt_properties_get_int( properties, "refresh" );
273
274                         // Decrement refresh and clear changed
275                         mlt_events_block( properties, properties );
276                         mlt_properties_set_int( properties, "refresh", 0 );
277                         mlt_events_unblock( properties, properties );
278
279                         // Unlock after the operation
280                         mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
281
282                         // Set the changed property on this frame for the benefit of still
283                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "refresh", refresh );
284
285                         // Make sure the recipient knows that this frame isn't really rendered
286                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 0 );
287
288                         // Optimisation to reduce latency
289                         if ( speed == 1.0 )
290                         {
291                                 if ( last_position != -1 && last_position + 1 != mlt_frame_get_position( frame ) )
292                                         mlt_consumer_purge( this->play );
293                                 last_position = mlt_frame_get_position( frame );
294                         }
295                         else
296                         {
297                                 //mlt_consumer_purge( this->play );
298                                 last_position = -1;
299                         }
300
301                         // If we're not the first frame and both consumers are stopped, then stop ourselves
302                         if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
303                         {
304                                 this->running = 0;
305                                 mlt_frame_close( frame );
306                         }
307                         // Allow a little grace time before switching consumers on speed changes
308                         else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
309                         {
310                                 mlt_consumer_put_frame( this->active, frame );
311                         }
312                         // If we aren't playing normally, then use the still
313                         else if ( speed != 1 )
314                         {
315                                 if ( !mlt_consumer_is_stopped( this->play ) )
316                                         mlt_consumer_stop( this->play );
317                                 if ( mlt_consumer_is_stopped( this->still ) )
318                                 {
319                                         this->last_speed = speed;
320                                         this->active = this->still;
321                                         this->ignore_change = 0;
322                                         mlt_consumer_start( this->still );
323                                 }
324                                 mlt_consumer_put_frame( this->still, frame );
325                         }
326                         // Otherwise use the normal player
327                         else
328                         {
329                                 if ( !mlt_consumer_is_stopped( this->still ) )
330                                         mlt_consumer_stop( this->still );
331                                 if ( mlt_consumer_is_stopped( this->play ) )
332                                 {
333                                         this->last_speed = speed;
334                                         this->active = this->play;
335                                         this->ignore_change = 25;
336                                         mlt_consumer_start( this->play );
337                                 }
338                                 mlt_consumer_put_frame( this->play, frame );
339                         }
340
341                         // Copy the rectangle info from the active consumer
342                         if ( this->running )
343                         {
344                                 mlt_properties active = MLT_CONSUMER_PROPERTIES( this->active );
345                                 mlt_service_lock( MLT_CONSUMER_SERVICE( consumer ) );
346                                 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
347                                 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
348                                 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
349                                 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
350                                 mlt_service_unlock( MLT_CONSUMER_SERVICE( consumer ) );
351                         }
352
353                         if ( this->active == this->still )
354                         {
355                                 pthread_mutex_lock( &this->refresh_mutex );
356                                 if ( speed == 0 && this->refresh_count <= 0 )
357                                         pthread_cond_wait( &this->refresh_cond, &this->refresh_mutex );
358                                 this->refresh_count --;
359                                 pthread_mutex_unlock( &this->refresh_mutex );
360                         }
361
362                         // We are definitely not waiting on the first frame any more
363                         first = 0;
364                 }
365                 else
366                 {
367                         this->running = 0;
368                 }
369         }
370
371         //mlt_consumer_stop( this->play );
372         //mlt_consumer_stop( this->still );
373
374         //SDL_Quit( );
375
376         return NULL;
377 }
378
379 /** Callback to allow override of the close method.
380 */
381
382 static void consumer_close( mlt_consumer parent )
383 {
384         // Get the actual object
385         consumer_sdl this = parent->child;
386
387         // Stop the consumer
388         mlt_consumer_stop( parent );
389
390         // Close the child consumers
391         mlt_consumer_close( this->play );
392         mlt_consumer_close( this->still );
393
394         // Now clean up the rest
395         mlt_consumer_close( parent );
396
397         // Finally clean up this
398         free( this );
399 }