8c7d9ca64fc740bc1e2903f3a9ec1ad5a63bfb81
[melted] / src / modules / westley / producer_westley.c
1 /*
2 * producer_westley.c -- a libxml2 parser of mlt service networks
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 // TODO: destroy unreferenced producers
22
23 #include "producer_westley.h"
24 #include <framework/mlt.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <libxml/parser.h>
30
31 #define STACK_SIZE 1000
32
33 struct deserialise_context_s
34 {
35 mlt_service stack_service[ STACK_SIZE ];
36 int stack_service_size;
37 int track_count;
38 mlt_properties producer_map;
39 mlt_properties destructors;
40 };
41 typedef struct deserialise_context_s *deserialise_context;
42
43
44 /** Push a service.
45 */
46
47 static int context_push_service( deserialise_context this, mlt_service that )
48 {
49 int ret = this->stack_service_size >= STACK_SIZE;
50 if ( ret == 0 )
51 this->stack_service[ this->stack_service_size ++ ] = that;
52 return ret;
53 }
54
55 /** Pop a service.
56 */
57
58 static mlt_service context_pop_service( deserialise_context this )
59 {
60 mlt_service result = NULL;
61 if ( this->stack_service_size > 0 )
62 result = this->stack_service[ -- this->stack_service_size ];
63 return result;
64 }
65
66 // Set the destructor on a new service
67 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
68 {
69 int registered = mlt_properties_get_int( properties, "registered" );
70 char *key = mlt_properties_get( properties, "registered" );
71 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
72 mlt_properties_set_int( properties, "registered", ++ registered );
73 }
74
75 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
76 {
77 mlt_service service = mlt_tractor_service( mlt_tractor_init() );
78
79 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
80
81 for ( ; atts != NULL && *atts != NULL; atts += 2 )
82 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
83
84 context_push_service( context, service );
85 }
86
87 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
88 {
89 mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
90
91 track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
92
93 for ( ; atts != NULL && *atts != NULL; atts += 2 )
94 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
95
96 context_push_service( context, service );
97 }
98
99 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
100 {
101 mlt_service service = mlt_playlist_service( mlt_playlist_init() );
102 mlt_properties properties = mlt_service_properties( service );
103
104 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
105
106 for ( ; atts != NULL && *atts != NULL; atts += 2 )
107 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
108
109 if ( mlt_properties_get( properties, "id" ) != NULL )
110 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
111
112 context_push_service( context, service );
113 }
114
115 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
116 {
117 mlt_properties properties = mlt_properties_new();
118 mlt_service service = NULL;
119
120 for ( ; atts != NULL && *atts != NULL; atts += 2 )
121 {
122 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
123 }
124
125 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
126 {
127 service = MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties, "mlt_service" ),
128 mlt_properties_get( properties, "resource" ) ) );
129 }
130 else
131 {
132 // Unspecified producer, use inigo
133 char *args[2] = { mlt_properties_get( properties, "resource" ), 0 };
134 service = MLT_SERVICE( mlt_factory_producer( "inigo", args ) );
135 }
136
137 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
138
139 // Add the producer to the producer map
140 if ( mlt_properties_get( properties, "id" ) != NULL )
141 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
142
143 mlt_properties_inherit( mlt_service_properties( service ), properties );
144 mlt_properties_close( properties );
145
146 context_push_service( context, service );
147 }
148
149 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
150 {
151 // Get the playlist from the stack
152 mlt_service service = context_pop_service( context );
153 mlt_position length = 0;
154
155 // Look for the length attribute
156 for ( ; atts != NULL && *atts != NULL; atts += 2 )
157 {
158 if ( strcmp( atts[0], "length" ) == 0 )
159 {
160 length = atoll( atts[1] );
161 break;
162 }
163 }
164
165 // Append a blank to the playlist
166 mlt_playlist_blank( MLT_PLAYLIST( service ), length );
167
168 // Push the playlist back onto the stack
169 context_push_service( context, service );
170 }
171
172 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
173 {
174 // Look for the producer attribute
175 for ( ; atts != NULL && *atts != NULL; atts += 2 )
176 {
177 if ( strcmp( atts[0], "producer" ) == 0 )
178 {
179 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
180 // Push the referenced producer onto the stack
181 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
182 break;
183 }
184 }
185 }
186
187 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
188 {
189 char *id;
190 mlt_properties properties = mlt_properties_new();
191 char key[11];
192 key[ 10 ] = '\0';
193
194 // Get the producer from the stack
195 mlt_service producer = context_pop_service( context );
196
197 // Set the properties
198 for ( ; atts != NULL && *atts != NULL; atts += 2 )
199 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
200
201 // Create the filter
202 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
203
204 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
205
206 // Connect the filter to the producer
207 mlt_filter_connect( MLT_FILTER( service ), producer,
208 mlt_properties_get_int( properties, "track" ) );
209
210 // Set in and out from producer if non existant
211 if ( mlt_properties_get( properties, "in" ) == NULL )
212 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
213 if ( mlt_properties_get( properties, "out" ) == NULL )
214 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
215
216 // Propogate the properties
217 mlt_properties_inherit( mlt_service_properties( service ), properties );
218 mlt_properties_close( properties );
219
220 // Get the parent producer from the stack
221 mlt_service tractor = context_pop_service( context );
222
223 if ( tractor != NULL )
224 {
225 // Connect the filter to the tractor
226 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor ), "resource" ), "<tractor>" ) == 0 )
227 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
228
229 // Push the parent producer back onto the stack
230 context_push_service( context, tractor );
231 }
232
233 // If a producer alias is in the producer_map, get it
234 snprintf( key, 10, "%p", producer );
235 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
236 producer = mlt_properties_get_data( context->producer_map, key, NULL );
237
238 // Put the producer in the producer map
239 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
240 if ( id != NULL )
241 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
242
243 // For filter chain support, add an alias to the producer map
244 snprintf( key, 10, "%p", service );
245 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
246
247 // Push the filter onto the stack
248 context_push_service( context, service );
249 }
250
251 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
252 {
253 mlt_properties properties = mlt_properties_new();
254
255 // Get the producer from the stack
256 mlt_service producer = context_pop_service( context );
257
258 // Set the properties
259 for ( ; atts != NULL && *atts != NULL; atts += 2 )
260 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
261
262 // Create the transition
263 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
264
265 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
266
267 // Propogate the properties
268 mlt_properties_inherit( mlt_service_properties( service ), properties );
269 mlt_properties_close( properties );
270
271 // Connect the filter to the producer
272 mlt_transition_connect( MLT_TRANSITION( service ), producer,
273 mlt_properties_get_int( mlt_service_properties( service ), "a_track" ),
274 mlt_properties_get_int( mlt_service_properties( service ), "b_track" ) );
275
276 // Get the tractor from the stack
277 mlt_service tractor = context_pop_service( context );
278
279 // Connect the tractor to the transition
280 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
281
282 // Push the tractor back onto the stack
283 context_push_service( context, tractor );
284
285 // Push the transition onto the stack
286 context_push_service( context, service );
287 }
288
289 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
290 {
291 // Get the producer (multitrack) from the stack
292 mlt_service producer = context_pop_service( context );
293
294 // Get the tractor from the stack
295 mlt_service service = context_pop_service( context );
296
297 // Connect the tractor to the producer
298 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
299 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
300 MLT_MULTITRACK( producer ), 0, NULL, NULL );
301
302 // Push the tractor back onto the stack
303 context_push_service( context, service );
304
305 // Push the producer back onto the stack
306 context_push_service( context, producer );
307 }
308
309 static void on_end_playlist( deserialise_context context, const xmlChar *name )
310 {
311 // Get the producer (playlist) from the stack
312 mlt_service producer = context_pop_service( context );
313
314 // Push the producer back onto the stack
315 context_push_service( context, producer );
316 }
317
318 static void on_end_track( deserialise_context context, const xmlChar *name )
319 {
320 // Get the producer from the stack
321 mlt_service producer = context_pop_service( context );
322
323 // Get the multitrack from the stack
324 mlt_service service = context_pop_service( context );
325
326 // Set the track on the multitrack
327 mlt_multitrack_connect( MLT_MULTITRACK( service ),
328 MLT_PRODUCER( producer ),
329 context->track_count++ );
330
331 // Push the multitrack back onto the stack
332 context_push_service( context, service );
333 }
334
335 static void on_end_entry( deserialise_context context, const xmlChar *name )
336 {
337 // Get the producer from the stack
338 mlt_service producer = context_pop_service( context );
339
340 // Get the playlist from the stack
341 mlt_service service = context_pop_service( context );
342
343 // Append the producer to the playlist
344 mlt_playlist_append_io( MLT_PLAYLIST( service ),
345 MLT_PRODUCER( producer ),
346 mlt_properties_get_position( mlt_service_properties( producer ), "in" ),
347 mlt_properties_get_position( mlt_service_properties( producer ), "out" ) );
348
349 // Push the playlist back onto the stack
350 context_push_service( context, service );
351 }
352
353 static void on_end_tractor( deserialise_context context, const xmlChar *name )
354 {
355 // Get and discard the last producer
356 mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
357
358 // Inherit the producer's properties
359 mlt_properties properties = mlt_producer_properties( multitrack );
360 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
361 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
362 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
363 }
364
365 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
366 {
367 deserialise_context context = ( deserialise_context ) ctx;
368
369 if ( strcmp( name, "tractor" ) == 0 )
370 on_start_tractor( context, name, atts );
371 else if ( strcmp( name, "multitrack" ) == 0 )
372 on_start_multitrack( context, name, atts );
373 else if ( strcmp( name, "playlist" ) == 0 )
374 on_start_playlist( context, name, atts );
375 else if ( strcmp( name, "producer" ) == 0 )
376 on_start_producer( context, name, atts );
377 else if ( strcmp( name, "blank" ) == 0 )
378 on_start_blank( context, name, atts );
379 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
380 on_start_entry_track( context, name, atts );
381 else if ( strcmp( name, "filter" ) == 0 )
382 on_start_filter( context, name, atts );
383 else if ( strcmp( name, "transition" ) == 0 )
384 on_start_transition( context, name, atts );
385 }
386
387 static void on_end_element( void *ctx, const xmlChar *name )
388 {
389 deserialise_context context = ( deserialise_context ) ctx;
390
391 if ( strcmp( name, "multitrack" ) == 0 )
392 on_end_multitrack( context, name );
393 else if ( strcmp( name, "playlist" ) == 0 )
394 on_end_playlist( context, name );
395 else if ( strcmp( name, "track" ) == 0 )
396 on_end_track( context, name );
397 else if ( strcmp( name, "entry" ) == 0 )
398 on_end_entry( context, name );
399 else if ( strcmp( name, "tractor" ) == 0 )
400 on_end_tractor( context, name );
401 }
402
403
404 mlt_producer producer_westley_init( char *filename )
405 {
406 static int init = 0;
407 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
408 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
409 mlt_properties properties = NULL;
410 int i = 0;
411
412 context->producer_map = mlt_properties_new();
413 context->destructors = mlt_properties_new();
414 // We need to track the number of registered filters
415 mlt_properties_set_int( context->destructors, "registered", 0 );
416 sax->startElement = on_start_element;
417 sax->endElement = on_end_element;
418
419 if ( !init )
420 {
421 xmlInitParser();
422 init = 1;
423 }
424
425 xmlSAXUserParseFile( sax, context, filename );
426 free( sax );
427
428 // Need the complete producer list for various reasons
429 properties = context->destructors;
430
431 // Get the last producer on the stack
432 mlt_service service = context_pop_service( context );
433
434 // Do we actually have a producer here?
435 if ( service != NULL )
436 {
437 // Now make sure we don't have a reference to the service in the properties
438 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
439 {
440 char *name = mlt_properties_get_name( properties, i );
441 if ( mlt_properties_get_data( properties, name, NULL ) == service )
442 {
443 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
444 break;
445 }
446 }
447
448 // We are done referencing destructor property list
449 // Set this var to service properties for convenience
450 properties = mlt_service_properties( service );
451
452 // make the returned service destroy the connected services
453 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
454
455 // Now assign additional properties
456 mlt_properties_set( properties, "resource", filename );
457
458 // This tells consumer_westley not to deep copy
459 mlt_properties_set( properties, "westley", "was here" );
460 }
461 else
462 {
463 // Clean up
464 mlt_properties_close( properties );
465 }
466
467 free( context->stack_service );
468 mlt_properties_close( context->producer_map );
469 free( context );
470
471 return MLT_PRODUCER( service );
472 }
473