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