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 // Default audio frequency and channels
75 mlt_properties_set_int( properties, "frequency", 48000 );
76 mlt_properties_set_int( properties, "channels", 2 );
78 // Default of all consumers is real time
79 mlt_properties_set_int( properties, "real_time", 1 );
81 // Hmm - default all consumers to yuv422 :-/
82 this->format = mlt_image_yuv422;
87 /** Create a new consumer.
90 mlt_consumer mlt_consumer_new( )
92 // Create the memory for the structure
93 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
97 mlt_consumer_init( this, NULL );
103 /** Get the parent service object.
106 mlt_service mlt_consumer_service( mlt_consumer this )
108 return &this->parent;
111 /** Get the consumer properties.
114 mlt_properties mlt_consumer_properties( mlt_consumer this )
116 return mlt_service_properties( &this->parent );
119 /** Connect the consumer to the producer.
122 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
124 return mlt_service_connect_producer( &this->parent, producer, 0 );
127 /** Start the consumer.
130 int mlt_consumer_start( mlt_consumer this )
133 mlt_properties properties = mlt_consumer_properties( this );
135 // Determine if there's a test card producer
136 char *test_card = mlt_properties_get( properties, "test_card" );
139 if ( test_card != NULL )
141 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
143 // Create a test card producer
144 // TODO: do we want to use fezzik here?
145 mlt_producer producer = mlt_factory_producer( "fezzik", test_card );
147 // Do we have a producer
148 if ( producer != NULL )
150 // Test card should loop I guess...
151 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
153 // Set the test card on the consumer
154 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
157 // Check and run an ante command
158 if ( mlt_properties_get( properties, "ante" ) )
159 system( mlt_properties_get( properties, "ante" ) );
164 // Allow the hash table to speed things up
165 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
169 if ( this->start != NULL )
170 return this->start( this );
175 /** Protected method :-/ for consumer to get frames from connected service
178 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
181 mlt_frame frame = NULL;
183 // Get the service assoicated to the consumer
184 mlt_service service = mlt_consumer_service( this );
187 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
189 // Get the consumer properties
190 mlt_properties properties = mlt_consumer_properties( this );
192 // Get the frame properties
193 mlt_properties frame_properties = mlt_frame_properties( frame );
195 // Get the test card producer
196 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
198 // Attach the test frame producer to it.
199 if ( test_card != NULL )
200 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
202 // Attach the rescale property
203 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
205 // Aspect ratio and other jiggery pokery
206 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
207 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
208 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
215 static inline long time_difference( struct timeval *time1 )
217 struct timeval time2;
218 time2.tv_sec = time1->tv_sec;
219 time2.tv_usec = time1->tv_usec;
220 gettimeofday( time1, NULL );
221 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
224 static void *consumer_read_ahead_thread( void *arg )
226 // The argument is the consumer
227 mlt_consumer this = arg;
229 // Get the properties of the consumer
230 mlt_properties properties = mlt_consumer_properties( this );
232 // Get the width and height
233 int width = mlt_properties_get_int( properties, "width" );
234 int height = mlt_properties_get_int( properties, "height" );
236 // Get the maximum size of the buffer
237 int buffer = mlt_properties_get_int( properties, "buffer" );
239 // General frame variable
240 mlt_frame frame = NULL;
241 uint8_t *image = NULL;
246 // Average time for get_frame and get_image
249 int64_t time_wait = 0;
250 int64_t time_frame = 0;
251 int64_t time_image = 0;
253 // Get the first frame
254 frame = mlt_consumer_get_frame( this );
256 // Get the image of the first frame
257 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
258 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
260 // Get the starting time (can ignore the times above)
261 gettimeofday( &ante, NULL );
263 // Continue to read ahead
264 while ( this->ahead )
266 // Put the current frame into the queue
267 pthread_mutex_lock( &this->mutex );
268 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
269 pthread_cond_wait( &this->cond, &this->mutex );
270 mlt_deque_push_back( this->queue, frame );
271 pthread_cond_broadcast( &this->cond );
272 pthread_mutex_unlock( &this->mutex );
273 time_wait += time_difference( &ante );
275 // Get the next frame
276 frame = mlt_consumer_get_frame( this );
277 time_frame += time_difference( &ante );
279 // Increment the count
283 if ( ( time_frame + time_image ) / count < ( 40000 - ( time_wait / count ) ) )
285 // Get the image, mark as rendered and time it
286 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
287 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
288 time_image += time_difference( &ante );
290 // Reset the skipped count
295 // Increment the number of sequentially skipped frames
298 // If we've reached an unacceptable level, reset everything
310 // Remove the last frame
311 mlt_frame_close( frame );
316 static void consumer_read_ahead_start( mlt_consumer this )
318 pthread_attr_t thread_attributes;
323 // Create the frame queue
324 this->queue = mlt_deque_init( );
327 pthread_mutex_init( &this->mutex, NULL );
329 // Create the condition
330 pthread_cond_init( &this->cond, NULL );
332 // Inherit the scheduling priority
333 pthread_attr_init( &thread_attributes );
334 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
336 // Create the read ahead
337 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
341 static void consumer_read_ahead_stop( mlt_consumer this )
343 // Make sure we're running
346 // Inform thread to stop
349 // Broadcast to the condition in case it's waiting
350 pthread_mutex_lock( &this->mutex );
351 pthread_cond_broadcast( &this->cond );
352 pthread_mutex_unlock( &this->mutex );
355 pthread_join( this->ahead_thread, NULL );
358 pthread_mutex_destroy( &this->mutex );
360 // Destroy the condition
361 pthread_cond_destroy( &this->cond );
364 while ( mlt_deque_count( this->queue ) )
365 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
369 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
372 mlt_frame frame = NULL;
374 // Get the properties
375 mlt_properties properties = mlt_consumer_properties( this );
377 // Check if the user has requested real time or not
378 if ( mlt_properties_get_int( properties, "real_time" ) )
382 // Is the read ahead running?
383 if ( this->ahead == 0 )
385 int buffer = mlt_properties_get_int( properties, "buffer" );
386 consumer_read_ahead_start( this );
391 // Get frame from queue
392 pthread_mutex_lock( &this->mutex );
393 while( this->ahead && mlt_deque_count( this->queue ) < size )
394 pthread_cond_wait( &this->cond, &this->mutex );
395 frame = mlt_deque_pop_front( this->queue );
396 pthread_cond_broadcast( &this->cond );
397 pthread_mutex_unlock( &this->mutex );
401 // Get the frame in non real time
402 frame = mlt_consumer_get_frame( this );
404 // This isn't true, but from the consumers perspective it is
405 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
411 /** Stop the consumer.
414 int mlt_consumer_stop( mlt_consumer this )
417 mlt_properties properties = mlt_consumer_properties( this );
420 if ( this->stop != NULL )
423 // Check if the user has requested real time or not and stop if necessary
424 if ( mlt_properties_get_int( properties, "real_time" ) )
425 consumer_read_ahead_stop( this );
427 // Kill the test card
428 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
430 // Check and run a post command
431 if ( mlt_properties_get( properties, "post" ) )
432 system( mlt_properties_get( properties, "post" ) );
437 /** Determine if the consumer is stopped.
440 int mlt_consumer_is_stopped( mlt_consumer this )
442 // Check if the consumer is stopped
443 if ( this->is_stopped != NULL )
444 return this->is_stopped( this );
449 /** Close the consumer.
452 void mlt_consumer_close( mlt_consumer this )
454 // Get the childs close function
455 void ( *consumer_close )( ) = this->close;
457 // Make sure it only gets called once
460 // Call the childs close if available
461 if ( consumer_close != NULL )
462 consumer_close( this );
464 mlt_service_close( &this->parent );