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