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 );
56 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
60 mlt_properties_set( properties, "normalisation", "NTSC" );
61 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
62 mlt_properties_set_int( properties, "width", 720 );
63 mlt_properties_set_int( properties, "height", 480 );
64 mlt_properties_set_int( properties, "progressive", 0 );
65 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
68 // Default rescaler for all consumers
69 mlt_properties_set( properties, "rescale", "bilinear" );
71 // Default read ahead buffer size
72 mlt_properties_set_int( properties, "buffer", 25 );
74 // Hmm - default all consumers to yuv422 :-/
75 this->format = mlt_image_yuv422;
80 /** Create a new consumer.
83 mlt_consumer mlt_consumer_new( )
85 // Create the memory for the structure
86 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
90 mlt_consumer_init( this, NULL );
96 /** Get the parent service object.
99 mlt_service mlt_consumer_service( mlt_consumer this )
101 return &this->parent;
104 /** Get the consumer properties.
107 mlt_properties mlt_consumer_properties( mlt_consumer this )
109 return mlt_service_properties( &this->parent );
112 /** Connect the consumer to the producer.
115 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
117 return mlt_service_connect_producer( &this->parent, producer, 0 );
120 /** Start the consumer.
123 int mlt_consumer_start( mlt_consumer this )
126 mlt_properties properties = mlt_consumer_properties( this );
128 // Determine if there's a test card producer
129 char *test_card = mlt_properties_get( properties, "test_card" );
132 if ( test_card != NULL )
134 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
136 // Create a test card producer
137 // TODO: do we want to use fezzik here?
138 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
140 // Do we have a producer
141 if ( producer != NULL )
143 // Test card should loop I guess...
144 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
146 // Set the test card on the consumer
147 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
150 // Check and run an ante command
151 if ( mlt_properties_get( properties, "ante" ) )
152 system( mlt_properties_get( properties, "ante" ) );
157 // Allow the hash table to speed things up
158 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
162 if ( this->start != NULL )
163 return this->start( this );
168 /** Protected method :-/ for consumer to get frames from connected service
171 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
174 mlt_frame frame = NULL;
176 // Get the service assoicated to the consumer
177 mlt_service service = mlt_consumer_service( this );
180 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
182 // Get the consumer properties
183 mlt_properties properties = mlt_consumer_properties( this );
185 // Get the frame properties
186 mlt_properties frame_properties = mlt_frame_properties( frame );
188 // Get the test card producer
189 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
191 // Attach the test frame producer to it.
192 if ( test_card != NULL )
193 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
195 // Attach the rescale property
196 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
198 // Aspect ratio and other jiggery pokery
199 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
200 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
201 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
208 static inline long time_difference( struct timeval *time1 )
210 struct timeval time2;
211 time2.tv_sec = time1->tv_sec;
212 time2.tv_usec = time1->tv_usec;
213 gettimeofday( time1, NULL );
214 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
217 static void *consumer_read_ahead_thread( void *arg )
219 // The argument is the consumer
220 mlt_consumer this = arg;
222 // Get the properties of the consumer
223 mlt_properties properties = mlt_consumer_properties( this );
225 // Get the width and height
226 int width = mlt_properties_get_int( properties, "width" );
227 int height = mlt_properties_get_int( properties, "height" );
229 // Get the maximum size of the buffer
230 int buffer = mlt_properties_get_int( properties, "buffer" );
232 // General frame variable
233 mlt_frame frame = NULL;
234 uint8_t *image = NULL;
239 // Average time for get_frame and get_image
242 int64_t time_wait = 0;
243 int64_t time_frame = 0;
244 int64_t time_image = 0;
246 // Get the first frame
247 frame = mlt_consumer_get_frame( this );
249 // Get the image of the first frame
250 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
251 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
253 // Get the starting time (can ignore the times above)
254 gettimeofday( &ante, NULL );
256 // Continue to read ahead
257 while ( this->ahead )
259 // Put the current frame into the queue
260 pthread_mutex_lock( &this->mutex );
261 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
262 pthread_cond_wait( &this->cond, &this->mutex );
263 mlt_deque_push_back( this->queue, frame );
264 pthread_cond_broadcast( &this->cond );
265 pthread_mutex_unlock( &this->mutex );
266 time_wait += time_difference( &ante );
268 // Get the next frame
269 frame = mlt_consumer_get_frame( this );
270 time_frame += time_difference( &ante );
272 // Increment the count
276 if ( ( time_frame + time_image ) / count < ( 40000 - ( time_wait / count ) ) )
278 // Get the image, mark as rendered and time it
279 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
280 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
281 time_image += time_difference( &ante );
283 // Reset the skipped count
288 // Increment the number of sequentially skipped frames
291 // If we've reached an unacceptable level, reset everything
303 // Remove the last frame
304 mlt_frame_close( frame );
309 static void consumer_read_ahead_start( mlt_consumer this )
314 // Create the frame queue
315 this->queue = mlt_deque_init( );
318 pthread_mutex_init( &this->mutex, NULL );
320 // Create the condition
321 pthread_cond_init( &this->cond, NULL );
323 // Create the read ahead
324 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
328 static void consumer_read_ahead_stop( mlt_consumer this )
330 // Make sure we're running
333 // Inform thread to stop
336 // Broadcast to the condition in case it's waiting
337 pthread_mutex_lock( &this->mutex );
338 pthread_cond_broadcast( &this->cond );
339 pthread_mutex_unlock( &this->mutex );
342 pthread_join( this->ahead_thread, NULL );
345 pthread_mutex_destroy( &this->mutex );
347 // Destroy the condition
348 pthread_cond_destroy( &this->cond );
351 while ( mlt_deque_count( this->queue ) )
352 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
356 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
359 mlt_frame frame = NULL;
362 // Is the read ahead running?
363 if ( this->ahead == 0 )
365 int buffer = mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" );
366 consumer_read_ahead_start( this );
371 // Get frame from queue
372 pthread_mutex_lock( &this->mutex );
373 while( this->ahead && mlt_deque_count( this->queue ) < size )
374 pthread_cond_wait( &this->cond, &this->mutex );
375 frame = mlt_deque_pop_front( this->queue );
376 pthread_cond_broadcast( &this->cond );
377 pthread_mutex_unlock( &this->mutex );
382 /** Stop the consumer.
385 int mlt_consumer_stop( mlt_consumer this )
388 mlt_properties properties = mlt_consumer_properties( this );
391 if ( this->stop != NULL )
394 // Kill the read ahead
395 consumer_read_ahead_stop( this );
397 // Kill the test card
398 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
400 // Check and run a post command
401 if ( mlt_properties_get( properties, "post" ) )
402 system( mlt_properties_get( properties, "post" ) );
407 /** Determine if the consumer is stopped.
410 int mlt_consumer_is_stopped( mlt_consumer this )
412 // Check if the consumer is stopped
413 if ( this->is_stopped != NULL )
414 return this->is_stopped( this );
419 /** Close the consumer.
422 void mlt_consumer_close( mlt_consumer this )
424 // Get the childs close function
425 void ( *consumer_close )( ) = this->close;
427 // Make sure it only gets called once
430 // Call the childs close if available
431 if ( consumer_close != NULL )
432 consumer_close( this );
434 mlt_service_close( &this->parent );