More work with events
[melted] / src / framework / mlt_producer.c
1 /*
2 * mlt_producer.c -- abstraction for all producer 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_producer.h"
23 #include "mlt_factory.h"
24 #include "mlt_frame.h"
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <math.h>
29
30 /** Forward references.
31 */
32
33 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
34 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
35 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
36
37 /** Constructor
38 */
39
40 int mlt_producer_init( mlt_producer this, void *child )
41 {
42 // Check that we haven't received NULL
43 int error = this == NULL;
44
45 // Continue if no error
46 if ( error == 0 )
47 {
48 // Initialise the producer
49 memset( this, 0, sizeof( struct mlt_producer_s ) );
50
51 // Associate with the child
52 this->child = child;
53
54 // Initialise the service
55 if ( mlt_service_init( &this->parent, this ) == 0 )
56 {
57 // Get the normalisation preference
58 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
59
60 // The parent is the service
61 mlt_service parent = &this->parent;
62
63 // Define the parent close
64 parent->close = ( mlt_destructor )mlt_producer_close;
65 parent->close_object = this;
66
67 // For convenience, we'll assume the close_object is this
68 this->close_object = this;
69
70 // Get the properties of the parent
71 mlt_properties properties = mlt_service_properties( parent );
72
73 // Set the default properties
74 mlt_properties_set( properties, "mlt_type", "mlt_producer" );
75 mlt_properties_set_position( properties, "_position", 0.0 );
76 mlt_properties_set_double( properties, "_frame", 0 );
77 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
78 {
79 mlt_properties_set_double( properties, "fps", 25.0 );
80 mlt_properties_set_double( properties, "aspect_ratio", 72.0 / 79.0 );
81 }
82 else
83 {
84 mlt_properties_set_double( properties, "fps", 30000.0 / 1001.0 );
85 mlt_properties_set_double( properties, "aspect_ratio", 128.0 / 117.0 );
86 }
87 mlt_properties_set_double( properties, "_speed", 1.0 );
88 mlt_properties_set_position( properties, "in", 0 );
89 mlt_properties_set_position( properties, "out", 14999 );
90 mlt_properties_set_position( properties, "length", 15000 );
91 mlt_properties_set( properties, "eof", "pause" );
92 mlt_properties_set( properties, "resource", "<producer>" );
93
94 // Override service get_frame
95 parent->get_frame = producer_get_frame;
96
97 mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
98 mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
99 mlt_events_register( properties, "producer-changed", NULL );
100 }
101 }
102
103 return error;
104 }
105
106 /** Listener for property changes.
107 */
108
109 static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
110 {
111 if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
112 mlt_events_fire( mlt_producer_properties( this ), "producer-changed", NULL );
113 }
114
115 /** Listener for service changes.
116 */
117
118 static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
119 {
120 mlt_events_fire( mlt_producer_properties( this ), "producer-changed", NULL );
121 }
122
123 /** Create a new producer.
124 */
125
126 mlt_producer mlt_producer_new( )
127 {
128 mlt_producer this = malloc( sizeof( struct mlt_producer_s ) );
129 mlt_producer_init( this, NULL );
130 return this;
131 }
132
133 /** Get the parent service object.
134 */
135
136 mlt_service mlt_producer_service( mlt_producer this )
137 {
138 return this != NULL ? &this->parent : NULL;
139 }
140
141 /** Get the producer properties.
142 */
143
144 mlt_properties mlt_producer_properties( mlt_producer this )
145 {
146 return mlt_service_properties( &this->parent );
147 }
148
149 /** Seek to a specified position.
150 */
151
152 int mlt_producer_seek( mlt_producer this, mlt_position position )
153 {
154 // Determine eof handling
155 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
156 int use_points = 1 - mlt_properties_get_int( mlt_producer_properties( this ), "ignore_points" );
157
158 // Check bounds
159 if ( position < 0 )
160 {
161 position = 0;
162 }
163 else if ( use_points && !strcmp( eof, "pause" ) && position >= mlt_producer_get_playtime( this ) )
164 {
165 mlt_producer_set_speed( this, 0 );
166 position = mlt_producer_get_playtime( this ) - 1;
167 }
168 else if ( use_points && !strcmp( eof, "loop" ) && position >= mlt_producer_get_playtime( this ) )
169 {
170 position = position % mlt_producer_get_playtime( this );
171 }
172
173 // Set the position
174 mlt_properties_set_position( mlt_producer_properties( this ), "_position", position );
175
176 // Calculate the absolute frame
177 mlt_properties_set_position( mlt_producer_properties( this ), "_frame", use_points * mlt_producer_get_in( this ) + position );
178
179 return 0;
180 }
181
182 /** Get the current position (relative to in point).
183 */
184
185 mlt_position mlt_producer_position( mlt_producer this )
186 {
187 return mlt_properties_get_position( mlt_producer_properties( this ), "_position" );
188 }
189
190 /** Get the current position (relative to start of producer).
191 */
192
193 mlt_position mlt_producer_frame( mlt_producer this )
194 {
195 return mlt_properties_get_position( mlt_producer_properties( this ), "_frame" );
196 }
197
198 /** Set the playing speed.
199 */
200
201 int mlt_producer_set_speed( mlt_producer this, double speed )
202 {
203 return mlt_properties_set_double( mlt_producer_properties( this ), "_speed", speed );
204 }
205
206 /** Get the playing speed.
207 */
208
209 double mlt_producer_get_speed( mlt_producer this )
210 {
211 return mlt_properties_get_double( mlt_producer_properties( this ), "_speed" );
212 }
213
214 /** Get the frames per second.
215 */
216
217 double mlt_producer_get_fps( mlt_producer this )
218 {
219 return mlt_properties_get_double( mlt_producer_properties( this ), "fps" );
220 }
221
222 /** Set the in and out points.
223 */
224
225 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
226 {
227 mlt_properties properties = mlt_producer_properties( this );
228
229 // Correct ins and outs if necessary
230 if ( in < 0 )
231 in = 0;
232 else if ( in > mlt_producer_get_length( this ) )
233 in = mlt_producer_get_length( this );
234
235 if ( out < 0 )
236 out = 0;
237 else if ( out > mlt_producer_get_length( this ) )
238 out = mlt_producer_get_length( this );
239
240 // Swap ins and outs if wrong
241 if ( out < in )
242 {
243 mlt_position t = in;
244 in = out;
245 out = t;
246 }
247
248 // Set the values
249 mlt_events_block( properties, properties );
250 mlt_properties_set_position( properties, "in", in );
251 mlt_properties_set_position( properties, "out", out );
252 mlt_events_unblock( properties, properties );
253 mlt_events_fire( properties, "producer-changed", NULL );
254
255 return 0;
256 }
257
258 /** Get the in point.
259 */
260
261 mlt_position mlt_producer_get_in( mlt_producer this )
262 {
263 return mlt_properties_get_position( mlt_producer_properties( this ), "in" );
264 }
265
266 /** Get the out point.
267 */
268
269 mlt_position mlt_producer_get_out( mlt_producer this )
270 {
271 return mlt_properties_get_position( mlt_producer_properties( this ), "out" );
272 }
273
274 /** Get the total play time.
275 */
276
277 mlt_position mlt_producer_get_playtime( mlt_producer this )
278 {
279 return mlt_producer_get_out( this ) - mlt_producer_get_in( this ) + 1;
280 }
281
282 /** Get the total length of the producer.
283 */
284
285 mlt_position mlt_producer_get_length( mlt_producer this )
286 {
287 return mlt_properties_get_position( mlt_producer_properties( this ), "length" );
288 }
289
290 /** Prepare for next frame.
291 */
292
293 void mlt_producer_prepare_next( mlt_producer this )
294 {
295 mlt_producer_seek( this, mlt_producer_position( this ) + mlt_producer_get_speed( this ) );
296 }
297
298 /** Get a frame.
299 */
300
301 static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int index )
302 {
303 int result = 1;
304 mlt_producer this = service->child;
305
306 // Determine eof handling
307 char *eof = mlt_properties_get( mlt_producer_properties( this ), "eof" );
308
309 // A properly instatiated producer will have a get_frame method...
310 if ( this->get_frame == NULL || ( !strcmp( eof, "continue" ) && mlt_producer_position( this ) > mlt_producer_get_out( this ) ) )
311 {
312 // Generate a test frame
313 *frame = mlt_frame_init( );
314
315 // Set the position
316 result = mlt_frame_set_position( *frame, mlt_producer_position( this ) );
317
318 // Mark as a test card
319 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 1 );
320 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_audio", 1 );
321
322 // Calculate the next position
323 mlt_producer_prepare_next( this );
324 }
325 else
326 {
327 // Get the frame from the implementation
328 result = this->get_frame( this, frame, index );
329 }
330
331 // Copy the fps and speed of the producer onto the frame
332 mlt_properties properties = mlt_frame_properties( *frame );
333 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( this ) );
334 double speed = mlt_producer_get_speed( this );
335 mlt_properties_set_double( properties, "_speed", speed );
336 mlt_properties_set_int( properties, "test_audio", mlt_frame_is_test_audio( *frame ) );
337 mlt_properties_set_int( properties, "test_image", mlt_frame_is_test_card( *frame ) );
338
339 return 0;
340 }
341
342 /** Attach a filter.
343 */
344
345 int mlt_producer_attach( mlt_producer this, mlt_filter filter )
346 {
347 return mlt_service_attach( mlt_producer_service( this ), filter );
348 }
349
350 /** Detach a filter.
351 */
352
353 int mlt_producer_detach( mlt_producer this, mlt_filter filter )
354 {
355 return mlt_service_detach( mlt_producer_service( this ), filter );
356 }
357
358 /** Retrieve a filter.
359 */
360
361 mlt_filter mlt_producer_filter( mlt_producer this, int index )
362 {
363 return mlt_service_filter( mlt_producer_service( this ), index );
364 }
365
366 /** Close the producer.
367 */
368
369 void mlt_producer_close( mlt_producer this )
370 {
371 if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
372 {
373 this->parent.close = NULL;
374
375 if ( this->close != NULL )
376 this->close( this->close_object );
377 else
378 mlt_service_close( &this->parent );
379 }
380 }