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