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