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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, 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"
26 #include "mlt_profile.h"
33 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
34 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
35 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
);
36 static void apply_profile_properties( mlt_profile profile
, mlt_properties properties
);
38 static mlt_event g_event_listener
= NULL
;
40 /** Public final methods
43 int mlt_consumer_init( mlt_consumer
this, void *child
)
46 memset( this, 0, sizeof( struct mlt_consumer_s
) );
48 error
= mlt_service_init( &this->parent
, this );
51 // Get the properties from the service
52 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
54 // Apply the profile to properties for legacy integration
55 apply_profile_properties( mlt_profile_get(), properties
);
57 // Default rescaler for all consumers
58 mlt_properties_set( properties
, "rescale", "bilinear" );
60 // Default read ahead buffer size
61 mlt_properties_set_int( properties
, "buffer", 25 );
63 // Default audio frequency and channels
64 mlt_properties_set_int( properties
, "frequency", 48000 );
65 mlt_properties_set_int( properties
, "channels", 2 );
67 // Default of all consumers is real time
68 mlt_properties_set_int( properties
, "real_time", 1 );
70 // Default to environment test card
71 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
73 // Hmm - default all consumers to yuv422 :-/
74 this->format
= mlt_image_yuv422
;
76 mlt_events_register( properties
, "consumer-frame-show", ( mlt_transmitter
)mlt_consumer_frame_show
);
77 mlt_events_register( properties
, "consumer-frame-render", ( mlt_transmitter
)mlt_consumer_frame_render
);
78 mlt_events_register( properties
, "consumer-stopped", NULL
);
80 // Register a property-changed listener to handle the profile property -
81 // subsequent properties can override the profile
82 g_event_listener
= mlt_events_listen( properties
, this, "property-changed", ( mlt_listener
)mlt_consumer_property_changed
);
84 // Create the push mutex and condition
85 pthread_mutex_init( &this->put_mutex
, NULL
);
86 pthread_cond_init( &this->put_cond
, NULL
);
92 static void apply_profile_properties( mlt_profile profile
, mlt_properties properties
)
94 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
95 mlt_properties_set_int( properties
, "frame_rate_num", profile
->frame_rate_num
);
96 mlt_properties_set_int( properties
, "frame_rate_den", profile
->frame_rate_den
);
97 mlt_properties_set_int( properties
, "width", profile
->width
);
98 mlt_properties_set_int( properties
, "height", profile
->height
);
99 mlt_properties_set_int( properties
, "progressive", profile
->progressive
);
100 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
101 mlt_properties_set_int( properties
, "sample_aspect_num", profile
->sample_aspect_num
);
102 mlt_properties_set_int( properties
, "sample_aspect_den", profile
->sample_aspect_den
);
103 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
104 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
105 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
108 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
)
110 if ( !strcmp( name
, "profile" ) )
113 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
115 // Locate the profile
116 mlt_profile_select( mlt_properties_get( properties
, "profile" ) );
118 // Stop listening to this
119 mlt_event_block( g_event_listener
);
121 // Apply to properties
122 apply_profile_properties( mlt_profile_get(), properties
);
126 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
128 if ( listener
!= NULL
)
129 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
132 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
134 if ( listener
!= NULL
)
135 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
138 /** Create a new consumer.
141 mlt_consumer
mlt_consumer_new( )
143 // Create the memory for the structure
144 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
148 mlt_consumer_init( this, NULL
);
154 /** Get the parent service object.
157 mlt_service
mlt_consumer_service( mlt_consumer
this )
159 return this != NULL ?
&this->parent
: NULL
;
162 /** Get the consumer properties.
165 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
167 return this != NULL ?
MLT_SERVICE_PROPERTIES( &this->parent
) : NULL
;
170 /** Connect the consumer to the producer.
173 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
175 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
178 /** Start the consumer.
181 int mlt_consumer_start( mlt_consumer
this )
184 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
186 // Determine if there's a test card producer
187 char *test_card
= mlt_properties_get( properties
, "test_card" );
189 // Just to make sure nothing is hanging around...
190 mlt_frame_close( this->put
);
192 this->put_active
= 1;
195 if ( test_card
!= NULL
)
197 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
199 // Create a test card producer
200 mlt_producer producer
= mlt_factory_producer( NULL
, test_card
);
202 // Do we have a producer
203 if ( producer
!= NULL
)
205 // Test card should loop I guess...
206 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
207 //mlt_producer_set_speed( producer, 0 );
208 //mlt_producer_set_in_and_out( producer, 0, 0 );
210 // Set the test card on the consumer
211 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
217 // Allow the hash table to speed things up
218 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
221 // Check and run an ante command
222 if ( mlt_properties_get( properties
, "ante" ) )
223 system( mlt_properties_get( properties
, "ante" ) );
225 // Set the real_time preference
226 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
229 if ( this->start
!= NULL
)
230 return this->start( this );
235 /** An alternative method to feed frames into the consumer - only valid if
236 the consumer itself is not connected.
239 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
243 // Get the service assoicated to the consumer
244 mlt_service service
= MLT_CONSUMER_SERVICE( this );
246 if ( mlt_service_producer( service
) == NULL
)
250 pthread_mutex_lock( &this->put_mutex
);
251 while ( this->put_active
&& this->put
!= NULL
)
253 gettimeofday( &now
, NULL
);
254 tm
.tv_sec
= now
.tv_sec
+ 1;
255 tm
.tv_nsec
= now
.tv_usec
* 1000;
256 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
258 if ( this->put_active
&& this->put
== NULL
)
261 mlt_frame_close( frame
);
262 pthread_cond_broadcast( &this->put_cond
);
263 pthread_mutex_unlock( &this->put_mutex
);
267 mlt_frame_close( frame
);
273 /** Protected method for consumer to get frames from connected service
276 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
279 mlt_frame frame
= NULL
;
281 // Get the service assoicated to the consumer
282 mlt_service service
= MLT_CONSUMER_SERVICE( this );
284 // Get the consumer properties
285 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
288 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
292 pthread_mutex_lock( &this->put_mutex
);
293 while ( this->put_active
&& this->put
== NULL
)
295 gettimeofday( &now
, NULL
);
296 tm
.tv_sec
= now
.tv_sec
+ 1;
297 tm
.tv_nsec
= now
.tv_usec
* 1000;
298 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
302 pthread_cond_broadcast( &this->put_cond
);
303 pthread_mutex_unlock( &this->put_mutex
);
305 mlt_service_apply_filters( service
, frame
, 0 );
307 else if ( mlt_service_producer( service
) != NULL
)
309 mlt_service_get_frame( service
, &frame
, 0 );
313 frame
= mlt_frame_init( );
318 // Get the frame properties
319 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
321 // Get the test card producer
322 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
324 // Attach the test frame producer to it.
325 if ( test_card
!= NULL
)
326 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
328 // Attach the rescale property
329 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
331 // Aspect ratio and other jiggery pokery
332 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
333 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
334 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
341 static inline long time_difference( struct timeval
*time1
)
343 struct timeval time2
;
344 time2
.tv_sec
= time1
->tv_sec
;
345 time2
.tv_usec
= time1
->tv_usec
;
346 gettimeofday( time1
, NULL
);
347 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
350 int mlt_consumer_profile( mlt_properties properties
, char *profile
)
352 mlt_profile p
= mlt_profile_select( profile
);
355 apply_profile_properties( p
, properties
);
364 static void *consumer_read_ahead_thread( void *arg
)
366 // The argument is the consumer
367 mlt_consumer
this = arg
;
369 // Get the properties of the consumer
370 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
372 // Get the width and height
373 int width
= mlt_properties_get_int( properties
, "width" );
374 int height
= mlt_properties_get_int( properties
, "height" );
376 // See if video is turned off
377 int video_off
= mlt_properties_get_int( properties
, "video_off" );
378 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
379 int preview_format
= mlt_properties_get_int( properties
, "preview_format" );
381 // Get the audio settings
382 mlt_audio_format afmt
= mlt_audio_pcm
;
384 double fps
= mlt_properties_get_double( properties
, "fps" );
385 int channels
= mlt_properties_get_int( properties
, "channels" );
386 int frequency
= mlt_properties_get_int( properties
, "frequency" );
390 // See if audio is turned off
391 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
393 // Get the maximum size of the buffer
394 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
396 // General frame variable
397 mlt_frame frame
= NULL
;
398 uint8_t *image
= NULL
;
403 // Average time for get_frame and get_image
406 int64_t time_wait
= 0;
407 int64_t time_frame
= 0;
408 int64_t time_process
= 0;
410 mlt_service lock_object
= NULL
;
412 if ( preview_off
&& preview_format
!= 0 )
413 this->format
= preview_format
;
415 // Get the first frame
416 frame
= mlt_consumer_get_frame( this );
418 // Get the lock object
419 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
422 if ( lock_object
) mlt_service_lock( lock_object
);
424 // Get the image of the first frame
427 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
428 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
433 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
434 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
437 // Unlock the lock object
438 if ( lock_object
) mlt_service_unlock( lock_object
);
441 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
443 // Get the starting time (can ignore the times above)
444 gettimeofday( &ante
, NULL
);
446 // Continue to read ahead
447 while ( this->ahead
)
449 // Fetch width/height again
450 width
= mlt_properties_get_int( properties
, "width" );
451 height
= mlt_properties_get_int( properties
, "height" );
453 // Put the current frame into the queue
454 pthread_mutex_lock( &this->mutex
);
455 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
456 pthread_cond_wait( &this->cond
, &this->mutex
);
457 mlt_deque_push_back( this->queue
, frame
);
458 pthread_cond_broadcast( &this->cond
);
459 pthread_mutex_unlock( &this->mutex
);
461 time_wait
+= time_difference( &ante
);
463 // Get the next frame
464 frame
= mlt_consumer_get_frame( this );
465 time_frame
+= time_difference( &ante
);
467 // If there's no frame, we're probably stopped...
471 // Attempt to fetch the lock object
472 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
474 // Increment the count
477 // Lock if there's a lock object
478 if ( lock_object
) mlt_service_lock( lock_object
);
480 // All non normal playback frames should be shown
481 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
483 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
495 // Get the image, mark as rendered and time it
498 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
499 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
501 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
505 // Increment the number of sequentially skipped frames
509 // If we've reached an unacceptable level, reset everything
520 // Always process audio
523 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
524 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
527 // Increment the time take for this frame
528 time_process
+= time_difference( &ante
);
530 // Determine if the next frame should be skipped
531 if ( mlt_deque_count( this->queue
) <= 5 && ( ( time_wait
+ time_frame
+ time_process
) / count
) > 40000 )
534 // Unlock if there's a lock object
535 if ( lock_object
) mlt_service_unlock( lock_object
);
538 // Remove the last frame
539 mlt_frame_close( frame
);
544 static void consumer_read_ahead_start( mlt_consumer
this )
549 // Create the frame queue
550 this->queue
= mlt_deque_init( );
553 pthread_mutex_init( &this->mutex
, NULL
);
555 // Create the condition
556 pthread_cond_init( &this->cond
, NULL
);
558 // Create the read ahead
559 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
562 static void consumer_read_ahead_stop( mlt_consumer
this )
564 // Make sure we're running
567 // Inform thread to stop
570 // Broadcast to the condition in case it's waiting
571 pthread_mutex_lock( &this->mutex
);
572 pthread_cond_broadcast( &this->cond
);
573 pthread_mutex_unlock( &this->mutex
);
575 // Broadcast to the put condition in case it's waiting
576 pthread_mutex_lock( &this->put_mutex
);
577 pthread_cond_broadcast( &this->put_cond
);
578 pthread_mutex_unlock( &this->put_mutex
);
581 pthread_join( this->ahead_thread
, NULL
);
584 pthread_mutex_destroy( &this->mutex
);
586 // Destroy the condition
587 pthread_cond_destroy( &this->cond
);
590 while ( mlt_deque_count( this->queue
) )
591 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
594 mlt_deque_close( this->queue
);
598 void mlt_consumer_purge( mlt_consumer
this )
602 pthread_mutex_lock( &this->mutex
);
603 while ( mlt_deque_count( this->queue
) )
604 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
605 pthread_cond_broadcast( &this->cond
);
606 pthread_mutex_unlock( &this->mutex
);
610 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
613 mlt_frame frame
= NULL
;
615 // Get the properties
616 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
618 // Check if the user has requested real time or not
619 if ( this->real_time
)
623 // Is the read ahead running?
624 if ( this->ahead
== 0 )
626 int buffer
= mlt_properties_get_int( properties
, "buffer" );
627 int prefill
= mlt_properties_get_int( properties
, "prefill" );
628 consumer_read_ahead_start( this );
630 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
633 // Get frame from queue
634 pthread_mutex_lock( &this->mutex
);
635 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
636 pthread_cond_wait( &this->cond
, &this->mutex
);
637 frame
= mlt_deque_pop_front( this->queue
);
638 pthread_cond_broadcast( &this->cond
);
639 pthread_mutex_unlock( &this->mutex
);
643 // Get the frame in non real time
644 frame
= mlt_consumer_get_frame( this );
646 // This isn't true, but from the consumers perspective it is
648 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
654 /** Callback for the implementation to indicate a stopped condition.
657 void mlt_consumer_stopped( mlt_consumer
this )
659 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
660 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
663 /** Stop the consumer.
666 int mlt_consumer_stop( mlt_consumer
this )
669 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
670 char *debug
= mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
673 if ( debug
) fprintf( stderr
, "%s: stopping put waiting\n", debug
);
674 pthread_mutex_lock( &this->put_mutex
);
675 this->put_active
= 0;
676 pthread_cond_broadcast( &this->put_cond
);
677 pthread_mutex_unlock( &this->put_mutex
);
680 if ( debug
) fprintf( stderr
, "%s: stopping consumer\n", debug
);
681 if ( this->stop
!= NULL
)
684 // Check if the user has requested real time or not and stop if necessary
685 if ( debug
) fprintf( stderr
, "%s: stopping read_ahead\n", debug
);
686 if ( mlt_properties_get_int( properties
, "real_time" ) )
687 consumer_read_ahead_stop( this );
689 // Kill the test card
690 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
692 // Check and run a post command
693 if ( mlt_properties_get( properties
, "post" ) )
694 system( mlt_properties_get( properties
, "post" ) );
696 if ( debug
) fprintf( stderr
, "%s: stopped\n", debug
);
701 /** Determine if the consumer is stopped.
704 int mlt_consumer_is_stopped( mlt_consumer
this )
706 // Check if the consumer is stopped
707 if ( this->is_stopped
!= NULL
)
708 return this->is_stopped( this );
713 /** Close the consumer.
716 void mlt_consumer_close( mlt_consumer
this )
718 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
720 // Get the childs close function
721 void ( *consumer_close
)( ) = this->close
;
723 if ( consumer_close
)
726 //mlt_consumer_stop( this );
729 consumer_close( this );
733 // Make sure it only gets called once
734 this->parent
.close
= NULL
;
736 // Destroy the push mutex and condition
737 pthread_mutex_destroy( &this->put_mutex
);
738 pthread_cond_destroy( &this->put_cond
);
740 mlt_service_close( &this->parent
);