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 // Default to environment test card
82 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
84 // Hmm - default all consumers to yuv422 :-/
85 this->format
= mlt_image_yuv422
;
90 /** Create a new consumer.
93 mlt_consumer
mlt_consumer_new( )
95 // Create the memory for the structure
96 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
100 mlt_consumer_init( this, NULL
);
106 /** Get the parent service object.
109 mlt_service
mlt_consumer_service( mlt_consumer
this )
111 return &this->parent
;
114 /** Get the consumer properties.
117 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
119 return mlt_service_properties( &this->parent
);
122 /** Connect the consumer to the producer.
125 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
127 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
130 /** Start the consumer.
133 int mlt_consumer_start( mlt_consumer
this )
136 mlt_properties properties
= mlt_consumer_properties( this );
138 // Determine if there's a test card producer
139 char *test_card
= mlt_properties_get( properties
, "test_card" );
142 if ( test_card
!= NULL
)
144 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
146 // Create a test card producer
147 mlt_producer producer
= mlt_factory_producer( NULL
, test_card
);
149 // Do we have a producer
150 if ( producer
!= NULL
)
152 // Test card should loop I guess...
153 mlt_properties_set( mlt_producer_properties( producer
), "eof", "loop" );
155 // Set the test card on the consumer
156 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
159 // Check and run an ante command
160 if ( mlt_properties_get( properties
, "ante" ) )
161 system( mlt_properties_get( properties
, "ante" ) );
166 // Allow the hash table to speed things up
167 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
170 // Set the real_time preference
171 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
174 if ( this->start
!= NULL
)
175 return this->start( this );
180 /** Protected method for consumer to get frames from connected service
183 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
186 mlt_frame frame
= NULL
;
188 // Get the service assoicated to the consumer
189 mlt_service service
= mlt_consumer_service( this );
192 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
194 // Get the consumer properties
195 mlt_properties properties
= mlt_consumer_properties( this );
197 // Get the frame properties
198 mlt_properties frame_properties
= mlt_frame_properties( frame
);
200 // Get the test card producer
201 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
203 // Attach the test frame producer to it.
204 if ( test_card
!= NULL
)
205 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
207 // Attach the rescale property
208 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
210 // Aspect ratio and other jiggery pokery
211 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
212 mlt_properties_set_int( frame_properties
, "consumer_progressive", mlt_properties_get_int( properties
, "progressive" ) );
213 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "deinterlace" ) );
220 static inline long time_difference( struct timeval
*time1
)
222 struct timeval time2
;
223 time2
.tv_sec
= time1
->tv_sec
;
224 time2
.tv_usec
= time1
->tv_usec
;
225 gettimeofday( time1
, NULL
);
226 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
229 static void *consumer_read_ahead_thread( void *arg
)
231 // The argument is the consumer
232 mlt_consumer
this = arg
;
234 // Get the properties of the consumer
235 mlt_properties properties
= mlt_consumer_properties( this );
237 // Get the width and height
238 int width
= mlt_properties_get_int( properties
, "width" );
239 int height
= mlt_properties_get_int( properties
, "height" );
241 // See if video is turned off
242 int video_off
= mlt_properties_get_int( properties
, "video_off" );
244 // Get the audio settings
245 mlt_audio_format afmt
= mlt_audio_pcm
;
247 double fps
= mlt_properties_get_double( properties
, "fps" );
248 int channels
= mlt_properties_get_int( properties
, "channels" );
249 int frequency
= mlt_properties_get_int( properties
, "frequency" );
253 // See if audio is turned off
254 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
256 // Get the maximum size of the buffer
257 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
259 // General frame variable
260 mlt_frame frame
= NULL
;
261 uint8_t *image
= NULL
;
266 // Average time for get_frame and get_image
269 int64_t time_wait
= 0;
270 int64_t time_frame
= 0;
271 int64_t time_image
= 0;
273 // Get the first frame
274 frame
= mlt_consumer_get_frame( this );
276 // Get the image of the first frame
278 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
282 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
283 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
284 frame
->get_audio
= NULL
;
287 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 1 );
289 // Get the starting time (can ignore the times above)
290 gettimeofday( &ante
, NULL
);
292 // Continue to read ahead
293 while ( this->ahead
)
295 // Put the current frame into the queue
296 time_difference( &ante
);
297 pthread_mutex_lock( &this->mutex
);
298 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
299 pthread_cond_wait( &this->cond
, &this->mutex
);
300 mlt_deque_push_back( this->queue
, frame
);
301 pthread_cond_broadcast( &this->cond
);
302 pthread_mutex_unlock( &this->mutex
);
303 time_wait
= time_difference( &ante
);
305 // Get the next frame
306 frame
= mlt_consumer_get_frame( this );
307 time_frame
+= time_difference( &ante
);
309 // Increment the count
312 // All non normal playback frames should be shown
313 if ( mlt_properties_get_int( mlt_frame_properties( frame
), "_speed" ) != 1 )
322 if ( ( time_frame
+ time_image
) / count
< 40000 )
324 // Get the image, mark as rendered and time it
326 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
327 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 1 );
329 // Reset the skipped count
334 // Increment the number of sequentially skipped frames
337 // If we've reached an unacceptable level, reset everything
347 // Always process audio
350 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
351 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
352 frame
->get_audio
= NULL
;
355 // Increment the time take for this frame
356 time_image
+= time_difference( &ante
);
359 // Remove the last frame
360 mlt_frame_close( frame
);
365 static void consumer_read_ahead_start( mlt_consumer
this )
367 pthread_attr_t thread_attributes
;
372 // Create the frame queue
373 this->queue
= mlt_deque_init( );
376 pthread_mutex_init( &this->mutex
, NULL
);
378 // Create the condition
379 pthread_cond_init( &this->cond
, NULL
);
381 // Inherit the scheduling priority
382 pthread_attr_init( &thread_attributes
);
383 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
385 // Create the read ahead
386 pthread_create( &this->ahead_thread
, &thread_attributes
, consumer_read_ahead_thread
, this );
389 static void consumer_read_ahead_stop( mlt_consumer
this )
391 // Make sure we're running
394 // Inform thread to stop
397 // Broadcast to the condition in case it's waiting
398 pthread_mutex_lock( &this->mutex
);
399 pthread_cond_broadcast( &this->cond
);
400 pthread_mutex_unlock( &this->mutex
);
403 pthread_join( this->ahead_thread
, NULL
);
406 pthread_mutex_destroy( &this->mutex
);
408 // Destroy the condition
409 pthread_cond_destroy( &this->cond
);
412 while ( mlt_deque_count( this->queue
) )
413 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
416 mlt_deque_close( this->queue
);
420 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
423 mlt_frame frame
= NULL
;
425 // Get the properties
426 mlt_properties properties
= mlt_consumer_properties( this );
428 // Check if the user has requested real time or not
429 if ( this->real_time
)
433 // Is the read ahead running?
434 if ( this->ahead
== 0 )
436 int buffer
= mlt_properties_get_int( properties
, "buffer" );
437 consumer_read_ahead_start( this );
442 // Get frame from queue
443 pthread_mutex_lock( &this->mutex
);
444 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
445 pthread_cond_wait( &this->cond
, &this->mutex
);
446 frame
= mlt_deque_pop_front( this->queue
);
447 pthread_cond_broadcast( &this->cond
);
448 pthread_mutex_unlock( &this->mutex
);
452 // Get the frame in non real time
453 frame
= mlt_consumer_get_frame( this );
455 // This isn't true, but from the consumers perspective it is
456 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 1 );
462 /** Stop the consumer.
465 int mlt_consumer_stop( mlt_consumer
this )
468 mlt_properties properties
= mlt_consumer_properties( this );
471 if ( this->stop
!= NULL
)
474 // Check if the user has requested real time or not and stop if necessary
475 if ( mlt_properties_get_int( properties
, "real_time" ) )
476 consumer_read_ahead_stop( this );
478 // Kill the test card
479 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
481 // Check and run a post command
482 if ( mlt_properties_get( properties
, "post" ) )
483 system( mlt_properties_get( properties
, "post" ) );
488 /** Determine if the consumer is stopped.
491 int mlt_consumer_is_stopped( mlt_consumer
this )
493 // Check if the consumer is stopped
494 if ( this->is_stopped
!= NULL
)
495 return this->is_stopped( this );
500 /** Close the consumer.
503 void mlt_consumer_close( mlt_consumer
this )
505 // Get the childs close function
506 void ( *consumer_close
)( ) = this->close
;
508 // Make sure it only gets called once
511 // Call the childs close if available
512 if ( consumer_close
!= NULL
)
513 consumer_close( this );
515 mlt_service_close( &this->parent
);