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