cfdc4ac9aae6b24fb76a33d098949bbdcc0f2d40
[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 return error;
88 }
89
90 /** Create a new consumer.
91 */
92
93 mlt_consumer mlt_consumer_new( )
94 {
95 // Create the memory for the structure
96 mlt_consumer this = malloc( sizeof( struct mlt_consumer_s ) );
97
98 // Initialise it
99 if ( this != NULL )
100 mlt_consumer_init( this, NULL );
101
102 // Return it
103 return this;
104 }
105
106 /** Get the parent service object.
107 */
108
109 mlt_service mlt_consumer_service( mlt_consumer this )
110 {
111 return &this->parent;
112 }
113
114 /** Get the consumer properties.
115 */
116
117 mlt_properties mlt_consumer_properties( mlt_consumer this )
118 {
119 return mlt_service_properties( &this->parent );
120 }
121
122 /** Connect the consumer to the producer.
123 */
124
125 int mlt_consumer_connect( mlt_consumer this, mlt_service producer )
126 {
127 return mlt_service_connect_producer( &this->parent, producer, 0 );
128 }
129
130 /** Start the consumer.
131 */
132
133 int mlt_consumer_start( mlt_consumer this )
134 {
135 // Get the properies
136 mlt_properties properties = mlt_consumer_properties( this );
137
138 // Determine if there's a test card producer
139 char *test_card = mlt_properties_get( properties, "test_card" );
140
141 // Deal with it now.
142 if ( test_card != NULL )
143 {
144 if ( mlt_properties_get_data( properties, "test_card_producer", NULL ) == NULL )
145 {
146 // Create a test card producer
147 mlt_producer producer = mlt_factory_producer( NULL, test_card );
148
149 // Do we have a producer
150 if ( producer != NULL )
151 {
152 // Test card should loop I guess...
153 mlt_properties_set( mlt_producer_properties( producer ), "eof", "loop" );
154
155 // Set the test card on the consumer
156 mlt_properties_set_data( properties, "test_card_producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
157 }
158
159 }
160 }
161 else
162 {
163 // Allow the hash table to speed things up
164 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
165 }
166
167 // Check and run an ante command
168 if ( mlt_properties_get( properties, "ante" ) )
169 system( mlt_properties_get( properties, "ante" ) );
170
171 // Set the real_time preference
172 this->real_time = mlt_properties_get_int( properties, "real_time" );
173
174 // Start the service
175 if ( this->start != NULL )
176 return this->start( this );
177
178 return 0;
179 }
180
181 /** Protected method for consumer to get frames from connected service
182 */
183
184 mlt_frame mlt_consumer_get_frame( mlt_consumer this )
185 {
186 // Frame to return
187 mlt_frame frame = NULL;
188
189 // Get the service assoicated to the consumer
190 mlt_service service = mlt_consumer_service( this );
191
192 // Get the frame
193 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
194 {
195 // Get the consumer properties
196 mlt_properties properties = mlt_consumer_properties( this );
197
198 // Get the frame properties
199 mlt_properties frame_properties = mlt_frame_properties( frame );
200
201 // Get the test card producer
202 mlt_producer test_card = mlt_properties_get_data( properties, "test_card_producer", NULL );
203
204 // Attach the test frame producer to it.
205 if ( test_card != NULL )
206 mlt_properties_set_data( frame_properties, "test_card_producer", test_card, 0, NULL, NULL );
207
208 // Attach the rescale property
209 mlt_properties_set( frame_properties, "rescale.interp", mlt_properties_get( properties, "rescale" ) );
210
211 // Aspect ratio and other jiggery pokery
212 mlt_properties_set_double( frame_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "aspect_ratio" ) );
213 mlt_properties_set_int( frame_properties, "consumer_progressive", mlt_properties_get_int( properties, "progressive" ) );
214 mlt_properties_set_int( frame_properties, "consumer_deinterlace", mlt_properties_get_int( properties, "deinterlace" ) );
215 }
216
217 // Return the frame
218 return frame;
219 }
220
221 static inline long time_difference( struct timeval *time1 )
222 {
223 struct timeval time2;
224 time2.tv_sec = time1->tv_sec;
225 time2.tv_usec = time1->tv_usec;
226 gettimeofday( time1, NULL );
227 return time1->tv_sec * 1000000 + time1->tv_usec - time2.tv_sec * 1000000 - time2.tv_usec;
228 }
229
230 static void *consumer_read_ahead_thread( void *arg )
231 {
232 // The argument is the consumer
233 mlt_consumer this = arg;
234
235 // Get the properties of the consumer
236 mlt_properties properties = mlt_consumer_properties( this );
237
238 // Get the width and height
239 int width = mlt_properties_get_int( properties, "width" );
240 int height = mlt_properties_get_int( properties, "height" );
241
242 // See if video is turned off
243 int video_off = mlt_properties_get_int( properties, "video_off" );
244
245 // Get the audio settings
246 mlt_audio_format afmt = mlt_audio_pcm;
247 int counter = 0;
248 double fps = mlt_properties_get_double( properties, "fps" );
249 int channels = mlt_properties_get_int( properties, "channels" );
250 int frequency = mlt_properties_get_int( properties, "frequency" );
251 int samples = 0;
252 int16_t *pcm = NULL;
253
254 // See if audio is turned off
255 int audio_off = mlt_properties_get_int( properties, "audio_off" );
256
257 // Get the maximum size of the buffer
258 int buffer = mlt_properties_get_int( properties, "buffer" ) + 1;
259
260 // General frame variable
261 mlt_frame frame = NULL;
262 uint8_t *image = NULL;
263
264 // Time structures
265 struct timeval ante;
266
267 // Average time for get_frame and get_image
268 int count = 1;
269 int skipped = 0;
270 int64_t time_wait = 0;
271 int64_t time_frame = 0;
272 int64_t time_process = 0;
273 int skip_next = 0;
274
275 // Get the first frame
276 frame = mlt_consumer_get_frame( this );
277
278 // Get the image of the first frame
279 if ( !video_off )
280 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
281
282 if ( !audio_off )
283 {
284 samples = mlt_sample_calculator( fps, frequency, counter++ );
285 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
286 frame->get_audio = NULL;
287 }
288
289 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
290
291 // Get the starting time (can ignore the times above)
292 gettimeofday( &ante, NULL );
293
294 // Continue to read ahead
295 while ( this->ahead )
296 {
297 // Put the current frame into the queue
298 pthread_mutex_lock( &this->mutex );
299 while( this->ahead && mlt_deque_count( this->queue ) >= buffer )
300 pthread_cond_wait( &this->cond, &this->mutex );
301 mlt_deque_push_back( this->queue, frame );
302 pthread_cond_broadcast( &this->cond );
303 pthread_mutex_unlock( &this->mutex );
304 time_wait += time_difference( &ante );
305
306 // Get the next frame
307 frame = mlt_consumer_get_frame( this );
308 time_frame += time_difference( &ante );
309
310 // Increment the count
311 count ++;
312
313 // All non normal playback frames should be shown
314 if ( mlt_properties_get_int( mlt_frame_properties( frame ), "_speed" ) != 1 )
315 {
316 skipped = 0;
317 time_frame = 0;
318 time_process = 0;
319 time_wait = 0;
320 count = 1;
321 skip_next = 0;
322 }
323
324 // Get the image
325 if ( !skip_next )
326 {
327 // Get the image, mark as rendered and time it
328 if ( !video_off )
329 mlt_frame_get_image( frame, &image, &this->format, &width, &height, 0 );
330 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
331 }
332 else
333 {
334 // Increment the number of sequentially skipped frames
335 skipped ++;
336 skip_next = 0;
337
338 // If we've reached an unacceptable level, reset everything
339 if ( skipped > 5 )
340 {
341 skipped = 0;
342 time_frame = 0;
343 time_process = 0;
344 time_wait = 0;
345 count = 1;
346 }
347 }
348
349 // Always process audio
350 if ( !audio_off )
351 {
352 samples = mlt_sample_calculator( fps, frequency, counter++ );
353 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
354 frame->get_audio = NULL;
355 }
356
357 // Increment the time take for this frame
358 time_process += time_difference( &ante );
359
360 // Determine if the next frame should be skipped
361 if ( mlt_deque_count( this->queue ) <= 5 && ( ( time_wait + time_frame + time_process ) / count ) > 40000 )
362 skip_next = 1;
363 }
364
365 // Remove the last frame
366 mlt_frame_close( frame );
367
368 return NULL;
369 }
370
371 static void consumer_read_ahead_start( mlt_consumer this )
372 {
373 pthread_attr_t thread_attributes;
374
375 // We're running now
376 this->ahead = 1;
377
378 // Create the frame queue
379 this->queue = mlt_deque_init( );
380
381 // Create the mutex
382 pthread_mutex_init( &this->mutex, NULL );
383
384 // Create the condition
385 pthread_cond_init( &this->cond, NULL );
386
387 // Inherit the scheduling priority
388 pthread_attr_init( &thread_attributes );
389 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
390
391 // Create the read ahead
392 pthread_create( &this->ahead_thread, &thread_attributes, consumer_read_ahead_thread, this );
393 }
394
395 static void consumer_read_ahead_stop( mlt_consumer this )
396 {
397 // Make sure we're running
398 if ( this->ahead )
399 {
400 // Inform thread to stop
401 this->ahead = 0;
402
403 // Broadcast to the condition in case it's waiting
404 pthread_mutex_lock( &this->mutex );
405 pthread_cond_broadcast( &this->cond );
406 pthread_mutex_unlock( &this->mutex );
407
408 // Join the thread
409 pthread_join( this->ahead_thread, NULL );
410
411 // Destroy the mutex
412 pthread_mutex_destroy( &this->mutex );
413
414 // Destroy the condition
415 pthread_cond_destroy( &this->cond );
416
417 // Wipe the queue
418 while ( mlt_deque_count( this->queue ) )
419 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
420
421 // Close the queue
422 mlt_deque_close( this->queue );
423 }
424 }
425
426 mlt_frame mlt_consumer_rt_frame( mlt_consumer this )
427 {
428 // Frame to return
429 mlt_frame frame = NULL;
430
431 // Get the properties
432 mlt_properties properties = mlt_consumer_properties( this );
433
434 // Check if the user has requested real time or not
435 if ( this->real_time )
436 {
437 int size = 1;
438
439 // Is the read ahead running?
440 if ( this->ahead == 0 )
441 {
442 int buffer = mlt_properties_get_int( properties, "buffer" );
443 consumer_read_ahead_start( this );
444 if ( buffer > 1 )
445 size = buffer;
446 }
447
448 // Get frame from queue
449 pthread_mutex_lock( &this->mutex );
450 while( this->ahead && mlt_deque_count( this->queue ) < size )
451 pthread_cond_wait( &this->cond, &this->mutex );
452 frame = mlt_deque_pop_front( this->queue );
453 pthread_cond_broadcast( &this->cond );
454 pthread_mutex_unlock( &this->mutex );
455 }
456 else
457 {
458 // Get the frame in non real time
459 frame = mlt_consumer_get_frame( this );
460
461 // This isn't true, but from the consumers perspective it is
462 mlt_properties_set_int( mlt_frame_properties( frame ), "rendered", 1 );
463 }
464
465 return frame;
466 }
467
468 /** Stop the consumer.
469 */
470
471 int mlt_consumer_stop( mlt_consumer this )
472 {
473 // Get the properies
474 mlt_properties properties = mlt_consumer_properties( this );
475
476 // Stop the consumer
477 if ( this->stop != NULL )
478 this->stop( this );
479
480 // Check if the user has requested real time or not and stop if necessary
481 if ( mlt_properties_get_int( properties, "real_time" ) )
482 consumer_read_ahead_stop( this );
483
484 // Kill the test card
485 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
486
487 // Check and run a post command
488 if ( mlt_properties_get( properties, "post" ) )
489 system( mlt_properties_get( properties, "post" ) );
490
491 return 0;
492 }
493
494 /** Determine if the consumer is stopped.
495 */
496
497 int mlt_consumer_is_stopped( mlt_consumer this )
498 {
499 // Check if the consumer is stopped
500 if ( this->is_stopped != NULL )
501 return this->is_stopped( this );
502
503 return 0;
504 }
505
506 /** Close the consumer.
507 */
508
509 void mlt_consumer_close( mlt_consumer this )
510 {
511 // Get the childs close function
512 void ( *consumer_close )( ) = this->close;
513
514 // Make sure it only gets called once
515 this->close = NULL;
516
517 // Call the childs close if available
518 if ( consumer_close != NULL )
519 consumer_close( this );
520 else
521 mlt_service_close( &this->parent );
522 }