]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_preview.c
Minor config fixes and gtk2 consumer added
[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 <pthread.h>
27 #include <SDL/SDL.h>
28 #include <SDL/SDL_syswm.h>
29
30 typedef struct consumer_sdl_s *consumer_sdl;
31
32 struct consumer_sdl_s
33 {
34         struct mlt_consumer_s parent;
35         mlt_consumer active;
36         int ignore_change;
37         mlt_consumer play;
38         mlt_consumer still;
39         pthread_t thread;
40         int joined;
41         int running;
42         int sdl_flags;
43         double last_speed;
44 };
45
46 /** Forward references to static functions.
47 */
48
49 static int consumer_start( mlt_consumer parent );
50 static int consumer_stop( mlt_consumer parent );
51 static int consumer_is_stopped( mlt_consumer parent );
52 static void consumer_close( mlt_consumer parent );
53 static void *consumer_thread( void * );
54 static void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer this, mlt_frame frame );
55 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer this, SDL_Event *event );
56
57 mlt_consumer consumer_sdl_preview_init( char *arg )
58 {
59         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
60         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
61         {
62                 // Get the parent consumer object
63                 mlt_consumer parent = &this->parent;
64                 this->play = mlt_factory_consumer( "sdl", arg );
65                 this->still = mlt_factory_consumer( "sdl_still", arg );
66                 mlt_properties_set( mlt_consumer_properties( parent ), "real_time", "0" );
67                 parent->close = consumer_close;
68                 parent->start = consumer_start;
69                 parent->stop = consumer_stop;
70                 parent->is_stopped = consumer_is_stopped;
71                 this->joined = 1;
72                 mlt_events_listen( mlt_consumer_properties( this->play ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
73                 mlt_events_listen( mlt_consumer_properties( this->still ), this, "consumer-frame-show", ( mlt_listener )consumer_frame_show_cb );
74                 mlt_events_listen( mlt_consumer_properties( this->play ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
75                 mlt_events_listen( mlt_consumer_properties( this->still ), this, "consumer-sdl-event", ( mlt_listener )consumer_sdl_event_cb );
76                 return parent;
77         }
78         free( this );
79         return NULL;
80 }
81
82 void consumer_frame_show_cb( mlt_consumer sdl, mlt_consumer parent, mlt_frame frame )
83 {
84         consumer_sdl this = parent->child;
85         this->last_speed = mlt_properties_get_double( mlt_frame_properties( frame ), "_speed" );
86         mlt_events_fire( mlt_consumer_properties( parent ), "consumer-frame-show", frame, NULL );
87 }
88
89 static void consumer_sdl_event_cb( mlt_consumer sdl, mlt_consumer parent, SDL_Event *event )
90 {
91         mlt_events_fire( mlt_consumer_properties( parent ), "consumer-sdl-event", event, NULL );
92 }
93
94 static int consumer_start( mlt_consumer parent )
95 {
96         consumer_sdl this = parent->child;
97
98         if ( !this->running )
99         {
100                 pthread_attr_t thread_attributes;
101                 
102                 consumer_stop( parent );
103
104                 this->running = 1;
105                 this->joined = 0;
106                 this->last_speed = 1;
107
108                 // Inherit the scheduling priority
109                 pthread_attr_init( &thread_attributes );
110                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
111
112                 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
113         }
114
115         return 0;
116 }
117
118 static int consumer_stop( mlt_consumer parent )
119 {
120         // Get the actual object
121         consumer_sdl this = parent->child;
122
123         if ( this->joined == 0 )
124         {
125                 // Kill the thread and clean up
126                 this->running = 0;
127
128                 pthread_join( this->thread, NULL );
129                 this->joined = 1;
130         }
131
132         return 0;
133 }
134
135 static int consumer_is_stopped( mlt_consumer parent )
136 {
137         consumer_sdl this = parent->child;
138         return !this->running;
139 }
140
141 static void *consumer_thread( void *arg )
142 {
143         // Identify the arg
144         consumer_sdl this = arg;
145
146         // Get the consumer
147         mlt_consumer consumer = &this->parent;
148
149         // internal intialization
150         int first = 1;
151         mlt_frame frame = NULL;
152
153         // properties
154         mlt_properties properties = mlt_consumer_properties( consumer );
155         mlt_properties play = mlt_consumer_properties( this->play );
156         mlt_properties still = mlt_consumer_properties( this->still );
157
158         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
159         {
160                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
161                 return NULL;
162         }
163
164         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
165         SDL_EnableUNICODE( 1 );
166
167         // Inform child consumers that we control the sdl
168         mlt_properties_set_int( play, "sdl_started", 1 );
169         mlt_properties_set_int( still, "sdl_started", 1 );
170
171         // Pass properties down
172         mlt_properties_set_data( play, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
173         mlt_properties_set_data( still, "transport_producer", mlt_properties_get_data( properties, "transport_producer", NULL ), 0, NULL, NULL );
174         mlt_properties_set_data( play, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
175         mlt_properties_set_data( still, "transport_callback", mlt_properties_get_data( properties, "transport_callback", NULL ), 0, NULL, NULL );
176         mlt_properties_set( play, "rescale", mlt_properties_get( properties, "rescale" ) );
177         mlt_properties_set( still, "rescale", mlt_properties_get( properties, "rescale" ) );
178         mlt_properties_set( play, "width", mlt_properties_get( properties, "width" ) );
179         mlt_properties_set( still, "width", mlt_properties_get( properties, "width" ) );
180         mlt_properties_set( play, "height", mlt_properties_get( properties, "height" ) );
181         mlt_properties_set( still, "height", mlt_properties_get( properties, "height" ) );
182
183         mlt_properties_pass( play, mlt_consumer_properties( consumer ), "play." );
184         mlt_properties_pass( still, mlt_consumer_properties( consumer ), "still." );
185
186         mlt_properties_set_data( play, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
187         mlt_properties_set_data( still, "app_lock", mlt_properties_get_data( properties, "app_lock", NULL ), 0, NULL, NULL );
188         mlt_properties_set_data( play, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
189         mlt_properties_set_data( still, "app_unlock", mlt_properties_get_data( properties, "app_unlock", NULL ), 0, NULL, NULL );
190
191         mlt_properties_set_int( play, "put_mode", 1 );
192         mlt_properties_set_int( still, "put_mode", 1 );
193
194         // Loop until told not to
195         while( this->running )
196         {
197                 // Get a frame from the attached producer
198                 frame = mlt_consumer_get_frame( consumer );
199
200                 // Ensure that we have a frame
201                 if ( frame != NULL )
202                 {
203                         // Get the speed of the frame
204                         double speed = mlt_properties_get_double( mlt_frame_properties( frame ), "_speed" );
205
206                         // Determine which speed to use
207                         double use_speed = first ? speed : this->last_speed;
208
209                         // Get changed requests to the preview (focus changes)
210                         int changed = mlt_properties_get_int( properties, "changed" );
211
212                         // Get refresh request for the current frame (effect changes in still mode)
213                         int refresh = mlt_properties_get_int( properties, "refresh" );
214
215                         // Decrement refresh and clear changed
216                         mlt_properties_set_int( properties, "refresh", refresh > 0 ? refresh - 1 : 0 );
217                         mlt_properties_set_int( properties, "changed", 0 );
218
219                         // Set the changed property on this frame for the benefit of still
220                         mlt_properties_set_int( mlt_frame_properties( frame ), "refresh", refresh );
221
222                         // Make sure the recipient knows that this frame isn't really rendered
223                         mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 0 );
224
225                         // If we're not the first frame and both consumers are stopped, then stop ourselves
226                         if ( !first && mlt_consumer_is_stopped( this->play ) && mlt_consumer_is_stopped( this->still ) )
227                         {
228                                 this->running = 0;
229                                 mlt_frame_close( frame );
230                         }
231                         // Allow a little grace time before switching consumers on speed changes
232                         else if ( this->ignore_change -- > 0 && this->active != NULL && !mlt_consumer_is_stopped( this->active ) )
233                         {
234                                 mlt_consumer_put_frame( this->active, frame );
235                                 mlt_properties_set_int( still, "changed", changed );
236                         }
237                         // If we aren't playing normally, then use the still
238                         else if ( use_speed != 1 )
239                         {
240                                 if ( !mlt_consumer_is_stopped( this->play ) )
241                                         mlt_consumer_stop( this->play );
242                                 if ( mlt_consumer_is_stopped( this->still ) )
243                                 {
244                                         this->last_speed = use_speed;
245                                         this->active = this->still;
246                                         this->ignore_change = 5;
247                                         mlt_consumer_start( this->still );
248                                 }
249                                 mlt_properties_set_int( still, "changed", changed );
250                                 mlt_consumer_put_frame( this->still, frame );
251                         }
252                         // Otherwise use the normal player
253                         else
254                         {
255                                 if ( !mlt_consumer_is_stopped( this->still ) )
256                                         mlt_consumer_stop( this->still );
257                                 if ( mlt_consumer_is_stopped( this->play ) )
258                                 {
259                                         this->last_speed = use_speed;
260                                         this->active = this->play;
261                                         this->ignore_change = 25;
262                                         mlt_consumer_start( this->play );
263                                 }
264                                 mlt_properties_set_int( still, "changed", changed );
265                                 mlt_consumer_put_frame( this->play, frame );
266                         }
267
268                         // Copy the rectangle info from the active consumer
269                         if ( this->running )
270                         {
271                                 mlt_properties active = mlt_consumer_properties( this->active );
272                                 mlt_properties_set_int( properties, "rect_x", mlt_properties_get_int( active, "rect_x" ) );
273                                 mlt_properties_set_int( properties, "rect_y", mlt_properties_get_int( active, "rect_y" ) );
274                                 mlt_properties_set_int( properties, "rect_w", mlt_properties_get_int( active, "rect_w" ) );
275                                 mlt_properties_set_int( properties, "rect_h", mlt_properties_get_int( active, "rect_h" ) );
276                         }
277
278                         // We are definitely not waiting on the first frame any more
279                         first = 0;
280                 }
281         }
282
283         mlt_consumer_stop( this->play );
284         mlt_consumer_stop( this->still );
285
286         SDL_Quit( );
287
288         return NULL;
289 }
290
291 /** Callback to allow override of the close method.
292 */
293
294 static void consumer_close( mlt_consumer parent )
295 {
296         // Get the actual object
297         consumer_sdl this = parent->child;
298
299         // Stop the consumer
300         mlt_consumer_stop( parent );
301
302         // Now clean up the rest
303         mlt_consumer_close( parent );
304
305         // Close the child consumers
306         mlt_consumer_close( this->play );
307         mlt_consumer_close( this->still );
308
309         // Finally clean up this
310         free( this );
311 }