2 * producer_libdv.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() );
79 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_tractor_close
);
81 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
82 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
84 context_push_service( context
, service
);
87 static void on_start_multitrack( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
89 mlt_service service
= mlt_multitrack_service( mlt_multitrack_init() );
91 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_multitrack_close
);
93 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
94 mlt_properties_set( mlt_service_properties( service
), (char*) atts
[0], (char*) atts
[1] );
96 context_push_service( context
, service
);
99 static void on_start_playlist( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
101 mlt_service service
= mlt_playlist_service( mlt_playlist_init() );
102 mlt_properties properties
= mlt_service_properties( service
);
104 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_playlist_close
);
106 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
107 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
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
);
112 context_push_service( context
, service
);
115 static void on_start_producer( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
117 mlt_properties properties
= mlt_properties_new();
118 mlt_service service
= NULL
;
120 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
121 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
123 if ( mlt_properties_get( properties
, "mlt_service" ) != NULL
)
125 service
= MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties
, "mlt_service" ),
126 mlt_properties_get( properties
, "resource" ) ) );
130 // Unspecified producer, use inigo
131 char *args
[2] = { mlt_properties_get( properties
, "resource" ), 0 };
132 service
= MLT_SERVICE( mlt_factory_producer( "inigo", args
) );
135 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_producer_close
);
137 // Add the producer to the producer map
138 if ( mlt_properties_get( properties
, "id" ) != NULL
)
139 mlt_properties_set_data( context
->producer_map
, mlt_properties_get( properties
, "id" ), service
, 0, NULL
, NULL
);
141 mlt_properties_inherit( mlt_service_properties( service
), properties
);
142 mlt_properties_close( properties
);
144 context_push_service( context
, service
);
147 static void on_start_blank( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
149 // Get the playlist from the stack
150 mlt_service service
= context_pop_service( context
);
151 mlt_position length
= 0;
153 // Look for the length attribute
154 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
156 if ( strcmp( atts
[0], "length" ) == 0 )
158 length
= atoll( atts
[1] );
163 // Append a blank to the playlist
164 mlt_playlist_blank( MLT_PLAYLIST( service
), length
);
166 // Push the playlist back onto the stack
167 context_push_service( context
, service
);
170 static void on_start_entry_track( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
172 // Look for the producer attribute
173 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
175 if ( strcmp( atts
[0], "producer" ) == 0 )
177 if ( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) != NULL
)
178 // Push the referenced producer onto the stack
179 context_push_service( context
, MLT_SERVICE( mlt_properties_get_data( context
->producer_map
, (char*) atts
[1], NULL
) ) );
185 static void on_start_filter( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
188 mlt_properties properties
= mlt_properties_new();
192 // Get the producer from the stack
193 mlt_service producer
= context_pop_service( context
);
195 // Set the properties
196 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
197 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
200 mlt_service service
= MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
202 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_filter_close
);
204 // Connect the filter to the producer
205 mlt_filter_connect( MLT_FILTER( service
), producer
,
206 mlt_properties_get_int( properties
, "track" ) );
208 // Propogate the properties
209 mlt_properties_inherit( mlt_service_properties( service
), properties
);
210 mlt_properties_close( properties
);
212 // Get the parent producer from the stack
213 mlt_service tractor
= context_pop_service( context
);
215 if ( tractor
!= NULL
)
217 // Connect the filter to the tractor
218 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor
), "resource" ), "<tractor>" ) == 0 )
219 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
221 // Push the parent producer back onto the stack
222 context_push_service( context
, tractor
);
225 // If a producer alias is in the producer_map, get it
226 snprintf( key
, 10, "%p", producer
);
227 if ( mlt_properties_get_data( context
->producer_map
, key
, NULL
) != NULL
)
228 producer
= mlt_properties_get_data( context
->producer_map
, key
, NULL
);
230 // Put the producer in the producer map
231 id
= mlt_properties_get( mlt_service_properties( producer
), "id" );
233 mlt_properties_set_data( context
->producer_map
, id
, service
, 0, NULL
, NULL
);
235 // For filter chain support, add an alias to the producer map
236 snprintf( key
, 10, "%p", service
);
237 mlt_properties_set_data( context
->producer_map
, key
, producer
, 0, NULL
, NULL
);
239 // Push the filter onto the stack
240 context_push_service( context
, service
);
243 static void on_start_transition( deserialise_context context
, const xmlChar
*name
, const xmlChar
**atts
)
245 mlt_properties properties
= mlt_properties_new();
247 // Get the producer from the stack
248 mlt_service producer
= context_pop_service( context
);
250 // Set the properties
251 for ( ; atts
!= NULL
&& *atts
!= NULL
; atts
+= 2 )
252 mlt_properties_set( properties
, (char*) atts
[0], (char*) atts
[1] );
254 // Create the transition
255 mlt_service service
= MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties
, "mlt_service" ), NULL
) );
257 track_service( context
->destructors
, service
, (mlt_destructor
) mlt_transition_close
);
259 // Propogate the properties
260 mlt_properties_inherit( mlt_service_properties( service
), properties
);
261 mlt_properties_close( properties
);
263 // Connect the filter to the producer
264 mlt_transition_connect( MLT_TRANSITION( service
), producer
,
265 mlt_properties_get_int( mlt_service_properties( service
), "a_track" ),
266 mlt_properties_get_int( mlt_service_properties( service
), "b_track" ) );
268 // Get the tractor from the stack
269 mlt_service tractor
= context_pop_service( context
);
271 // Connect the tractor to the transition
272 mlt_tractor_connect( MLT_TRACTOR( tractor
), service
);
274 // Push the tractor back onto the stack
275 context_push_service( context
, tractor
);
277 // Push the transition onto the stack
278 context_push_service( context
, service
);
281 static void on_end_multitrack( deserialise_context context
, const xmlChar
*name
)
283 // Get the producer (multitrack) from the stack
284 mlt_service producer
= context_pop_service( context
);
286 // Get the tractor from the stack
287 mlt_service service
= context_pop_service( context
);
289 // Connect the tractor to the producer
290 mlt_tractor_connect( MLT_TRACTOR( service
), producer
);
291 mlt_properties_set_data( mlt_service_properties( service
), "multitrack",
292 MLT_MULTITRACK( producer
), 0, NULL
, NULL
);
294 // Push the tractor back onto the stack
295 context_push_service( context
, service
);
297 // Push the producer back onto the stack
298 context_push_service( context
, producer
);
301 static void on_end_playlist( deserialise_context context
, const xmlChar
*name
)
303 // Get the producer (playlist) from the stack
304 mlt_service producer
= context_pop_service( context
);
306 // Push the producer back onto the stack
307 context_push_service( context
, producer
);
310 static void on_end_track( deserialise_context context
, const xmlChar
*name
)
312 // Get the producer from the stack
313 mlt_service producer
= context_pop_service( context
);
315 // Get the multitrack from the stack
316 mlt_service service
= context_pop_service( context
);
318 // Set the track on the multitrack
319 mlt_multitrack_connect( MLT_MULTITRACK( service
),
320 MLT_PRODUCER( producer
),
321 context
->track_count
++ );
323 // Push the multitrack back onto the stack
324 context_push_service( context
, service
);
327 static void on_end_entry( deserialise_context context
, const xmlChar
*name
)
329 // Get the producer from the stack
330 mlt_service producer
= context_pop_service( context
);
332 // Get the playlist from the stack
333 mlt_service service
= context_pop_service( context
);
335 // Append the producer to the playlist
336 mlt_playlist_append_io( MLT_PLAYLIST( service
),
337 MLT_PRODUCER( producer
),
338 mlt_properties_get_position( mlt_service_properties( producer
), "in" ),
339 mlt_properties_get_position( mlt_service_properties( producer
), "out" ) );
341 // Push the playlist back onto the stack
342 context_push_service( context
, service
);
345 static void on_end_tractor( deserialise_context context
, const xmlChar
*name
)
347 // Discard the last producer
348 mlt_producer multitrack
= MLT_PRODUCER( context_pop_service( context
) );
350 // Inherit the producer's properties
351 mlt_properties properties
= mlt_producer_properties( multitrack
);
352 mlt_properties_set_position( properties
, "length", mlt_producer_get_out( multitrack
) + 1 );
353 mlt_producer_set_in_and_out( multitrack
, 0, mlt_producer_get_out( multitrack
) );
354 mlt_properties_set_double( properties
, "fps", mlt_producer_get_fps( multitrack
) );
357 static void on_start_element( void *ctx
, const xmlChar
*name
, const xmlChar
**atts
)
359 deserialise_context context
= ( deserialise_context
) ctx
;
361 if ( strcmp( name
, "tractor" ) == 0 )
362 on_start_tractor( context
, name
, atts
);
363 else if ( strcmp( name
, "multitrack" ) == 0 )
364 on_start_multitrack( context
, name
, atts
);
365 else if ( strcmp( name
, "playlist" ) == 0 )
366 on_start_playlist( context
, name
, atts
);
367 else if ( strcmp( name
, "producer" ) == 0 )
368 on_start_producer( context
, name
, atts
);
369 else if ( strcmp( name
, "blank" ) == 0 )
370 on_start_blank( context
, name
, atts
);
371 else if ( strcmp( name
, "entry" ) == 0 || strcmp( name
, "track" ) == 0 )
372 on_start_entry_track( context
, name
, atts
);
373 else if ( strcmp( name
, "filter" ) == 0 )
374 on_start_filter( context
, name
, atts
);
375 else if ( strcmp( name
, "transition" ) == 0 )
376 on_start_transition( context
, name
, atts
);
379 static void on_end_element( void *ctx
, const xmlChar
*name
)
381 deserialise_context context
= ( deserialise_context
) ctx
;
383 if ( strcmp( name
, "multitrack" ) == 0 )
384 on_end_multitrack( context
, name
);
385 else if ( strcmp( name
, "playlist" ) == 0 )
386 on_end_playlist( context
, name
);
387 else if ( strcmp( name
, "track" ) == 0 )
388 on_end_track( context
, name
);
389 else if ( strcmp( name
, "entry" ) == 0 )
390 on_end_entry( context
, name
);
391 else if ( strcmp( name
, "tractor" ) == 0 )
392 on_end_tractor( context
, name
);
396 mlt_producer
producer_westley_init( char *filename
)
398 xmlSAXHandler
*sax
= calloc( 1, sizeof( xmlSAXHandler
) );
399 struct deserialise_context_s
*context
= calloc( 1, sizeof( struct deserialise_context_s
) );
401 context
->producer_map
= mlt_properties_new();
402 context
->destructors
= mlt_properties_new();
403 // We need to track the number of registered filters
404 mlt_properties_set_int( context
->destructors
, "registered", 0 );
405 sax
->startElement
= on_start_element
;
406 sax
->endElement
= on_end_element
;
409 xmlSAXUserParseFile( sax
, context
, filename
);
413 mlt_properties_close( context
->producer_map
);
415 mlt_service service
= context_pop_service( context
);
416 // make the returned service destroy the connected services
417 mlt_properties_set_data( mlt_service_properties( service
), "__destructors__", context
->destructors
, 0, (mlt_destructor
) mlt_properties_close
, NULL
);
420 mlt_properties_set( mlt_service_properties( service
), "resource", filename
);
422 return MLT_PRODUCER( service
);