]> git.sesse.net Git - mlt/blob - src/framework/mlt_consumer.c
mlt_profile.c: bugfix string allocation length
[mlt] / src / framework / mlt_consumer.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include "mlt_profile.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32
33 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
34 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
35 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name );
36 static void apply_profile_properties( mlt_profile profile, mlt_properties properties );
37
38 static mlt_event g_event_listener = NULL;
39
40 /** Public final methods
41 */
42
43 int mlt_consumer_init( mlt_consumer this, void *child )
44 {
45         int error = 0;
46         memset( this, 0, sizeof( struct mlt_consumer_s ) );
47         this->child = child;
48         error = mlt_service_init( &this->parent, this );
49         if ( error == 0 )
50         {
51                 // Get the properties from the service
52                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
53         
54                 // Apply the profile to properties for legacy integration
55                 apply_profile_properties( mlt_profile_get(), properties );
56
57                 // Default rescaler for all consumers
58                 mlt_properties_set( properties, "rescale", "bilinear" );
59
60                 // Default read ahead buffer size
61                 mlt_properties_set_int( properties, "buffer", 25 );
62
63                 // Default audio frequency and channels
64                 mlt_properties_set_int( properties, "frequency", 48000 );
65                 mlt_properties_set_int( properties, "channels", 2 );
66
67                 // Default of all consumers is real time
68                 mlt_properties_set_int( properties, "real_time", 1 );
69
70                 // Default to environment test card
71                 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
72
73                 // Hmm - default all consumers to yuv422 :-/
74                 this->format = mlt_image_yuv422;
75
76                 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
77                 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
78                 mlt_events_register( properties, "consumer-stopped", NULL );
79
80                 // Register a property-changed listener to handle the profile property -
81                 // subsequent properties can override the profile
82                 g_event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
83
84                 // Create the push mutex and condition
85                 pthread_mutex_init( &this->put_mutex, NULL );
86                 pthread_cond_init( &this->put_cond, NULL );
87
88         }
89         return error;
90 }
91
92 static void apply_profile_properties( mlt_profile profile, mlt_properties properties )
93 {
94         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
95         mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
96         mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
97         mlt_properties_set_int( properties, "width", profile->width );
98         mlt_properties_set_int( properties, "height", profile->height );
99         mlt_properties_set_int( properties, "progressive", profile->progressive );
100         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
101         mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
102         mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
103         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
104         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
105         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
106 }
107
108 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
109 {
110         if ( !strcmp( name, "profile" ) )
111         {
112                 // Get the properies
113                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
114
115                 // Locate the profile
116                 mlt_profile_select( mlt_properties_get( properties, "profile" ) );
117
118                 // Stop listening to this
119                 mlt_event_block( g_event_listener );
120
121                 // Apply to properties
122                 apply_profile_properties( mlt_profile_get(), properties );
123         }
124 }
125
126 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
127 {
128         if ( listener != NULL )
129                 listener( owner, this, ( mlt_frame )args[ 0 ] );
130 }
131
132 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
133 {
134         if ( listener != NULL )
135                 listener( owner, this, ( mlt_frame )args[ 0 ] );
136 }
137
138 /** Create a new consumer.
139 */
140
141 mlt_consumer mlt_consumer_new( )
142 {
143         // Create the memory for the structure
144         mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
145
146         // Initialise it
147         if ( this != NULL )
148                 mlt_consumer_init( this, NULL );
149
150         // Return it
151         return this;
152 }
153
154 /** Get the parent service object.
155 */
156
157 mlt_service mlt_consumer_service( mlt_consumer this )
158 {
159         return this != NULL ? &this->parent : NULL;
160 }
161
162 /** Get the consumer properties.
163 */
164
165 mlt_properties mlt_consumer_properties( mlt_consumer this )
166 {
167         return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
168 }
169
170 /** Connect the consumer to the producer.
171 */
172
173 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
174 {
175         return mlt_service_connect_producer( &this->parent, producer, 0 );
176 }
177
178 /** Start the consumer.
179 */
180
181 int mlt_consumer_start( mlt_consumer this )
182 {
183         // Get the properies
184         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
185
186         // Determine if there's a test card producer
187         char *test_card = mlt_properties_get( properties, "test_card" );
188
189         // Just to make sure nothing is hanging around...
190         mlt_frame_close( this->put );
191         this->put = NULL;
192         this->put_active = 1;
193
194         // Deal with it now.
195         if ( test_card != NULL )
196         {
197                 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
198                 {
199                         // Create a test card producer
200                         mlt_producer producer = mlt_factory_producer( NULL, test_card );
201
202                         // Do we have a producer
203                         if ( producer != NULL )
204                         {
205                                 // Test card should loop I guess...
206                                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
207                                 //mlt_producer_set_speed( producer, 0 );
208                                 //mlt_producer_set_in_and_out( producer, 0, 0 );
209
210                                 // Set the test card on the consumer
211                                 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
212                         }
213                 }
214         }
215         else
216         {
217                 // Allow the hash table to speed things up
218                 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
219         }
220
221         // Check and run an ante command
222         if ( mlt_properties_get( properties, "ante" ) )
223                 system( mlt_properties_get( properties, "ante" ) );
224
225         // Set the real_time preference
226         this->real_time = mlt_properties_get_int( properties, "real_time" );
227
228         // Start the service
229         if ( this->start != NULL )
230                 return this->start( this );
231
232         return 0;
233 }
234
235 /** An alternative method to feed frames into the consumer - only valid if
236         the consumer itself is not connected.
237 */
238
239 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
240 {
241         int error = 1;
242
243         // Get the service assoicated to the consumer
244         mlt_service service = MLT_CONSUMER_SERVICE( this );
245
246         if ( mlt_service_producer( service ) == NULL )
247         {
248                 struct timeval now;
249                 struct timespec tm;
250                 pthread_mutex_lock( &this->put_mutex );
251                 while ( this->put_active && this->put != NULL )
252                 {
253                         gettimeofday( &now, NULL );
254                         tm.tv_sec = now.tv_sec + 1;
255                         tm.tv_nsec = now.tv_usec * 1000;
256                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
257                 }
258                 if ( this->put_active && this->put == NULL )
259                         this->put = frame;
260                 else
261                         mlt_frame_close( frame );
262                 pthread_cond_broadcast( &this->put_cond );
263                 pthread_mutex_unlock( &this->put_mutex );
264         }
265         else
266         {
267                 mlt_frame_close( frame );
268         }
269
270         return error;
271 }
272
273 /** Protected method for consumer to get frames from connected service
274 */
275
276 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
277 {
278         // Frame to return
279         mlt_frame frame = NULL;
280
281         // Get the service assoicated to the consumer
282         mlt_service service = MLT_CONSUMER_SERVICE( this );
283
284         // Get the consumer properties
285         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
286
287         // Get the frame
288         if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
289         {
290                 struct timeval now;
291                 struct timespec tm;
292                 pthread_mutex_lock( &this->put_mutex );
293                 while ( this->put_active && this->put == NULL )
294                 {
295                         gettimeofday( &now, NULL );
296                         tm.tv_sec = now.tv_sec + 1;
297                         tm.tv_nsec = now.tv_usec * 1000;
298                         pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
299                 }
300                 frame = this->put;
301                 this->put = NULL;
302                 pthread_cond_broadcast( &this->put_cond );
303                 pthread_mutex_unlock( &this->put_mutex );
304                 if ( frame != NULL )
305                         mlt_service_apply_filters( service, frame, 0 );
306         }
307         else if ( mlt_service_producer( service ) != NULL )
308         {
309                 mlt_service_get_frame( service, &frame, 0 );
310         }
311         else
312         {
313                 frame = mlt_frame_init( );
314         }
315
316         if ( frame != NULL )
317         {
318                 // Get the frame properties
319                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
320
321                 // Get the test card producer
322                 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
323
324                 // Attach the test frame producer to it.
325                 if ( test_card != NULL )
326                         mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
327
328                 // Attach the rescale property
329                 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
330
331                 // Aspect ratio and other jiggery pokery
332                 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
333                 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
334                 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
335         }
336
337         // Return the frame
338         return frame;
339 }
340
341 static inline long time_difference( struct timeval *time1 )
342 {
343         struct timeval time2;
344         time2.tv_sec = time1->tv_sec;
345         time2.tv_usec = time1->tv_usec;
346         gettimeofday( time1, NULL );
347         return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
348 }
349
350 int mlt_consumer_profile( mlt_properties properties, char *profile )
351 {
352         mlt_profile p = mlt_profile_select( profile );
353         if ( p )
354         {
355                 apply_profile_properties( p, properties );
356                 return 1;
357         }
358         else
359         {
360                 return 0;
361         }
362 }
363
364 static void *consumer_read_ahead_thread( void *arg )
365 {
366         // The argument is the consumer
367         mlt_consumer this = arg;
368
369         // Get the properties of the consumer
370         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
371
372         // Get the width and height
373         int width = mlt_properties_get_int( properties, "width" );
374         int height = mlt_properties_get_int( properties, "height" );
375
376         // See if video is turned off
377         int video_off = mlt_properties_get_int( properties, "video_off" );
378         int preview_off = mlt_properties_get_int( properties, "preview_off" );
379         int preview_format = mlt_properties_get_int( properties, "preview_format" );
380
381         // Get the audio settings
382         mlt_audio_format afmt = mlt_audio_pcm;
383         int counter = 0;
384         double fps = mlt_properties_get_double( properties, "fps" );
385         int channels = mlt_properties_get_int( properties, "channels" );
386         int frequency = mlt_properties_get_int( properties, "frequency" );
387         int samples = 0;
388         int16_t *pcm = NULL;
389
390         // See if audio is turned off
391         int audio_off = mlt_properties_get_int( properties, "audio_off" );
392
393         // Get the maximum size of the buffer
394         int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
395
396         // General frame variable
397         mlt_frame frame = NULL;
398         uint8_t *image = NULL;
399
400         // Time structures
401         struct timeval ante;
402
403         // Average time for get_frame and get_image
404         int count = 1;
405         int skipped = 0;
406         int64_t time_wait = 0;
407         int64_t time_frame = 0;
408         int64_t time_process = 0;
409         int skip_next = 0;
410         mlt_service lock_object = NULL;
411
412         if ( preview_off && preview_format != 0 )
413                 this->format = preview_format;
414
415         // Get the first frame
416         frame = mlt_consumer_get_frame( this );
417
418         // Get the lock object
419         lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
420
421         // Lock it
422         if ( lock_object ) mlt_service_lock( lock_object );
423
424         // Get the image of the first frame
425         if ( !video_off )
426         {
427                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
428                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
429         }
430
431         if ( !audio_off )
432         {
433                 samples = mlt_sample_calculator( fps, frequency, counter++ );
434                 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
435         }
436
437         // Unlock the lock object
438         if ( lock_object ) mlt_service_unlock( lock_object );
439
440         // Mark as rendered
441         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
442
443         // Get the starting time (can ignore the times above)
444         gettimeofday( &ante, NULL );
445
446         // Continue to read ahead
447         while ( this->ahead )
448         {
449                 // Fetch width/height again
450                 width = mlt_properties_get_int( properties, "width" );
451                 height = mlt_properties_get_int( properties, "height" );
452
453                 // Put the current frame into the queue
454                 pthread_mutex_lock( &this->mutex );
455                 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
456                         pthread_cond_wait( &this->cond, &this->mutex );
457                 mlt_deque_push_back( this->queue, frame );
458                 pthread_cond_broadcast( &this->cond );
459                 pthread_mutex_unlock( &this->mutex );
460
461                 time_wait += time_difference( &ante );
462
463                 // Get the next frame
464                 frame = mlt_consumer_get_frame( this );
465                 time_frame += time_difference( &ante );
466
467                 // If there's no frame, we're probably stopped...
468                 if ( frame == NULL )
469                         continue;
470
471                 // Attempt to fetch the lock object
472                 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
473
474                 // Increment the count
475                 count ++;
476
477                 // Lock if there's a lock object
478                 if ( lock_object ) mlt_service_lock( lock_object );
479
480                 // All non normal playback frames should be shown
481                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
482                 {
483                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
484                         skipped = 0;
485                         time_frame = 0;
486                         time_process = 0;
487                         time_wait = 0;
488                         count = 1;
489                         skip_next = 0;
490                 }
491
492                 // Get the image
493                 if ( !skip_next )
494                 {
495                         // Get the image, mark as rendered and time it
496                         if ( !video_off )
497                         {
498                                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
499                                 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
500                         }
501                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
502                 }
503                 else
504                 {
505                         // Increment the number of sequentially skipped frames
506                         skipped ++;
507                         skip_next = 0;
508
509                         // If we've reached an unacceptable level, reset everything
510                         if ( skipped > 5 )
511                         {
512                                 skipped = 0;
513                                 time_frame = 0;
514                                 time_process = 0;
515                                 time_wait = 0;
516                                 count = 1;
517                         }
518                 }
519
520                 // Always process audio
521                 if ( !audio_off )
522                 {
523                         samples = mlt_sample_calculator( fps, frequency, counter++ );
524                         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
525                 }
526
527                 // Increment the time take for this frame
528                 time_process += time_difference( &ante );
529
530                 // Determine if the next frame should be skipped
531                 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
532                         skip_next = 1;
533
534                 // Unlock if there's a lock object
535                 if ( lock_object ) mlt_service_unlock( lock_object );
536         }
537
538         // Remove the last frame
539         mlt_frame_close( frame );
540
541         return NULL;
542 }
543
544 static void consumer_read_ahead_start( mlt_consumer this )
545 {
546         // We're running now
547         this->ahead = 1;
548
549         // Create the frame queue
550         this->queue = mlt_deque_init( );
551
552         // Create the mutex
553         pthread_mutex_init( &this->mutex, NULL );
554
555         // Create the condition
556         pthread_cond_init( &this->cond, NULL );
557
558         // Create the read ahead 
559         pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
560 }
561
562 static void consumer_read_ahead_stop( mlt_consumer this )
563 {
564         // Make sure we're running
565         if ( this->ahead )
566         {
567                 // Inform thread to stop
568                 this->ahead = 0;
569
570                 // Broadcast to the condition in case it's waiting
571                 pthread_mutex_lock( &this->mutex );
572                 pthread_cond_broadcast( &this->cond );
573                 pthread_mutex_unlock( &this->mutex );
574
575                 // Broadcast to the put condition in case it's waiting
576                 pthread_mutex_lock( &this->put_mutex );
577                 pthread_cond_broadcast( &this->put_cond );
578                 pthread_mutex_unlock( &this->put_mutex );
579
580                 // Join the thread
581                 pthread_join( this->ahead_thread, NULL );
582
583                 // Destroy the mutex
584                 pthread_mutex_destroy( &this->mutex );
585
586                 // Destroy the condition
587                 pthread_cond_destroy( &this->cond );
588
589                 // Wipe the queue
590                 while ( mlt_deque_count( this->queue ) )
591                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
592
593                 // Close the queue
594                 mlt_deque_close( this->queue );
595         }
596 }
597
598 void mlt_consumer_purge( mlt_consumer this )
599 {
600         if ( this->ahead )
601         {
602                 pthread_mutex_lock( &this->mutex );
603                 while ( mlt_deque_count( this->queue ) )
604                         mlt_frame_close( mlt_deque_pop_back( this->queue ) );
605                 pthread_cond_broadcast( &this->cond );
606                 pthread_mutex_unlock( &this->mutex );
607         }
608 }
609
610 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
611 {
612         // Frame to return
613         mlt_frame frame = NULL;
614
615         // Get the properties
616         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
617
618         // Check if the user has requested real time or not
619         if ( this->real_time )
620         {
621                 int size = 1;
622
623                 // Is the read ahead running?
624                 if ( this->ahead == 0 )
625                 {
626                         int buffer = mlt_properties_get_int( properties, "buffer" );
627                         int prefill = mlt_properties_get_int( properties, "prefill" );
628                         consumer_read_ahead_start( this );
629                         if ( buffer > 1 )
630                                 size = prefill > 0 && prefill < buffer ? prefill : buffer;
631                 }
632         
633                 // Get frame from queue
634                 pthread_mutex_lock( &this->mutex );
635                 while( this->ahead && mlt_deque_count( this->queue ) < size )
636                         pthread_cond_wait( &this->cond, &this->mutex );
637                 frame = mlt_deque_pop_front( this->queue );
638                 pthread_cond_broadcast( &this->cond );
639                 pthread_mutex_unlock( &this->mutex );
640         }
641         else
642         {
643                 // Get the frame in non real time
644                 frame = mlt_consumer_get_frame( this );
645
646                 // This isn't true, but from the consumers perspective it is
647                 if ( frame != NULL )
648                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
649         }
650
651         return frame;
652 }
653
654 /** Callback for the implementation to indicate a stopped condition.
655 */
656
657 void mlt_consumer_stopped( mlt_consumer this )
658 {
659         mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
660         mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
661 }
662
663 /** Stop the consumer.
664 */
665
666 int mlt_consumer_stop( mlt_consumer this )
667 {
668         // Get the properies
669         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
670         char *debug = mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "debug" );
671
672         // Just in case...
673         if ( debug ) fprintf( stderr, "%s: stopping put waiting\n", debug );
674         pthread_mutex_lock( &this->put_mutex );
675         this->put_active = 0;
676         pthread_cond_broadcast( &this->put_cond );
677         pthread_mutex_unlock( &this->put_mutex );
678
679         // Stop the consumer
680         if ( debug ) fprintf( stderr, "%s: stopping consumer\n", debug );
681         if ( this->stop != NULL )
682                 this->stop( this );
683
684         // Check if the user has requested real time or not and stop if necessary
685         if ( debug ) fprintf( stderr, "%s: stopping read_ahead\n", debug );
686         if ( mlt_properties_get_int( properties, "real_time" ) )
687                 consumer_read_ahead_stop( this );
688
689         // Kill the test card
690         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
691
692         // Check and run a post command
693         if ( mlt_properties_get( properties, "post" ) )
694                 system( mlt_properties_get( properties, "post" ) );
695
696         if ( debug ) fprintf( stderr, "%s: stopped\n", debug );
697
698         return 0;
699 }
700
701 /** Determine if the consumer is stopped.
702 */
703
704 int mlt_consumer_is_stopped( mlt_consumer this )
705 {
706         // Check if the consumer is stopped
707         if ( this->is_stopped != NULL )
708                 return this->is_stopped( this );
709
710         return 0;
711 }
712
713 /** Close the consumer.
714 */
715
716 void mlt_consumer_close( mlt_consumer this )
717 {
718         if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
719         {
720                 // Get the childs close function
721                 void ( *consumer_close )( ) = this->close;
722
723                 if ( consumer_close )
724                 {
725                         // Just in case...
726                         //mlt_consumer_stop( this );
727
728                         this->close = NULL;
729                         consumer_close( this );
730                 }
731                 else
732                 {
733                         // Make sure it only gets called once
734                         this->parent.close = NULL;
735
736                         // Destroy the push mutex and condition
737                         pthread_mutex_destroy( &this->put_mutex );
738                         pthread_cond_destroy( &this->put_cond );
739
740                         mlt_service_close( &this->parent );
741                 }
742         }
743 }