]> git.sesse.net Git - mlt/blob - src/framework/mlt_consumer.c
Improve frame-dropping for real_time=1.
[mlt] / src / framework / mlt_consumer.c
1 /**
2  * \file mlt_consumer.c
3  * \brief abstraction for all consumer services
4  * \see mlt_consumer_s
5  *
6  * Copyright (C) 2003-2010 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  * \author Dan Dennedy <dan@dennedy.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "mlt_consumer.h"
26 #include "mlt_factory.h"
27 #include "mlt_producer.h"
28 #include "mlt_frame.h"
29 #include "mlt_profile.h"
30 #include "mlt_log.h"
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36
37 /** Define this if you want an automatic deinterlace (if necessary) when the
38  * consumer's producer is not running at normal speed.
39  */
40 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
41
42 /** This is not the ideal place for this, but it is needed by VDPAU as well.
43  */
44 pthread_mutex_t mlt_sdl_mutex = PTHREAD_MUTEX_INITIALIZER;
45
46 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
47 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args );
48 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name );
49 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties );
50 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer self, mlt_frame frame );
51
52 /** Initialize a consumer service.
53  *
54  * \public \memberof mlt_consumer_s
55  * \param self the consumer to initialize
56  * \param child a pointer to the object for the subclass
57  * \param profile the \p mlt_profile_s to use (optional but recommended,
58  * uses the environment variable MLT if self is NULL)
59  * \return true if there was an error
60  */
61
62 int mlt_consumer_init( mlt_consumer self, void *child, mlt_profile profile )
63 {
64         int error = 0;
65         memset( self, 0, sizeof( struct mlt_consumer_s ) );
66         self->child = child;
67         error = mlt_service_init( &self->parent, self );
68         if ( error == 0 )
69         {
70                 // Get the properties from the service
71                 mlt_properties properties = MLT_SERVICE_PROPERTIES( &self->parent );
72
73                 // Apply profile to properties
74                 if ( profile == NULL )
75                 {
76                         // Normally the application creates the profile and controls its lifetime
77                         // This is the fallback exception handling
78                         profile = mlt_profile_init( NULL );
79                         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
80                         mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
81                 }
82                 apply_profile_properties( self, profile, properties );
83
84                 // Default rescaler for all consumers
85                 mlt_properties_set( properties, "rescale", "bilinear" );
86
87                 // Default read ahead buffer size
88                 mlt_properties_set_int( properties, "buffer", 25 );
89
90                 // Default audio frequency and channels
91                 mlt_properties_set_int( properties, "frequency", 48000 );
92                 mlt_properties_set_int( properties, "channels", 2 );
93
94                 // Default of all consumers is real time
95                 mlt_properties_set_int( properties, "real_time", 1 );
96
97                 // Default to environment test card
98                 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
99
100                 // Hmm - default all consumers to yuv422 :-/
101                 self->format = mlt_image_yuv422;
102
103                 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
104                 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
105                 mlt_events_register( properties, "consumer-stopped", NULL );
106                 mlt_events_listen( properties, self, "consumer-frame-show", ( mlt_listener )on_consumer_frame_show );
107
108                 // Register a property-changed listener to handle the profile property -
109                 // subsequent properties can override the profile
110                 self->event_listener = mlt_events_listen( properties, self, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
111
112                 // Create the push mutex and condition
113                 pthread_mutex_init( &self->put_mutex, NULL );
114                 pthread_cond_init( &self->put_cond, NULL );
115
116         }
117         return error;
118 }
119
120 /** Convert the profile into properties on the consumer.
121  *
122  * \private \memberof mlt_consumer_s
123  * \param self a consumer
124  * \param profile a profile
125  * \param properties a properties list (typically, the consumer's)
126  */
127
128 static void apply_profile_properties( mlt_consumer self, mlt_profile profile, mlt_properties properties )
129 {
130         mlt_event_block( self->event_listener );
131         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
132         mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
133         mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
134         mlt_properties_set_int( properties, "width", profile->width );
135         mlt_properties_set_int( properties, "height", profile->height );
136         mlt_properties_set_int( properties, "progressive", profile->progressive );
137         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
138         mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
139         mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
140         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
141         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
142         mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
143         mlt_properties_set_int( properties, "colorspace", profile->colorspace );
144         mlt_event_unblock( self->event_listener );
145 }
146
147 /** The property-changed event listener
148  *
149  * \private \memberof mlt_consumer_s
150  * \param owner the events object
151  * \param self the consumer
152  * \param name the name of the property that changed
153  */
154
155 static void mlt_consumer_property_changed( mlt_properties owner, mlt_consumer self, char *name )
156 {
157         if ( !strcmp( name, "profile" ) )
158         {
159                 // Get the properies
160                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
161
162                 // Get the current profile
163                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
164
165                 // Load the new profile
166                 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
167
168                 if ( new_profile )
169                 {
170                         // Copy the profile
171                         if ( profile != NULL )
172                         {
173                                 free( profile->description );
174                                 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
175                                 profile->description = strdup( new_profile->description );
176                                 mlt_profile_close( new_profile );
177                         }
178                         else
179                         {
180                                 profile = new_profile;
181                         }
182
183                         // Apply to properties
184                         apply_profile_properties( self, profile, properties );
185                 }
186         }
187         else if ( !strcmp( name, "frame_rate_num" ) )
188         {
189                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
190                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
191                 if ( profile )
192                 {
193                         profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
194                         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
195                 }
196         }
197         else if ( !strcmp( name, "frame_rate_den" ) )
198         {
199                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
200                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
201                 if ( profile )
202                 {
203                         profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
204                         mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
205                 }
206         }
207         else if ( !strcmp( name, "width" ) )
208         {
209                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
210                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
211                 if ( profile )
212                         profile->width = mlt_properties_get_int( properties, "width" );
213         }
214         else if ( !strcmp( name, "height" ) )
215         {
216                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
217                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
218                 if ( profile )
219                         profile->height = mlt_properties_get_int( properties, "height" );
220         }
221         else if ( !strcmp( name, "progressive" ) )
222         {
223                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
224                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
225                 if ( profile )
226                         profile->progressive = mlt_properties_get_int( properties, "progressive" );
227         }
228         else if ( !strcmp( name, "sample_aspect_num" ) )
229         {
230                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
231                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
232                 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
233                 if ( profile )
234                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
235         }
236         else if ( !strcmp( name, "sample_aspect_den" ) )
237         {
238                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
239                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
240                 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
241                 if ( profile )
242                         mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile )  );
243         }
244         else if ( !strcmp( name, "display_aspect_num" ) )
245         {
246                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
247                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
248                 if ( profile )
249                 {
250                         profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
251                         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
252                 }
253         }
254         else if ( !strcmp( name, "display_aspect_den" ) )
255         {
256                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
257                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
258                 if ( profile )
259                 {
260                         profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
261                         mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile )  );
262                 }
263         }
264         else if ( !strcmp( name, "colorspace" ) )
265         {
266                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
267                 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
268                 if ( profile )
269                         profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
270         }
271 }
272
273 /** The transmitter for the consumer-frame-show event
274  *
275  * Invokes the listener.
276  *
277  * \private \memberof mlt_consumer_s
278  * \param listener a function pointer that will be invoked
279  * \param owner the events object that will be passed to \p listener
280  * \param self  a service that will be passed to \p listener
281  * \param args an array of pointers - the first entry is passed as a string to \p listener
282  */
283
284 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
285 {
286         if ( listener != NULL )
287                 listener( owner, self, ( mlt_frame )args[ 0 ] );
288 }
289
290 /** The transmitter for the consumer-frame-render event
291  *
292  * Invokes the listener.
293  *
294  * \private \memberof mlt_consumer_s
295  * \param listener a function pointer that will be invoked
296  * \param owner the events object that will be passed to \p listener
297  * \param self  a service that will be passed to \p listener
298  * \param args an array of pointers - the first entry is passed as a string to \p listener
299  */
300
301 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service self, void **args )
302 {
303         if ( listener != NULL )
304                 listener( owner, self, ( mlt_frame )args[ 0 ] );
305 }
306
307 /** A listener on the consumer-frame-show event
308  *
309  * Saves the position of the frame shown.
310  *
311  * \private \memberof mlt_consumer_s
312  * \param owner the events object
313  * \param consumer the consumer on which this event occurred
314  * \param frame the frame that was shown
315  */
316
317 static void on_consumer_frame_show( mlt_properties owner, mlt_consumer consumer, mlt_frame frame )
318 {
319         if ( frame )
320                 consumer->position = mlt_frame_get_position( frame );
321 }
322
323 /** Create a new consumer.
324  *
325  * \public \memberof mlt_consumer_s
326  * \param profile a profile (optional, but recommended)
327  * \return a new consumer
328  */
329
330 mlt_consumer mlt_consumer_new( mlt_profile profile )
331 {
332         // Create the memory for the structure
333         mlt_consumer self = malloc( sizeof( struct mlt_consumer_s ) );
334
335         // Initialise it
336         if ( self != NULL )
337                 mlt_consumer_init( self, NULL, profile );
338
339         // Return it
340         return self;
341 }
342
343 /** Get the parent service object.
344  *
345  * \public \memberof mlt_consumer_s
346  * \param self a consumer
347  * \return the parent service class
348  * \see MLT_CONSUMER_SERVICE
349  */
350
351 mlt_service mlt_consumer_service( mlt_consumer self )
352 {
353         return self != NULL ? &self->parent : NULL;
354 }
355
356 /** Get the consumer properties.
357  *
358  * \public \memberof mlt_consumer_s
359  * \param self a consumer
360  * \return the consumer's properties list
361  * \see MLT_CONSUMER_PROPERTIES
362  */
363
364 mlt_properties mlt_consumer_properties( mlt_consumer self )
365 {
366         return self != NULL ? MLT_SERVICE_PROPERTIES( &self->parent ) : NULL;
367 }
368
369 /** Connect the consumer to the producer.
370  *
371  * \public \memberof mlt_consumer_s
372  * \param self a consumer
373  * \param producer a producer
374  * \return > 0 warning, == 0 success, < 0 serious error,
375  *         1 = this service does not accept input,
376  *         2 = the producer is invalid,
377  *         3 = the producer is already registered with this consumer
378  */
379
380 int mlt_consumer_connect( mlt_consumer self, mlt_service producer )
381 {
382         return mlt_service_connect_producer( &self->parent, producer, 0 );
383 }
384
385 /** Start the consumer.
386  *
387  * \public \memberof mlt_consumer_s
388  * \param self a consumer
389  * \return true if there was an error
390  */
391
392 int mlt_consumer_start( mlt_consumer self )
393 {
394         // Stop listening to the property-changed event
395         mlt_event_block( self->event_listener );
396
397         // Get the properies
398         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
399
400         // Determine if there's a test card producer
401         char *test_card = mlt_properties_get( properties, "test_card" );
402
403         // Just to make sure nothing is hanging around...
404         self->put = NULL;
405         self->put_active = 1;
406
407         // Deal with it now.
408         if ( test_card != NULL )
409         {
410                 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
411                 {
412                         // Create a test card producer
413                         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( self ) );
414                         mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
415
416                         // Do we have a producer
417                         if ( producer != NULL )
418                         {
419                                 // Test card should loop I guess...
420                                 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
421                                 //mlt_producer_set_speed( producer, 0 );
422                                 //mlt_producer_set_in_and_out( producer, 0, 0 );
423
424                                 // Set the test card on the consumer
425                                 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
426                         }
427                 }
428         }
429         else
430         {
431                 // Allow the hash table to speed things up
432                 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
433         }
434
435         // Set the frame duration in microseconds for the frame-dropping heuristic
436         int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
437                         mlt_properties_get_int( properties, "frame_rate_den" );
438         mlt_properties_set_int( properties, "frame_duration", frame_duration );
439
440         // Check and run an ante command
441         if ( mlt_properties_get( properties, "ante" ) )
442                 if ( system( mlt_properties_get( properties, "ante" ) ) == -1 )
443                         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "ante" ) );
444
445         // Set the real_time preference
446         self->real_time = mlt_properties_get_int( properties, "real_time" );
447
448         // For worker threads implementation, buffer must be at least # threads
449         if ( abs( self->real_time ) > 1 && mlt_properties_get_int( properties, "buffer" ) <= abs( self->real_time ) )
450                 mlt_properties_set_int( properties, "buffer", abs( self->real_time ) + 1 );
451
452         // Start the service
453         if ( self->start != NULL )
454                 return self->start( self );
455
456         return 0;
457 }
458
459 /** An alternative method to feed frames into the consumer.
460  *
461  * Only valid if the consumer itself is not connected.
462  *
463  * \public \memberof mlt_consumer_s
464  * \param self a consumer
465  * \param frame a frame
466  * \return true (ignore self for now)
467  */
468
469 int mlt_consumer_put_frame( mlt_consumer self, mlt_frame frame )
470 {
471         int error = 1;
472
473         // Get the service assoicated to the consumer
474         mlt_service service = MLT_CONSUMER_SERVICE( self );
475
476         if ( mlt_service_producer( service ) == NULL )
477         {
478                 struct timeval now;
479                 struct timespec tm;
480                 pthread_mutex_lock( &self->put_mutex );
481                 while ( self->put_active && self->put != NULL )
482                 {
483                         gettimeofday( &now, NULL );
484                         tm.tv_sec = now.tv_sec + 1;
485                         tm.tv_nsec = now.tv_usec * 1000;
486                         pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
487                 }
488                 if ( self->put_active && self->put == NULL )
489                         self->put = frame;
490                 else
491                         mlt_frame_close( frame );
492                 pthread_cond_broadcast( &self->put_cond );
493                 pthread_mutex_unlock( &self->put_mutex );
494         }
495         else
496         {
497                 mlt_frame_close( frame );
498         }
499
500         return error;
501 }
502
503 /** Protected method for consumer to get frames from connected service
504  *
505  * \public \memberof mlt_consumer_s
506  * \param self a consumer
507  * \return a frame
508  */
509
510 mlt_frame mlt_consumer_get_frame( mlt_consumer self )
511 {
512         // Frame to return
513         mlt_frame frame = NULL;
514
515         // Get the service assoicated to the consumer
516         mlt_service service = MLT_CONSUMER_SERVICE( self );
517
518         // Get the consumer properties
519         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
520
521         // Get the frame
522         if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
523         {
524                 struct timeval now;
525                 struct timespec tm;
526                 pthread_mutex_lock( &self->put_mutex );
527                 while ( self->put_active && self->put == NULL )
528                 {
529                         gettimeofday( &now, NULL );
530                         tm.tv_sec = now.tv_sec + 1;
531                         tm.tv_nsec = now.tv_usec * 1000;
532                         pthread_cond_timedwait( &self->put_cond, &self->put_mutex, &tm );
533                 }
534                 frame = self->put;
535                 self->put = NULL;
536                 pthread_cond_broadcast( &self->put_cond );
537                 pthread_mutex_unlock( &self->put_mutex );
538                 if ( frame != NULL )
539                         mlt_service_apply_filters( service, frame, 0 );
540         }
541         else if ( mlt_service_producer( service ) != NULL )
542         {
543                 mlt_service_get_frame( service, &frame, 0 );
544         }
545         else
546         {
547                 frame = mlt_frame_init( service );
548         }
549
550         if ( frame != NULL )
551         {
552                 // Get the frame properties
553                 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
554
555                 // Get the test card producer
556                 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
557
558                 // Attach the test frame producer to it.
559                 if ( test_card != NULL )
560                         mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
561
562                 // Attach the rescale property
563                 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
564
565                 // Aspect ratio and other jiggery pokery
566                 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
567                 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
568                 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
569         }
570
571         // Return the frame
572         return frame;
573 }
574
575 /** Compute the time difference between now and a time value.
576  *
577  * \private \memberof mlt_consumer_s
578  * \param time1 a time value to be compared against now
579  * \return the difference in microseconds
580  */
581
582 static inline long time_difference( struct timeval *time1 )
583 {
584         struct timeval time2;
585         time2.tv_sec = time1->tv_sec;
586         time2.tv_usec = time1->tv_usec;
587         gettimeofday( time1, NULL );
588         return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
589 }
590
591 /** The thread procedure for asynchronously pulling frames through the service
592  * network connected to a consumer.
593  *
594  * \private \memberof mlt_consumer_s
595  * \param arg a consumer
596  */
597
598 static void *consumer_read_ahead_thread( void *arg )
599 {
600         // The argument is the consumer
601         mlt_consumer self = arg;
602
603         // Get the properties of the consumer
604         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
605
606         // Get the width and height
607         int width = mlt_properties_get_int( properties, "width" );
608         int height = mlt_properties_get_int( properties, "height" );
609
610         // See if video is turned off
611         int video_off = mlt_properties_get_int( properties, "video_off" );
612         int preview_off = mlt_properties_get_int( properties, "preview_off" );
613         int preview_format = mlt_properties_get_int( properties, "preview_format" );
614
615         // Get the audio settings
616         mlt_audio_format afmt = mlt_audio_s16;
617         int counter = 0;
618         double fps = mlt_properties_get_double( properties, "fps" );
619         int channels = mlt_properties_get_int( properties, "channels" );
620         int frequency = mlt_properties_get_int( properties, "frequency" );
621         int samples = 0;
622         void *audio = NULL;
623
624         // See if audio is turned off
625         int audio_off = mlt_properties_get_int( properties, "audio_off" );
626
627         // Get the maximum size of the buffer
628         int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
629
630         // General frame variable
631         mlt_frame frame = NULL;
632         uint8_t *image = NULL;
633
634         // Time structures
635         struct timeval ante;
636
637         // Average time for get_frame and get_image
638         int count = 1;
639         int skipped = 0;
640         int64_t time_wait = 0;
641         int64_t time_frame = 0;
642         int64_t time_process = 0;
643         int skip_next = 0;
644         mlt_position pos = 0;
645         mlt_position start_pos = 0;
646         mlt_position last_pos = 0;
647
648         if ( preview_off && preview_format != 0 )
649                 self->format = preview_format;
650
651         // Get the first frame
652         frame = mlt_consumer_get_frame( self );
653
654         if ( frame )
655         {
656                 // Get the image of the first frame
657                 if ( !video_off )
658                 {
659                         mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
660                         mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
661                 }
662
663                 if ( !audio_off )
664                 {
665                         samples = mlt_sample_calculator( fps, frequency, counter++ );
666                         mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
667                 }
668
669                 // Mark as rendered
670                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
671                 last_pos = start_pos = pos = mlt_frame_get_position( frame );
672         }
673
674         // Get the starting time (can ignore the times above)
675         gettimeofday( &ante, NULL );
676
677         // Continue to read ahead
678         while ( self->ahead )
679         {
680                 // Fetch width/height again
681                 width = mlt_properties_get_int( properties, "width" );
682                 height = mlt_properties_get_int( properties, "height" );
683
684                 // Put the current frame into the queue
685                 pthread_mutex_lock( &self->queue_mutex );
686                 while( self->ahead && mlt_deque_count( self->queue ) >= buffer )
687                         pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
688                 mlt_deque_push_back( self->queue, frame );
689                 pthread_cond_broadcast( &self->queue_cond );
690                 pthread_mutex_unlock( &self->queue_mutex );
691
692                 time_wait += time_difference( &ante );
693
694                 // Get the next frame
695                 frame = mlt_consumer_get_frame( self );
696                 time_frame += time_difference( &ante );
697
698                 // If there's no frame, we're probably stopped...
699                 if ( frame == NULL )
700                         continue;
701                 pos = mlt_frame_get_position( frame );
702
703                 // Increment the count
704                 count ++;
705
706                 // All non normal playback frames should be shown
707                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
708                 {
709 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
710                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
711 #endif
712                         skipped = 0;
713                         time_frame = 0;
714                         time_process = 0;
715                         time_wait = 0;
716                         count = 1;
717                         skip_next = 0;
718                         start_pos = pos;
719                 }
720
721                 // Get the image
722                 if ( !skip_next || self->real_time == -1 )
723                 {
724                         // Get the image, mark as rendered and time it
725                         if ( !video_off )
726                         {
727                                 mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
728                                 mlt_frame_get_image( frame, &image, &self->format, &width, &height, 0 );
729                         }
730                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
731                         skipped = 0;
732                 }
733                 else
734                 {
735                         // Increment the number of sequentially skipped frames
736                         skipped ++;
737                         skip_next = 0;
738
739                         // If we've reached an unacceptable level, reset everything
740                         if ( skipped > fps * 2 )
741                         {
742                                 skipped = 0;
743                                 time_frame = 0;
744                                 time_process = 0;
745                                 time_wait = 0;
746                                 count = 1;
747                         }
748                 }
749
750                 // Always process audio
751                 if ( !audio_off )
752                 {
753                         samples = mlt_sample_calculator( fps, frequency, counter++ );
754                         mlt_frame_get_audio( frame, &audio, &afmt, &frequency, &channels, &samples );
755                 }
756
757                 // Increment the time take for self frame
758                 time_process += time_difference( &ante );
759
760                 // Determine if the next frame should be skipped
761                 if ( pos != last_pos + 1 )
762                         start_pos = pos;
763                 last_pos = pos;
764                 if ( pos - start_pos <= buffer/5 )
765                 {
766                         // Do not skip the first 20% of buffer at start, resuming, or seeking
767                         skipped = 0;
768                         time_frame = 0;
769                         time_process = 0;
770                         time_wait = 0;
771                         count = 1;
772                 }
773                 if ( mlt_deque_count( self->queue ) <= buffer/5 )
774                 {
775                         int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
776                         if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
777                                 skip_next = 1;
778                 }
779         }
780
781         // Remove the last frame
782         mlt_frame_close( frame );
783
784         return NULL;
785 }
786
787 /** Locate the first unprocessed frame in the queue.
788  *
789  * When playing with realtime behavior, we do not use the true head, but
790  * rather an adjusted process_head. The process_head is adjusted based on
791  * the rate of frame-dropping or recovery from frame-dropping. The idea is
792  * that as the level of frame-dropping increases to move the process_head
793  * closer to the tail because the frames are not completing processing prior
794  * to their playout! Then, as frames are not dropped the process_head moves
795  * back closer to the head of the queue so that worker threads can work 
796  * ahead of the playout point (queue head).
797  *
798  * \private \memberof mlt_consumer_s
799  * \param self a consumer
800  * \return an index into the queue
801  */
802
803 static inline int first_unprocessed_frame( mlt_consumer self )
804 {
805         int index = self->real_time <= 0 ? 0 : self->process_head;
806         while ( index < mlt_deque_count( self->queue ) && MLT_FRAME( mlt_deque_peek( self->queue, index ) )->is_processing )
807                 index++;
808         return index;
809 }
810
811 /** The worker thread procedure for parallel processing frames.
812  *
813  * \private \memberof mlt_consumer_s
814  * \param arg a consumer
815  */
816
817 static void *consumer_worker_thread( void *arg )
818 {
819         // The argument is the consumer
820         mlt_consumer self = arg;
821
822         // Get the properties of the consumer
823         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
824
825         // Get the width and height
826         int width = mlt_properties_get_int( properties, "width" );
827         int height = mlt_properties_get_int( properties, "height" );
828         mlt_image_format format = self->format;
829
830         // See if video is turned off
831         int video_off = mlt_properties_get_int( properties, "video_off" );
832         int preview_off = mlt_properties_get_int( properties, "preview_off" );
833         int preview_format = mlt_properties_get_int( properties, "preview_format" );
834
835         // General frame variable
836         mlt_frame frame = NULL;
837         uint8_t *image = NULL;
838
839         if ( preview_off && preview_format != 0 )
840                 format = preview_format;
841
842         // Continue to read ahead
843         while ( self->ahead )
844         {
845                 // Get the next unprocessed frame from the work queue
846                 pthread_mutex_lock( &self->queue_mutex );
847                 int index = first_unprocessed_frame( self );
848                 while ( self->ahead && index >= mlt_deque_count( self->queue ) )
849                 {
850                         mlt_log_debug( MLT_CONSUMER_SERVICE(self), "waiting in worker index = %d queue count = %d\n",
851                                 index, mlt_deque_count( self->queue ) );
852                         pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
853                         index = first_unprocessed_frame( self );
854                 }
855
856                 // Mark the frame for processing
857                 frame = mlt_deque_peek( self->queue, index );
858                 if ( frame )
859                 {
860                         mlt_log_debug( MLT_CONSUMER_SERVICE(self), "worker processing index = %d frame %d queue count = %d\n",
861                                 index, mlt_frame_get_position(frame), mlt_deque_count( self->queue ) );
862                         frame->is_processing = 1;
863                         mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( frame ) );
864                 }
865                 pthread_mutex_unlock( &self->queue_mutex );
866
867                 // If there's no frame, we're probably stopped...
868                 if ( frame == NULL )
869                         continue;
870
871 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
872                 // All non normal playback frames should be shown
873                 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
874                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
875 #endif
876
877                 // Get the image
878                 if ( !video_off )
879                 {
880                         // Fetch width/height again
881                         width = mlt_properties_get_int( properties, "width" );
882                         height = mlt_properties_get_int( properties, "height" );
883                         mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-frame-render", frame, NULL );
884                         mlt_frame_get_image( frame, &image, &format, &width, &height, 0 );
885                 }
886                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
887                 mlt_frame_close( frame );
888
889                 // Tell a waiting thread (non-realtime main consumer thread) that we are done.
890                 pthread_mutex_lock( &self->done_mutex );
891                 pthread_cond_broadcast( &self->done_cond );
892                 pthread_mutex_unlock( &self->done_mutex );
893         }
894
895         return NULL;
896 }
897
898 /** Start the read/render thread.
899  *
900  * \private \memberof mlt_consumer_s
901  * \param self a consumer
902  */
903
904 static void consumer_read_ahead_start( mlt_consumer self )
905 {
906         // We're running now
907         self->ahead = 1;
908
909         // Create the frame queue
910         self->queue = mlt_deque_init( );
911
912         // Create the queue mutex
913         pthread_mutex_init( &self->queue_mutex, NULL );
914
915         // Create the condition
916         pthread_cond_init( &self->queue_cond, NULL );
917
918         // Create the read ahead
919         if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
920         {
921                 struct sched_param priority;
922                 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
923                 pthread_attr_t thread_attributes;
924                 pthread_attr_init( &thread_attributes );
925                 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
926                 pthread_attr_setschedparam( &thread_attributes, &priority );
927                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
928                 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
929                 if ( pthread_create( &self->ahead_thread, &thread_attributes, consumer_read_ahead_thread, self ) < 0 )
930                         pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
931                 pthread_attr_destroy( &thread_attributes );
932         }
933         else
934         {
935                 pthread_create( &self->ahead_thread, NULL, consumer_read_ahead_thread, self );
936         }
937         self->started = 1;
938 }
939
940 /** Start the worker threads.
941  *
942  * \private \memberof mlt_consumer_s
943  * \param self a consumer
944  */
945
946 static void consumer_work_start( mlt_consumer self )
947 {
948         int n = abs( self->real_time );
949         pthread_t *thread = calloc( 1, sizeof( pthread_t ) * n );
950
951         // We're running now
952         self->ahead = 1;
953         
954         // These keep track of the accelleration of frame dropping or recovery.
955         self->consecutive_dropped = 0;
956         self->consecutive_rendered = 0;
957         
958         // This is the position in the queue from which to look for a frame to process.
959         // If we always start from the head, then we may likely not complete processing
960         // before the frame is played out.
961         self->process_head = 0;
962
963         // Create the queues
964         self->queue = mlt_deque_init();
965         self->worker_threads = mlt_deque_init();
966
967         // Create the mutexes
968         pthread_mutex_init( &self->queue_mutex, NULL );
969         pthread_mutex_init( &self->done_mutex, NULL );
970
971         // Create the conditions
972         pthread_cond_init( &self->queue_cond, NULL );
973         pthread_cond_init( &self->done_cond, NULL );
974
975         // Create the read ahead
976         if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( self ), "priority" ) )
977         {
978
979                 struct sched_param priority;
980                 pthread_attr_t thread_attributes;
981
982                 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( self ), "priority" );
983                 pthread_attr_init( &thread_attributes );
984                 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
985                 pthread_attr_setschedparam( &thread_attributes, &priority );
986                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
987                 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
988
989                 while ( n-- )
990                 {
991                         if ( pthread_create( thread, &thread_attributes, consumer_worker_thread, self ) < 0 )
992                                 if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
993                                         mlt_deque_push_back( self->worker_threads, thread );
994                         thread++;
995                 }
996                 pthread_attr_destroy( &thread_attributes );
997         }
998
999         else
1000         {
1001                 while ( n-- )
1002                 {
1003                         if ( pthread_create( thread, NULL, consumer_worker_thread, self ) == 0 )
1004                                 mlt_deque_push_back( self->worker_threads, thread );
1005                         thread++;
1006                 }
1007         }
1008         self->started = 1;
1009 }
1010
1011 /** Stop the read/render thread.
1012  *
1013  * \private \memberof mlt_consumer_s
1014  * \param self a consumer
1015  */
1016
1017 static void consumer_read_ahead_stop( mlt_consumer self )
1018 {
1019         // Make sure we're running
1020         if ( self->started )
1021         {
1022                 // Inform thread to stop
1023                 self->ahead = 0;
1024
1025                 // Broadcast to the condition in case it's waiting
1026                 pthread_mutex_lock( &self->queue_mutex );
1027                 pthread_cond_broadcast( &self->queue_cond );
1028                 pthread_mutex_unlock( &self->queue_mutex );
1029
1030                 // Broadcast to the put condition in case it's waiting
1031                 pthread_mutex_lock( &self->put_mutex );
1032                 pthread_cond_broadcast( &self->put_cond );
1033                 pthread_mutex_unlock( &self->put_mutex );
1034
1035                 // Join the thread
1036                 pthread_join( self->ahead_thread, NULL );
1037                 self->started = 0;
1038
1039                 // Destroy the frame queue mutex
1040                 pthread_mutex_destroy( &self->queue_mutex );
1041
1042                 // Destroy the condition
1043                 pthread_cond_destroy( &self->queue_cond );
1044
1045                 // Wipe the queue
1046                 while ( mlt_deque_count( self->queue ) )
1047                         mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1048
1049                 // Close the queue
1050                 mlt_deque_close( self->queue );
1051         }
1052 }
1053
1054 /** Stop the worker threads.
1055  *
1056  * \private \memberof mlt_consumer_s
1057  * \param self a consumer
1058  */
1059
1060 static void consumer_work_stop( mlt_consumer self )
1061 {
1062         // Make sure we're running
1063         if ( self->started )
1064         {
1065                 // Inform thread to stop
1066                 self->ahead = 0;
1067
1068                 // Broadcast to the queue condition in case it's waiting
1069                 pthread_mutex_lock( &self->queue_mutex );
1070                 pthread_cond_broadcast( &self->queue_cond );
1071                 pthread_mutex_unlock( &self->queue_mutex );
1072
1073                 // Broadcast to the put condition in case it's waiting
1074                 pthread_mutex_lock( &self->put_mutex );
1075                 pthread_cond_broadcast( &self->put_cond );
1076                 pthread_mutex_unlock( &self->put_mutex );
1077
1078                 // Broadcast to the done condition in case it's waiting
1079                 pthread_mutex_lock( &self->done_mutex );
1080                 pthread_cond_broadcast( &self->done_cond );
1081                 pthread_mutex_unlock( &self->done_mutex );
1082
1083                 // Join the threads
1084                 pthread_t *thread;
1085                 while ( ( thread = mlt_deque_pop_back( self->worker_threads ) ) )
1086                         pthread_join( *thread, NULL );
1087
1088                 // Deallocate the array of threads
1089                 if ( thread )
1090                         free( thread );
1091
1092                 // Indicate that worker threads no longer running
1093                 self->started = 0;
1094
1095                 // Destroy the mutexes
1096                 pthread_mutex_destroy( &self->queue_mutex );
1097                 pthread_mutex_destroy( &self->done_mutex );
1098
1099                 // Destroy the conditions
1100                 pthread_cond_destroy( &self->queue_cond );
1101                 pthread_cond_destroy( &self->done_cond );
1102
1103                 // Wipe the queues
1104                 while ( mlt_deque_count( self->queue ) )
1105                         mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1106
1107                 // Close the queues
1108                 mlt_deque_close( self->queue );
1109                 mlt_deque_close( self->worker_threads );
1110         }
1111 }
1112
1113 /** Flush the read/render thread's buffer.
1114  *
1115  * \public \memberof mlt_consumer_s
1116  * \param self a consumer
1117  */
1118
1119 void mlt_consumer_purge( mlt_consumer self )
1120 {
1121         if ( self->ahead )
1122         {
1123                 pthread_mutex_lock( &self->queue_mutex );
1124                 while ( mlt_deque_count( self->queue ) )
1125                         mlt_frame_close( mlt_deque_pop_back( self->queue ) );
1126                 pthread_cond_broadcast( &self->queue_cond );
1127                 pthread_mutex_unlock( &self->queue_mutex );
1128         }
1129 }
1130
1131 /** Use multiple worker threads and a work queue.
1132  */
1133
1134 static mlt_frame worker_get_frame( mlt_consumer self, mlt_properties properties )
1135 {
1136         // Frame to return
1137         mlt_frame frame = NULL;
1138
1139         int size = abs( self->real_time );
1140         int buffer = mlt_properties_get_int( properties, "buffer" );
1141         // This is a heuristic to determine a suitable minimum buffer size for the number of threads.
1142         int headroom = 2 + size * size;
1143         buffer = buffer < headroom ? headroom : buffer;
1144
1145         // Start worker threads if not already started.
1146         if ( ! self->ahead )
1147         {
1148                 int prefill = mlt_properties_get_int( properties, "prefill" );
1149                 prefill = prefill > 0 && prefill < buffer ? prefill : buffer;
1150
1151                 consumer_work_start( self );
1152
1153                 // Fill the work queue.
1154                 int i = buffer;
1155                 while ( self->ahead && i-- )
1156                 {
1157                         frame = mlt_consumer_get_frame( self );
1158                         if ( frame )
1159                         {
1160                                 pthread_mutex_lock( &self->queue_mutex );
1161                                 mlt_deque_push_back( self->queue, frame );
1162                                 pthread_cond_signal( &self->queue_cond );
1163                                 pthread_mutex_unlock( &self->queue_mutex );
1164                         }
1165                 }
1166
1167                 // Wait for prefill
1168                 while ( self->ahead && first_unprocessed_frame( self ) < prefill )
1169                 {
1170                         pthread_mutex_lock( &self->done_mutex );
1171                         pthread_cond_wait( &self->done_cond, &self->done_mutex );
1172                         pthread_mutex_unlock( &self->done_mutex );
1173                 }
1174                 self->process_head = size;
1175         }
1176
1177 //      mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "size %d done count %d work count %d process_head %d\n",
1178 //              size, first_unprocessed_frame( self ), mlt_deque_count( self->queue ), self->process_head );
1179
1180         // Feed the work queue
1181         while ( self->ahead && mlt_deque_count( self->queue ) < buffer )
1182         {
1183                 frame = mlt_consumer_get_frame( self );
1184                 if ( ! frame )
1185                         return frame;
1186                 pthread_mutex_lock( &self->queue_mutex );
1187                 mlt_deque_push_back( self->queue, frame );
1188                 pthread_cond_signal( &self->queue_cond );
1189                 pthread_mutex_unlock( &self->queue_mutex );
1190         }
1191
1192         // Wait if not realtime.
1193         while( self->ahead && self->real_time < 0 &&
1194                ! mlt_properties_get_int( MLT_FRAME_PROPERTIES( MLT_FRAME( mlt_deque_peek_front( self->queue ) ) ), "rendered" ) )
1195         {
1196                 pthread_mutex_lock( &self->done_mutex );
1197                 pthread_cond_wait( &self->done_cond, &self->done_mutex );
1198                 pthread_mutex_unlock( &self->done_mutex );
1199         }
1200         
1201         // Get the frame from the queue.
1202         pthread_mutex_lock( &self->queue_mutex );
1203         frame = mlt_deque_pop_front( self->queue );
1204         pthread_mutex_unlock( &self->queue_mutex );
1205
1206         // Adapt the worker process head to the runtime conditions.
1207         if ( self->real_time > 0 )
1208         {
1209                 if ( frame && mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "rendered" ) )
1210                 {
1211                         self->consecutive_dropped = 0;
1212                         if ( self->process_head > size && self->consecutive_rendered >= self->process_head )
1213                                 self->process_head--;
1214                         else
1215                                 self->consecutive_rendered++;
1216                 }
1217                 else
1218                 {
1219                         self->consecutive_rendered = 0;
1220                         if ( self->process_head < buffer - size && self->consecutive_dropped > size )
1221                                 self->process_head++;
1222                         else
1223                                 self->consecutive_dropped++;
1224                 }
1225 //              mlt_log_verbose( MLT_CONSUMER_SERVICE(self), "dropped %d rendered %d process_head %d\n",
1226 //                      self->consecutive_dropped, self->consecutive_rendered, self->process_head );
1227         }
1228         
1229         return frame;
1230 }
1231
1232 /** Get the next frame from the producer connected to a consumer.
1233  *
1234  * Typically, one uses this instead of \p mlt_consumer_get_frame to make
1235  * the asynchronous/real-time behavior configurable at runtime.
1236  * You should close the frame returned from this when you are done with it.
1237  *
1238  * \public \memberof mlt_consumer_s
1239  * \param self a consumer
1240  * \return a frame
1241  */
1242
1243 mlt_frame mlt_consumer_rt_frame( mlt_consumer self )
1244 {
1245         // Frame to return
1246         mlt_frame frame = NULL;
1247
1248         // Get the properties
1249         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1250
1251         // Check if the user has requested real time or not
1252         if ( self->real_time > 1 || self->real_time < -1 )
1253         {
1254                 // see above
1255                 return worker_get_frame( self, properties );
1256         }
1257         else if ( self->real_time == 1 || self->real_time == -1 )
1258         {
1259                 int size = 1;
1260
1261                 // Is the read ahead running?
1262                 if ( self->ahead == 0 )
1263                 {
1264                         int buffer = mlt_properties_get_int( properties, "buffer" );
1265                         int prefill = mlt_properties_get_int( properties, "prefill" );
1266                         consumer_read_ahead_start( self );
1267                         if ( buffer > 1 )
1268                                 size = prefill > 0 && prefill < buffer ? prefill : buffer;
1269                 }
1270
1271                 // Get frame from queue
1272                 pthread_mutex_lock( &self->queue_mutex );
1273                 while( self->ahead && mlt_deque_count( self->queue ) < size )
1274                         pthread_cond_wait( &self->queue_cond, &self->queue_mutex );
1275                 frame = mlt_deque_pop_front( self->queue );
1276                 pthread_cond_broadcast( &self->queue_cond );
1277                 pthread_mutex_unlock( &self->queue_mutex );
1278         }
1279         else // real_time == 0
1280         {
1281                 // Get the frame in non real time
1282                 frame = mlt_consumer_get_frame( self );
1283
1284                 // This isn't true, but from the consumers perspective it is
1285                 if ( frame != NULL )
1286                         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
1287         }
1288
1289         return frame;
1290 }
1291
1292 /** Callback for the implementation to indicate a stopped condition.
1293  *
1294  * \public \memberof mlt_consumer_s
1295  * \param self a consumer
1296  */
1297
1298 void mlt_consumer_stopped( mlt_consumer self )
1299 {
1300         mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( self ), "running", 0 );
1301         mlt_events_fire( MLT_CONSUMER_PROPERTIES( self ), "consumer-stopped", NULL );
1302         mlt_event_unblock( self->event_listener );
1303 }
1304
1305 /** Stop the consumer.
1306  *
1307  * \public \memberof mlt_consumer_s
1308  * \param self a consumer
1309  * \return true if there was an error
1310  */
1311
1312 int mlt_consumer_stop( mlt_consumer self )
1313 {
1314         // Get the properies
1315         mlt_properties properties = MLT_CONSUMER_PROPERTIES( self );
1316
1317         // Just in case...
1318         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping put waiting\n" );
1319         pthread_mutex_lock( &self->put_mutex );
1320         self->put_active = 0;
1321         pthread_cond_broadcast( &self->put_cond );
1322         pthread_mutex_unlock( &self->put_mutex );
1323
1324         // Stop the consumer
1325         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping consumer\n" );
1326         
1327         // Cancel the read ahead threads
1328         self->ahead = 0;
1329         if ( self->started )
1330         {
1331                 // Unblock the consumer calling mlt_consumer_rt_frame
1332                 pthread_mutex_lock( &self->queue_mutex );
1333                 pthread_cond_broadcast( &self->queue_cond );
1334                 pthread_mutex_unlock( &self->queue_mutex );             
1335         }
1336         
1337         // Invoke the child callback
1338         if ( self->stop != NULL )
1339                 self->stop( self );
1340
1341         // Check if the user has requested real time or not and stop if necessary
1342         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
1343         if ( abs( self->real_time ) == 1 )
1344                 consumer_read_ahead_stop( self );
1345         else if ( abs( self->real_time ) > 1 )
1346                 consumer_work_stop( self );
1347
1348         // Kill the test card
1349         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
1350
1351         // Check and run a post command
1352         if ( mlt_properties_get( properties, "post" ) )
1353                 if (system( mlt_properties_get( properties, "post" ) ) == -1 )
1354                         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_ERROR, "system(%s) failed!\n", mlt_properties_get( properties, "post" ) );
1355
1356         mlt_log( MLT_CONSUMER_SERVICE( self ), MLT_LOG_DEBUG, "stopped\n" );
1357
1358         return 0;
1359 }
1360
1361 /** Determine if the consumer is stopped.
1362  *
1363  * \public \memberof mlt_consumer_s
1364  * \param self a consumer
1365  * \return true if the consumer is stopped
1366  */
1367
1368 int mlt_consumer_is_stopped( mlt_consumer self )
1369 {
1370         // Check if the consumer is stopped
1371         if ( self->is_stopped != NULL )
1372                 return self->is_stopped( self );
1373
1374         return 0;
1375 }
1376
1377 /** Close and destroy the consumer.
1378  *
1379  * \public \memberof mlt_consumer_s
1380  * \param self a consumer
1381  */
1382
1383 void mlt_consumer_close( mlt_consumer self )
1384 {
1385         if ( self != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( self ) ) <= 0 )
1386         {
1387                 // Get the childs close function
1388                 void ( *consumer_close )( ) = self->close;
1389
1390                 if ( consumer_close )
1391                 {
1392                         // Just in case...
1393                         //mlt_consumer_stop( self );
1394
1395                         self->close = NULL;
1396                         consumer_close( self );
1397                 }
1398                 else
1399                 {
1400                         // Make sure it only gets called once
1401                         self->parent.close = NULL;
1402
1403                         // Destroy the push mutex and condition
1404                         pthread_mutex_destroy( &self->put_mutex );
1405                         pthread_cond_destroy( &self->put_cond );
1406
1407                         mlt_service_close( &self->parent );
1408                 }
1409         }
1410 }
1411
1412 /** Get the position of the last frame shown.
1413  *
1414  * \public \memberof mlt_consumer_s
1415  * \param consumer a consumer
1416  * \return the position
1417  */
1418
1419 mlt_position mlt_consumer_position( mlt_consumer consumer )
1420 {
1421         return consumer->position;
1422 }
1423