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>
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.
21 // TODO: destroy unreferenced producers
23 #include "producer_westley.h"
24 #include <framework/mlt.h>
29 #include <libxml/parser.h>
31 #define STACK_SIZE 1000
33 struct deserialise_context_s
35 mlt_service stack_service
[ STACK_SIZE
];
36 int stack_service_size
;
38 mlt_properties producer_map
;
39 mlt_properties destructors
;
41 typedef struct deserialise_context_s
*deserialise_context
;
47 static int context_push_service( deserialise_context
this, mlt_service that
)
49 int ret
= this->stack_service_size
>= STACK_SIZE
;
51 this->stack_service
[ this->stack_service_size
++ ] = that
;
58 static mlt_service
context_pop_service( deserialise_context
this )
60 mlt_service result
= NULL
;
61 if ( this->stack_service_size
> 0 )
62 result
= this->stack_service
[ -- this->stack_service_size
];
66 // Set the destructor on a new service
67 static void track_service( mlt_properties properties
, void *service
, mlt_destructor destructor
)
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
);
75 static void on_start_tractor( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
77 mlt_service service
= mlt_tractor_service( mlt_tractor_init() );
78 mlt_properties properties
= mlt_service_properties( service
);
80 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
82 mlt_properties_set_position( properties
, "length", 0 );
84 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
85 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
87 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
89 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
90 mlt_properties_set_position( properties
, "length", length
);
93 context_push_service( context
, service
);
96 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
98 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
100 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
102 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
103 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
105 context_push_service( context
, service
);
108 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
110 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
111 mlt_properties properties
= mlt_service_properties( service
);
113 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
115 mlt_properties_set_position( properties
, "length", 0 );
117 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
118 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
120 if ( mlt_properties_get( properties
, "id" ) != NULL
)
121 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
123 if ( mlt_properties_get_position( properties
, "length" ) < mlt_properties_get_position( properties
, "out" ) )
125 mlt_position length
= mlt_properties_get_position( properties
, "out" ) + 1;
126 mlt_properties_set_position( properties
, "length", length
);
129 context_push_service( context
, service
);
132 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
134 mlt_properties properties
= mlt_properties_new();
135 mlt_service service
= NULL
;
137 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
139 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
142 service
= MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties
, "resource" ) ) );
144 if ( service
== NULL
&& mlt_properties_get( properties
, "mlt_service" ) != NULL
)
146 service
= MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties
, "mlt_service" ),
147 mlt_properties_get( properties
, "resource" ) ) );
150 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
152 // Add the producer to the producer map
153 if ( mlt_properties_get( properties
, "id" ) != NULL
)
154 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
156 mlt_properties_inherit( mlt_service_properties( service
), properties
);
157 mlt_properties_close( properties
);
159 context_push_service( context
, service
);
162 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
164 // Get the playlist from the stack
165 mlt_service service
= context_pop_service( context
);
166 mlt_position length
= 0;
168 // Look for the length attribute
169 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
171 if ( strcmp( atts
[0], "length" ) == 0 )
173 length
= atoll( atts
[1] );
178 // Append a blank to the playlist
179 mlt_playlist_blank( MLT_PLAYLIST( service
), length
- 1 );
181 // Push the playlist back onto the stack
182 context_push_service( context
, service
);
185 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
187 // Look for the producer attribute
188 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
190 if ( strcmp( atts
[0], "producer" ) == 0 )
192 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
193 // Push the referenced producer onto the stack
194 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
200 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
203 mlt_properties properties
= mlt_properties_new();
207 // Get the producer from the stack
208 mlt_service producer
= context_pop_service( context
);
210 // Set the properties
211 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
212 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
215 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
217 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
219 // Connect the filter to the producer
220 mlt_filter_connect( MLT_FILTER( service
), producer
,
221 mlt_properties_get_int( properties
, "track" ) );
223 // Set in and out from producer if non existant
224 if ( mlt_properties_get( properties
, "in" ) == NULL
)
225 mlt_properties_set_position( properties
, "in", mlt_producer_get_in( MLT_PRODUCER( producer
) ) );
226 if ( mlt_properties_get( properties
, "out" ) == NULL
)
227 mlt_properties_set_position( properties
, "out", mlt_producer_get_out( MLT_PRODUCER( producer
) ) );
229 // Propogate the properties
230 mlt_properties_inherit( mlt_service_properties( service
), properties
);
231 mlt_properties_close( properties
);
233 // Get the parent producer from the stack
234 mlt_service tractor
= context_pop_service( context
);
236 if ( tractor
!= NULL
)
238 // Connect the filter to the tractor
239 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor
), "resource" ), "<tractor>" ) == 0 )
240 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
242 // Push the parent producer back onto the stack
243 context_push_service( context
, tractor
);
246 // If a producer alias is in the producer_map, get it
247 snprintf( key
, 10, "%p", producer
);
248 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
249 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
251 // Put the producer in the producer map
252 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
254 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
256 // For filter chain support, add an alias to the producer map
257 snprintf( key
, 10, "%p", service
);
258 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
260 // Push the filter onto the stack
261 context_push_service( context
, service
);
264 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
266 mlt_properties properties
= mlt_properties_new();
268 // Get the producer from the stack
269 mlt_service producer
= context_pop_service( context
);
271 // Set the properties
272 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
273 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
275 // Create the transition
276 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
278 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
280 // Propogate the properties
281 mlt_properties_inherit( mlt_service_properties( service
), properties
);
282 mlt_properties_close( properties
);
284 // Connect the filter to the producer
285 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
286 mlt_properties_get_int( mlt_service_properties( service
), "a_track" ),
287 mlt_properties_get_int( mlt_service_properties( service
), "b_track" ) );
289 // Get the tractor from the stack
290 mlt_service tractor
= context_pop_service( context
);
292 // Connect the tractor to the transition
293 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
295 // Push the tractor back onto the stack
296 context_push_service( context
, tractor
);
298 // Push the transition onto the stack
299 context_push_service( context
, service
);
302 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
304 // Get the producer (multitrack) from the stack
305 mlt_service producer
= context_pop_service( context
);
307 // Get the tractor from the stack
308 mlt_service service
= context_pop_service( context
);
310 // Connect the tractor to the producer
311 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
312 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
313 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
315 // Push the tractor back onto the stack
316 context_push_service( context
, service
);
318 // Push the producer back onto the stack
319 context_push_service( context
, producer
);
322 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
324 // Get the producer (playlist) from the stack
325 mlt_service producer
= context_pop_service( context
);
327 // Push the producer back onto the stack
328 context_push_service( context
, producer
);
331 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
333 // Get the producer from the stack
334 mlt_service producer
= context_pop_service( context
);
336 // Get the multitrack from the stack
337 mlt_service service
= context_pop_service( context
);
339 // Set the track on the multitrack
340 mlt_multitrack_connect( MLT_MULTITRACK( service
),
341 MLT_PRODUCER( producer
),
342 context
->track_count
++ );
344 // Push the multitrack back onto the stack
345 context_push_service( context
, service
);
348 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
350 // Get the producer from the stack
351 mlt_service producer
= context_pop_service( context
);
353 // Get the playlist from the stack
354 mlt_service service
= context_pop_service( context
);
356 // Append the producer to the playlist
357 mlt_playlist_append_io( MLT_PLAYLIST( service
),
358 MLT_PRODUCER( producer
),
359 mlt_properties_get_position( mlt_service_properties( producer
), "in" ),
360 mlt_properties_get_position( mlt_service_properties( producer
), "out" ) );
362 // Push the playlist back onto the stack
363 context_push_service( context
, service
);
366 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
368 // Get and discard the last producer
369 mlt_producer multitrack
= MLT_PRODUCER( context_pop_service( context
) );
371 // Inherit the producer's properties
372 mlt_properties properties
= mlt_producer_properties( multitrack
);
373 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
374 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
375 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
378 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
380 deserialise_context context
= ( deserialise_context
) ctx
;
382 if ( strcmp( name
, "tractor" ) == 0 )
383 on_start_tractor( context
, name
, atts
);
384 else if ( strcmp( name
, "multitrack" ) == 0 )
385 on_start_multitrack( context
, name
, atts
);
386 else if ( strcmp( name
, "playlist" ) == 0 )
387 on_start_playlist( context
, name
, atts
);
388 else if ( strcmp( name
, "producer" ) == 0 )
389 on_start_producer( context
, name
, atts
);
390 else if ( strcmp( name
, "blank" ) == 0 )
391 on_start_blank( context
, name
, atts
);
392 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
393 on_start_entry_track( context
, name
, atts
);
394 else if ( strcmp( name
, "filter" ) == 0 )
395 on_start_filter( context
, name
, atts
);
396 else if ( strcmp( name
, "transition" ) == 0 )
397 on_start_transition( context
, name
, atts
);
400 static void on_end_element( void *ctx
, const xmlChar
*name
)
402 deserialise_context context
= ( deserialise_context
) ctx
;
404 if ( strcmp( name
, "multitrack" ) == 0 )
405 on_end_multitrack( context
, name
);
406 else if ( strcmp( name
, "playlist" ) == 0 )
407 on_end_playlist( context
, name
);
408 else if ( strcmp( name
, "track" ) == 0 )
409 on_end_track( context
, name
);
410 else if ( strcmp( name
, "entry" ) == 0 )
411 on_end_entry( context
, name
);
412 else if ( strcmp( name
, "tractor" ) == 0 )
413 on_end_tractor( context
, name
);
417 mlt_producer
producer_westley_init( char *filename
)
420 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
421 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
422 mlt_properties properties
= NULL
;
425 context
->producer_map
= mlt_properties_new();
426 context
->destructors
= mlt_properties_new();
427 // We need to track the number of registered filters
428 mlt_properties_set_int( context
->destructors
, "registered", 0 );
429 sax
->startElement
= on_start_element
;
430 sax
->endElement
= on_end_element
;
438 xmlSAXUserParseFile( sax
, context
, filename
);
440 // Need the complete producer list for various reasons
441 properties
= context
->destructors
;
443 // Get the last producer on the stack
444 mlt_service service
= context_pop_service( context
);
446 // Do we actually have a producer here?
447 if ( service
!= NULL
)
449 // Now make sure we don't have a reference to the service in the properties
450 for ( i
= mlt_properties_count( properties
) - 1; i
>= 1; i
-- )
452 char *name
= mlt_properties_get_name( properties
, i
);
453 if ( mlt_properties_get_data( properties
, name
, NULL
) == service
)
455 mlt_properties_set_data( properties
, name
, service
, 0, NULL
, NULL
);
460 // We are done referencing destructor property list
461 // Set this var to service properties for convenience
462 properties
= mlt_service_properties( service
);
464 // make the returned service destroy the connected services
465 mlt_properties_set_data( properties
, "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
467 // Now assign additional properties
468 mlt_properties_set( properties
, "resource", filename
);
470 // This tells consumer_westley not to deep copy
471 mlt_properties_set( properties
, "westley", "was here" );
476 mlt_properties_close( properties
);
479 free( context
->stack_service
);
480 mlt_properties_close( context
->producer_map
);
487 return MLT_PRODUCER( service
);