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 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
32 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
34 /** Public final methods
37 int mlt_consumer_init( mlt_consumer
this, void *child
)
40 memset( this, 0, sizeof( struct mlt_consumer_s
) );
42 error
= mlt_service_init( &this->parent
, this );
45 // Get the properties from the service
46 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
48 // Get the normalisation preference
49 char *normalisation
= mlt_environment( "MLT_NORMALISATION" );
51 // Deal with normalisation
52 if ( normalisation
== NULL
|| strcmp( normalisation
, "NTSC" ) )
54 mlt_properties_set( properties
, "normalisation", "PAL" );
55 mlt_properties_set_double( properties
, "fps", 25.0 );
56 mlt_properties_set_int( properties
, "width", 720 );
57 mlt_properties_set_int( properties
, "height", 576 );
58 mlt_properties_set_int( properties
, "progressive", 0 );
59 mlt_properties_set_double( properties
, "aspect_ratio", 59.0 / 54.0 );
63 mlt_properties_set( properties
, "normalisation", "NTSC" );
64 mlt_properties_set_double( properties
, "fps", 30000.0 / 1001.0 );
65 mlt_properties_set_int( properties
, "width", 720 );
66 mlt_properties_set_int( properties
, "height", 480 );
67 mlt_properties_set_int( properties
, "progressive", 0 );
68 mlt_properties_set_double( properties
, "aspect_ratio", 10.0 / 11.0 );
71 // Default rescaler for all consumers
72 mlt_properties_set( properties
, "rescale", "bilinear" );
74 // Default read ahead buffer size
75 mlt_properties_set_int( properties
, "buffer", 25 );
77 // Default audio frequency and channels
78 mlt_properties_set_int( properties
, "frequency", 48000 );
79 mlt_properties_set_int( properties
, "channels", 2 );
81 // Default of all consumers is real time
82 mlt_properties_set_int( properties
, "real_time", 1 );
84 // Default to environment test card
85 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
87 // Hmm - default all consumers to yuv422 :-/
88 this->format
= mlt_image_yuv422
;
90 mlt_events_register( properties
, "consumer-frame-show", ( mlt_transmitter
)mlt_consumer_frame_show
);
91 mlt_events_register( properties
, "consumer-frame-render", ( mlt_transmitter
)mlt_consumer_frame_render
);
92 mlt_events_register( properties
, "consumer-stopped", NULL
);
94 // Create the push mutex and condition
95 pthread_mutex_init( &this->put_mutex
, NULL
);
96 pthread_cond_init( &this->put_cond
, NULL
);
102 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
104 if ( listener
!= NULL
)
105 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
108 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
110 if ( listener
!= NULL
)
111 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
114 /** Create a new consumer.
117 mlt_consumer
mlt_consumer_new( )
119 // Create the memory for the structure
120 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
124 mlt_consumer_init( this, NULL
);
130 /** Get the parent service object.
133 mlt_service
mlt_consumer_service( mlt_consumer
this )
135 return this != NULL ?
&this->parent
: NULL
;
138 /** Get the consumer properties.
141 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
143 return this != NULL ?
MLT_SERVICE_PROPERTIES( &this->parent
) : NULL
;
146 /** Connect the consumer to the producer.
149 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
151 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
154 /** Start the consumer.
157 int mlt_consumer_start( mlt_consumer
this )
160 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
162 // Determine if there's a test card producer
163 char *test_card
= mlt_properties_get( properties
, "test_card" );
165 // Just to make sure nothing is hanging around...
166 mlt_frame_close( this->put
);
168 this->put_active
= 1;
171 if ( test_card
!= NULL
)
173 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
175 // Create a test card producer
176 mlt_producer producer
= mlt_factory_producer( NULL
, test_card
);
178 // Do we have a producer
179 if ( producer
!= NULL
)
181 // Test card should loop I guess...
182 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
183 //mlt_producer_set_speed( producer, 0 );
184 //mlt_producer_set_in_and_out( producer, 0, 0 );
186 // Set the test card on the consumer
187 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
193 // Allow the hash table to speed things up
194 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
197 // Check and run an ante command
198 if ( mlt_properties_get( properties
, "ante" ) )
199 system( mlt_properties_get( properties
, "ante" ) );
201 // Set the real_time preference
202 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
205 if ( this->start
!= NULL
)
206 return this->start( this );
211 /** An alternative method to feed frames into the consumer - only valid if
212 the consumer itself is not connected.
215 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
219 // Get the service assoicated to the consumer
220 mlt_service service
= MLT_CONSUMER_SERVICE( this );
222 if ( mlt_service_producer( service
) == NULL
)
226 pthread_mutex_lock( &this->put_mutex
);
227 while ( this->put_active
&& this->put
!= NULL
)
229 gettimeofday( &now
, NULL
);
230 tm
.tv_sec
= now
.tv_sec
+ 1;
231 tm
.tv_nsec
= now
.tv_usec
* 1000;
232 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
234 if ( this->put_active
&& this->put
== NULL
)
237 mlt_frame_close( frame
);
238 pthread_cond_broadcast( &this->put_cond
);
239 pthread_mutex_unlock( &this->put_mutex
);
243 mlt_frame_close( frame
);
249 /** Protected method for consumer to get frames from connected service
252 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
255 mlt_frame frame
= NULL
;
257 // Get the service assoicated to the consumer
258 mlt_service service
= MLT_CONSUMER_SERVICE( this );
260 // Get the consumer properties
261 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
264 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
268 pthread_mutex_lock( &this->put_mutex
);
269 while ( this->put_active
&& this->put
== NULL
)
271 gettimeofday( &now
, NULL
);
272 tm
.tv_sec
= now
.tv_sec
+ 1;
273 tm
.tv_nsec
= now
.tv_usec
* 1000;
274 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
278 pthread_cond_broadcast( &this->put_cond
);
279 pthread_mutex_unlock( &this->put_mutex
);
281 mlt_service_apply_filters( service
, frame
, 0 );
283 else if ( mlt_service_producer( service
) != NULL
)
285 mlt_service_get_frame( service
, &frame
, 0 );
289 frame
= mlt_frame_init( );
294 // Get the frame properties
295 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
297 // Get the test card producer
298 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
300 // Attach the test frame producer to it.
301 if ( test_card
!= NULL
)
302 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
304 // Attach the rescale property
305 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
307 // Aspect ratio and other jiggery pokery
308 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
309 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
316 static inline long time_difference( struct timeval
*time1
)
318 struct timeval time2
;
319 time2
.tv_sec
= time1
->tv_sec
;
320 time2
.tv_usec
= time1
->tv_usec
;
321 gettimeofday( time1
, NULL
);
322 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
325 static void *consumer_read_ahead_thread( void *arg
)
327 // The argument is the consumer
328 mlt_consumer
this = arg
;
330 // Get the properties of the consumer
331 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
333 // Get the width and height
334 int width
= mlt_properties_get_int( properties
, "width" );
335 int height
= mlt_properties_get_int( properties
, "height" );
337 // See if video is turned off
338 int video_off
= mlt_properties_get_int( properties
, "video_off" );
340 // Get the audio settings
341 mlt_audio_format afmt
= mlt_audio_pcm
;
343 double fps
= mlt_properties_get_double( properties
, "fps" );
344 int channels
= mlt_properties_get_int( properties
, "channels" );
345 int frequency
= mlt_properties_get_int( properties
, "frequency" );
349 // See if audio is turned off
350 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
352 // Get the maximum size of the buffer
353 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
355 // General frame variable
356 mlt_frame frame
= NULL
;
357 uint8_t *image
= NULL
;
362 // Average time for get_frame and get_image
365 int64_t time_wait
= 0;
366 int64_t time_frame
= 0;
367 int64_t time_process
= 0;
369 mlt_service lock_object
= NULL
;
371 // Get the first frame
372 frame
= mlt_consumer_get_frame( this );
374 // Get the lock object
375 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
378 if ( lock_object
) mlt_service_lock( lock_object
);
380 // Get the image of the first frame
383 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
384 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
389 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
390 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
393 // Unlock the lock object
394 if ( lock_object
) mlt_service_unlock( lock_object
);
397 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
399 // Get the starting time (can ignore the times above)
400 gettimeofday( &ante
, NULL
);
402 // Continue to read ahead
403 while ( this->ahead
)
405 // Put the current frame into the queue
406 pthread_mutex_lock( &this->mutex
);
407 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
408 pthread_cond_wait( &this->cond
, &this->mutex
);
409 mlt_deque_push_back( this->queue
, frame
);
410 pthread_cond_broadcast( &this->cond
);
411 pthread_mutex_unlock( &this->mutex
);
413 time_wait
+= time_difference( &ante
);
415 // Get the next frame
416 frame
= mlt_consumer_get_frame( this );
417 time_frame
+= time_difference( &ante
);
419 // If there's no frame, we're probably stopped...
423 // Attempt to fetch the lock object
424 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
426 // Increment the count
429 // Lock if there's a lock object
430 if ( lock_object
) mlt_service_lock( lock_object
);
432 // All non normal playback frames should be shown
433 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
435 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
447 // Get the image, mark as rendered and time it
450 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
451 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
453 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
457 // Increment the number of sequentially skipped frames
461 // If we've reached an unacceptable level, reset everything
472 // Always process audio
475 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
476 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
479 // Increment the time take for this frame
480 time_process
+= time_difference( &ante
);
482 // Determine if the next frame should be skipped
483 if ( mlt_deque_count( this->queue
) <= 5 && ( ( time_wait
+ time_frame
+ time_process
) / count
) > 40000 )
486 // Unlock if there's a lock object
487 if ( lock_object
) mlt_service_unlock( lock_object
);
490 // Remove the last frame
491 mlt_frame_close( frame
);
496 static void consumer_read_ahead_start( mlt_consumer
this )
501 // Create the frame queue
502 this->queue
= mlt_deque_init( );
505 pthread_mutex_init( &this->mutex
, NULL
);
507 // Create the condition
508 pthread_cond_init( &this->cond
, NULL
);
510 // Create the read ahead
511 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
514 static void consumer_read_ahead_stop( mlt_consumer
this )
516 // Make sure we're running
519 // Inform thread to stop
522 // Broadcast to the condition in case it's waiting
523 pthread_mutex_lock( &this->mutex
);
524 pthread_cond_broadcast( &this->cond
);
525 pthread_mutex_unlock( &this->mutex
);
527 // Broadcast to the put condition in case it's waiting
528 pthread_mutex_lock( &this->put_mutex
);
529 pthread_cond_broadcast( &this->put_cond
);
530 pthread_mutex_unlock( &this->put_mutex
);
533 pthread_join( this->ahead_thread
, NULL
);
536 pthread_mutex_destroy( &this->mutex
);
538 // Destroy the condition
539 pthread_cond_destroy( &this->cond
);
542 while ( mlt_deque_count( this->queue
) )
543 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
546 mlt_deque_close( this->queue
);
550 void mlt_consumer_purge( mlt_consumer
this )
554 pthread_mutex_lock( &this->mutex
);
555 while ( mlt_deque_count( this->queue
) )
556 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
557 pthread_cond_broadcast( &this->cond
);
558 pthread_mutex_unlock( &this->mutex
);
562 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
565 mlt_frame frame
= NULL
;
567 // Get the properties
568 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
570 // Check if the user has requested real time or not
571 if ( this->real_time
)
575 // Is the read ahead running?
576 if ( this->ahead
== 0 )
578 int buffer
= mlt_properties_get_int( properties
, "buffer" );
579 int prefill
= mlt_properties_get_int( properties
, "prefill" );
580 consumer_read_ahead_start( this );
582 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
585 // Get frame from queue
586 pthread_mutex_lock( &this->mutex
);
587 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
588 pthread_cond_wait( &this->cond
, &this->mutex
);
589 frame
= mlt_deque_pop_front( this->queue
);
590 pthread_cond_broadcast( &this->cond
);
591 pthread_mutex_unlock( &this->mutex
);
595 // Get the frame in non real time
596 frame
= mlt_consumer_get_frame( this );
598 // This isn't true, but from the consumers perspective it is
600 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
606 /** Callback for the implementation to indicate a stopped condition.
609 void mlt_consumer_stopped( mlt_consumer
this )
611 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
612 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
615 /** Stop the consumer.
618 int mlt_consumer_stop( mlt_consumer
this )
621 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
622 char *debug
= mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
625 if ( debug
) fprintf( stderr
, "%s: stopping put waiting\n", debug
);
626 pthread_mutex_lock( &this->put_mutex
);
627 this->put_active
= 0;
628 pthread_cond_broadcast( &this->put_cond
);
629 pthread_mutex_unlock( &this->put_mutex
);
632 if ( debug
) fprintf( stderr
, "%s: stopping consumer\n", debug
);
633 if ( this->stop
!= NULL
)
636 // Check if the user has requested real time or not and stop if necessary
637 if ( debug
) fprintf( stderr
, "%s: stopping read_ahead\n", debug
);
638 if ( mlt_properties_get_int( properties
, "real_time" ) )
639 consumer_read_ahead_stop( this );
641 // Kill the test card
642 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
644 // Check and run a post command
645 if ( mlt_properties_get( properties
, "post" ) )
646 system( mlt_properties_get( properties
, "post" ) );
648 if ( debug
) fprintf( stderr
, "%s: stopped\n", debug
);
653 /** Determine if the consumer is stopped.
656 int mlt_consumer_is_stopped( mlt_consumer
this )
658 // Check if the consumer is stopped
659 if ( this->is_stopped
!= NULL
)
660 return this->is_stopped( this );
665 /** Close the consumer.
668 void mlt_consumer_close( mlt_consumer
this )
670 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
672 // Get the childs close function
673 void ( *consumer_close
)( ) = this->close
;
675 if ( consumer_close
)
678 //mlt_consumer_stop( this );
681 consumer_close( this );
685 // Make sure it only gets called once
686 this->parent
.close
= NULL
;
688 // Destroy the push mutex and condition
689 pthread_mutex_destroy( &this->put_mutex
);
690 pthread_cond_destroy( &this->put_cond
);
692 mlt_service_close( &this->parent
);