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
;
44 /** Forward references to static functions.
47 static int consumer_start( mlt_consumer parent
);
48 static int consumer_stop( mlt_consumer parent
);
49 static int consumer_is_stopped( mlt_consumer parent
);
50 static void consumer_close( mlt_consumer parent
);
51 static void *consumer_thread( void * );
52 static void consumer_frame_show_cb( mlt_consumer sdl
, mlt_consumer
this, mlt_frame frame
);
54 mlt_consumer
consumer_sdl_preview_init( char *arg
)
56 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
57 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
59 // Get the parent consumer object
60 mlt_consumer parent
= &this->parent
;
61 this->play
= mlt_factory_consumer( "sdl", arg
);
62 this->still
= mlt_factory_consumer( "sdl_still", arg
);
63 mlt_properties_set( mlt_consumer_properties( parent
), "real_time", "0" );
64 parent
->close
= consumer_close
;
65 parent
->start
= consumer_start
;
66 parent
->stop
= consumer_stop
;
67 parent
->is_stopped
= consumer_is_stopped
;
69 mlt_events_listen( mlt_consumer_properties( this->play
), this, "consumer-frame-show", ( mlt_listener
)consumer_frame_show_cb
);
70 mlt_events_listen( mlt_consumer_properties( this->still
), this, "consumer-frame-show", ( mlt_listener
)consumer_frame_show_cb
);
77 void consumer_frame_show_cb( mlt_consumer sdl
, mlt_consumer parent
, mlt_frame frame
)
79 consumer_sdl
this = parent
->child
;
80 this->last_speed
= mlt_properties_get_double( mlt_frame_properties( frame
), "_speed" );
81 mlt_events_fire( mlt_consumer_properties( parent
), "consumer-frame-show", frame
, NULL
);
84 static int consumer_start( mlt_consumer parent
)
86 consumer_sdl
this = parent
->child
;
90 pthread_attr_t thread_attributes
;
92 consumer_stop( parent
);
98 // Inherit the scheduling priority
99 pthread_attr_init( &thread_attributes
);
100 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
102 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
108 static int consumer_stop( mlt_consumer parent
)
110 // Get the actual object
111 consumer_sdl
this = parent
->child
;
113 if ( this->joined
== 0 )
115 // Kill the thread and clean up
118 pthread_join( this->thread
, NULL
);
125 static int consumer_is_stopped( mlt_consumer parent
)
127 consumer_sdl
this = parent
->child
;
128 return !this->running
;
131 static void *consumer_thread( void *arg
)
134 consumer_sdl
this = arg
;
137 mlt_consumer consumer
= &this->parent
;
139 // internal intialization
141 mlt_frame frame
= NULL
;
144 mlt_properties properties
= mlt_consumer_properties( consumer
);
145 mlt_properties play
= mlt_consumer_properties( this->play
);
146 mlt_properties still
= mlt_consumer_properties( this->still
);
148 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
150 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
154 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
155 SDL_EnableUNICODE( 1 );
157 // Inform child consumers that we control the sdl
158 mlt_properties_set_int( play
, "sdl_started", 1 );
159 mlt_properties_set_int( still
, "sdl_started", 1 );
161 // Pass properties down
162 mlt_properties_set_data( play
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
163 mlt_properties_set_data( still
, "transport_producer", mlt_properties_get_data( properties
, "transport_producer", NULL
), 0, NULL
, NULL
);
164 mlt_properties_set_data( play
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
165 mlt_properties_set_data( still
, "transport_callback", mlt_properties_get_data( properties
, "transport_callback", NULL
), 0, NULL
, NULL
);
166 mlt_properties_set( play
, "rescale", mlt_properties_get( properties
, "rescale" ) );
167 mlt_properties_set( still
, "rescale", mlt_properties_get( properties
, "rescale" ) );
168 mlt_properties_set( play
, "width", mlt_properties_get( properties
, "width" ) );
169 mlt_properties_set( still
, "width", mlt_properties_get( properties
, "width" ) );
170 mlt_properties_set( play
, "height", mlt_properties_get( properties
, "height" ) );
171 mlt_properties_set( still
, "height", mlt_properties_get( properties
, "height" ) );
173 mlt_properties_pass( play
, mlt_consumer_properties( consumer
), "play." );
174 mlt_properties_pass( still
, mlt_consumer_properties( consumer
), "still." );
176 // Loop until told not to
177 while( this->running
)
179 // Get a frame from the attached producer
180 frame
= mlt_consumer_get_frame( consumer
);
182 // Ensure that we have a frame
185 // Get the speed of the frame
186 double speed
= mlt_properties_get_double( mlt_frame_properties( frame
), "_speed" );
188 // Make sure the recipient knows that this frame isn't really rendered
189 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 0 );
191 if ( !first
&& mlt_consumer_is_stopped( this->play
) && mlt_consumer_is_stopped( this->still
) )
194 mlt_frame_close( frame
);
196 else if ( this->last_speed
!= 1 )
198 if ( !mlt_consumer_is_stopped( this->play
) )
199 mlt_consumer_stop( this->play
);
200 if ( mlt_consumer_is_stopped( this->still
) )
202 this->last_speed
= speed
;
203 mlt_consumer_start( this->still
);
205 mlt_consumer_put_frame( this->still
, frame
);
209 if ( !mlt_consumer_is_stopped( this->still
) )
210 mlt_consumer_stop( this->still
);
211 if ( mlt_consumer_is_stopped( this->play
) )
213 this->last_speed
= speed
;
214 mlt_consumer_start( this->play
);
216 mlt_consumer_put_frame( this->play
, frame
);
222 mlt_consumer_stop( this->play
);
223 mlt_consumer_stop( this->still
);
230 /** Callback to allow override of the close method.
233 static void consumer_close( mlt_consumer parent
)
235 // Get the actual object
236 consumer_sdl
this = parent
->child
;
239 mlt_consumer_stop( parent
);
241 // Now clean up the rest
242 mlt_consumer_close( parent
);
244 // Close the child consumers
245 mlt_consumer_close( this->play
);
246 mlt_consumer_close( this->still
);
248 // Finally clean up this