mlt_log.[hc], mlt_transition.c, mlt_tractor.c, mlt_repository.c, mlt_properties.c,
[melted] / src / framework / mlt_consumer.c
1 /**
2 * \file mlt_consumer.c
3 * \brief abstraction for all consumer services
4 *
5 * Copyright (C) 2003-2008 Ushodaya Enterprises Limited
6 * \author Charles Yates <charles.yates@pandora.be>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 #include "mlt_consumer.h"
24 #include "mlt_factory.h"
25 #include "mlt_producer.h"
26 #include "mlt_frame.h"
27 #include "mlt_profile.h"
28 #include "mlt_log.h"
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <sys/time.h>
34
35 /** Define this if you want an automatic deinterlace (if necessary) when the
36 * consumer's producer is not running at normal speed.
37 */
38 #undef DEINTERLACE_ON_NOT_NORMAL_SPEED
39
40 static void mlt_consumer_frame_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
41 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
42 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name );
43 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties );
44
45 /** Initialize a consumer service.
46 *
47 * \public \memberof mlt_consumer_s
48 * \param this the consumer to initialize
49 * \param child a pointer to the object for the subclass
50 * \param profile the \p mlt_profile_s to use (optional but recommended,
51 * uses the environment variable MLT if this is NULL)
52 * \return true if there was an error
53 */
54
55 int mlt_consumer_init( mlt_consumer this, void *child, mlt_profile profile )
56 {
57 int error = 0;
58 memset( this, 0, sizeof( struct mlt_consumer_s ) );
59 this->child = child;
60 error = mlt_service_init( &this->parent, this );
61 if ( error == 0 )
62 {
63 // Get the properties from the service
64 mlt_properties properties = MLT_SERVICE_PROPERTIES( &this->parent );
65
66 // Apply profile to properties
67 if ( profile == NULL )
68 {
69 // Normally the application creates the profile and controls its lifetime
70 // This is the fallback exception handling
71 profile = mlt_profile_init( NULL );
72 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
73 mlt_properties_set_data( properties, "_profile", profile, 0, (mlt_destructor)mlt_profile_close, NULL );
74 }
75 apply_profile_properties( this, profile, properties );
76
77 // Default rescaler for all consumers
78 mlt_properties_set( properties, "rescale", "bilinear" );
79
80 // Default read ahead buffer size
81 mlt_properties_set_int( properties, "buffer", 25 );
82
83 // Default audio frequency and channels
84 mlt_properties_set_int( properties, "frequency", 48000 );
85 mlt_properties_set_int( properties, "channels", 2 );
86
87 // Default of all consumers is real time
88 mlt_properties_set_int( properties, "real_time", 1 );
89
90 // Default to environment test card
91 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
92
93 // Hmm - default all consumers to yuv422 :-/
94 this->format = mlt_image_yuv422;
95
96 mlt_events_register( properties, "consumer-frame-show", ( mlt_transmitter )mlt_consumer_frame_show );
97 mlt_events_register( properties, "consumer-frame-render", ( mlt_transmitter )mlt_consumer_frame_render );
98 mlt_events_register( properties, "consumer-stopped", NULL );
99
100 // Register a property-changed listener to handle the profile property -
101 // subsequent properties can override the profile
102 this->event_listener = mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_consumer_property_changed );
103
104 // Create the push mutex and condition
105 pthread_mutex_init( &this->put_mutex, NULL );
106 pthread_cond_init( &this->put_cond, NULL );
107
108 }
109 return error;
110 }
111
112 /** Convert the profile into properties on the consumer.
113 *
114 * \private \memberof mlt_consumer_s
115 * \param this a consumer
116 * \param profile a profile
117 * \param properties a properties list (typically, the consumer's)
118 */
119
120 static void apply_profile_properties( mlt_consumer this, mlt_profile profile, mlt_properties properties )
121 {
122 mlt_event_block( this->event_listener );
123 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
124 mlt_properties_set_int( properties, "frame_rate_num", profile->frame_rate_num );
125 mlt_properties_set_int( properties, "frame_rate_den", profile->frame_rate_den );
126 mlt_properties_set_int( properties, "width", profile->width );
127 mlt_properties_set_int( properties, "height", profile->height );
128 mlt_properties_set_int( properties, "progressive", profile->progressive );
129 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
130 mlt_properties_set_int( properties, "sample_aspect_num", profile->sample_aspect_num );
131 mlt_properties_set_int( properties, "sample_aspect_den", profile->sample_aspect_den );
132 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
133 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
134 mlt_properties_set_int( properties, "display_aspect_num", profile->display_aspect_num );
135 mlt_event_unblock( this->event_listener );
136 }
137
138 /** The property-changed event listener
139 *
140 * \private \memberof mlt_consumer_s
141 * \param owner the service a service (ignored)
142 * \param this the consumer
143 * \param name the name of the property that changed
144 */
145
146 static void mlt_consumer_property_changed( mlt_service owner, mlt_consumer this, char *name )
147 {
148 if ( !strcmp( name, "profile" ) )
149 {
150 // Get the properies
151 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
152
153 // Get the current profile
154 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
155
156 // Load the new profile
157 mlt_profile new_profile = mlt_profile_init( mlt_properties_get( properties, name ) );
158
159 if ( new_profile )
160 {
161 // Copy the profile
162 if ( profile != NULL )
163 {
164 free( profile->description );
165 memcpy( profile, new_profile, sizeof( struct mlt_profile_s ) );
166 profile->description = strdup( new_profile->description );
167 mlt_profile_close( new_profile );
168 }
169 else
170 {
171 profile = new_profile;
172 }
173
174 // Apply to properties
175 apply_profile_properties( this, profile, properties );
176 }
177 }
178 else if ( !strcmp( name, "frame_rate_num" ) )
179 {
180 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
181 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
182 if ( profile )
183 {
184 profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
185 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
186 }
187 }
188 else if ( !strcmp( name, "frame_rate_den" ) )
189 {
190 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
191 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
192 if ( profile )
193 {
194 profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
195 mlt_properties_set_double( properties, "fps", mlt_profile_fps( profile ) );
196 }
197 }
198 else if ( !strcmp( name, "width" ) )
199 {
200 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
201 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
202 if ( profile )
203 profile->width = mlt_properties_get_int( properties, "width" );
204 }
205 else if ( !strcmp( name, "height" ) )
206 {
207 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
208 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
209 if ( profile )
210 profile->height = mlt_properties_get_int( properties, "height" );
211 }
212 else if ( !strcmp( name, "progressive" ) )
213 {
214 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
215 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
216 if ( profile )
217 profile->progressive = mlt_properties_get_int( properties, "progressive" );
218 }
219 else if ( !strcmp( name, "sample_aspect_num" ) )
220 {
221 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
222 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
223 profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
224 if ( profile )
225 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
226 }
227 else if ( !strcmp( name, "sample_aspect_den" ) )
228 {
229 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
230 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
231 profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
232 if ( profile )
233 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
234 }
235 else if ( !strcmp( name, "display_aspect_num" ) )
236 {
237 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
238 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
239 if ( profile )
240 {
241 profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
242 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
243 }
244 }
245 else if ( !strcmp( name, "display_aspect_den" ) )
246 {
247 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
248 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
249 if ( profile )
250 {
251 profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
252 mlt_properties_set_double( properties, "display_ratio", mlt_profile_dar( profile ) );
253 }
254 }
255 }
256
257 /** The transmitter for the consumer-frame-show event
258 *
259 * Invokes the listener.
260 *
261 * \private \memberof mlt_consumer_s
262 * \param listener a function pointer that will be invoked
263 * \param owner a properties list that will be passed to \p listener
264 * \param this a service that will be passed to \p listener
265 * \param args an array of pointers - the first entry is passed as a string to \p listener
266 */
267
268 static void mlt_consumer_frame_show( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
269 {
270 if ( listener != NULL )
271 listener( owner, this, ( mlt_frame )args[ 0 ] );
272 }
273
274 /** The transmitter for the consumer-frame-render 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 a properties list that will be passed to \p listener
281 * \param this 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_render( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
286 {
287 if ( listener != NULL )
288 listener( owner, this, ( mlt_frame )args[ 0 ] );
289 }
290
291 /** Create a new consumer.
292 *
293 * \public \memberof mlt_consumer_s
294 * \param profile a profile (optional, but recommended)
295 * \return a new consumer
296 */
297
298 mlt_consumer mlt_consumer_new( mlt_profile profile )
299 {
300 // Create the memory for the structure
301 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
302
303 // Initialise it
304 if ( this != NULL )
305 mlt_consumer_init( this, NULL, profile );
306
307 // Return it
308 return this;
309 }
310
311 /** Get the parent service object.
312 *
313 * \public \memberof mlt_consumer_s
314 * \param this a consumer
315 * \return the parent service class
316 * \see MLT_CONSUMER_SERVICE
317 */
318
319 mlt_service mlt_consumer_service( mlt_consumer this )
320 {
321 return this != NULL ? &this->parent : NULL;
322 }
323
324 /** Get the consumer properties.
325 *
326 * \public \memberof mlt_consumer_s
327 * \param this a consumer
328 * \return the consumer's properties list
329 * \see MLT_CONSUMER_PROPERTIES
330 */
331
332 mlt_properties mlt_consumer_properties( mlt_consumer this )
333 {
334 return this != NULL ? MLT_SERVICE_PROPERTIES( &this->parent ) : NULL;
335 }
336
337 /** Connect the consumer to the producer.
338 *
339 * \public \memberof mlt_consumer_s
340 * \param this a consumer
341 * \param producer a producer
342 * \return > 0 warning, == 0 success, < 0 serious error,
343 * 1 = this service does not accept input,
344 * 2 = the producer is invalid,
345 * 3 = the producer is already registered with this consumer
346 */
347
348 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
349 {
350 return mlt_service_connect_producer( &this->parent, producer, 0 );
351 }
352
353 /** Start the consumer.
354 *
355 * \public \memberof mlt_consumer_s
356 * \param this a consumer
357 * \return true if there was an error
358 */
359
360 int mlt_consumer_start( mlt_consumer this )
361 {
362 // Stop listening to the property-changed event
363 mlt_event_block( this->event_listener );
364
365 // Get the properies
366 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
367
368 // Determine if there's a test card producer
369 char *test_card = mlt_properties_get( properties, "test_card" );
370
371 // Just to make sure nothing is hanging around...
372 mlt_frame_close( this->put );
373 this->put = NULL;
374 this->put_active = 1;
375
376 // Deal with it now.
377 if ( test_card != NULL )
378 {
379 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
380 {
381 // Create a test card producer
382 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( this ) );
383 mlt_producer producer = mlt_factory_producer( profile, NULL, test_card );
384
385 // Do we have a producer
386 if ( producer != NULL )
387 {
388 // Test card should loop I guess...
389 mlt_properties_set( MLT_PRODUCER_PROPERTIES( producer ), "eof", "loop" );
390 //mlt_producer_set_speed( producer, 0 );
391 //mlt_producer_set_in_and_out( producer, 0, 0 );
392
393 // Set the test card on the consumer
394 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
395 }
396 }
397 }
398 else
399 {
400 // Allow the hash table to speed things up
401 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
402 }
403
404 // Set the frame duration in microseconds for the frame-dropping heuristic
405 int frame_duration = 1000000 / mlt_properties_get_int( properties, "frame_rate_num" ) *
406 mlt_properties_get_int( properties, "frame_rate_den" );
407 mlt_properties_set_int( properties, "frame_duration", frame_duration );
408
409 // Check and run an ante command
410 if ( mlt_properties_get( properties, "ante" ) )
411 system( mlt_properties_get( properties, "ante" ) );
412
413 // Set the real_time preference
414 this->real_time = mlt_properties_get_int( properties, "real_time" );
415
416 // Start the service
417 if ( this->start != NULL )
418 return this->start( this );
419
420 return 0;
421 }
422
423 /** An alternative method to feed frames into the consumer.
424 *
425 * Only valid if the consumer itself is not connected.
426 *
427 * \public \memberof mlt_consumer_s
428 * \param this a consumer
429 * \param frame a frame
430 * \return true (ignore this for now)
431 */
432
433 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
434 {
435 int error = 1;
436
437 // Get the service assoicated to the consumer
438 mlt_service service = MLT_CONSUMER_SERVICE( this );
439
440 if ( mlt_service_producer( service ) == NULL )
441 {
442 struct timeval now;
443 struct timespec tm;
444 pthread_mutex_lock( &this->put_mutex );
445 while ( this->put_active && this->put != NULL )
446 {
447 gettimeofday( &now, NULL );
448 tm.tv_sec = now.tv_sec + 1;
449 tm.tv_nsec = now.tv_usec * 1000;
450 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
451 }
452 if ( this->put_active && this->put == NULL )
453 this->put = frame;
454 else
455 mlt_frame_close( frame );
456 pthread_cond_broadcast( &this->put_cond );
457 pthread_mutex_unlock( &this->put_mutex );
458 }
459 else
460 {
461 mlt_frame_close( frame );
462 }
463
464 return error;
465 }
466
467 /** Protected method for consumer to get frames from connected service
468 *
469 * \public \memberof mlt_consumer_s
470 * \param this a consumer
471 * \return a frame
472 */
473
474 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
475 {
476 // Frame to return
477 mlt_frame frame = NULL;
478
479 // Get the service assoicated to the consumer
480 mlt_service service = MLT_CONSUMER_SERVICE( this );
481
482 // Get the consumer properties
483 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
484
485 // Get the frame
486 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
487 {
488 struct timeval now;
489 struct timespec tm;
490 pthread_mutex_lock( &this->put_mutex );
491 while ( this->put_active && this->put == NULL )
492 {
493 gettimeofday( &now, NULL );
494 tm.tv_sec = now.tv_sec + 1;
495 tm.tv_nsec = now.tv_usec * 1000;
496 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
497 }
498 frame = this->put;
499 this->put = NULL;
500 pthread_cond_broadcast( &this->put_cond );
501 pthread_mutex_unlock( &this->put_mutex );
502 if ( frame != NULL )
503 mlt_service_apply_filters( service, frame, 0 );
504 }
505 else if ( mlt_service_producer( service ) != NULL )
506 {
507 mlt_service_get_frame( service, &frame, 0 );
508 }
509 else
510 {
511 frame = mlt_frame_init( service );
512 }
513
514 if ( frame != NULL )
515 {
516 // Get the frame properties
517 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
518
519 // Get the test card producer
520 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
521
522 // Attach the test frame producer to it.
523 if ( test_card != NULL )
524 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
525
526 // Attach the rescale property
527 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
528
529 // Aspect ratio and other jiggery pokery
530 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
531 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
532 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
533 }
534
535 // Return the frame
536 return frame;
537 }
538
539 /** Compute the time difference between now and a time value.
540 *
541 * \private \memberof mlt_consumer_s
542 * \param time1 a time value to be compared against now
543 * \return the difference in microseconds
544 */
545
546 static inline long time_difference( struct timeval *time1 )
547 {
548 struct timeval time2;
549 time2.tv_sec = time1->tv_sec;
550 time2.tv_usec = time1->tv_usec;
551 gettimeofday( time1, NULL );
552 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
553 }
554
555 /** The thread procedure for asynchronously pulling frames through the service
556 * network connected to a consumer.
557 *
558 * \private \memberof mlt_consumer_s
559 * \param arg a consumer
560 */
561
562 static void *consumer_read_ahead_thread( void *arg )
563 {
564 // The argument is the consumer
565 mlt_consumer this = arg;
566
567 // Get the properties of the consumer
568 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
569
570 // Get the width and height
571 int width = mlt_properties_get_int( properties, "width" );
572 int height = mlt_properties_get_int( properties, "height" );
573
574 // See if video is turned off
575 int video_off = mlt_properties_get_int( properties, "video_off" );
576 int preview_off = mlt_properties_get_int( properties, "preview_off" );
577 int preview_format = mlt_properties_get_int( properties, "preview_format" );
578
579 // Get the audio settings
580 mlt_audio_format afmt = mlt_audio_pcm;
581 int counter = 0;
582 double fps = mlt_properties_get_double( properties, "fps" );
583 int channels = mlt_properties_get_int( properties, "channels" );
584 int frequency = mlt_properties_get_int( properties, "frequency" );
585 int samples = 0;
586 int16_t *pcm = NULL;
587
588 // See if audio is turned off
589 int audio_off = mlt_properties_get_int( properties, "audio_off" );
590
591 // Get the maximum size of the buffer
592 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
593
594 // General frame variable
595 mlt_frame frame = NULL;
596 uint8_t *image = NULL;
597
598 // Time structures
599 struct timeval ante;
600
601 // Average time for get_frame and get_image
602 int count = 1;
603 int skipped = 0;
604 int64_t time_wait = 0;
605 int64_t time_frame = 0;
606 int64_t time_process = 0;
607 int skip_next = 0;
608 mlt_service lock_object = NULL;
609
610 if ( preview_off && preview_format != 0 )
611 this->format = preview_format;
612
613 // Get the first frame
614 frame = mlt_consumer_get_frame( this );
615
616 // Get the lock object
617 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
618
619 // Lock it
620 if ( lock_object ) mlt_service_lock( lock_object );
621
622 // Get the image of the first frame
623 if ( !video_off )
624 {
625 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
626 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
627 }
628
629 if ( !audio_off )
630 {
631 samples = mlt_sample_calculator( fps, frequency, counter++ );
632 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
633 }
634
635 // Unlock the lock object
636 if ( lock_object ) mlt_service_unlock( lock_object );
637
638 // Mark as rendered
639 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
640
641 // Get the starting time (can ignore the times above)
642 gettimeofday( &ante, NULL );
643
644 // Continue to read ahead
645 while ( this->ahead )
646 {
647 // Fetch width/height again
648 width = mlt_properties_get_int( properties, "width" );
649 height = mlt_properties_get_int( properties, "height" );
650
651 // Put the current frame into the queue
652 pthread_mutex_lock( &this->mutex );
653 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
654 pthread_cond_wait( &this->cond, &this->mutex );
655 mlt_deque_push_back( this->queue, frame );
656 pthread_cond_broadcast( &this->cond );
657 pthread_mutex_unlock( &this->mutex );
658
659 time_wait += time_difference( &ante );
660
661 // Get the next frame
662 frame = mlt_consumer_get_frame( this );
663 time_frame += time_difference( &ante );
664
665 // If there's no frame, we're probably stopped...
666 if ( frame == NULL )
667 continue;
668
669 // Attempt to fetch the lock object
670 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
671
672 // Increment the count
673 count ++;
674
675 // Lock if there's a lock object
676 if ( lock_object ) mlt_service_lock( lock_object );
677
678 // All non normal playback frames should be shown
679 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
680 {
681 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
682 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
683 #endif
684 skipped = 0;
685 time_frame = 0;
686 time_process = 0;
687 time_wait = 0;
688 count = 1;
689 skip_next = 0;
690 }
691
692 // Get the image
693 if ( !skip_next || this->real_time == -1 )
694 {
695 // Get the image, mark as rendered and time it
696 if ( !video_off )
697 {
698 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
699 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
700 }
701 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
702 }
703 else
704 {
705 // Increment the number of sequentially skipped frames
706 skipped ++;
707 skip_next = 0;
708
709 // If we've reached an unacceptable level, reset everything
710 if ( skipped > 5 )
711 {
712 skipped = 0;
713 time_frame = 0;
714 time_process = 0;
715 time_wait = 0;
716 count = 1;
717 }
718 }
719
720 // Always process audio
721 if ( !audio_off )
722 {
723 samples = mlt_sample_calculator( fps, frequency, counter++ );
724 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
725 }
726
727 // Increment the time take for this frame
728 time_process += time_difference( &ante );
729
730 // Determine if the next frame should be skipped
731 if ( mlt_deque_count( this->queue ) <= 5 )
732 {
733 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
734 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
735 skip_next = 1;
736 }
737
738 // Unlock if there's a lock object
739 if ( lock_object ) mlt_service_unlock( lock_object );
740 }
741
742 // Remove the last frame
743 mlt_frame_close( frame );
744
745 return NULL;
746 }
747
748 /** Start the read/render thread.
749 *
750 * \private \memberof mlt_consumer_s
751 * \param this a consumer
752 */
753
754 static void consumer_read_ahead_start( mlt_consumer this )
755 {
756 // We're running now
757 this->ahead = 1;
758
759 // Create the frame queue
760 this->queue = mlt_deque_init( );
761
762 // Create the mutex
763 pthread_mutex_init( &this->mutex, NULL );
764
765 // Create the condition
766 pthread_cond_init( &this->cond, NULL );
767
768 // Create the read ahead
769 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
770 {
771 struct sched_param priority;
772 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
773 pthread_attr_t thread_attributes;
774 pthread_attr_init( &thread_attributes );
775 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
776 pthread_attr_setschedparam( &thread_attributes, &priority );
777 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
778 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
779 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
780 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
781 pthread_attr_destroy( &thread_attributes );
782 }
783 else
784 {
785 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
786 }
787 }
788
789 /** Stop the read/render thread.
790 *
791 * \private \memberof mlt_consumer_s
792 * \param this a consumer
793 */
794
795 static void consumer_read_ahead_stop( mlt_consumer this )
796 {
797 // Make sure we're running
798 if ( this->ahead )
799 {
800 // Inform thread to stop
801 this->ahead = 0;
802
803 // Broadcast to the condition in case it's waiting
804 pthread_mutex_lock( &this->mutex );
805 pthread_cond_broadcast( &this->cond );
806 pthread_mutex_unlock( &this->mutex );
807
808 // Broadcast to the put condition in case it's waiting
809 pthread_mutex_lock( &this->put_mutex );
810 pthread_cond_broadcast( &this->put_cond );
811 pthread_mutex_unlock( &this->put_mutex );
812
813 // Join the thread
814 pthread_join( this->ahead_thread, NULL );
815
816 // Destroy the mutex
817 pthread_mutex_destroy( &this->mutex );
818
819 // Destroy the condition
820 pthread_cond_destroy( &this->cond );
821
822 // Wipe the queue
823 while ( mlt_deque_count( this->queue ) )
824 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
825
826 // Close the queue
827 mlt_deque_close( this->queue );
828 }
829 }
830
831 /** Flush the read/render thread's buffer.
832 *
833 * \public \memberof mlt_consumer_s
834 * \param this a consumer
835 */
836
837 void mlt_consumer_purge( mlt_consumer this )
838 {
839 if ( this->ahead )
840 {
841 pthread_mutex_lock( &this->mutex );
842 while ( mlt_deque_count( this->queue ) )
843 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
844 pthread_cond_broadcast( &this->cond );
845 pthread_mutex_unlock( &this->mutex );
846 }
847 }
848
849 /** Get the next frame from the producer connected to a consumer.
850 *
851 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
852 * the asynchronous/real-time behavior configurable at runtime.
853 * You should close the frame returned from this when you are done with it.
854 *
855 * \public \memberof mlt_consumer_s
856 * \param this a consumer
857 * \return a frame
858 */
859
860 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
861 {
862 // Frame to return
863 mlt_frame frame = NULL;
864
865 // Get the properties
866 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
867
868 // Check if the user has requested real time or not
869 if ( this->real_time )
870 {
871 int size = 1;
872
873 // Is the read ahead running?
874 if ( this->ahead == 0 )
875 {
876 int buffer = mlt_properties_get_int( properties, "buffer" );
877 int prefill = mlt_properties_get_int( properties, "prefill" );
878 consumer_read_ahead_start( this );
879 if ( buffer > 1 )
880 size = prefill > 0 && prefill < buffer ? prefill : buffer;
881 }
882
883 // Get frame from queue
884 pthread_mutex_lock( &this->mutex );
885 while( this->ahead && mlt_deque_count( this->queue ) < size )
886 pthread_cond_wait( &this->cond, &this->mutex );
887 frame = mlt_deque_pop_front( this->queue );
888 pthread_cond_broadcast( &this->cond );
889 pthread_mutex_unlock( &this->mutex );
890 }
891 else
892 {
893 // Get the frame in non real time
894 frame = mlt_consumer_get_frame( this );
895
896 // This isn't true, but from the consumers perspective it is
897 if ( frame != NULL )
898 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
899 }
900
901 return frame;
902 }
903
904 /** Callback for the implementation to indicate a stopped condition.
905 *
906 * \public \memberof mlt_consumer_s
907 * \param this a consumer
908 */
909
910 void mlt_consumer_stopped( mlt_consumer this )
911 {
912 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
913 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
914 mlt_event_unblock( this->event_listener );
915 }
916
917 /** Stop the consumer.
918 *
919 * \public \memberof mlt_consumer_s
920 * \param this a consumer
921 * \return true if there was an error
922 */
923
924 int mlt_consumer_stop( mlt_consumer this )
925 {
926 // Get the properies
927 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
928
929 // Just in case...
930 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
931 pthread_mutex_lock( &this->put_mutex );
932 this->put_active = 0;
933 pthread_cond_broadcast( &this->put_cond );
934 pthread_mutex_unlock( &this->put_mutex );
935
936 // Stop the consumer
937 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
938 if ( this->stop != NULL )
939 this->stop( this );
940
941 // Check if the user has requested real time or not and stop if necessary
942 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
943 if ( mlt_properties_get_int( properties, "real_time" ) )
944 consumer_read_ahead_stop( this );
945
946 // Kill the test card
947 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
948
949 // Check and run a post command
950 if ( mlt_properties_get( properties, "post" ) )
951 system( mlt_properties_get( properties, "post" ) );
952
953 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
954
955 return 0;
956 }
957
958 /** Determine if the consumer is stopped.
959 *
960 * \public \memberof mlt_consumer_s
961 * \param this a consumer
962 * \return true if the consumer is stopped
963 */
964
965 int mlt_consumer_is_stopped( mlt_consumer this )
966 {
967 // Check if the consumer is stopped
968 if ( this->is_stopped != NULL )
969 return this->is_stopped( this );
970
971 return 0;
972 }
973
974 /** Close and destroy the consumer.
975 *
976 * \public \memberof mlt_consumer_s
977 * \param this a consumer
978 */
979
980 void mlt_consumer_close( mlt_consumer this )
981 {
982 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
983 {
984 // Get the childs close function
985 void ( *consumer_close )( ) = this->close;
986
987 if ( consumer_close )
988 {
989 // Just in case...
990 //mlt_consumer_stop( this );
991
992 this->close = NULL;
993 consumer_close( this );
994 }
995 else
996 {
997 // Make sure it only gets called once
998 this->parent.close = NULL;
999
1000 // Destroy the push mutex and condition
1001 pthread_mutex_destroy( &this->put_mutex );
1002 pthread_cond_destroy( &this->put_cond );
1003
1004 mlt_service_close( &this->parent );
1005 }
1006 }
1007 }