2 * mlt_consumer.c -- abstraction for all consumer services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
31 /** Public final methods
34 int mlt_consumer_init( mlt_consumer this, void *child )
37 memset( this, 0, sizeof( struct mlt_consumer_s ) );
39 error = mlt_service_init( &this->parent, this );
42 // Get the properties from the service
43 mlt_properties properties = mlt_service_properties( &this->parent );
45 // Get the normalisation preference
46 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
48 // Deal with normalisation
49 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
51 mlt_properties_set( properties, "normalisation", "PAL" );
52 mlt_properties_set_double( properties, "fps", 25.0 );
53 mlt_properties_set_int( properties, "width", 720 );
54 mlt_properties_set_int( properties, "height", 576 );
55 mlt_properties_set_int( properties, "progressive", 0 );
59 mlt_properties_set( properties, "normalisation", "NTSC" );
60 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
61 mlt_properties_set_int( properties, "width", 720 );
62 mlt_properties_set_int( properties, "height", 480 );
63 mlt_properties_set_int( properties, "progressive", 0 );
65 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
67 // Default rescaler for all consumers
68 mlt_properties_set( properties, "rescale", "bilinear" );
73 /** Get the parent service object.
76 mlt_service mlt_consumer_service( mlt_consumer this )
81 /** Get the consumer properties.
84 mlt_properties mlt_consumer_properties( mlt_consumer this )
86 return mlt_service_properties( &this->parent );
89 /** Connect the consumer to the producer.
92 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
94 return mlt_service_connect_producer( &this->parent, producer, 0 );
97 /** Start the consumer.
100 int mlt_consumer_start( mlt_consumer this )
103 mlt_properties properties = mlt_consumer_properties( this );
105 // Determine if there's a test card producer
106 char *test_card = mlt_properties_get( properties, "test_card" );
109 if ( test_card != NULL )
111 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
113 // Create a test card producer
114 // TODO: do we want to use fezzik here?
115 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
117 // Do we have a producer
118 if ( producer != NULL )
120 // Test card should loop I guess...
121 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
123 // Set the test card on the consumer
124 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
127 // Check and run an ante command
128 if ( mlt_properties_get( properties, "ante" ) )
129 system( mlt_properties_get( properties, "ante" ) );
134 // Allow the hash table to speed things up
135 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
139 if ( this->start != NULL )
140 return this->start( this );
145 /** Protected method :-/ for consumer to get frames from connected service
148 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
151 mlt_frame frame = NULL;
153 // Get the service assoicated to the consumer
154 mlt_service service = mlt_consumer_service( this );
157 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
159 // Get the consumer properties
160 mlt_properties properties = mlt_consumer_properties( this );
162 // Get the frame properties
163 mlt_properties frame_properties = mlt_frame_properties( frame );
165 // Get the test card producer
166 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
168 // Attach the test frame producer to it.
169 if ( test_card != NULL )
170 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
172 // Attach the rescale property
173 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
175 // Aspect ratio and other jiggery pokery
176 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
177 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
178 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
185 static inline long time_difference( struct timeval *time1 )
187 struct timeval time2;
188 time2.tv_sec = time1->tv_sec;
189 time2.tv_usec = time1->tv_usec;
190 gettimeofday( time1, NULL );
191 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
194 static void *consumer_read_ahead_thread( void *arg )
196 // The argument is the consumer
197 mlt_consumer this = arg;
199 // Get the properties of the consumer
200 mlt_properties properties = mlt_consumer_properties( this );
202 // Get the width and height
203 int width = mlt_properties_get_int( properties, "width" );
204 int height = mlt_properties_get_int( properties, "height" );
206 // General frame variable
207 mlt_frame frame = NULL;
208 uint8_t *image = NULL;
213 // Average time for get_frame and get_image
215 int64_t time_wait = 0;
216 int64_t time_frame = 0;
217 int64_t time_image = 0;
219 // Get the first frame
220 gettimeofday( &ante, NULL );
221 frame = mlt_consumer_get_frame( this );
222 time_frame = time_difference( &ante );
224 // Get the image of the first frame
225 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
226 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
227 time_image = time_difference( &ante );
229 // Continue to read ahead
230 while ( this->ahead )
232 // Put the current frame into the queue
233 pthread_mutex_lock( &this->mutex );
234 while( this->ahead && mlt_deque_count( this->queue ) >= 25 )
235 pthread_cond_wait( &this->cond, &this->mutex );
236 mlt_deque_push_back( this->queue, frame );
237 pthread_cond_broadcast( &this->cond );
238 pthread_mutex_unlock( &this->mutex );
239 time_wait += time_difference( &ante );
241 // Get the next frame
242 frame = mlt_consumer_get_frame( this );
243 time_frame += time_difference( &ante );
245 // Increment the count
249 if ( ( time_frame + time_image + time_wait ) / count < 40000 )
251 // Get the image, mark as rendered and time it
252 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
253 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
254 time_image += time_difference( &ante );
258 // This is wrong, but it avoids slower machines getting starved
259 // It is, after all, impossible to go back in time :-/
260 time_image -= ( time_image / count / 8 );
264 // Remove the last frame
265 mlt_frame_close( frame );
270 static void consumer_read_ahead_start( mlt_consumer this )
275 // Create the frame queue
276 this->queue = mlt_deque_init( );
279 pthread_mutex_init( &this->mutex, NULL );
281 // Create the condition
282 pthread_cond_init( &this->cond, NULL );
284 // Create the read ahead
285 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
289 static void consumer_read_ahead_stop( mlt_consumer this )
291 // Make sure we're running
294 // Inform thread to stop
297 // Broadcast to the condition in case it's waiting
298 pthread_mutex_lock( &this->mutex );
299 pthread_cond_broadcast( &this->cond );
300 pthread_mutex_unlock( &this->mutex );
303 pthread_join( this->ahead_thread, NULL );
306 pthread_mutex_destroy( &this->mutex );
308 // Destroy the condition
309 pthread_cond_destroy( &this->cond );
312 while ( mlt_deque_count( this->queue ) )
313 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
317 mlt_frame mlt_consumer_rt_frame( mlt_consumer this, mlt_image_format format )
320 mlt_frame frame = NULL;
324 // Is the read ahead running?
325 if ( this->ahead == 0 )
327 this->format = format;
328 consumer_read_ahead_start( this );
332 // Get frame from queue
333 pthread_mutex_lock( &this->mutex );
334 while( this->ahead && mlt_deque_count( this->queue ) < size )
335 pthread_cond_wait( &this->cond, &this->mutex );
336 frame = mlt_deque_pop_front( this->queue );
337 pthread_cond_broadcast( &this->cond );
338 pthread_mutex_unlock( &this->mutex );
343 /** Stop the consumer.
346 int mlt_consumer_stop( mlt_consumer this )
349 mlt_properties properties = mlt_consumer_properties( this );
352 if ( this->stop != NULL )
355 // Kill the read ahead
356 consumer_read_ahead_stop( this );
358 // Kill the test card
359 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
361 // Check and run a post command
362 if ( mlt_properties_get( properties, "post" ) )
363 system( mlt_properties_get( properties, "post" ) );
368 /** Determine if the consumer is stopped.
371 int mlt_consumer_is_stopped( mlt_consumer this )
373 // Check if the consumer is stopped
374 if ( this->is_stopped != NULL )
375 return this->is_stopped( this );
380 /** Close the consumer.
383 void mlt_consumer_close( mlt_consumer this )
385 // Get the childs close function
386 void ( *consumer_close )( ) = this->close;
388 // Make sure it only gets called once
391 // Call the childs close if available
392 if ( consumer_close != NULL )
393 consumer_close( this );
395 mlt_service_close( &this->parent );