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>
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.
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.
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.
22 #include "mlt_consumer.h"
23 #include "mlt_factory.h"
24 #include "mlt_producer.h"
25 #include "mlt_frame.h"
31 /** Public final methods
34 int mlt_consumer_init( mlt_consumer
this, void *child
)
37 memset( this, 0, sizeof( struct mlt_consumer_s
) );
39 error
= mlt_service_init( &this->parent
, this );
42 // Get the properties from the service
43 mlt_properties properties
= mlt_service_properties( &this->parent
);
45 // Get the normalisation preference
46 char *normalisation
= mlt_environment( "MLT_NORMALISATION" );
48 // Deal with normalisation
49 if ( normalisation
== NULL
|| strcmp( normalisation
, "NTSC" ) )
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 );
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 );
68 // Default rescaler for all consumers
69 mlt_properties_set( properties
, "rescale", "bilinear" );
71 // Default read ahead buffer size
72 mlt_properties_set_int( properties
, "buffer", 25 );
74 // Hmm - default all consumers to yuv422 :-/
75 this->format
= mlt_image_yuv422
;
80 /** Get the parent service object.
83 mlt_service
mlt_consumer_service( mlt_consumer
this )
88 /** Get the consumer properties.
91 mlt_properties
mlt_consumer_properties( mlt_consumer
this )
93 return mlt_service_properties( &this->parent
);
96 /** Connect the consumer to the producer.
99 int mlt_consumer_connect( mlt_consumer
this, mlt_service producer
)
101 return mlt_service_connect_producer( &this->parent
, producer
, 0 );
104 /** Start the consumer.
107 int mlt_consumer_start( mlt_consumer
this )
110 mlt_properties properties
= mlt_consumer_properties( this );
112 // Determine if there's a test card producer
113 char *test_card
= mlt_properties_get( properties
, "test_card" );
116 if ( test_card
!= NULL
)
118 if ( mlt_properties_get_data( properties
, "test_card_producer", NULL
) == NULL
)
120 // Create a test card producer
121 // TODO: do we want to use fezzik here?
122 mlt_producer producer
= mlt_factory_producer( "fezzik", test_card
);
124 // Do we have a producer
125 if ( producer
!= NULL
)
127 // Test card should loop I guess...
128 mlt_properties_set( mlt_producer_properties( producer
), "eof", "loop" );
130 // Set the test card on the consumer
131 mlt_properties_set_data( properties
, "test_card_producer", producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
134 // Check and run an ante command
135 if ( mlt_properties_get( properties
, "ante" ) )
136 system( mlt_properties_get( properties
, "ante" ) );
141 // Allow the hash table to speed things up
142 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
146 if ( this->start
!= NULL
)
147 return this->start( this );
152 /** Protected method :-/ for consumer to get frames from connected service
155 mlt_frame
mlt_consumer_get_frame( mlt_consumer
this )
158 mlt_frame frame
= NULL
;
160 // Get the service assoicated to the consumer
161 mlt_service service
= mlt_consumer_service( this );
164 if ( mlt_service_get_frame( service
, &frame
, 0 ) == 0 )
166 // Get the consumer properties
167 mlt_properties properties
= mlt_consumer_properties( this );
169 // Get the frame properties
170 mlt_properties frame_properties
= mlt_frame_properties( frame
);
172 // Get the test card producer
173 mlt_producer test_card
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
175 // Attach the test frame producer to it.
176 if ( test_card
!= NULL
)
177 mlt_properties_set_data( frame_properties
, "test_card_producer", test_card
, 0, NULL
, NULL
);
179 // Attach the rescale property
180 mlt_properties_set( frame_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale" ) );
182 // Aspect ratio and other jiggery pokery
183 mlt_properties_set_double( frame_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "aspect_ratio" ) );
184 mlt_properties_set_int( frame_properties
, "consumer_progressive", mlt_properties_get_int( properties
, "progressive" ) );
185 mlt_properties_set_int( frame_properties
, "consumer_deinterlace", mlt_properties_get_int( properties
, "deinterlace" ) );
192 static inline long time_difference( struct timeval
*time1
)
194 struct timeval time2
;
195 time2
.tv_sec
= time1
->tv_sec
;
196 time2
.tv_usec
= time1
->tv_usec
;
197 gettimeofday( time1
, NULL
);
198 return time1
->tv_sec
* 1000000 + time1
->tv_usec
- time2
.tv_sec
* 1000000 - time2
.tv_usec
;
201 static void *consumer_read_ahead_thread( void *arg
)
203 // The argument is the consumer
204 mlt_consumer
this = arg
;
206 // Get the properties of the consumer
207 mlt_properties properties
= mlt_consumer_properties( this );
209 // Get the width and height
210 int width
= mlt_properties_get_int( properties
, "width" );
211 int height
= mlt_properties_get_int( properties
, "height" );
213 // Get the maximum size of the buffer
214 int buffer
= mlt_properties_get_int( properties
, "buffer" );
216 // General frame variable
217 mlt_frame frame
= NULL
;
218 uint8_t *image
= NULL
;
223 // Average time for get_frame and get_image
225 int64_t time_wait
= 0;
226 int64_t time_frame
= 0;
227 int64_t time_image
= 0;
229 // Get the first frame
230 frame
= mlt_consumer_get_frame( this );
232 // Get the image of the first frame
233 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
234 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 1 );
236 // Get the starting time (can ignore the times above)
237 gettimeofday( &ante
, NULL
);
239 // Continue to read ahead
240 while ( this->ahead
)
242 // Put the current frame into the queue
243 pthread_mutex_lock( &this->mutex
);
244 while( this->ahead
&& mlt_deque_count( this->queue
) >= buffer
)
245 pthread_cond_wait( &this->cond
, &this->mutex
);
246 mlt_deque_push_back( this->queue
, frame
);
247 pthread_cond_broadcast( &this->cond
);
248 pthread_mutex_unlock( &this->mutex
);
249 time_wait
+= time_difference( &ante
);
251 // Get the next frame
252 frame
= mlt_consumer_get_frame( this );
253 time_frame
+= time_difference( &ante
);
255 // Increment the count
259 if ( ( time_frame
+ time_image
) / count
< ( 40000 - ( time_wait
/ count
) ) )
261 // Get the image, mark as rendered and time it
262 mlt_frame_get_image( frame
, &image
, &this->format
, &width
, &height
, 0 );
263 mlt_properties_set_int( mlt_frame_properties( frame
), "rendered", 1 );
264 time_image
+= time_difference( &ante
);
268 // Remove the last frame
269 mlt_frame_close( frame
);
274 static void consumer_read_ahead_start( mlt_consumer
this )
279 // Create the frame queue
280 this->queue
= mlt_deque_init( );
283 pthread_mutex_init( &this->mutex
, NULL
);
285 // Create the condition
286 pthread_cond_init( &this->cond
, NULL
);
288 // Create the read ahead
289 pthread_create( &this->ahead_thread
, NULL
, consumer_read_ahead_thread
, this );
293 static void consumer_read_ahead_stop( mlt_consumer
this )
295 // Make sure we're running
298 // Inform thread to stop
301 // Broadcast to the condition in case it's waiting
302 pthread_mutex_lock( &this->mutex
);
303 pthread_cond_broadcast( &this->cond
);
304 pthread_mutex_unlock( &this->mutex
);
307 pthread_join( this->ahead_thread
, NULL
);
310 pthread_mutex_destroy( &this->mutex
);
312 // Destroy the condition
313 pthread_cond_destroy( &this->cond
);
316 while ( mlt_deque_count( this->queue
) )
317 mlt_frame_close( mlt_deque_pop_back( this->queue
) );
321 mlt_frame
mlt_consumer_rt_frame( mlt_consumer
this )
324 mlt_frame frame
= NULL
;
327 // Is the read ahead running?
328 if ( this->ahead
== 0 )
330 int buffer
= mlt_properties_get_int( mlt_consumer_properties( this ), "buffer" );
331 consumer_read_ahead_start( this );
336 // Get frame from queue
337 pthread_mutex_lock( &this->mutex
);
338 while( this->ahead
&& mlt_deque_count( this->queue
) < size
)
339 pthread_cond_wait( &this->cond
, &this->mutex
);
340 frame
= mlt_deque_pop_front( this->queue
);
341 pthread_cond_broadcast( &this->cond
);
342 pthread_mutex_unlock( &this->mutex
);
347 /** Stop the consumer.
350 int mlt_consumer_stop( mlt_consumer
this )
353 mlt_properties properties
= mlt_consumer_properties( this );
356 if ( this->stop
!= NULL
)
359 // Kill the read ahead
360 consumer_read_ahead_stop( this );
362 // Kill the test card
363 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
365 // Check and run a post command
366 if ( mlt_properties_get( properties
, "post" ) )
367 system( mlt_properties_get( properties
, "post" ) );
372 /** Determine if the consumer is stopped.
375 int mlt_consumer_is_stopped( mlt_consumer
this )
377 // Check if the consumer is stopped
378 if ( this->is_stopped
!= NULL
)
379 return this->is_stopped( this );
384 /** Close the consumer.
387 void mlt_consumer_close( mlt_consumer
this )
389 // Get the childs close function
390 void ( *consumer_close
)( ) = this->close
;
392 // Make sure it only gets called once
395 // Call the childs close if available
396 if ( consumer_close
!= NULL
)
397 consumer_close( this );
399 mlt_service_close( &this->parent
);