6074655b24a759396d57f6d20a15f1f1d5413257
[melted] / src / framework / mlt_consumer.c
1 /*
2 * mlt_consumer.c -- abstraction for all consumer services
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <sys/time.h>
30
31 /** Public final methods
32 */
33
34 int mlt_consumer_init( mlt_consumer this, void *child )
35 {
36 int error = 0;
37 memset( this, 0, sizeof( struct mlt_consumer_s ) );
38 this->child = child;
39 error = mlt_service_init( &this->parent, this );
40 if ( error == 0 )
41 {
42 // Get the properties from the service
43 mlt_properties properties = mlt_service_properties( &this->parent );
44
45 // Get the normalisation preference
46 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
47
48 // Deal with normalisation
49 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
50 {
51 mlt_properties_set( properties, "normalisation", "PAL" );
52 mlt_properties_set_double( properties, "fps", 25.0 );
53 mlt_properties_set_int( properties, "width", 720 );
54 mlt_properties_set_int( properties, "height", 576 );
55 mlt_properties_set_int( properties, "progressive", 0 );
56 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
57 }
58 else
59 {
60 mlt_properties_set( properties, "normalisation", "NTSC" );
61 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
62 mlt_properties_set_int( properties, "width", 720 );
63 mlt_properties_set_int( properties, "height", 480 );
64 mlt_properties_set_int( properties, "progressive", 0 );
65 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
66 }
67
68 // Default rescaler for all consumers
69 mlt_properties_set( properties, "rescale", "bilinear" );
70
71 // Default read ahead buffer size
72 mlt_properties_set_int( properties, "buffer", 25 );
73
74 // Default audio frequency and channels
75 mlt_properties_set_int( properties, "frequency", 48000 );
76 mlt_properties_set_int( properties, "channels", 2 );
77
78 // Default of all consumers is real time
79 mlt_properties_set_int( properties, "real_time", 1 );
80
81 // Default to environment test card
82 mlt_properties_set( properties, "test_card", mlt_environment( "MLT_TEST_CARD" ) );
83
84 // Hmm - default all consumers to yuv422 :-/
85 this->format = mlt_image_yuv422;
86
87 mlt_events_register( properties, "consumer-stopped", NULL );
88 }
89 return error;
90 }
91
92 /** Create a new consumer.
93 */
94
95 mlt_consumer mlt_consumer_new( )
96 {
97 // Create the memory for the structure
98 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
99
100 // Initialise it
101 if ( this != NULL )
102 mlt_consumer_init( this, NULL );
103
104 // Return it
105 return this;
106 }
107
108 /** Get the parent service object.
109 */
110
111 mlt_service mlt_consumer_service( mlt_consumer this )
112 {
113 return this != NULL ? &this->parent : NULL;
114 }
115
116 /** Get the consumer properties.
117 */
118
119 mlt_properties mlt_consumer_properties( mlt_consumer this )
120 {
121 return this != NULL ? mlt_service_properties( &this->parent ) : NULL;
122 }
123
124 /** Connect the consumer to the producer.
125 */
126
127 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
128 {
129 return mlt_service_connect_producer( &this->parent, producer, 0 );
130 }
131
132 /** Start the consumer.
133 */
134
135 int mlt_consumer_start( mlt_consumer this )
136 {
137 // Get the properies
138 mlt_properties properties = mlt_consumer_properties( this );
139
140 // Determine if there's a test card producer
141 char *test_card = mlt_properties_get( properties, "test_card" );
142
143 // Deal with it now.
144 if ( test_card != NULL )
145 {
146 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
147 {
148 // Create a test card producer
149 mlt_producer producer = mlt_factory_producer( NULL, test_card );
150
151 // Do we have a producer
152 if ( producer != NULL )
153 {
154 // Test card should loop I guess...
155 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
156
157 // Set the test card on the consumer
158 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
159 }
160 }
161 }
162 else
163 {
164 // Allow the hash table to speed things up
165 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
166 }
167
168 // Check and run an ante command
169 if ( mlt_properties_get( properties, "ante" ) )
170 system( mlt_properties_get( properties, "ante" ) );
171
172 // Set the real_time preference
173 this->real_time = mlt_properties_get_int( properties, "real_time" );
174
175 // Start the service
176 if ( this->start != NULL )
177 return this->start( this );
178
179 return 0;
180 }
181
182 /** Protected method for consumer to get frames from connected service
183 */
184
185 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
186 {
187 // Frame to return
188 mlt_frame frame = NULL;
189
190 // Get the service assoicated to the consumer
191 mlt_service service = mlt_consumer_service( this );
192
193 // Get the frame
194 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
195 {
196 // Get the consumer properties
197 mlt_properties properties = mlt_consumer_properties( this );
198
199 // Get the frame properties
200 mlt_properties frame_properties = mlt_frame_properties( frame );
201
202 // Get the test card producer
203 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
204
205 // Attach the test frame producer to it.
206 if ( test_card != NULL )
207 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
208
209 // Attach the rescale property
210 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
211
212 // Aspect ratio and other jiggery pokery
213 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
214 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
215 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
216 }
217
218 // Return the frame
219 return frame;
220 }
221
222 static inline long time_difference( struct timeval *time1 )
223 {
224 struct timeval time2;
225 time2.tv_sec = time1->tv_sec;
226 time2.tv_usec = time1->tv_usec;
227 gettimeofday( time1, NULL );
228 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
229 }
230
231 static void *consumer_read_ahead_thread( void *arg )
232 {
233 // The argument is the consumer
234 mlt_consumer this = arg;
235
236 // Get the properties of the consumer
237 mlt_properties properties = mlt_consumer_properties( this );
238
239 // Get the width and height
240 int width = mlt_properties_get_int( properties, "width" );
241 int height = mlt_properties_get_int( properties, "height" );
242
243 // See if video is turned off
244 int video_off = mlt_properties_get_int( properties, "video_off" );
245
246 // Get the audio settings
247 mlt_audio_format afmt = mlt_audio_pcm;
248 int counter = 0;
249 double fps = mlt_properties_get_double( properties, "fps" );
250 int channels = mlt_properties_get_int( properties, "channels" );
251 int frequency = mlt_properties_get_int( properties, "frequency" );
252 int samples = 0;
253 int16_t *pcm = NULL;
254
255 // See if audio is turned off
256 int audio_off = mlt_properties_get_int( properties, "audio_off" );
257
258 // Get the maximum size of the buffer
259 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
260
261 // General frame variable
262 mlt_frame frame = NULL;
263 uint8_t *image = NULL;
264
265 // Time structures
266 struct timeval ante;
267
268 // Average time for get_frame and get_image
269 int count = 1;
270 int skipped = 0;
271 int64_t time_wait = 0;
272 int64_t time_frame = 0;
273 int64_t time_process = 0;
274 int skip_next = 0;
275
276 // Get the first frame
277 frame = mlt_consumer_get_frame( this );
278
279 // Get the image of the first frame
280 if ( !video_off )
281 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
282
283 if ( !audio_off )
284 {
285 samples = mlt_sample_calculator( fps, frequency, counter++ );
286 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
287 frame->get_audio = NULL;
288 }
289
290 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
291
292 // Get the starting time (can ignore the times above)
293 gettimeofday( &ante, NULL );
294
295 // Continue to read ahead
296 while ( this->ahead )
297 {
298 // Put the current frame into the queue
299 pthread_mutex_lock( &this->mutex );
300 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
301 pthread_cond_wait( &this->cond, &this->mutex );
302 mlt_deque_push_back( this->queue, frame );
303 pthread_cond_broadcast( &this->cond );
304 pthread_mutex_unlock( &this->mutex );
305 time_wait += time_difference( &ante );
306
307 // Get the next frame
308 frame = mlt_consumer_get_frame( this );
309 time_frame += time_difference( &ante );
310
311 // Increment the count
312 count ++;
313
314 // All non normal playback frames should be shown
315 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "_speed" ) != 1 )
316 {
317 skipped = 0;
318 time_frame = 0;
319 time_process = 0;
320 time_wait = 0;
321 count = 1;
322 skip_next = 0;
323 }
324
325 // Get the image
326 if ( !skip_next )
327 {
328 // Get the image, mark as rendered and time it
329 if ( !video_off )
330 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
331 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
332 }
333 else
334 {
335 // Increment the number of sequentially skipped frames
336 skipped ++;
337 skip_next = 0;
338
339 // If we've reached an unacceptable level, reset everything
340 if ( skipped > 5 )
341 {
342 skipped = 0;
343 time_frame = 0;
344 time_process = 0;
345 time_wait = 0;
346 count = 1;
347 }
348 }
349
350 // Always process audio
351 if ( !audio_off )
352 {
353 samples = mlt_sample_calculator( fps, frequency, counter++ );
354 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
355 frame->get_audio = NULL;
356 }
357
358 // Increment the time take for this frame
359 time_process += time_difference( &ante );
360
361 // Determine if the next frame should be skipped
362 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
363 skip_next = 1;
364 }
365
366 // Remove the last frame
367 mlt_frame_close( frame );
368
369 return NULL;
370 }
371
372 static void consumer_read_ahead_start( mlt_consumer this )
373 {
374 pthread_attr_t thread_attributes;
375
376 // We're running now
377 this->ahead = 1;
378
379 // Create the frame queue
380 this->queue = mlt_deque_init( );
381
382 // Create the mutex
383 pthread_mutex_init( &this->mutex, NULL );
384
385 // Create the condition
386 pthread_cond_init( &this->cond, NULL );
387
388 // Inherit the scheduling priority
389 pthread_attr_init( &thread_attributes );
390 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
391
392 // Create the read ahead
393 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
394 }
395
396 static void consumer_read_ahead_stop( mlt_consumer this )
397 {
398 // Make sure we're running
399 if ( this->ahead )
400 {
401 // Inform thread to stop
402 this->ahead = 0;
403
404 // Broadcast to the condition in case it's waiting
405 pthread_mutex_lock( &this->mutex );
406 pthread_cond_broadcast( &this->cond );
407 pthread_mutex_unlock( &this->mutex );
408
409 // Join the thread
410 pthread_join( this->ahead_thread, NULL );
411
412 // Destroy the mutex
413 pthread_mutex_destroy( &this->mutex );
414
415 // Destroy the condition
416 pthread_cond_destroy( &this->cond );
417
418 // Wipe the queue
419 while ( mlt_deque_count( this->queue ) )
420 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
421
422 // Close the queue
423 mlt_deque_close( this->queue );
424 }
425 }
426
427 void mlt_consumer_purge( mlt_consumer this )
428 {
429 if ( this->ahead )
430 {
431 pthread_mutex_lock( &this->mutex );
432 while ( mlt_deque_count( this->queue ) )
433 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
434 pthread_cond_broadcast( &this->cond );
435 pthread_mutex_unlock( &this->mutex );
436 }
437 }
438
439 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
440 {
441 // Frame to return
442 mlt_frame frame = NULL;
443
444 // Get the properties
445 mlt_properties properties = mlt_consumer_properties( this );
446
447 // Check if the user has requested real time or not
448 if ( this->real_time )
449 {
450 int size = 1;
451
452 // Is the read ahead running?
453 if ( this->ahead == 0 )
454 {
455 int buffer = mlt_properties_get_int( properties, "buffer" );
456 int prefill = mlt_properties_get_int( properties, "prefill" );
457 consumer_read_ahead_start( this );
458 if ( buffer > 1 )
459 size = prefill > 0 && prefill < buffer ? prefill : buffer;
460 }
461
462 // Get frame from queue
463 pthread_mutex_lock( &this->mutex );
464 while( this->ahead && mlt_deque_count( this->queue ) < size )
465 pthread_cond_wait( &this->cond, &this->mutex );
466 frame = mlt_deque_pop_front( this->queue );
467 pthread_cond_broadcast( &this->cond );
468 pthread_mutex_unlock( &this->mutex );
469 }
470 else
471 {
472 // Get the frame in non real time
473 frame = mlt_consumer_get_frame( this );
474
475 // This isn't true, but from the consumers perspective it is
476 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
477 }
478
479 return frame;
480 }
481
482 /** Callback for the implementation to indicate a stopped condition.
483 */
484
485 void mlt_consumer_stopped( mlt_consumer this )
486 {
487 mlt_properties_set_int( mlt_consumer_properties( this ), "running", 0 );
488 mlt_events_fire( mlt_consumer_properties( this ), "consumer-stopped", NULL );
489 }
490
491 /** Stop the consumer.
492 */
493
494 int mlt_consumer_stop( mlt_consumer this )
495 {
496 // Get the properies
497 mlt_properties properties = mlt_consumer_properties( this );
498
499 // Stop the consumer
500 if ( this->stop != NULL )
501 this->stop( this );
502
503 // Check if the user has requested real time or not and stop if necessary
504 if ( mlt_properties_get_int( properties, "real_time" ) )
505 consumer_read_ahead_stop( this );
506
507 // Kill the test card
508 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
509
510 // Check and run a post command
511 if ( mlt_properties_get( properties, "post" ) )
512 system( mlt_properties_get( properties, "post" ) );
513
514 return 0;
515 }
516
517 /** Determine if the consumer is stopped.
518 */
519
520 int mlt_consumer_is_stopped( mlt_consumer this )
521 {
522 // Check if the consumer is stopped
523 if ( this->is_stopped != NULL )
524 return this->is_stopped( this );
525
526 return 0;
527 }
528
529 /** Close the consumer.
530 */
531
532 void mlt_consumer_close( mlt_consumer this )
533 {
534 if ( this != NULL && mlt_properties_dec_ref( mlt_consumer_properties( this ) ) <= 0 )
535 {
536 // Get the childs close function
537 void ( *consumer_close )( ) = this->close;
538
539 // Make sure it only gets called once
540 this->close = NULL;
541 this->parent.close = NULL;
542
543 // Call the childs close if available
544 if ( consumer_close != NULL )
545 consumer_close( this );
546 else
547 mlt_service_close( &this->parent );
548 }
549 }