Constness changes
[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 system( mlt_properties_get( properties, "ante" ) );
413
414 // Set the real_time preference
415 this->real_time = mlt_properties_get_int( properties, "real_time" );
416
417 // Start the service
418 if ( this->start != NULL )
419 return this->start( this );
420
421 return 0;
422 }
423
424 /** An alternative method to feed frames into the consumer.
425 *
426 * Only valid if the consumer itself is not connected.
427 *
428 * \public \memberof mlt_consumer_s
429 * \param this a consumer
430 * \param frame a frame
431 * \return true (ignore this for now)
432 */
433
434 int mlt_consumer_put_frame( mlt_consumer this, mlt_frame frame )
435 {
436 int error = 1;
437
438 // Get the service assoicated to the consumer
439 mlt_service service = MLT_CONSUMER_SERVICE( this );
440
441 if ( mlt_service_producer( service ) == NULL )
442 {
443 struct timeval now;
444 struct timespec tm;
445 pthread_mutex_lock( &this->put_mutex );
446 while ( this->put_active && this->put != NULL )
447 {
448 gettimeofday( &now, NULL );
449 tm.tv_sec = now.tv_sec + 1;
450 tm.tv_nsec = now.tv_usec * 1000;
451 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
452 }
453 if ( this->put_active && this->put == NULL )
454 this->put = frame;
455 else
456 mlt_frame_close( frame );
457 pthread_cond_broadcast( &this->put_cond );
458 pthread_mutex_unlock( &this->put_mutex );
459 }
460 else
461 {
462 mlt_frame_close( frame );
463 }
464
465 return error;
466 }
467
468 /** Protected method for consumer to get frames from connected service
469 *
470 * \public \memberof mlt_consumer_s
471 * \param this a consumer
472 * \return a frame
473 */
474
475 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
476 {
477 // Frame to return
478 mlt_frame frame = NULL;
479
480 // Get the service assoicated to the consumer
481 mlt_service service = MLT_CONSUMER_SERVICE( this );
482
483 // Get the consumer properties
484 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
485
486 // Get the frame
487 if ( mlt_service_producer( service ) == NULL && mlt_properties_get_int( properties, "put_mode" ) )
488 {
489 struct timeval now;
490 struct timespec tm;
491 pthread_mutex_lock( &this->put_mutex );
492 while ( this->put_active && this->put == NULL )
493 {
494 gettimeofday( &now, NULL );
495 tm.tv_sec = now.tv_sec + 1;
496 tm.tv_nsec = now.tv_usec * 1000;
497 pthread_cond_timedwait( &this->put_cond, &this->put_mutex, &tm );
498 }
499 frame = this->put;
500 this->put = NULL;
501 pthread_cond_broadcast( &this->put_cond );
502 pthread_mutex_unlock( &this->put_mutex );
503 if ( frame != NULL )
504 mlt_service_apply_filters( service, frame, 0 );
505 }
506 else if ( mlt_service_producer( service ) != NULL )
507 {
508 mlt_service_get_frame( service, &frame, 0 );
509 }
510 else
511 {
512 frame = mlt_frame_init( service );
513 }
514
515 if ( frame != NULL )
516 {
517 // Get the frame properties
518 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
519
520 // Get the test card producer
521 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
522
523 // Attach the test frame producer to it.
524 if ( test_card != NULL )
525 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
526
527 // Attach the rescale property
528 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
529
530 // Aspect ratio and other jiggery pokery
531 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
532 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "progressive" ) | mlt_properties_get_int( properties, "deinterlace" ) );
533 mlt_properties_set( frame_properties, "deinterlace_method", mlt_properties_get( properties, "deinterlace_method" ) );
534 }
535
536 // Return the frame
537 return frame;
538 }
539
540 /** Compute the time difference between now and a time value.
541 *
542 * \private \memberof mlt_consumer_s
543 * \param time1 a time value to be compared against now
544 * \return the difference in microseconds
545 */
546
547 static inline long time_difference( struct timeval *time1 )
548 {
549 struct timeval time2;
550 time2.tv_sec = time1->tv_sec;
551 time2.tv_usec = time1->tv_usec;
552 gettimeofday( time1, NULL );
553 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
554 }
555
556 /** The thread procedure for asynchronously pulling frames through the service
557 * network connected to a consumer.
558 *
559 * \private \memberof mlt_consumer_s
560 * \param arg a consumer
561 */
562
563 static void *consumer_read_ahead_thread( void *arg )
564 {
565 // The argument is the consumer
566 mlt_consumer this = arg;
567
568 // Get the properties of the consumer
569 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
570
571 // Get the width and height
572 int width = mlt_properties_get_int( properties, "width" );
573 int height = mlt_properties_get_int( properties, "height" );
574
575 // See if video is turned off
576 int video_off = mlt_properties_get_int( properties, "video_off" );
577 int preview_off = mlt_properties_get_int( properties, "preview_off" );
578 int preview_format = mlt_properties_get_int( properties, "preview_format" );
579
580 // Get the audio settings
581 mlt_audio_format afmt = mlt_audio_pcm;
582 int counter = 0;
583 double fps = mlt_properties_get_double( properties, "fps" );
584 int channels = mlt_properties_get_int( properties, "channels" );
585 int frequency = mlt_properties_get_int( properties, "frequency" );
586 int samples = 0;
587 int16_t *pcm = NULL;
588
589 // See if audio is turned off
590 int audio_off = mlt_properties_get_int( properties, "audio_off" );
591
592 // Get the maximum size of the buffer
593 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
594
595 // General frame variable
596 mlt_frame frame = NULL;
597 uint8_t *image = NULL;
598
599 // Time structures
600 struct timeval ante;
601
602 // Average time for get_frame and get_image
603 int count = 1;
604 int skipped = 0;
605 int64_t time_wait = 0;
606 int64_t time_frame = 0;
607 int64_t time_process = 0;
608 int skip_next = 0;
609 mlt_service lock_object = NULL;
610
611 if ( preview_off && preview_format != 0 )
612 this->format = preview_format;
613
614 // Get the first frame
615 frame = mlt_consumer_get_frame( this );
616
617 // Get the lock object
618 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
619
620 // Lock it
621 if ( lock_object ) mlt_service_lock( lock_object );
622
623 // Get the image of the first frame
624 if ( !video_off )
625 {
626 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
627 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
628 }
629
630 if ( !audio_off )
631 {
632 samples = mlt_sample_calculator( fps, frequency, counter++ );
633 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
634 }
635
636 // Unlock the lock object
637 if ( lock_object ) mlt_service_unlock( lock_object );
638
639 // Mark as rendered
640 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
641
642 // Get the starting time (can ignore the times above)
643 gettimeofday( &ante, NULL );
644
645 // Continue to read ahead
646 while ( this->ahead )
647 {
648 // Fetch width/height again
649 width = mlt_properties_get_int( properties, "width" );
650 height = mlt_properties_get_int( properties, "height" );
651
652 // Put the current frame into the queue
653 pthread_mutex_lock( &this->mutex );
654 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
655 pthread_cond_wait( &this->cond, &this->mutex );
656 mlt_deque_push_back( this->queue, frame );
657 pthread_cond_broadcast( &this->cond );
658 pthread_mutex_unlock( &this->mutex );
659
660 time_wait += time_difference( &ante );
661
662 // Get the next frame
663 frame = mlt_consumer_get_frame( this );
664 time_frame += time_difference( &ante );
665
666 // If there's no frame, we're probably stopped...
667 if ( frame == NULL )
668 continue;
669
670 // Attempt to fetch the lock object
671 lock_object = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "consumer_lock_service", NULL );
672
673 // Increment the count
674 count ++;
675
676 // Lock if there's a lock object
677 if ( lock_object ) mlt_service_lock( lock_object );
678
679 // All non normal playback frames should be shown
680 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "_speed" ) != 1 )
681 {
682 #ifdef DEINTERLACE_ON_NOT_NORMAL_SPEED
683 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "consumer_deinterlace", 1 );
684 #endif
685 skipped = 0;
686 time_frame = 0;
687 time_process = 0;
688 time_wait = 0;
689 count = 1;
690 skip_next = 0;
691 }
692
693 // Get the image
694 if ( !skip_next || this->real_time == -1 )
695 {
696 // Get the image, mark as rendered and time it
697 if ( !video_off )
698 {
699 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-frame-render", frame, NULL );
700 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
701 }
702 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
703 }
704 else
705 {
706 // Increment the number of sequentially skipped frames
707 skipped ++;
708 skip_next = 0;
709
710 // If we've reached an unacceptable level, reset everything
711 if ( skipped > 5 )
712 {
713 skipped = 0;
714 time_frame = 0;
715 time_process = 0;
716 time_wait = 0;
717 count = 1;
718 }
719 }
720
721 // Always process audio
722 if ( !audio_off )
723 {
724 samples = mlt_sample_calculator( fps, frequency, counter++ );
725 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
726 }
727
728 // Increment the time take for this frame
729 time_process += time_difference( &ante );
730
731 // Determine if the next frame should be skipped
732 if ( mlt_deque_count( this->queue ) <= 5 )
733 {
734 int frame_duration = mlt_properties_get_int( properties, "frame_duration" );
735 if ( ( ( time_wait + time_frame + time_process ) / count ) > frame_duration )
736 skip_next = 1;
737 }
738
739 // Unlock if there's a lock object
740 if ( lock_object ) mlt_service_unlock( lock_object );
741 }
742
743 // Remove the last frame
744 mlt_frame_close( frame );
745
746 return NULL;
747 }
748
749 /** Start the read/render thread.
750 *
751 * \private \memberof mlt_consumer_s
752 * \param this a consumer
753 */
754
755 static void consumer_read_ahead_start( mlt_consumer this )
756 {
757 // We're running now
758 this->ahead = 1;
759
760 // Create the frame queue
761 this->queue = mlt_deque_init( );
762
763 // Create the mutex
764 pthread_mutex_init( &this->mutex, NULL );
765
766 // Create the condition
767 pthread_cond_init( &this->cond, NULL );
768
769 // Create the read ahead
770 if ( mlt_properties_get( MLT_CONSUMER_PROPERTIES( this ), "priority" ) )
771 {
772 struct sched_param priority;
773 priority.sched_priority = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( this ), "priority" );
774 pthread_attr_t thread_attributes;
775 pthread_attr_init( &thread_attributes );
776 pthread_attr_setschedpolicy( &thread_attributes, SCHED_OTHER );
777 pthread_attr_setschedparam( &thread_attributes, &priority );
778 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_EXPLICIT_SCHED );
779 pthread_attr_setscope( &thread_attributes, PTHREAD_SCOPE_SYSTEM );
780 if ( pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this ) < 0 )
781 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
782 pthread_attr_destroy( &thread_attributes );
783 }
784 else
785 {
786 pthread_create( &this->ahead_thread, NULL, consumer_read_ahead_thread, this );
787 }
788 }
789
790 /** Stop the read/render thread.
791 *
792 * \private \memberof mlt_consumer_s
793 * \param this a consumer
794 */
795
796 static void consumer_read_ahead_stop( mlt_consumer this )
797 {
798 // Make sure we're running
799 if ( this->ahead )
800 {
801 // Inform thread to stop
802 this->ahead = 0;
803
804 // Broadcast to the condition in case it's waiting
805 pthread_mutex_lock( &this->mutex );
806 pthread_cond_broadcast( &this->cond );
807 pthread_mutex_unlock( &this->mutex );
808
809 // Broadcast to the put condition in case it's waiting
810 pthread_mutex_lock( &this->put_mutex );
811 pthread_cond_broadcast( &this->put_cond );
812 pthread_mutex_unlock( &this->put_mutex );
813
814 // Join the thread
815 pthread_join( this->ahead_thread, NULL );
816
817 // Destroy the mutex
818 pthread_mutex_destroy( &this->mutex );
819
820 // Destroy the condition
821 pthread_cond_destroy( &this->cond );
822
823 // Wipe the queue
824 while ( mlt_deque_count( this->queue ) )
825 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
826
827 // Close the queue
828 mlt_deque_close( this->queue );
829 }
830 }
831
832 /** Flush the read/render thread's buffer.
833 *
834 * \public \memberof mlt_consumer_s
835 * \param this a consumer
836 */
837
838 void mlt_consumer_purge( mlt_consumer this )
839 {
840 if ( this->ahead )
841 {
842 pthread_mutex_lock( &this->mutex );
843 while ( mlt_deque_count( this->queue ) )
844 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
845 pthread_cond_broadcast( &this->cond );
846 pthread_mutex_unlock( &this->mutex );
847 }
848 }
849
850 /** Get the next frame from the producer connected to a consumer.
851 *
852 * Typically, one uses this instead of \p mlt_consumer_get_frame to make
853 * the asynchronous/real-time behavior configurable at runtime.
854 * You should close the frame returned from this when you are done with it.
855 *
856 * \public \memberof mlt_consumer_s
857 * \param this a consumer
858 * \return a frame
859 */
860
861 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
862 {
863 // Frame to return
864 mlt_frame frame = NULL;
865
866 // Get the properties
867 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
868
869 // Check if the user has requested real time or not
870 if ( this->real_time )
871 {
872 int size = 1;
873
874 // Is the read ahead running?
875 if ( this->ahead == 0 )
876 {
877 int buffer = mlt_properties_get_int( properties, "buffer" );
878 int prefill = mlt_properties_get_int( properties, "prefill" );
879 consumer_read_ahead_start( this );
880 if ( buffer > 1 )
881 size = prefill > 0 && prefill < buffer ? prefill : buffer;
882 }
883
884 // Get frame from queue
885 pthread_mutex_lock( &this->mutex );
886 while( this->ahead && mlt_deque_count( this->queue ) < size )
887 pthread_cond_wait( &this->cond, &this->mutex );
888 frame = mlt_deque_pop_front( this->queue );
889 pthread_cond_broadcast( &this->cond );
890 pthread_mutex_unlock( &this->mutex );
891 }
892 else
893 {
894 // Get the frame in non real time
895 frame = mlt_consumer_get_frame( this );
896
897 // This isn't true, but from the consumers perspective it is
898 if ( frame != NULL )
899 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "rendered", 1 );
900 }
901
902 return frame;
903 }
904
905 /** Callback for the implementation to indicate a stopped condition.
906 *
907 * \public \memberof mlt_consumer_s
908 * \param this a consumer
909 */
910
911 void mlt_consumer_stopped( mlt_consumer this )
912 {
913 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "running", 0 );
914 mlt_events_fire( MLT_CONSUMER_PROPERTIES( this ), "consumer-stopped", NULL );
915 mlt_event_unblock( this->event_listener );
916 }
917
918 /** Stop the consumer.
919 *
920 * \public \memberof mlt_consumer_s
921 * \param this a consumer
922 * \return true if there was an error
923 */
924
925 int mlt_consumer_stop( mlt_consumer this )
926 {
927 // Get the properies
928 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
929
930 // Just in case...
931 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping put waiting\n" );
932 pthread_mutex_lock( &this->put_mutex );
933 this->put_active = 0;
934 pthread_cond_broadcast( &this->put_cond );
935 pthread_mutex_unlock( &this->put_mutex );
936
937 // Stop the consumer
938 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping consumer\n" );
939 if ( this->stop != NULL )
940 this->stop( this );
941
942 // Check if the user has requested real time or not and stop if necessary
943 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopping read_ahead\n" );
944 if ( mlt_properties_get_int( properties, "real_time" ) )
945 consumer_read_ahead_stop( this );
946
947 // Kill the test card
948 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
949
950 // Check and run a post command
951 if ( mlt_properties_get( properties, "post" ) )
952 system( mlt_properties_get( properties, "post" ) );
953
954 mlt_log( MLT_CONSUMER_SERVICE( this ), MLT_LOG_DEBUG, "stopped\n" );
955
956 return 0;
957 }
958
959 /** Determine if the consumer is stopped.
960 *
961 * \public \memberof mlt_consumer_s
962 * \param this a consumer
963 * \return true if the consumer is stopped
964 */
965
966 int mlt_consumer_is_stopped( mlt_consumer this )
967 {
968 // Check if the consumer is stopped
969 if ( this->is_stopped != NULL )
970 return this->is_stopped( this );
971
972 return 0;
973 }
974
975 /** Close and destroy the consumer.
976 *
977 * \public \memberof mlt_consumer_s
978 * \param this a consumer
979 */
980
981 void mlt_consumer_close( mlt_consumer this )
982 {
983 if ( this != NULL && mlt_properties_dec_ref( MLT_CONSUMER_PROPERTIES( this ) ) <= 0 )
984 {
985 // Get the childs close function
986 void ( *consumer_close )( ) = this->close;
987
988 if ( consumer_close )
989 {
990 // Just in case...
991 //mlt_consumer_stop( this );
992
993 this->close = NULL;
994 consumer_close( this );
995 }
996 else
997 {
998 // Make sure it only gets called once
999 this->parent.close = NULL;
1000
1001 // Destroy the push mutex and condition
1002 pthread_mutex_destroy( &this->put_mutex );
1003 pthread_cond_destroy( &this->put_cond );
1004
1005 mlt_service_close( &this->parent );
1006 }
1007 }
1008 }