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 if ( system( mlt_properties_get( properties
, "ante" ) ) == -1 )
413 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR
, "system(%s) failed!\n", mlt_properties_get( properties
, "ante" ) );
415 // Set the real_time preference
416 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
419 if ( this->start
!= NULL
)
420 return this->start( this );
425 /** An alternative method to feed frames into the consumer.
427 * Only valid if the consumer itself is not connected.
429 * \public \memberof mlt_consumer_s
430 * \param this a consumer
431 * \param frame a frame
432 * \return true (ignore this for now)
435 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
439 // Get the service assoicated to the consumer
440 mlt_service service
= MLT_CONSUMER_SERVICE( this );
442 if ( mlt_service_producer( service
) == NULL
)
446 pthread_mutex_lock( &this->put_mutex
);
447 while ( this->put_active
&& this->put
!= NULL
)
449 gettimeofday( &now
, NULL
);
450 tm
.tv_sec
= now
.tv_sec
+ 1;
451 tm
.tv_nsec
= now
.tv_usec
* 1000;
452 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
454 if ( this->put_active
&& this->put
== NULL
)
457 mlt_frame_close( frame
);
458 pthread_cond_broadcast( &this->put_cond
);
459 pthread_mutex_unlock( &this->put_mutex
);
463 mlt_frame_close( frame
);
469 /** Protected method for consumer to get frames from connected service
471 * \public \memberof mlt_consumer_s
472 * \param this a consumer
476 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
479 mlt_frame frame
= NULL
;
481 // Get the service assoicated to the consumer
482 mlt_service service
= MLT_CONSUMER_SERVICE( this );
484 // Get the consumer properties
485 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
488 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
492 pthread_mutex_lock( &this->put_mutex
);
493 while ( this->put_active
&& this->put
== NULL
)
495 gettimeofday( &now
, NULL
);
496 tm
.tv_sec
= now
.tv_sec
+ 1;
497 tm
.tv_nsec
= now
.tv_usec
* 1000;
498 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
502 pthread_cond_broadcast( &this->put_cond
);
503 pthread_mutex_unlock( &this->put_mutex
);
505 mlt_service_apply_filters( service
, frame
, 0 );
507 else if ( mlt_service_producer( service
) != NULL
)
509 mlt_service_get_frame( service
, &frame
, 0 );
513 frame
= mlt_frame_init( service
);
518 // Get the frame properties
519 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
521 // Get the test card producer
522 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
524 // Attach the test frame producer to it.
525 if ( test_card
!= NULL
)
526 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
528 // Attach the rescale property
529 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
531 // Aspect ratio and other jiggery pokery
532 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
533 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
534 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
541 /** Compute the time difference between now and a time value.
543 * \private \memberof mlt_consumer_s
544 * \param time1 a time value to be compared against now
545 * \return the difference in microseconds
548 static inline long time_difference( struct timeval
*time1
)
550 struct timeval time2
;
551 time2
.tv_sec
= time1
->tv_sec
;
552 time2
.tv_usec
= time1
->tv_usec
;
553 gettimeofday( time1
, NULL
);
554 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
557 /** The thread procedure for asynchronously pulling frames through the service
558 * network connected to a consumer.
560 * \private \memberof mlt_consumer_s
561 * \param arg a consumer
564 static void *consumer_read_ahead_thread( void *arg
)
566 // The argument is the consumer
567 mlt_consumer
this = arg
;
569 // Get the properties of the consumer
570 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
572 // Get the width and height
573 int width
= mlt_properties_get_int( properties
, "width" );
574 int height
= mlt_properties_get_int( properties
, "height" );
576 // See if video is turned off
577 int video_off
= mlt_properties_get_int( properties
, "video_off" );
578 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
579 int preview_format
= mlt_properties_get_int( properties
, "preview_format" );
581 // Get the audio settings
582 mlt_audio_format afmt
= mlt_audio_pcm
;
584 double fps
= mlt_properties_get_double( properties
, "fps" );
585 int channels
= mlt_properties_get_int( properties
, "channels" );
586 int frequency
= mlt_properties_get_int( properties
, "frequency" );
590 // See if audio is turned off
591 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
593 // Get the maximum size of the buffer
594 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
596 // General frame variable
597 mlt_frame frame
= NULL
;
598 uint8_t *image
= NULL
;
603 // Average time for get_frame and get_image
606 int64_t time_wait
= 0;
607 int64_t time_frame
= 0;
608 int64_t time_process
= 0;
610 mlt_service lock_object
= NULL
;
612 if ( preview_off
&& preview_format
!= 0 )
613 this->format
= preview_format
;
615 // Get the first frame
616 frame
= mlt_consumer_get_frame( this );
618 // Get the lock object
619 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
622 if ( lock_object
) mlt_service_lock( lock_object
);
624 // Get the image of the first frame
627 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
628 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
633 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
634 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
637 // Unlock the lock object
638 if ( lock_object
) mlt_service_unlock( lock_object
);
641 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
643 // Get the starting time (can ignore the times above)
644 gettimeofday( &ante
, NULL
);
646 // Continue to read ahead
647 while ( this->ahead
)
649 // Fetch width/height again
650 width
= mlt_properties_get_int( properties
, "width" );
651 height
= mlt_properties_get_int( properties
, "height" );
653 // Put the current frame into the queue
654 pthread_mutex_lock( &this->mutex
);
655 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
656 pthread_cond_wait( &this->cond
, &this->mutex
);
657 mlt_deque_push_back( this->queue
, frame
);
658 pthread_cond_broadcast( &this->cond
);
659 pthread_mutex_unlock( &this->mutex
);
661 time_wait
+= time_difference( &ante
);
663 // Get the next frame
664 frame
= mlt_consumer_get_frame( this );
665 time_frame
+= time_difference( &ante
);
667 // If there's no frame, we're probably stopped...
671 // Attempt to fetch the lock object
672 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
674 // Increment the count
677 // Lock if there's a lock object
678 if ( lock_object
) mlt_service_lock( lock_object
);
680 // All non normal playback frames should be shown
681 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
683 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
684 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
695 if ( !skip_next
|| this->real_time
== -1 )
697 // Get the image, mark as rendered and time it
700 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
701 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
703 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
707 // Increment the number of sequentially skipped frames
711 // If we've reached an unacceptable level, reset everything
722 // Always process audio
725 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
726 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
729 // Increment the time take for this frame
730 time_process
+= time_difference( &ante
);
732 // Determine if the next frame should be skipped
733 if ( mlt_deque_count( this->queue
) <= 5 )
735 int frame_duration
= mlt_properties_get_int( properties
, "frame_duration" );
736 if ( ( ( time_wait
+ time_frame
+ time_process
) / count
) > frame_duration
)
740 // Unlock if there's a lock object
741 if ( lock_object
) mlt_service_unlock( lock_object
);
744 // Remove the last frame
745 mlt_frame_close( frame
);
750 /** Start the read/render thread.
752 * \private \memberof mlt_consumer_s
753 * \param this a consumer
756 static void consumer_read_ahead_start( mlt_consumer
this )
761 // Create the frame queue
762 this->queue
= mlt_deque_init( );
765 pthread_mutex_init( &this->mutex
, NULL
);
767 // Create the condition
768 pthread_cond_init( &this->cond
, NULL
);
770 // Create the read ahead
771 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
773 struct sched_param priority
;
774 priority
.sched_priority
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
775 pthread_attr_t thread_attributes
;
776 pthread_attr_init( &thread_attributes
);
777 pthread_attr_setschedpolicy( &thread_attributes
, SCHED_OTHER
);
778 pthread_attr_setschedparam( &thread_attributes
, &priority
);
779 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_EXPLICIT_SCHED
);
780 pthread_attr_setscope( &thread_attributes
, PTHREAD_SCOPE_SYSTEM
);
781 if ( pthread_create( &this->ahead_thread
, &thread_attributes
, consumer_read_ahead_thread
, this ) < 0 )
782 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
783 pthread_attr_destroy( &thread_attributes
);
787 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
791 /** Stop the read/render thread.
793 * \private \memberof mlt_consumer_s
794 * \param this a consumer
797 static void consumer_read_ahead_stop( mlt_consumer
this )
799 // Make sure we're running
802 // Inform thread to stop
805 // Broadcast to the condition in case it's waiting
806 pthread_mutex_lock( &this->mutex
);
807 pthread_cond_broadcast( &this->cond
);
808 pthread_mutex_unlock( &this->mutex
);
810 // Broadcast to the put condition in case it's waiting
811 pthread_mutex_lock( &this->put_mutex
);
812 pthread_cond_broadcast( &this->put_cond
);
813 pthread_mutex_unlock( &this->put_mutex
);
816 pthread_join( this->ahead_thread
, NULL
);
819 pthread_mutex_destroy( &this->mutex
);
821 // Destroy the condition
822 pthread_cond_destroy( &this->cond
);
825 while ( mlt_deque_count( this->queue
) )
826 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
829 mlt_deque_close( this->queue
);
833 /** Flush the read/render thread's buffer.
835 * \public \memberof mlt_consumer_s
836 * \param this a consumer
839 void mlt_consumer_purge( mlt_consumer
this )
843 pthread_mutex_lock( &this->mutex
);
844 while ( mlt_deque_count( this->queue
) )
845 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
846 pthread_cond_broadcast( &this->cond
);
847 pthread_mutex_unlock( &this->mutex
);
851 /** Get the next frame from the producer connected to a consumer.
853 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
854 * the asynchronous/real-time behavior configurable at runtime.
855 * You should close the frame returned from this when you are done with it.
857 * \public \memberof mlt_consumer_s
858 * \param this a consumer
862 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
865 mlt_frame frame
= NULL
;
867 // Get the properties
868 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
870 // Check if the user has requested real time or not
871 if ( this->real_time
)
875 // Is the read ahead running?
876 if ( this->ahead
== 0 )
878 int buffer
= mlt_properties_get_int( properties
, "buffer" );
879 int prefill
= mlt_properties_get_int( properties
, "prefill" );
880 consumer_read_ahead_start( this );
882 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
885 // Get frame from queue
886 pthread_mutex_lock( &this->mutex
);
887 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
888 pthread_cond_wait( &this->cond
, &this->mutex
);
889 frame
= mlt_deque_pop_front( this->queue
);
890 pthread_cond_broadcast( &this->cond
);
891 pthread_mutex_unlock( &this->mutex
);
895 // Get the frame in non real time
896 frame
= mlt_consumer_get_frame( this );
898 // This isn't true, but from the consumers perspective it is
900 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
906 /** Callback for the implementation to indicate a stopped condition.
908 * \public \memberof mlt_consumer_s
909 * \param this a consumer
912 void mlt_consumer_stopped( mlt_consumer
this )
914 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
915 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
916 mlt_event_unblock( this->event_listener
);
919 /** Stop the consumer.
921 * \public \memberof mlt_consumer_s
922 * \param this a consumer
923 * \return true if there was an error
926 int mlt_consumer_stop( mlt_consumer
this )
929 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
932 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping put waiting\n" );
933 pthread_mutex_lock( &this->put_mutex
);
934 this->put_active
= 0;
935 pthread_cond_broadcast( &this->put_cond
);
936 pthread_mutex_unlock( &this->put_mutex
);
939 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping consumer\n" );
940 if ( this->stop
!= NULL
)
943 // Check if the user has requested real time or not and stop if necessary
944 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopping read_ahead\n" );
945 if ( mlt_properties_get_int( properties
, "real_time" ) )
946 consumer_read_ahead_stop( this );
948 // Kill the test card
949 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
951 // Check and run a post command
952 if ( mlt_properties_get( properties
, "post" ) )
953 if (system( mlt_properties_get( properties
, "post" ) ) == -1 )
954 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_ERROR
, "system(%s) failed!\n", mlt_properties_get( properties
, "post" ) );
956 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG
, "stopped\n" );
961 /** Determine if the consumer is stopped.
963 * \public \memberof mlt_consumer_s
964 * \param this a consumer
965 * \return true if the consumer is stopped
968 int mlt_consumer_is_stopped( mlt_consumer
this )
970 // Check if the consumer is stopped
971 if ( this->is_stopped
!= NULL
)
972 return this->is_stopped( this );
977 /** Close and destroy the consumer.
979 * \public \memberof mlt_consumer_s
980 * \param this a consumer
983 void mlt_consumer_close( mlt_consumer
this )
985 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
987 // Get the childs close function
988 void ( *consumer_close
)( ) = this->close
;
990 if ( consumer_close
)
993 //mlt_consumer_stop( this );
996 consumer_close( this );
1000 // Make sure it only gets called once
1001 this->parent
.close
= NULL
;
1003 // Destroy the push mutex and condition
1004 pthread_mutex_destroy( &this->put_mutex
);
1005 pthread_cond_destroy( &this->put_cond
);
1007 mlt_service_close( &this->parent
);