3 * \brief abstraction for all consumer services
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "mlt_consumer.h"
24 #include "mlt_factory.h"
25 #include "mlt_producer.h"
26 #include "mlt_frame.h"
27 #include "mlt_profile.h"
34 /** Define this if you want an automatic deinterlace (if necessary) when the
35 * consumer's producer is not running at normal speed.
37 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
39 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
40 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
41 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
);
42 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
);
44 /** Initialize a consumer service.
46 * \public \memberof mlt_consumer_s
47 * \param this the consumer to initialize
48 * \param child a pointer to the object for the subclass
49 * \param profile the \p mlt_profile_s to use (optional but recommended,
50 * uses the environment variable MLT if this is NULL)
51 * \return true if there was an error
54 int mlt_consumer_init( mlt_consumer
this, void *child
, mlt_profile profile
)
57 memset( this, 0, sizeof( struct mlt_consumer_s
) );
59 error
= mlt_service_init( &this->parent
, this );
62 // Get the properties from the service
63 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
65 // Apply profile to properties
66 if ( profile
== NULL
)
68 // Normally the application creates the profile and controls its lifetime
69 // This is the fallback exception handling
70 profile
= mlt_profile_init( NULL
);
71 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
72 mlt_properties_set_data( properties
, "_profile", profile
, 0, (mlt_destructor
)mlt_profile_close
, NULL
);
74 apply_profile_properties( this, profile
, properties
);
76 // Default rescaler for all consumers
77 mlt_properties_set( properties
, "rescale", "bilinear" );
79 // Default read ahead buffer size
80 mlt_properties_set_int( properties
, "buffer", 25 );
82 // Default audio frequency and channels
83 mlt_properties_set_int( properties
, "frequency", 48000 );
84 mlt_properties_set_int( properties
, "channels", 2 );
86 // Default of all consumers is real time
87 mlt_properties_set_int( properties
, "real_time", 1 );
89 // Default to environment test card
90 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
92 // Hmm - default all consumers to yuv422 :-/
93 this->format
= mlt_image_yuv422
;
95 mlt_events_register( properties
, "consumer-frame-show", ( mlt_transmitter
)mlt_consumer_frame_show
);
96 mlt_events_register( properties
, "consumer-frame-render", ( mlt_transmitter
)mlt_consumer_frame_render
);
97 mlt_events_register( properties
, "consumer-stopped", NULL
);
99 // Register a property-changed listener to handle the profile property -
100 // subsequent properties can override the profile
101 this->event_listener
= mlt_events_listen( properties
, this, "property-changed", ( mlt_listener
)mlt_consumer_property_changed
);
103 // Create the push mutex and condition
104 pthread_mutex_init( &this->put_mutex
, NULL
);
105 pthread_cond_init( &this->put_cond
, NULL
);
111 /** Convert the profile into properties on the consumer.
113 * \private \memberof mlt_consumer_s
114 * \param this a consumer
115 * \param profile a profile
116 * \param properties a properties list (typically, the consumer's)
119 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
)
121 mlt_event_block( this->event_listener
);
122 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
123 mlt_properties_set_int( properties
, "frame_rate_num", profile
->frame_rate_num
);
124 mlt_properties_set_int( properties
, "frame_rate_den", profile
->frame_rate_den
);
125 mlt_properties_set_int( properties
, "width", profile
->width
);
126 mlt_properties_set_int( properties
, "height", profile
->height
);
127 mlt_properties_set_int( properties
, "progressive", profile
->progressive
);
128 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
129 mlt_properties_set_int( properties
, "sample_aspect_num", profile
->sample_aspect_num
);
130 mlt_properties_set_int( properties
, "sample_aspect_den", profile
->sample_aspect_den
);
131 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
132 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
133 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
134 mlt_event_unblock( this->event_listener
);
137 /** The property-changed event listener
139 * \private \memberof mlt_consumer_s
140 * \param owner the service a service (ignored)
141 * \param this the consumer
142 * \param name the name of the property that changed
145 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
)
147 if ( !strcmp( name
, "profile" ) )
150 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
152 // Get the current profile
153 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
155 // Load the new profile
156 mlt_profile new_profile
= mlt_profile_init( mlt_properties_get( properties
, name
) );
161 if ( profile
!= NULL
)
163 free( profile
->description
);
164 memcpy( profile
, new_profile
, sizeof( struct mlt_profile_s
) );
165 profile
->description
= strdup( new_profile
->description
);
166 mlt_profile_close( new_profile
);
170 profile
= new_profile
;
173 // Apply to properties
174 apply_profile_properties( this, profile
, properties
);
177 else if ( !strcmp( name
, "frame_rate_num" ) )
179 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
180 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
183 profile
->frame_rate_num
= mlt_properties_get_int( properties
, "frame_rate_num" );
184 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
187 else if ( !strcmp( name
, "frame_rate_den" ) )
189 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
190 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
193 profile
->frame_rate_den
= mlt_properties_get_int( properties
, "frame_rate_den" );
194 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
197 else if ( !strcmp( name
, "width" ) )
199 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
200 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
202 profile
->width
= mlt_properties_get_int( properties
, "width" );
204 else if ( !strcmp( name
, "height" ) )
206 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
207 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
209 profile
->height
= mlt_properties_get_int( properties
, "height" );
211 else if ( !strcmp( name
, "progressive" ) )
213 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
214 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
216 profile
->progressive
= mlt_properties_get_int( properties
, "progressive" );
218 else if ( !strcmp( name
, "sample_aspect_num" ) )
220 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
221 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
222 profile
->sample_aspect_num
= mlt_properties_get_int( properties
, "sample_aspect_num" );
224 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
226 else if ( !strcmp( name
, "sample_aspect_den" ) )
228 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
229 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
230 profile
->sample_aspect_den
= mlt_properties_get_int( properties
, "sample_aspect_den" );
232 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
234 else if ( !strcmp( name
, "display_aspect_num" ) )
236 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
237 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
240 profile
->display_aspect_num
= mlt_properties_get_int( properties
, "display_aspect_num" );
241 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
244 else if ( !strcmp( name
, "display_aspect_den" ) )
246 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
247 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
250 profile
->display_aspect_den
= mlt_properties_get_int( properties
, "display_aspect_den" );
251 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
256 /** The transmitter for the consumer-frame-show event
258 * Invokes the listener.
260 * \private \memberof mlt_consumer_s
261 * \param listener a function pointer that will be invoked
262 * \param owner a properties list that will be passed to \p listener
263 * \param this a service that will be passed to \p listener
264 * \param args an array of pointers - the first entry is passed as a string to \p listener
267 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
269 if ( listener
!= NULL
)
270 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
273 /** The transmitter for the consumer-frame-render event
275 * Invokes the listener.
277 * \private \memberof mlt_consumer_s
278 * \param listener a function pointer that will be invoked
279 * \param owner a properties list that will be passed to \p listener
280 * \param this a service that will be passed to \p listener
281 * \param args an array of pointers - the first entry is passed as a string to \p listener
284 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
286 if ( listener
!= NULL
)
287 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
290 /** Create a new consumer.
292 * \public \memberof mlt_consumer_s
293 * \param profile a profile (optional, but recommended)
294 * \return a new consumer
297 mlt_consumer
mlt_consumer_new( mlt_profile profile
)
299 // Create the memory for the structure
300 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
304 mlt_consumer_init( this, NULL
, profile
);
310 /** Get the parent service object.
312 * \public \memberof mlt_consumer_s
313 * \param this a consumer
314 * \return the parent service class
315 * \see MLT_CONSUMER_SERVICE
318 mlt_service
mlt_consumer_service( mlt_consumer
this )
320 return this != NULL ?
&this->parent
: NULL
;
323 /** Get the consumer properties.
325 * \public \memberof mlt_consumer_s
326 * \param this a consumer
327 * \return the consumer's properties list
328 * \see MLT_CONSUMER_PROPERTIES
331 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
333 return this != NULL ?
MLT_SERVICE_PROPERTIES( &this->parent
) : NULL
;
336 /** Connect the consumer to the producer.
338 * \public \memberof mlt_consumer_s
339 * \param this a consumer
340 * \param producer a producer
341 * \return > 0 warning, == 0 success, < 0 serious error,
342 * 1 = this service does not accept input,
343 * 2 = the producer is invalid,
344 * 3 = the producer is already registered with this consumer
347 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
349 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
352 /** Start the consumer.
354 * \public \memberof mlt_consumer_s
355 * \param this a consumer
356 * \return true if there was an error
359 int mlt_consumer_start( mlt_consumer
this )
361 // Stop listening to the property-changed event
362 mlt_event_block( this->event_listener
);
365 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
367 // Determine if there's a test card producer
368 char *test_card
= mlt_properties_get( properties
, "test_card" );
370 // Just to make sure nothing is hanging around...
371 mlt_frame_close( this->put
);
373 this->put_active
= 1;
376 if ( test_card
!= NULL
)
378 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
380 // Create a test card producer
381 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
382 mlt_producer producer
= mlt_factory_producer( profile
, NULL
, test_card
);
384 // Do we have a producer
385 if ( producer
!= NULL
)
387 // Test card should loop I guess...
388 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
389 //mlt_producer_set_speed( producer, 0 );
390 //mlt_producer_set_in_and_out( producer, 0, 0 );
392 // Set the test card on the consumer
393 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
399 // Allow the hash table to speed things up
400 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
403 // Set the frame duration in microseconds for the frame-dropping heuristic
404 int frame_duration
= 1000000 / mlt_properties_get_int( properties
, "frame_rate_num" ) *
405 mlt_properties_get_int( properties
, "frame_rate_den" );
406 mlt_properties_set_int( properties
, "frame_duration", frame_duration
);
408 // Check and run an ante command
409 if ( mlt_properties_get( properties
, "ante" ) )
410 system( mlt_properties_get( properties
, "ante" ) );
412 // Set the real_time preference
413 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
416 if ( this->start
!= NULL
)
417 return this->start( this );
422 /** An alternative method to feed frames into the consumer.
424 * Only valid if the consumer itself is not connected.
426 * \public \memberof mlt_consumer_s
427 * \param this a consumer
428 * \param frame a frame
429 * \return true (ignore this for now)
432 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
436 // Get the service assoicated to the consumer
437 mlt_service service
= MLT_CONSUMER_SERVICE( this );
439 if ( mlt_service_producer( service
) == NULL
)
443 pthread_mutex_lock( &this->put_mutex
);
444 while ( this->put_active
&& this->put
!= NULL
)
446 gettimeofday( &now
, NULL
);
447 tm
.tv_sec
= now
.tv_sec
+ 1;
448 tm
.tv_nsec
= now
.tv_usec
* 1000;
449 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
451 if ( this->put_active
&& this->put
== NULL
)
454 mlt_frame_close( frame
);
455 pthread_cond_broadcast( &this->put_cond
);
456 pthread_mutex_unlock( &this->put_mutex
);
460 mlt_frame_close( frame
);
466 /** Protected method for consumer to get frames from connected service
468 * \public \memberof mlt_consumer_s
469 * \param this a consumer
473 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
476 mlt_frame frame
= NULL
;
478 // Get the service assoicated to the consumer
479 mlt_service service
= MLT_CONSUMER_SERVICE( this );
481 // Get the consumer properties
482 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
485 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
489 pthread_mutex_lock( &this->put_mutex
);
490 while ( this->put_active
&& this->put
== NULL
)
492 gettimeofday( &now
, NULL
);
493 tm
.tv_sec
= now
.tv_sec
+ 1;
494 tm
.tv_nsec
= now
.tv_usec
* 1000;
495 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
499 pthread_cond_broadcast( &this->put_cond
);
500 pthread_mutex_unlock( &this->put_mutex
);
502 mlt_service_apply_filters( service
, frame
, 0 );
504 else if ( mlt_service_producer( service
) != NULL
)
506 mlt_service_get_frame( service
, &frame
, 0 );
510 frame
= mlt_frame_init( service
);
515 // Get the frame properties
516 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
518 // Get the test card producer
519 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
521 // Attach the test frame producer to it.
522 if ( test_card
!= NULL
)
523 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
525 // Attach the rescale property
526 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
528 // Aspect ratio and other jiggery pokery
529 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
530 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
531 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
538 /** Compute the time difference between now and a time value.
540 * \private \memberof mlt_consumer_s
541 * \param time1 a time value to be compared against now
542 * \return the difference in microseconds
545 static inline long time_difference( struct timeval
*time1
)
547 struct timeval time2
;
548 time2
.tv_sec
= time1
->tv_sec
;
549 time2
.tv_usec
= time1
->tv_usec
;
550 gettimeofday( time1
, NULL
);
551 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
554 /** The thread procedure for asynchronously pulling frames through the service
555 * network connected to a consumer.
557 * \private \memberof mlt_consumer_s
558 * \param arg a consumer
561 static void *consumer_read_ahead_thread( void *arg
)
563 // The argument is the consumer
564 mlt_consumer
this = arg
;
566 // Get the properties of the consumer
567 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
569 // Get the width and height
570 int width
= mlt_properties_get_int( properties
, "width" );
571 int height
= mlt_properties_get_int( properties
, "height" );
573 // See if video is turned off
574 int video_off
= mlt_properties_get_int( properties
, "video_off" );
575 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
576 int preview_format
= mlt_properties_get_int( properties
, "preview_format" );
578 // Get the audio settings
579 mlt_audio_format afmt
= mlt_audio_pcm
;
581 double fps
= mlt_properties_get_double( properties
, "fps" );
582 int channels
= mlt_properties_get_int( properties
, "channels" );
583 int frequency
= mlt_properties_get_int( properties
, "frequency" );
587 // See if audio is turned off
588 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
590 // Get the maximum size of the buffer
591 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
593 // General frame variable
594 mlt_frame frame
= NULL
;
595 uint8_t *image
= NULL
;
600 // Average time for get_frame and get_image
603 int64_t time_wait
= 0;
604 int64_t time_frame
= 0;
605 int64_t time_process
= 0;
607 mlt_service lock_object
= NULL
;
609 if ( preview_off
&& preview_format
!= 0 )
610 this->format
= preview_format
;
612 // Get the first frame
613 frame
= mlt_consumer_get_frame( this );
615 // Get the lock object
616 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
619 if ( lock_object
) mlt_service_lock( lock_object
);
621 // Get the image of the first frame
624 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
625 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
630 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
631 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
634 // Unlock the lock object
635 if ( lock_object
) mlt_service_unlock( lock_object
);
638 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
640 // Get the starting time (can ignore the times above)
641 gettimeofday( &ante
, NULL
);
643 // Continue to read ahead
644 while ( this->ahead
)
646 // Fetch width/height again
647 width
= mlt_properties_get_int( properties
, "width" );
648 height
= mlt_properties_get_int( properties
, "height" );
650 // Put the current frame into the queue
651 pthread_mutex_lock( &this->mutex
);
652 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
653 pthread_cond_wait( &this->cond
, &this->mutex
);
654 mlt_deque_push_back( this->queue
, frame
);
655 pthread_cond_broadcast( &this->cond
);
656 pthread_mutex_unlock( &this->mutex
);
658 time_wait
+= time_difference( &ante
);
660 // Get the next frame
661 frame
= mlt_consumer_get_frame( this );
662 time_frame
+= time_difference( &ante
);
664 // If there's no frame, we're probably stopped...
668 // Attempt to fetch the lock object
669 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
671 // Increment the count
674 // Lock if there's a lock object
675 if ( lock_object
) mlt_service_lock( lock_object
);
677 // All non normal playback frames should be shown
678 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
680 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
681 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
692 if ( !skip_next
|| this->real_time
== -1 )
694 // Get the image, mark as rendered and time it
697 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
698 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
700 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
704 // Increment the number of sequentially skipped frames
708 // If we've reached an unacceptable level, reset everything
719 // Always process audio
722 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
723 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
726 // Increment the time take for this frame
727 time_process
+= time_difference( &ante
);
729 // Determine if the next frame should be skipped
730 if ( mlt_deque_count( this->queue
) <= 5 )
732 int frame_duration
= mlt_properties_get_int( properties
, "frame_duration" );
733 if ( ( ( time_wait
+ time_frame
+ time_process
) / count
) > frame_duration
)
737 // Unlock if there's a lock object
738 if ( lock_object
) mlt_service_unlock( lock_object
);
741 // Remove the last frame
742 mlt_frame_close( frame
);
747 /** Start the read/render thread.
749 * \private \memberof mlt_consumer_s
750 * \param this a consumer
753 static void consumer_read_ahead_start( mlt_consumer
this )
758 // Create the frame queue
759 this->queue
= mlt_deque_init( );
762 pthread_mutex_init( &this->mutex
, NULL
);
764 // Create the condition
765 pthread_cond_init( &this->cond
, NULL
);
767 // Create the read ahead
768 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
770 struct sched_param priority
;
771 priority
.sched_priority
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
772 pthread_attr_t thread_attributes
;
773 pthread_attr_init( &thread_attributes
);
774 pthread_attr_setschedpolicy( &thread_attributes
, SCHED_OTHER
);
775 pthread_attr_setschedparam( &thread_attributes
, &priority
);
776 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_EXPLICIT_SCHED
);
777 pthread_attr_setscope( &thread_attributes
, PTHREAD_SCOPE_SYSTEM
);
778 if ( pthread_create( &this->ahead_thread
, &thread_attributes
, consumer_read_ahead_thread
, this ) < 0 )
779 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
780 pthread_attr_destroy( &thread_attributes
);
784 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
788 /** Stop the read/render thread.
790 * \private \memberof mlt_consumer_s
791 * \param this a consumer
794 static void consumer_read_ahead_stop( mlt_consumer
this )
796 // Make sure we're running
799 // Inform thread to stop
802 // Broadcast to the condition in case it's waiting
803 pthread_mutex_lock( &this->mutex
);
804 pthread_cond_broadcast( &this->cond
);
805 pthread_mutex_unlock( &this->mutex
);
807 // Broadcast to the put condition in case it's waiting
808 pthread_mutex_lock( &this->put_mutex
);
809 pthread_cond_broadcast( &this->put_cond
);
810 pthread_mutex_unlock( &this->put_mutex
);
813 pthread_join( this->ahead_thread
, NULL
);
816 pthread_mutex_destroy( &this->mutex
);
818 // Destroy the condition
819 pthread_cond_destroy( &this->cond
);
822 while ( mlt_deque_count( this->queue
) )
823 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
826 mlt_deque_close( this->queue
);
830 /** Flush the read/render thread's buffer.
832 * \public \memberof mlt_consumer_s
833 * \param this a consumer
836 void mlt_consumer_purge( mlt_consumer
this )
840 pthread_mutex_lock( &this->mutex
);
841 while ( mlt_deque_count( this->queue
) )
842 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
843 pthread_cond_broadcast( &this->cond
);
844 pthread_mutex_unlock( &this->mutex
);
848 /** Get the next frame from the producer connected to a consumer.
850 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
851 * the asynchronous/real-time behavior configurable at runtime.
852 * You should close the frame returned from this when you are done with it.
854 * \public \memberof mlt_consumer_s
855 * \param this a consumer
859 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
862 mlt_frame frame
= NULL
;
864 // Get the properties
865 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
867 // Check if the user has requested real time or not
868 if ( this->real_time
)
872 // Is the read ahead running?
873 if ( this->ahead
== 0 )
875 int buffer
= mlt_properties_get_int( properties
, "buffer" );
876 int prefill
= mlt_properties_get_int( properties
, "prefill" );
877 consumer_read_ahead_start( this );
879 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
882 // Get frame from queue
883 pthread_mutex_lock( &this->mutex
);
884 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
885 pthread_cond_wait( &this->cond
, &this->mutex
);
886 frame
= mlt_deque_pop_front( this->queue
);
887 pthread_cond_broadcast( &this->cond
);
888 pthread_mutex_unlock( &this->mutex
);
892 // Get the frame in non real time
893 frame
= mlt_consumer_get_frame( this );
895 // This isn't true, but from the consumers perspective it is
897 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
903 /** Callback for the implementation to indicate a stopped condition.
905 * \public \memberof mlt_consumer_s
906 * \param this a consumer
909 void mlt_consumer_stopped( mlt_consumer
this )
911 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
912 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
913 mlt_event_unblock( this->event_listener
);
916 /** Stop the consumer.
918 * \public \memberof mlt_consumer_s
919 * \param this a consumer
920 * \return true if there was an error
923 int mlt_consumer_stop( mlt_consumer
this )
926 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
927 char *debug
= mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
930 if ( debug
) fprintf( stderr
, "%s: stopping put waiting\n", debug
);
931 pthread_mutex_lock( &this->put_mutex
);
932 this->put_active
= 0;
933 pthread_cond_broadcast( &this->put_cond
);
934 pthread_mutex_unlock( &this->put_mutex
);
937 if ( debug
) fprintf( stderr
, "%s: stopping consumer\n", debug
);
938 if ( this->stop
!= NULL
)
941 // Check if the user has requested real time or not and stop if necessary
942 if ( debug
) fprintf( stderr
, "%s: stopping read_ahead\n", debug
);
943 if ( mlt_properties_get_int( properties
, "real_time" ) )
944 consumer_read_ahead_stop( this );
946 // Kill the test card
947 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
949 // Check and run a post command
950 if ( mlt_properties_get( properties
, "post" ) )
951 system( mlt_properties_get( properties
, "post" ) );
953 if ( debug
) fprintf( stderr
, "%s: stopped\n", debug
);
958 /** Determine if the consumer is stopped.
960 * \public \memberof mlt_consumer_s
961 * \param this a consumer
962 * \return true if the consumer is stopped
965 int mlt_consumer_is_stopped( mlt_consumer
this )
967 // Check if the consumer is stopped
968 if ( this->is_stopped
!= NULL
)
969 return this->is_stopped( this );
974 /** Close and destroy the consumer.
976 * \public \memberof mlt_consumer_s
977 * \param this a consumer
980 void mlt_consumer_close( mlt_consumer
this )
982 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
984 // Get the childs close function
985 void ( *consumer_close
)( ) = this->close
;
987 if ( consumer_close
)
990 //mlt_consumer_stop( this );
993 consumer_close( this );
997 // Make sure it only gets called once
998 this->parent
.close
= NULL
;
1000 // Destroy the push mutex and condition
1001 pthread_mutex_destroy( &this->put_mutex
);
1002 pthread_cond_destroy( &this->put_cond
);
1004 mlt_service_close( &this->parent
);