2 * consumer_sdl_preview.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2004-2005 Ushodaya Enterprises Limited
4 * Author: Charles Yates
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.
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.
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.
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_factory.h>
24 #include <framework/mlt_producer.h>
28 #include <SDL/SDL_syswm.h>
30 typedef struct consumer_sdl_s
*consumer_sdl
;
34 struct mlt_consumer_s parent
;
46 /** Forward references to static functions.
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
);
57 mlt_consumer
consumer_sdl_preview_init( char *arg
)
59 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
60 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
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
;
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
);
80 void consumer_frame_show_cb( mlt_consumer sdl
, mlt_consumer parent
, mlt_frame frame
)
82 consumer_sdl
this = parent
->child
;
83 this->last_speed
= mlt_properties_get_double( mlt_frame_properties( frame
), "_speed" );
84 mlt_events_fire( mlt_consumer_properties( parent
), "consumer-frame-show", frame
, NULL
);
87 static void consumer_sdl_event_cb( mlt_consumer sdl
, mlt_consumer parent
, SDL_Event
*event
)
89 mlt_events_fire( mlt_consumer_properties( parent
), "consumer-sdl-event", event
, NULL
);
92 static int consumer_start( mlt_consumer parent
)
94 consumer_sdl
this = parent
->child
;
98 pthread_attr_t thread_attributes
;
100 consumer_stop( parent
);
104 this->last_speed
= 1;
106 // Inherit the scheduling priority
107 pthread_attr_init( &thread_attributes
);
108 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
110 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
116 static int consumer_stop( mlt_consumer parent
)
118 // Get the actual object
119 consumer_sdl
this = parent
->child
;
121 if ( this->joined
== 0 )
123 // Kill the thread and clean up
126 pthread_join( this->thread
, NULL
);
133 static int consumer_is_stopped( mlt_consumer parent
)
135 consumer_sdl
this = parent
->child
;
136 return !this->running
;
139 static void *consumer_thread( void *arg
)
142 consumer_sdl
this = arg
;
145 mlt_consumer consumer
= &this->parent
;
147 // internal intialization
149 mlt_frame frame
= NULL
;
152 mlt_properties properties
= mlt_consumer_properties( consumer
);
153 mlt_properties play
= mlt_consumer_properties( this->play
);
154 mlt_properties still
= mlt_consumer_properties( this->still
);
156 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
158 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
162 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
163 SDL_EnableUNICODE( 1 );
165 // Inform child consumers that we control the sdl
166 mlt_properties_set_int( play
, "sdl_started", 1 );
167 mlt_properties_set_int( still
, "sdl_started", 1 );
169 // Pass properties down
170 mlt_properties_set_data( play
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
171 mlt_properties_set_data( still
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
172 mlt_properties_set_data( play
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
173 mlt_properties_set_data( still
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
174 mlt_properties_set( play
, "rescale", mlt_properties_get( properties
, "rescale" ) );
175 mlt_properties_set( still
, "rescale", mlt_properties_get( properties
, "rescale" ) );
176 mlt_properties_set( play
, "width", mlt_properties_get( properties
, "width" ) );
177 mlt_properties_set( still
, "width", mlt_properties_get( properties
, "width" ) );
178 mlt_properties_set( play
, "height", mlt_properties_get( properties
, "height" ) );
179 mlt_properties_set( still
, "height", mlt_properties_get( properties
, "height" ) );
181 mlt_properties_pass( play
, mlt_consumer_properties( consumer
), "play." );
182 mlt_properties_pass( still
, mlt_consumer_properties( consumer
), "still." );
184 mlt_properties_set_data( play
, "app_lock", mlt_properties_get_data( properties
, "app_lock", NULL
), 0, NULL
, NULL
);
185 mlt_properties_set_data( still
, "app_lock", mlt_properties_get_data( properties
, "app_lock", NULL
), 0, NULL
, NULL
);
186 mlt_properties_set_data( play
, "app_unlock", mlt_properties_get_data( properties
, "app_unlock", NULL
), 0, NULL
, NULL
);
187 mlt_properties_set_data( still
, "app_unlock", mlt_properties_get_data( properties
, "app_unlock", NULL
), 0, NULL
, NULL
);
189 mlt_properties_set_int( play
, "put_mode", 1 );
190 mlt_properties_set_int( still
, "put_mode", 1 );
192 // Loop until told not to
193 while( this->running
)
195 // Get a frame from the attached producer
196 frame
= mlt_consumer_get_frame( consumer
);
198 // Ensure that we have a frame
201 // Get the speed of the frame
202 double speed
= mlt_properties_get_double( mlt_frame_properties( frame
), "_speed" );
204 // Determine which speed to use
205 double use_speed
= first ? speed
: this->last_speed
;
207 // Get changed requests to the preview
208 int changed
= mlt_properties_get_int( properties
, "changed" );
209 mlt_properties_set_int( properties
, "changed", 0 );
211 // Make sure the recipient knows that this frame isn't really rendered
212 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 0 );
214 if ( !first
&& mlt_consumer_is_stopped( this->play
) && mlt_consumer_is_stopped( this->still
) )
217 mlt_frame_close( frame
);
219 else if ( this->ignore_change
-- > 0 && this->active
!= NULL
&& !mlt_consumer_is_stopped( this->active
) )
221 mlt_consumer_put_frame( this->active
, frame
);
222 if ( this->active
== this->still
)
223 mlt_properties_set_int( still
, "changed", changed
);
225 else if ( use_speed
!= 1 )
227 if ( !mlt_consumer_is_stopped( this->play
) )
229 mlt_consumer_stop( this->play
);
231 if ( mlt_consumer_is_stopped( this->still
) )
233 this->last_speed
= use_speed
;
234 this->active
= this->still
;
235 this->ignore_change
= 5;
236 mlt_consumer_start( this->still
);
238 mlt_properties_set_int( still
, "changed", changed
);
239 mlt_consumer_put_frame( this->still
, frame
);
243 if ( !mlt_consumer_is_stopped( this->still
) )
245 mlt_consumer_stop( this->still
);
247 if ( mlt_consumer_is_stopped( this->play
) )
249 this->last_speed
= use_speed
;
250 this->active
= this->play
;
251 this->ignore_change
= 25;
252 mlt_consumer_start( this->play
);
254 mlt_consumer_put_frame( this->play
, frame
);
260 mlt_consumer_stop( this->play
);
261 mlt_consumer_stop( this->still
);
268 /** Callback to allow override of the close method.
271 static void consumer_close( mlt_consumer parent
)
273 // Get the actual object
274 consumer_sdl
this = parent
->child
;
277 mlt_consumer_stop( parent
);
279 // Now clean up the rest
280 mlt_consumer_close( parent
);
282 // Close the child consumers
283 mlt_consumer_close( this->play
);
284 mlt_consumer_close( this->still
);
286 // Finally clean up this