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