3 * \brief abstraction for all consumer services
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_consumer.h"
25 #include "mlt_factory.h"
26 #include "mlt_producer.h"
27 #include "mlt_frame.h"
28 #include "mlt_profile.h"
36 /** Define this if you want an automatic deinterlace (if necessary) when the
37 * consumer's producer is not running at normal speed.
39 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
41 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
42 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
43 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
);
44 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
);
46 /** Initialize a consumer service.
48 * \public \memberof mlt_consumer_s
49 * \param this the consumer to initialize
50 * \param child a pointer to the object for the subclass
51 * \param profile the \p mlt_profile_s to use (optional but recommended,
52 * uses the environment variable MLT if this is NULL)
53 * \return true if there was an error
56 int mlt_consumer_init( mlt_consumer
this, void *child
, mlt_profile profile
)
59 memset( this, 0, sizeof( struct mlt_consumer_s
) );
61 error
= mlt_service_init( &this->parent
, this );
64 // Get the properties from the service
65 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
67 // Apply profile to properties
68 if ( profile
== NULL
)
70 // Normally the application creates the profile and controls its lifetime
71 // This is the fallback exception handling
72 profile
= mlt_profile_init( NULL
);
73 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
74 mlt_properties_set_data( properties
, "_profile", profile
, 0, (mlt_destructor
)mlt_profile_close
, NULL
);
76 apply_profile_properties( this, profile
, properties
);
78 // Default rescaler for all consumers
79 mlt_properties_set( properties
, "rescale", "bilinear" );
81 // Default read ahead buffer size
82 mlt_properties_set_int( properties
, "buffer", 25 );
84 // Default audio frequency and channels
85 mlt_properties_set_int( properties
, "frequency", 48000 );
86 mlt_properties_set_int( properties
, "channels", 2 );
88 // Default of all consumers is real time
89 mlt_properties_set_int( properties
, "real_time", 1 );
91 // Default to environment test card
92 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
94 // Hmm - default all consumers to yuv422 :-/
95 this->format
= mlt_image_yuv422
;
97 mlt_events_register( properties
, "consumer-frame-show", ( mlt_transmitter
)mlt_consumer_frame_show
);
98 mlt_events_register( properties
, "consumer-frame-render", ( mlt_transmitter
)mlt_consumer_frame_render
);
99 mlt_events_register( properties
, "consumer-stopped", NULL
);
101 // Register a property-changed listener to handle the profile property -
102 // subsequent properties can override the profile
103 this->event_listener
= mlt_events_listen( properties
, this, "property-changed", ( mlt_listener
)mlt_consumer_property_changed
);
105 // Create the push mutex and condition
106 pthread_mutex_init( &this->put_mutex
, NULL
);
107 pthread_cond_init( &this->put_cond
, NULL
);
113 /** Convert the profile into properties on the consumer.
115 * \private \memberof mlt_consumer_s
116 * \param this a consumer
117 * \param profile a profile
118 * \param properties a properties list (typically, the consumer's)
121 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
)
123 mlt_event_block( this->event_listener
);
124 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
125 mlt_properties_set_int( properties
, "frame_rate_num", profile
->frame_rate_num
);
126 mlt_properties_set_int( properties
, "frame_rate_den", profile
->frame_rate_den
);
127 mlt_properties_set_int( properties
, "width", profile
->width
);
128 mlt_properties_set_int( properties
, "height", profile
->height
);
129 mlt_properties_set_int( properties
, "progressive", profile
->progressive
);
130 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
131 mlt_properties_set_int( properties
, "sample_aspect_num", profile
->sample_aspect_num
);
132 mlt_properties_set_int( properties
, "sample_aspect_den", profile
->sample_aspect_den
);
133 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
134 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
135 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
136 mlt_event_unblock( this->event_listener
);
139 /** The property-changed event listener
141 * \private \memberof mlt_consumer_s
142 * \param owner the service a service (ignored)
143 * \param this the consumer
144 * \param name the name of the property that changed
147 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
)
149 if ( !strcmp( name
, "profile" ) )
152 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
154 // Get the current profile
155 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
157 // Load the new profile
158 mlt_profile new_profile
= mlt_profile_init( mlt_properties_get( properties
, name
) );
163 if ( profile
!= NULL
)
165 free( profile
->description
);
166 memcpy( profile
, new_profile
, sizeof( struct mlt_profile_s
) );
167 profile
->description
= strdup( new_profile
->description
);
168 mlt_profile_close( new_profile
);
172 profile
= new_profile
;
175 // Apply to properties
176 apply_profile_properties( this, profile
, properties
);
179 else if ( !strcmp( name
, "frame_rate_num" ) )
181 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
182 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
185 profile
->frame_rate_num
= mlt_properties_get_int( properties
, "frame_rate_num" );
186 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
189 else if ( !strcmp( name
, "frame_rate_den" ) )
191 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
192 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
195 profile
->frame_rate_den
= mlt_properties_get_int( properties
, "frame_rate_den" );
196 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
199 else if ( !strcmp( name
, "width" ) )
201 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
202 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
204 profile
->width
= mlt_properties_get_int( properties
, "width" );
206 else if ( !strcmp( name
, "height" ) )
208 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
209 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
211 profile
->height
= mlt_properties_get_int( properties
, "height" );
213 else if ( !strcmp( name
, "progressive" ) )
215 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
216 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
218 profile
->progressive
= mlt_properties_get_int( properties
, "progressive" );
220 else if ( !strcmp( name
, "sample_aspect_num" ) )
222 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
223 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
224 profile
->sample_aspect_num
= mlt_properties_get_int( properties
, "sample_aspect_num" );
226 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
228 else if ( !strcmp( name
, "sample_aspect_den" ) )
230 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
231 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
232 profile
->sample_aspect_den
= mlt_properties_get_int( properties
, "sample_aspect_den" );
234 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
236 else if ( !strcmp( name
, "display_aspect_num" ) )
238 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
239 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
242 profile
->display_aspect_num
= mlt_properties_get_int( properties
, "display_aspect_num" );
243 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
246 else if ( !strcmp( name
, "display_aspect_den" ) )
248 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
249 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
252 profile
->display_aspect_den
= mlt_properties_get_int( properties
, "display_aspect_den" );
253 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
258 /** The transmitter for the consumer-frame-show event
260 * Invokes the listener.
262 * \private \memberof mlt_consumer_s
263 * \param listener a function pointer that will be invoked
264 * \param owner a properties list that will be passed to \p listener
265 * \param this a service that will be passed to \p listener
266 * \param args an array of pointers - the first entry is passed as a string to \p listener
269 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
271 if ( listener
!= NULL
)
272 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
275 /** The transmitter for the consumer-frame-render event
277 * Invokes the listener.
279 * \private \memberof mlt_consumer_s
280 * \param listener a function pointer that will be invoked
281 * \param owner a properties list that will be passed to \p listener
282 * \param this a service that will be passed to \p listener
283 * \param args an array of pointers - the first entry is passed as a string to \p listener
286 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
288 if ( listener
!= NULL
)
289 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
292 /** Create a new consumer.
294 * \public \memberof mlt_consumer_s
295 * \param profile a profile (optional, but recommended)
296 * \return a new consumer
299 mlt_consumer
mlt_consumer_new( mlt_profile profile
)
301 // Create the memory for the structure
302 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
306 mlt_consumer_init( this, NULL
, profile
);
312 /** Get the parent service object.
314 * \public \memberof mlt_consumer_s
315 * \param this a consumer
316 * \return the parent service class
317 * \see MLT_CONSUMER_SERVICE
320 mlt_service
mlt_consumer_service( mlt_consumer
this )
322 return this != NULL ?
&this->parent
: NULL
;
325 /** Get the consumer properties.
327 * \public \memberof mlt_consumer_s
328 * \param this a consumer
329 * \return the consumer's properties list
330 * \see MLT_CONSUMER_PROPERTIES
333 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
335 return this != NULL ?
MLT_SERVICE_PROPERTIES( &this->parent
) : NULL
;
338 /** Connect the consumer to the producer.
340 * \public \memberof mlt_consumer_s
341 * \param this a consumer
342 * \param producer a producer
343 * \return > 0 warning, == 0 success, < 0 serious error,
344 * 1 = this service does not accept input,
345 * 2 = the producer is invalid,
346 * 3 = the producer is already registered with this consumer
349 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
351 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
354 /** Start the consumer.
356 * \public \memberof mlt_consumer_s
357 * \param this a consumer
358 * \return true if there was an error
361 int mlt_consumer_start( mlt_consumer
this )
363 // Stop listening to the property-changed event
364 mlt_event_block( this->event_listener
);
367 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
369 // Determine if there's a test card producer
370 char *test_card
= mlt_properties_get( properties
, "test_card" );
372 // Just to make sure nothing is hanging around...
373 mlt_frame_close( this->put
);
375 this->put_active
= 1;
378 if ( test_card
!= NULL
)
380 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
382 // Create a test card producer
383 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
384 mlt_producer producer
= mlt_factory_producer( profile
, NULL
, test_card
);
386 // Do we have a producer
387 if ( producer
!= NULL
)
389 // Test card should loop I guess...
390 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
391 //mlt_producer_set_speed( producer, 0 );
392 //mlt_producer_set_in_and_out( producer, 0, 0 );
394 // Set the test card on the consumer
395 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
401 // Allow the hash table to speed things up
402 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
405 // Set the frame duration in microseconds for the frame-dropping heuristic
406 int frame_duration
= 1000000 / mlt_properties_get_int( properties
, "frame_rate_num" ) *
407 mlt_properties_get_int( properties
, "frame_rate_den" );
408 mlt_properties_set_int( properties
, "frame_duration", frame_duration
);
410 // Check and run an ante command
411 if ( mlt_properties_get( properties
, "ante" ) )
412 system( mlt_properties_get( properties
, "ante" ) );
414 // Set the real_time preference
415 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
418 if ( this->start
!= NULL
)
419 return this->start( this );
424 /** An alternative method to feed frames into the consumer.
426 * Only valid if the consumer itself is not connected.
428 * \public \memberof mlt_consumer_s
429 * \param this a consumer
430 * \param frame a frame
431 * \return true (ignore this for now)
434 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
438 // Get the service assoicated to the consumer
439 mlt_service service
= MLT_CONSUMER_SERVICE( this );
441 if ( mlt_service_producer( service
) == NULL
)
445 pthread_mutex_lock( &this->put_mutex
);
446 while ( this->put_active
&& this->put
!= NULL
)
448 gettimeofday( &now
, NULL
);
449 tm
.tv_sec
= now
.tv_sec
+ 1;
450 tm
.tv_nsec
= now
.tv_usec
* 1000;
451 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
453 if ( this->put_active
&& this->put
== NULL
)
456 mlt_frame_close( frame
);
457 pthread_cond_broadcast( &this->put_cond
);
458 pthread_mutex_unlock( &this->put_mutex
);
462 mlt_frame_close( frame
);
468 /** Protected method for consumer to get frames from connected service
470 * \public \memberof mlt_consumer_s
471 * \param this a consumer
475 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
478 mlt_frame frame
= NULL
;
480 // Get the service assoicated to the consumer
481 mlt_service service
= MLT_CONSUMER_SERVICE( this );
483 // Get the consumer properties
484 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
487 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
491 pthread_mutex_lock( &this->put_mutex
);
492 while ( this->put_active
&& this->put
== NULL
)
494 gettimeofday( &now
, NULL
);
495 tm
.tv_sec
= now
.tv_sec
+ 1;
496 tm
.tv_nsec
= now
.tv_usec
* 1000;
497 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
501 pthread_cond_broadcast( &this->put_cond
);
502 pthread_mutex_unlock( &this->put_mutex
);
504 mlt_service_apply_filters( service
, frame
, 0 );
506 else if ( mlt_service_producer( service
) != NULL
)
508 mlt_service_get_frame( service
, &frame
, 0 );
512 frame
= mlt_frame_init( service
);
517 // Get the frame properties
518 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
520 // Get the test card producer
521 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
523 // Attach the test frame producer to it.
524 if ( test_card
!= NULL
)
525 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
527 // Attach the rescale property
528 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
530 // Aspect ratio and other jiggery pokery
531 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
532 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
533 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
540 /** Compute the time difference between now and a time value.
542 * \private \memberof mlt_consumer_s
543 * \param time1 a time value to be compared against now
544 * \return the difference in microseconds
547 static inline long time_difference( struct timeval
*time1
)
549 struct timeval time2
;
550 time2
.tv_sec
= time1
->tv_sec
;
551 time2
.tv_usec
= time1
->tv_usec
;
552 gettimeofday( time1
, NULL
);
553 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
556 /** The thread procedure for asynchronously pulling frames through the service
557 * network connected to a consumer.
559 * \private \memberof mlt_consumer_s
560 * \param arg a consumer
563 static void *consumer_read_ahead_thread( void *arg
)
565 // The argument is the consumer
566 mlt_consumer
this = arg
;
568 // Get the properties of the consumer
569 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
571 // Get the width and height
572 int width
= mlt_properties_get_int( properties
, "width" );
573 int height
= mlt_properties_get_int( properties
, "height" );
575 // See if video is turned off
576 int video_off
= mlt_properties_get_int( properties
, "video_off" );
577 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
578 int preview_format
= mlt_properties_get_int( properties
, "preview_format" );
580 // Get the audio settings
581 mlt_audio_format afmt
= mlt_audio_pcm
;
583 double fps
= mlt_properties_get_double( properties
, "fps" );
584 int channels
= mlt_properties_get_int( properties
, "channels" );
585 int frequency
= mlt_properties_get_int( properties
, "frequency" );
589 // See if audio is turned off
590 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
592 // Get the maximum size of the buffer
593 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
595 // General frame variable
596 mlt_frame frame
= NULL
;
597 uint8_t *image
= NULL
;
602 // Average time for get_frame and get_image
605 int64_t time_wait
= 0;
606 int64_t time_frame
= 0;
607 int64_t time_process
= 0;
609 mlt_service lock_object
= NULL
;
611 if ( preview_off
&& preview_format
!= 0 )
612 this->format
= preview_format
;
614 // Get the first frame
615 frame
= mlt_consumer_get_frame( this );
617 // Get the lock object
618 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
621 if ( lock_object
) mlt_service_lock( lock_object
);
623 // Get the image of the first frame
626 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
627 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
632 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
633 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
636 // Unlock the lock object
637 if ( lock_object
) mlt_service_unlock( lock_object
);
640 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
642 // Get the starting time (can ignore the times above)
643 gettimeofday( &ante
, NULL
);
645 // Continue to read ahead
646 while ( this->ahead
)
648 // Fetch width/height again
649 width
= mlt_properties_get_int( properties
, "width" );
650 height
= mlt_properties_get_int( properties
, "height" );
652 // Put the current frame into the queue
653 pthread_mutex_lock( &this->mutex
);
654 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
655 pthread_cond_wait( &this->cond
, &this->mutex
);
656 mlt_deque_push_back( this->queue
, frame
);
657 pthread_cond_broadcast( &this->cond
);
658 pthread_mutex_unlock( &this->mutex
);
660 time_wait
+= time_difference( &ante
);
662 // Get the next frame
663 frame
= mlt_consumer_get_frame( this );
664 time_frame
+= time_difference( &ante
);
666 // If there's no frame, we're probably stopped...
670 // Attempt to fetch the lock object
671 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
673 // Increment the count
676 // Lock if there's a lock object
677 if ( lock_object
) mlt_service_lock( lock_object
);
679 // All non normal playback frames should be shown
680 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
682 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
683 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
694 if ( !skip_next
|| this->real_time
== -1 )
696 // Get the image, mark as rendered and time it
699 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
700 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
702 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
706 // Increment the number of sequentially skipped frames
710 // If we've reached an unacceptable level, reset everything
721 // Always process audio
724 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
725 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
728 // Increment the time take for this frame
729 time_process
+= time_difference( &ante
);
731 // Determine if the next frame should be skipped
732 if ( mlt_deque_count( this->queue
) <= 5 )
734 int frame_duration
= mlt_properties_get_int( properties
, "frame_duration" );
735 if ( ( ( time_wait
+ time_frame
+ time_process
) / count
) > frame_duration
)
739 // Unlock if there's a lock object
740 if ( lock_object
) mlt_service_unlock( lock_object
);
743 // Remove the last frame
744 mlt_frame_close( frame
);
749 /** Start the read/render thread.
751 * \private \memberof mlt_consumer_s
752 * \param this a consumer
755 static void consumer_read_ahead_start( mlt_consumer
this )
760 // Create the frame queue
761 this->queue
= mlt_deque_init( );
764 pthread_mutex_init( &this->mutex
, NULL
);
766 // Create the condition
767 pthread_cond_init( &this->cond
, NULL
);
769 // Create the read ahead
770 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
772 struct sched_param priority
;
773 priority
.sched_priority
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
774 pthread_attr_t thread_attributes
;
775 pthread_attr_init( &thread_attributes
);
776 pthread_attr_setschedpolicy( &thread_attributes
, SCHED_OTHER
);
777 pthread_attr_setschedparam( &thread_attributes
, &priority
);
778 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_EXPLICIT_SCHED
);
779 pthread_attr_setscope( &thread_attributes
, PTHREAD_SCOPE_SYSTEM
);
780 if ( pthread_create( &this->ahead_thread
, &thread_attributes
, consumer_read_ahead_thread
, this ) < 0 )
781 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
782 pthread_attr_destroy( &thread_attributes
);
786 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
790 /** Stop the read/render thread.
792 * \private \memberof mlt_consumer_s
793 * \param this a consumer
796 static void consumer_read_ahead_stop( mlt_consumer
this )
798 // Make sure we're running
801 // Inform thread to stop
804 // Broadcast to the condition in case it's waiting
805 pthread_mutex_lock( &this->mutex
);
806 pthread_cond_broadcast( &this->cond
);
807 pthread_mutex_unlock( &this->mutex
);
809 // Broadcast to the put condition in case it's waiting
810 pthread_mutex_lock( &this->put_mutex
);
811 pthread_cond_broadcast( &this->put_cond
);
812 pthread_mutex_unlock( &this->put_mutex
);
815 pthread_join( this->ahead_thread
, NULL
);
818 pthread_mutex_destroy( &this->mutex
);
820 // Destroy the condition
821 pthread_cond_destroy( &this->cond
);
824 while ( mlt_deque_count( this->queue
) )
825 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
828 mlt_deque_close( this->queue
);
832 /** Flush the read/render thread's buffer.
834 * \public \memberof mlt_consumer_s
835 * \param this a consumer
838 void mlt_consumer_purge( mlt_consumer
this )
842 pthread_mutex_lock( &this->mutex
);
843 while ( mlt_deque_count( this->queue
) )
844 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
845 pthread_cond_broadcast( &this->cond
);
846 pthread_mutex_unlock( &this->mutex
);
850 /** Get the next frame from the producer connected to a consumer.
852 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
853 * the asynchronous/real-time behavior configurable at runtime.
854 * You should close the frame returned from this when you are done with it.
856 * \public \memberof mlt_consumer_s
857 * \param this a consumer
861 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
864 mlt_frame frame
= NULL
;
866 // Get the properties
867 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
869 // Check if the user has requested real time or not
870 if ( this->real_time
)
874 // Is the read ahead running?
875 if ( this->ahead
== 0 )
877 int buffer
= mlt_properties_get_int( properties
, "buffer" );
878 int prefill
= mlt_properties_get_int( properties
, "prefill" );
879 consumer_read_ahead_start( this );
881 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
884 // Get frame from queue
885 pthread_mutex_lock( &this->mutex
);
886 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
887 pthread_cond_wait( &this->cond
, &this->mutex
);
888 frame
= mlt_deque_pop_front( this->queue
);
889 pthread_cond_broadcast( &this->cond
);
890 pthread_mutex_unlock( &this->mutex
);
894 // Get the frame in non real time
895 frame
= mlt_consumer_get_frame( this );
897 // This isn't true, but from the consumers perspective it is
899 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
905 /** Callback for the implementation to indicate a stopped condition.
907 * \public \memberof mlt_consumer_s
908 * \param this a consumer
911 void mlt_consumer_stopped( mlt_consumer
this )
913 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
914 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
915 mlt_event_unblock( this->event_listener
);
918 /** Stop the consumer.
920 * \public \memberof mlt_consumer_s
921 * \param this a consumer
922 * \return true if there was an error
925 int mlt_consumer_stop( mlt_consumer
this )
928 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
931 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping put waiting\n" );
932 pthread_mutex_lock( &this->put_mutex
);
933 this->put_active
= 0;
934 pthread_cond_broadcast( &this->put_cond
);
935 pthread_mutex_unlock( &this->put_mutex
);
938 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping consumer\n" );
939 if ( this->stop
!= NULL
)
942 // Check if the user has requested real time or not and stop if necessary
943 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping read_ahead\n" );
944 if ( mlt_properties_get_int( properties
, "real_time" ) )
945 consumer_read_ahead_stop( this );
947 // Kill the test card
948 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
950 // Check and run a post command
951 if ( mlt_properties_get( properties
, "post" ) )
952 system( mlt_properties_get( properties
, "post" ) );
954 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopped\n" );
959 /** Determine if the consumer is stopped.
961 * \public \memberof mlt_consumer_s
962 * \param this a consumer
963 * \return true if the consumer is stopped
966 int mlt_consumer_is_stopped( mlt_consumer
this )
968 // Check if the consumer is stopped
969 if ( this->is_stopped
!= NULL
)
970 return this->is_stopped( this );
975 /** Close and destroy the consumer.
977 * \public \memberof mlt_consumer_s
978 * \param this a consumer
981 void mlt_consumer_close( mlt_consumer
this )
983 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
985 // Get the childs close function
986 void ( *consumer_close
)( ) = this->close
;
988 if ( consumer_close
)
991 //mlt_consumer_stop( this );
994 consumer_close( this );
998 // Make sure it only gets called once
999 this->parent
.close
= NULL
;
1001 // Destroy the push mutex and condition
1002 pthread_mutex_destroy( &this->put_mutex
);
1003 pthread_cond_destroy( &this->put_cond
);
1005 mlt_service_close( &this->parent
);