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