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
21 #include "mlt_consumer.h"
22 #include "mlt_factory.h"
23 #include "mlt_producer.h"
24 #include "mlt_frame.h"
25 #include "mlt_profile.h"
32 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
34 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
35 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
36 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
);
37 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
);
39 /** Public final methods
42 int mlt_consumer_init( mlt_consumer
this, void *child
, mlt_profile profile
)
45 memset( this, 0, sizeof( struct mlt_consumer_s
) );
47 error
= mlt_service_init( &this->parent
, this );
50 // Get the properties from the service
51 mlt_properties properties
= MLT_SERVICE_PROPERTIES( &this->parent
);
53 // Apply profile to properties
54 if ( profile
== NULL
)
56 // Normally the application creates the profile and controls its lifetime
57 // This is the fallback exception handling
58 profile
= mlt_profile_init( NULL
);
59 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
60 mlt_properties_set_data( properties
, "_profile", profile
, 0, (mlt_destructor
)mlt_profile_close
, NULL
);
62 apply_profile_properties( this, profile
, properties
);
64 // Default rescaler for all consumers
65 mlt_properties_set( properties
, "rescale", "bilinear" );
67 // Default read ahead buffer size
68 mlt_properties_set_int( properties
, "buffer", 25 );
70 // Default audio frequency and channels
71 mlt_properties_set_int( properties
, "frequency", 48000 );
72 mlt_properties_set_int( properties
, "channels", 2 );
74 // Default of all consumers is real time
75 mlt_properties_set_int( properties
, "real_time", 1 );
77 // Default to environment test card
78 mlt_properties_set( properties
, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
80 // Hmm - default all consumers to yuv422 :-/
81 this->format
= mlt_image_yuv422
;
83 mlt_events_register( properties
, "consumer-frame-show", ( mlt_transmitter
)mlt_consumer_frame_show
);
84 mlt_events_register( properties
, "consumer-frame-render", ( mlt_transmitter
)mlt_consumer_frame_render
);
85 mlt_events_register( properties
, "consumer-stopped", NULL
);
87 // Register a property-changed listener to handle the profile property -
88 // subsequent properties can override the profile
89 this->event_listener
= mlt_events_listen( properties
, this, "property-changed", ( mlt_listener
)mlt_consumer_property_changed
);
91 // Create the push mutex and condition
92 pthread_mutex_init( &this->put_mutex
, NULL
);
93 pthread_cond_init( &this->put_cond
, NULL
);
99 static void apply_profile_properties( mlt_consumer
this, mlt_profile profile
, mlt_properties properties
)
101 mlt_event_block( this->event_listener
);
102 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
103 mlt_properties_set_int( properties
, "frame_rate_num", profile
->frame_rate_num
);
104 mlt_properties_set_int( properties
, "frame_rate_den", profile
->frame_rate_den
);
105 mlt_properties_set_int( properties
, "width", profile
->width
);
106 mlt_properties_set_int( properties
, "height", profile
->height
);
107 mlt_properties_set_int( properties
, "progressive", profile
->progressive
);
108 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
109 mlt_properties_set_int( properties
, "sample_aspect_num", profile
->sample_aspect_num
);
110 mlt_properties_set_int( properties
, "sample_aspect_den", profile
->sample_aspect_den
);
111 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
112 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
113 mlt_properties_set_int( properties
, "display_aspect_num", profile
->display_aspect_num
);
114 mlt_event_unblock( this->event_listener
);
117 static void mlt_consumer_property_changed( mlt_service owner
, mlt_consumer
this, char *name
)
119 if ( !strcmp( name
, "profile" ) )
122 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
124 // Get the current profile
125 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
127 // Load the new profile
128 mlt_profile new_profile
= mlt_profile_init( mlt_properties_get( properties
, name
) );
133 if ( profile
!= NULL
)
135 free( profile
->description
);
136 memcpy( profile
, new_profile
, sizeof( struct mlt_profile_s
) );
137 profile
->description
= strdup( new_profile
->description
);
138 mlt_profile_close( new_profile
);
142 profile
= new_profile
;
145 // Apply to properties
146 apply_profile_properties( this, profile
, properties
);
149 else if ( !strcmp( name
, "frame_rate_num" ) )
151 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
152 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
155 profile
->frame_rate_num
= mlt_properties_get_int( properties
, "frame_rate_num" );
156 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
159 else if ( !strcmp( name
, "frame_rate_den" ) )
161 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
162 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
165 profile
->frame_rate_den
= mlt_properties_get_int( properties
, "frame_rate_den" );
166 mlt_properties_set_double( properties
, "fps", mlt_profile_fps( profile
) );
169 else if ( !strcmp( name
, "width" ) )
171 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
172 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
174 profile
->width
= mlt_properties_get_int( properties
, "width" );
176 else if ( !strcmp( name
, "height" ) )
178 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
179 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
181 profile
->height
= mlt_properties_get_int( properties
, "height" );
183 else if ( !strcmp( name
, "progressive" ) )
185 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
186 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
188 profile
->progressive
= mlt_properties_get_int( properties
, "progressive" );
190 else if ( !strcmp( name
, "sample_aspect_num" ) )
192 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
193 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
194 profile
->sample_aspect_num
= mlt_properties_get_int( properties
, "sample_aspect_num" );
196 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
198 else if ( !strcmp( name
, "sample_aspect_den" ) )
200 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
201 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
202 profile
->sample_aspect_den
= mlt_properties_get_int( properties
, "sample_aspect_den" );
204 mlt_properties_set_double( properties
, "aspect_ratio", mlt_profile_sar( profile
) );
206 else if ( !strcmp( name
, "display_aspect_num" ) )
208 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
209 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
212 profile
->display_aspect_num
= mlt_properties_get_int( properties
, "display_aspect_num" );
213 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
216 else if ( !strcmp( name
, "display_aspect_den" ) )
218 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
219 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
222 profile
->display_aspect_den
= mlt_properties_get_int( properties
, "display_aspect_den" );
223 mlt_properties_set_double( properties
, "display_ratio", mlt_profile_dar( profile
) );
228 static void mlt_consumer_frame_show( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
230 if ( listener
!= NULL
)
231 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
234 static void mlt_consumer_frame_render( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
236 if ( listener
!= NULL
)
237 listener( owner
, this, ( mlt_frame
)args
[ 0 ] );
240 /** Create a new consumer.
243 mlt_consumer
mlt_consumer_new( mlt_profile profile
)
245 // Create the memory for the structure
246 mlt_consumer
this = malloc( sizeof( struct mlt_consumer_s
) );
250 mlt_consumer_init( this, NULL
, profile
);
256 /** Get the parent service object.
259 mlt_service
mlt_consumer_service( mlt_consumer
this )
261 return this != NULL ?
&this->parent
: NULL
;
264 /** Get the consumer properties.
267 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
269 return this != NULL ?
MLT_SERVICE_PROPERTIES( &this->parent
) : NULL
;
272 /** Connect the consumer to the producer.
275 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
277 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
280 /** Start the consumer.
283 int mlt_consumer_start( mlt_consumer
this )
285 // Stop listening to the property-changed event
286 mlt_event_block( this->event_listener
);
289 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
291 // Determine if there's a test card producer
292 char *test_card
= mlt_properties_get( properties
, "test_card" );
294 // Just to make sure nothing is hanging around...
295 mlt_frame_close( this->put
);
297 this->put_active
= 1;
300 if ( test_card
!= NULL
)
302 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
304 // Create a test card producer
305 mlt_profile profile
= mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
306 mlt_producer producer
= mlt_factory_producer( profile
, NULL
, test_card
);
308 // Do we have a producer
309 if ( producer
!= NULL
)
311 // Test card should loop I guess...
312 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer
), "eof", "loop" );
313 //mlt_producer_set_speed( producer, 0 );
314 //mlt_producer_set_in_and_out( producer, 0, 0 );
316 // Set the test card on the consumer
317 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
323 // Allow the hash table to speed things up
324 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
327 // Set the frame duration in microseconds for the frame-dropping heuristic
328 int frame_duration
= 1000000 / mlt_properties_get_int( properties
, "frame_rate_num" ) *
329 mlt_properties_get_int( properties
, "frame_rate_den" );
330 mlt_properties_set_int( properties
, "frame_duration", frame_duration
);
332 // Check and run an ante command
333 if ( mlt_properties_get( properties
, "ante" ) )
334 system( mlt_properties_get( properties
, "ante" ) );
336 // Set the real_time preference
337 this->real_time
= mlt_properties_get_int( properties
, "real_time" );
340 if ( this->start
!= NULL
)
341 return this->start( this );
346 /** An alternative method to feed frames into the consumer - only valid if
347 the consumer itself is not connected.
350 int mlt_consumer_put_frame( mlt_consumer
this, mlt_frame frame
)
354 // Get the service assoicated to the consumer
355 mlt_service service
= MLT_CONSUMER_SERVICE( this );
357 if ( mlt_service_producer( service
) == NULL
)
361 pthread_mutex_lock( &this->put_mutex
);
362 while ( this->put_active
&& this->put
!= NULL
)
364 gettimeofday( &now
, NULL
);
365 tm
.tv_sec
= now
.tv_sec
+ 1;
366 tm
.tv_nsec
= now
.tv_usec
* 1000;
367 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
369 if ( this->put_active
&& this->put
== NULL
)
372 mlt_frame_close( frame
);
373 pthread_cond_broadcast( &this->put_cond
);
374 pthread_mutex_unlock( &this->put_mutex
);
378 mlt_frame_close( frame
);
384 /** Protected method for consumer to get frames from connected service
387 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
390 mlt_frame frame
= NULL
;
392 // Get the service assoicated to the consumer
393 mlt_service service
= MLT_CONSUMER_SERVICE( this );
395 // Get the consumer properties
396 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
399 if ( mlt_service_producer( service
) == NULL
&& mlt_properties_get_int( properties
, "put_mode" ) )
403 pthread_mutex_lock( &this->put_mutex
);
404 while ( this->put_active
&& this->put
== NULL
)
406 gettimeofday( &now
, NULL
);
407 tm
.tv_sec
= now
.tv_sec
+ 1;
408 tm
.tv_nsec
= now
.tv_usec
* 1000;
409 pthread_cond_timedwait( &this->put_cond
, &this->put_mutex
, &tm
);
413 pthread_cond_broadcast( &this->put_cond
);
414 pthread_mutex_unlock( &this->put_mutex
);
416 mlt_service_apply_filters( service
, frame
, 0 );
418 else if ( mlt_service_producer( service
) != NULL
)
420 mlt_service_get_frame( service
, &frame
, 0 );
424 frame
= mlt_frame_init( service
);
429 // Get the frame properties
430 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
432 // Get the test card producer
433 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
435 // Attach the test frame producer to it.
436 if ( test_card
!= NULL
)
437 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
439 // Attach the rescale property
440 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
442 // Aspect ratio and other jiggery pokery
443 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
444 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "progressive" ) | mlt_properties_get_int( properties
, "deinterlace" ) );
445 mlt_properties_set( frame_properties
, "deinterlace_method", mlt_properties_get( properties
, "deinterlace_method" ) );
452 static inline long time_difference( struct timeval
*time1
)
454 struct timeval time2
;
455 time2
.tv_sec
= time1
->tv_sec
;
456 time2
.tv_usec
= time1
->tv_usec
;
457 gettimeofday( time1
, NULL
);
458 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
461 static void *consumer_read_ahead_thread( void *arg
)
463 // The argument is the consumer
464 mlt_consumer
this = arg
;
466 // Get the properties of the consumer
467 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
469 // Get the width and height
470 int width
= mlt_properties_get_int( properties
, "width" );
471 int height
= mlt_properties_get_int( properties
, "height" );
473 // See if video is turned off
474 int video_off
= mlt_properties_get_int( properties
, "video_off" );
475 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
476 int preview_format
= mlt_properties_get_int( properties
, "preview_format" );
478 // Get the audio settings
479 mlt_audio_format afmt
= mlt_audio_pcm
;
481 double fps
= mlt_properties_get_double( properties
, "fps" );
482 int channels
= mlt_properties_get_int( properties
, "channels" );
483 int frequency
= mlt_properties_get_int( properties
, "frequency" );
487 // See if audio is turned off
488 int audio_off
= mlt_properties_get_int( properties
, "audio_off" );
490 // Get the maximum size of the buffer
491 int buffer
= mlt_properties_get_int( properties
, "buffer" ) + 1;
493 // General frame variable
494 mlt_frame frame
= NULL
;
495 uint8_t *image
= NULL
;
500 // Average time for get_frame and get_image
503 int64_t time_wait
= 0;
504 int64_t time_frame
= 0;
505 int64_t time_process
= 0;
507 mlt_service lock_object
= NULL
;
509 if ( preview_off
&& preview_format
!= 0 )
510 this->format
= preview_format
;
512 // Get the first frame
513 frame
= mlt_consumer_get_frame( this );
515 // Get the lock object
516 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
519 if ( lock_object
) mlt_service_lock( lock_object
);
521 // Get the image of the first frame
524 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
525 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
530 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
531 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
534 // Unlock the lock object
535 if ( lock_object
) mlt_service_unlock( lock_object
);
538 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
540 // Get the starting time (can ignore the times above)
541 gettimeofday( &ante
, NULL
);
543 // Continue to read ahead
544 while ( this->ahead
)
546 // Fetch width/height again
547 width
= mlt_properties_get_int( properties
, "width" );
548 height
= mlt_properties_get_int( properties
, "height" );
550 // Put the current frame into the queue
551 pthread_mutex_lock( &this->mutex
);
552 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
553 pthread_cond_wait( &this->cond
, &this->mutex
);
554 mlt_deque_push_back( this->queue
, frame
);
555 pthread_cond_broadcast( &this->cond
);
556 pthread_mutex_unlock( &this->mutex
);
558 time_wait
+= time_difference( &ante
);
560 // Get the next frame
561 frame
= mlt_consumer_get_frame( this );
562 time_frame
+= time_difference( &ante
);
564 // If there's no frame, we're probably stopped...
568 // Attempt to fetch the lock object
569 lock_object
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "consumer_lock_service", NULL
);
571 // Increment the count
574 // Lock if there's a lock object
575 if ( lock_object
) mlt_service_lock( lock_object
);
577 // All non normal playback frames should be shown
578 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame
), "_speed" ) != 1 )
580 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
581 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "consumer_deinterlace", 1 );
592 if ( !skip_next
|| this->real_time
== -1 )
594 // Get the image, mark as rendered and time it
597 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame
, NULL
);
598 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
600 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
604 // Increment the number of sequentially skipped frames
608 // If we've reached an unacceptable level, reset everything
619 // Always process audio
622 samples
= mlt_sample_calculator( fps
, frequency
, counter
++ );
623 mlt_frame_get_audio( frame
, &pcm
, &afmt
, &frequency
, &channels
, &samples
);
626 // Increment the time take for this frame
627 time_process
+= time_difference( &ante
);
629 // Determine if the next frame should be skipped
630 if ( mlt_deque_count( this->queue
) <= 5 )
632 int frame_duration
= mlt_properties_get_int( properties
, "frame_duration" );
633 if ( ( ( time_wait
+ time_frame
+ time_process
) / count
) > frame_duration
)
637 // Unlock if there's a lock object
638 if ( lock_object
) mlt_service_unlock( lock_object
);
641 // Remove the last frame
642 mlt_frame_close( frame
);
647 static void consumer_read_ahead_start( mlt_consumer
this )
652 // Create the frame queue
653 this->queue
= mlt_deque_init( );
656 pthread_mutex_init( &this->mutex
, NULL
);
658 // Create the condition
659 pthread_cond_init( &this->cond
, NULL
);
661 // Create the read ahead
662 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
664 struct sched_param priority
;
665 priority
.sched_priority
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
666 pthread_attr_t thread_attributes
;
667 pthread_attr_init( &thread_attributes
);
668 pthread_attr_setschedpolicy( &thread_attributes
, SCHED_OTHER
);
669 pthread_attr_setschedparam( &thread_attributes
, &priority
);
670 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_EXPLICIT_SCHED
);
671 pthread_attr_setscope( &thread_attributes
, PTHREAD_SCOPE_SYSTEM
);
672 if ( pthread_create( &this->ahead_thread
, &thread_attributes
, consumer_read_ahead_thread
, this ) < 0 )
673 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
674 pthread_attr_destroy( &thread_attributes
);
678 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
682 static void consumer_read_ahead_stop( mlt_consumer
this )
684 // Make sure we're running
687 // Inform thread to stop
690 // Broadcast to the condition in case it's waiting
691 pthread_mutex_lock( &this->mutex
);
692 pthread_cond_broadcast( &this->cond
);
693 pthread_mutex_unlock( &this->mutex
);
695 // Broadcast to the put condition in case it's waiting
696 pthread_mutex_lock( &this->put_mutex
);
697 pthread_cond_broadcast( &this->put_cond
);
698 pthread_mutex_unlock( &this->put_mutex
);
701 pthread_join( this->ahead_thread
, NULL
);
704 pthread_mutex_destroy( &this->mutex
);
706 // Destroy the condition
707 pthread_cond_destroy( &this->cond
);
710 while ( mlt_deque_count( this->queue
) )
711 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
714 mlt_deque_close( this->queue
);
718 void mlt_consumer_purge( mlt_consumer
this )
722 pthread_mutex_lock( &this->mutex
);
723 while ( mlt_deque_count( this->queue
) )
724 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
725 pthread_cond_broadcast( &this->cond
);
726 pthread_mutex_unlock( &this->mutex
);
730 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
733 mlt_frame frame
= NULL
;
735 // Get the properties
736 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
738 // Check if the user has requested real time or not
739 if ( this->real_time
)
743 // Is the read ahead running?
744 if ( this->ahead
== 0 )
746 int buffer
= mlt_properties_get_int( properties
, "buffer" );
747 int prefill
= mlt_properties_get_int( properties
, "prefill" );
748 consumer_read_ahead_start( this );
750 size
= prefill
> 0 && prefill
< buffer ? prefill
: buffer
;
753 // Get frame from queue
754 pthread_mutex_lock( &this->mutex
);
755 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
756 pthread_cond_wait( &this->cond
, &this->mutex
);
757 frame
= mlt_deque_pop_front( this->queue
);
758 pthread_cond_broadcast( &this->cond
);
759 pthread_mutex_unlock( &this->mutex
);
763 // Get the frame in non real time
764 frame
= mlt_consumer_get_frame( this );
766 // This isn't true, but from the consumers perspective it is
768 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "rendered", 1 );
774 /** Callback for the implementation to indicate a stopped condition.
777 void mlt_consumer_stopped( mlt_consumer
this )
779 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
780 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL
);
781 mlt_event_unblock( this->event_listener
);
784 /** Stop the consumer.
787 int mlt_consumer_stop( mlt_consumer
this )
790 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( this );
791 char *debug
= mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
794 if ( debug
) fprintf( stderr
, "%s: stopping put waiting\n", debug
);
795 pthread_mutex_lock( &this->put_mutex
);
796 this->put_active
= 0;
797 pthread_cond_broadcast( &this->put_cond
);
798 pthread_mutex_unlock( &this->put_mutex
);
801 if ( debug
) fprintf( stderr
, "%s: stopping consumer\n", debug
);
802 if ( this->stop
!= NULL
)
805 // Check if the user has requested real time or not and stop if necessary
806 if ( debug
) fprintf( stderr
, "%s: stopping read_ahead\n", debug
);
807 if ( mlt_properties_get_int( properties
, "real_time" ) )
808 consumer_read_ahead_stop( this );
810 // Kill the test card
811 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
813 // Check and run a post command
814 if ( mlt_properties_get( properties
, "post" ) )
815 system( mlt_properties_get( properties
, "post" ) );
817 if ( debug
) fprintf( stderr
, "%s: stopped\n", debug
);
822 /** Determine if the consumer is stopped.
825 int mlt_consumer_is_stopped( mlt_consumer
this )
827 // Check if the consumer is stopped
828 if ( this->is_stopped
!= NULL
)
829 return this->is_stopped( this );
834 /** Close the consumer.
837 void mlt_consumer_close( mlt_consumer
this )
839 if ( this != NULL
&& mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
841 // Get the childs close function
842 void ( *consumer_close
)( ) = this->close
;
844 if ( consumer_close
)
847 //mlt_consumer_stop( this );
850 consumer_close( this );
854 // Make sure it only gets called once
855 this->parent
.close
= NULL
;
857 // Destroy the push mutex and condition
858 pthread_mutex_destroy( &this->put_mutex
);
859 pthread_cond_destroy( &this->put_cond
);
861 mlt_service_close( &this->parent
);