89ca817f04848d1e760763b8d1d9a3ce89b969ef
[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 (they are currently destroyed
22 // when the returned producer is closed).
23 // TODO: determine why deserialise_context can not be released.
24
25 #include "producer_westley.h"
26 #include <framework/mlt.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include <libxml/parser.h>
32 #include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
33 #include <libxml/tree.h>
34
35 #define STACK_SIZE 1000
36
37 struct deserialise_context_s
38 {
39 mlt_service stack_service[ STACK_SIZE ];
40 int stack_service_size;
41 mlt_properties producer_map;
42 mlt_properties destructors;
43 char *property;
44 mlt_properties producer_properties;
45 int is_value;
46 xmlDocPtr value_doc;
47 xmlNodePtr stack_node[ STACK_SIZE ];
48 int stack_node_size;
49 xmlDocPtr entity_doc;
50 };
51 typedef struct deserialise_context_s *deserialise_context;
52
53
54 /** Push a service.
55 */
56
57 static int context_push_service( deserialise_context this, mlt_service that )
58 {
59 int ret = this->stack_service_size >= STACK_SIZE - 1;
60 if ( ret == 0 )
61 this->stack_service[ this->stack_service_size ++ ] = that;
62 return ret;
63 }
64
65 /** Pop a service.
66 */
67
68 static mlt_service context_pop_service( deserialise_context this )
69 {
70 mlt_service result = NULL;
71 if ( this->stack_service_size > 0 )
72 result = this->stack_service[ -- this->stack_service_size ];
73 return result;
74 }
75
76 /** Push a node.
77 */
78
79 static int context_push_node( deserialise_context this, xmlNodePtr node )
80 {
81 int ret = this->stack_node_size >= STACK_SIZE - 1;
82 if ( ret == 0 )
83 this->stack_node[ this->stack_node_size ++ ] = node;
84 return ret;
85 }
86
87 /** Pop a node.
88 */
89
90 static xmlNodePtr context_pop_node( deserialise_context this )
91 {
92 xmlNodePtr result = NULL;
93 if ( this->stack_node_size > 0 )
94 result = this->stack_node[ -- this->stack_node_size ];
95 return result;
96 }
97
98
99 // Set the destructor on a new service
100 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
101 {
102 int registered = mlt_properties_get_int( properties, "registered" );
103 char *key = mlt_properties_get( properties, "registered" );
104 mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
105 mlt_properties_set_int( properties, "registered", ++ registered );
106 }
107
108
109 // Forward declarations
110 static void on_end_track( deserialise_context context, const xmlChar *name );
111 static void on_end_entry( deserialise_context context, const xmlChar *name );
112
113 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
114 {
115 mlt_service service = mlt_tractor_service( mlt_tractor_init() );
116 mlt_properties properties = mlt_service_properties( service );
117
118 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
119
120 mlt_properties_set_position( properties, "length", 0 );
121
122 for ( ; atts != NULL && *atts != NULL; atts += 2 )
123 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
124
125 if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
126 {
127 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
128 mlt_properties_set_position( properties, "length", length );
129 }
130
131 if ( mlt_properties_get( properties, "id" ) != NULL )
132 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
133
134 context_push_service( context, service );
135 }
136
137 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
138 {
139 mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
140 mlt_properties properties = mlt_service_properties( service );
141
142 track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
143
144 mlt_properties_set_position( properties, "length", 0 );
145
146 for ( ; atts != NULL && *atts != NULL; atts += 2 )
147 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
148
149 if ( mlt_properties_get( properties, "id" ) != NULL )
150 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
151
152 context_push_service( context, service );
153 }
154
155 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
156 {
157 mlt_service service = mlt_playlist_service( mlt_playlist_init() );
158 mlt_properties properties = mlt_service_properties( service );
159
160 track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
161
162 mlt_properties_set_position( properties, "length", 0 );
163
164 for ( ; atts != NULL && *atts != NULL; atts += 2 )
165 {
166 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
167
168 // Out will be overwritten later as we append, so we need to save it
169 if ( strcmp( atts[ 0 ], "out" ) == 0 )
170 mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
171 }
172
173 if ( mlt_properties_get( properties, "id" ) != NULL )
174 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
175
176 context_push_service( context, service );
177 }
178
179 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
180 {
181 mlt_properties properties = context->producer_properties = mlt_properties_new();
182
183 for ( ; atts != NULL && *atts != NULL; atts += 2 )
184 {
185 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
186 }
187 }
188
189 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
190 {
191 // Get the playlist from the stack
192 mlt_service service = context_pop_service( context );
193 mlt_position length = 0;
194
195 if ( service == NULL )
196 return;
197
198 // Look for the length attribute
199 for ( ; atts != NULL && *atts != NULL; atts += 2 )
200 {
201 if ( strcmp( atts[0], "length" ) == 0 )
202 {
203 length = atoll( atts[1] );
204 break;
205 }
206 }
207
208 // Append a blank to the playlist
209 mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
210
211 // Push the playlist back onto the stack
212 context_push_service( context, service );
213 }
214
215 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
216 {
217 // Use a dummy service to hold properties to allow arbitrary nesting
218 mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
219 mlt_service_init( service, NULL );
220
221 // Push the dummy service onto the stack
222 context_push_service( context, service );
223
224 if ( strcmp( name, "entry" ) == 0 )
225 mlt_properties_set( mlt_service_properties( service ), "resource", "<entry>" );
226 else
227 mlt_properties_set( mlt_service_properties( service ), "resource", "<track>" );
228
229 for ( ; atts != NULL && *atts != NULL; atts += 2 )
230 {
231 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
232
233 // Look for the producer attribute
234 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
235 {
236 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
237 // Push the referenced producer onto the stack
238 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
239 else
240 // Remove the dummy service to cause end element failure
241 context_pop_service( context );
242 }
243 }
244 }
245
246 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
247 {
248 mlt_properties properties = context->producer_properties = mlt_properties_new();
249
250 // Set the properties
251 for ( ; atts != NULL && *atts != NULL; atts += 2 )
252 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
253 }
254
255 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
256 {
257 mlt_properties properties = context->producer_properties = mlt_properties_new();
258
259 // Set the properties
260 for ( ; atts != NULL && *atts != NULL; atts += 2 )
261 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
262 }
263
264 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
265 {
266 mlt_properties properties = context->producer_properties;
267 char *value = NULL;
268
269 if ( properties == NULL )
270 return;
271
272 // Set the properties
273 for ( ; atts != NULL && *atts != NULL; atts += 2 )
274 {
275 if ( strcmp( atts[ 0 ], "name" ) == 0 )
276 {
277 context->property = strdup( atts[ 1 ] );
278 }
279 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
280 {
281 value = (char*) atts[ 1 ];
282 }
283 }
284
285 if ( context->property != NULL && value != NULL )
286 mlt_properties_set( properties, context->property, value );
287
288 // Tell parser to collect any further nodes for serialisation
289 context->is_value = 1;
290 }
291
292
293 /** This function adds a producer to a playlist or multitrack when
294 there is no entry or track element.
295 */
296
297 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
298 {
299 // Get the parent producer
300 mlt_service producer = context_pop_service( context );
301 if ( producer != NULL )
302 {
303 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
304
305 // Put the parent producer back
306 context_push_service( context, producer );
307
308 // If the parent producer is a multitrack or playlist (not a track or entry)
309 if ( resource && ( strcmp( resource, "<playlist>" ) == 0 ||
310 strcmp( resource, "<multitrack>" ) == 0 ) )
311 {
312 if ( strcmp( resource, "<playlist>" ) == 0 )
313 {
314 // Append this producer to the playlist
315 mlt_playlist_append_io( MLT_PLAYLIST( producer ),
316 MLT_PRODUCER( service ), in, out );
317 }
318 else
319 {
320 mlt_properties properties = mlt_service_properties( service );
321
322 // Set this producer on the multitrack
323 mlt_multitrack_connect( MLT_MULTITRACK( producer ),
324 MLT_PRODUCER( service ),
325 mlt_multitrack_count( MLT_MULTITRACK( producer ) ) );
326
327 // Set the hide state of the track producer
328 char *hide_s = mlt_properties_get( properties, "hide" );
329 if ( hide_s != NULL )
330 {
331 if ( strcmp( hide_s, "video" ) == 0 )
332 mlt_properties_set_int( properties, "hide", 1 );
333 else if ( strcmp( hide_s, "audio" ) == 0 )
334 mlt_properties_set_int( properties, "hide", 2 );
335 else if ( strcmp( hide_s, "both" ) == 0 )
336 mlt_properties_set_int( properties, "hide", 3 );
337 }
338
339 }
340 // Normally, the enclosing entry or track will pop this service off
341 // In its absence we do not push it on.
342 return 1;
343 }
344 }
345 return 0;
346 }
347
348 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
349 {
350 // Get the multitrack from the stack
351 mlt_service producer = context_pop_service( context );
352 if ( producer == NULL )
353 return;
354
355 // Get the tractor from the stack
356 mlt_service service = context_pop_service( context );
357
358 // Create a tractor if one does not exist
359 char *resource = NULL;
360 if ( service != NULL )
361 resource = mlt_properties_get( mlt_service_properties( service ), "resource" );
362 if ( service == NULL || resource == NULL || strcmp( resource, "<tractor>" ) )
363 {
364 // Put the anonymous service back onto the stack!
365 context_push_service( context, service );
366
367 service = mlt_tractor_service( mlt_tractor_init() );
368 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
369
370 // Inherit the producer's properties
371 mlt_properties properties = mlt_service_properties( service );
372 mlt_properties_set_position( properties, "length", mlt_producer_get_out( MLT_PRODUCER( producer ) ) + 1 );
373 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
374 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
375 }
376
377 // Connect the tractor to the multitrack
378 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
379 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
380 MLT_MULTITRACK( producer ), 0, NULL, NULL );
381
382 // See if the producer should be added to a playlist or multitrack
383 add_producer( context, service, 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
384
385 // Always push the multitrack back onto the stack for filters and transitions
386 context_push_service( context, producer );
387
388 // Always push the tractor back onto the stack for filters and transitions
389 context_push_service( context, service );
390 }
391
392 static void on_end_playlist( deserialise_context context, const xmlChar *name )
393 {
394 // Get the playlist from the stack
395 mlt_service producer = context_pop_service( context );
396 if ( producer == NULL )
397 return;
398 mlt_properties properties = mlt_service_properties( producer );
399
400 mlt_position in = mlt_properties_get_position( properties, "in" );
401 mlt_position out;
402
403 if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
404 out = mlt_properties_get_position( properties, "_westley.out" );
405 else
406 out = mlt_properties_get_position( properties, "length" ) - 1;
407
408 if ( mlt_properties_get_position( properties, "length" ) < out )
409 mlt_properties_set_position( properties, "length", out + 1 );
410
411 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
412
413 // See if the playlist should be added to a playlist or multitrack
414 if ( add_producer( context, producer, in, out ) == 0 )
415
416 // Otherwise, push the playlist back onto the stack
417 context_push_service( context, producer );
418 }
419
420 static void on_end_track( deserialise_context context, const xmlChar *name )
421 {
422 // Get the producer from the stack
423 mlt_service producer = context_pop_service( context );
424 if ( producer == NULL )
425 return;
426 mlt_properties producer_props = mlt_service_properties( producer );
427
428 // See if the producer is a tractor
429 char *resource = mlt_properties_get( producer_props, "resource" );
430 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
431 // If so chomp its producer
432 context_pop_service( context );
433
434 // Get the dummy track service from the stack
435 mlt_service track = context_pop_service( context );
436 if ( track == NULL || strcmp( mlt_properties_get( mlt_service_properties( track ), "resource" ), "<track>" ) )
437 {
438 context_push_service( context, producer );
439 return;
440 }
441 mlt_properties track_props = mlt_service_properties( track );
442
443 // Get the multitrack from the stack
444 mlt_service service = context_pop_service( context );
445 if ( service == NULL )
446 {
447 context_push_service( context, producer );
448 return;
449 }
450
451 // Set the track on the multitrack
452 mlt_multitrack_connect( MLT_MULTITRACK( service ),
453 MLT_PRODUCER( producer ),
454 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
455
456 // Set producer i/o if specified
457 if ( mlt_properties_get( track_props, "in" ) != NULL ||
458 mlt_properties_get( track_props, "out" ) != NULL )
459 {
460 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
461 mlt_properties_get_position( track_props, "in" ),
462 mlt_properties_get_position( track_props, "out" ) );
463 }
464
465 // Set the hide state of the track producer
466 char *hide_s = mlt_properties_get( track_props, "hide" );
467 if ( hide_s != NULL )
468 {
469 if ( strcmp( hide_s, "video" ) == 0 )
470 mlt_properties_set_int( producer_props, "hide", 1 );
471 else if ( strcmp( hide_s, "audio" ) == 0 )
472 mlt_properties_set_int( producer_props, "hide", 2 );
473 else if ( strcmp( hide_s, "both" ) == 0 )
474 mlt_properties_set_int( producer_props, "hide", 3 );
475 }
476
477 // Push the multitrack back onto the stack
478 context_push_service( context, service );
479
480 mlt_service_close( track );
481 }
482
483 static void on_end_entry( deserialise_context context, const xmlChar *name )
484 {
485 // Get the producer from the stack
486 mlt_service producer = context_pop_service( context );
487 if ( producer == NULL )
488 return;
489
490 // See if the producer is a tractor
491 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
492 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
493 // If so chomp its producer
494 context_pop_service( context );
495
496 // Get the dummy entry service from the stack
497 mlt_service entry = context_pop_service( context );
498 if ( entry == NULL || strcmp( mlt_properties_get( mlt_service_properties( entry ), "resource" ), "<entry>" ) )
499 {
500 context_push_service( context, producer );
501 return;
502 }
503
504 // Get the playlist from the stack
505 mlt_service service = context_pop_service( context );
506 if ( service == NULL )
507 {
508 context_push_service( context, producer );
509 return;
510 }
511
512 // Append the producer to the playlist
513 if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
514 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
515 {
516 mlt_playlist_append_io( MLT_PLAYLIST( service ),
517 MLT_PRODUCER( producer ),
518 mlt_properties_get_position( mlt_service_properties( entry ), "in" ),
519 mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
520 }
521 else
522 {
523 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
524 }
525
526 // Push the playlist back onto the stack
527 context_push_service( context, service );
528
529 mlt_service_close( entry );
530 }
531
532 static void on_end_tractor( deserialise_context context, const xmlChar *name )
533 {
534 // Get the tractor
535 mlt_service tractor = context_pop_service( context );
536 if ( tractor == NULL )
537 return;
538
539 // Get the tractor's multitrack
540 mlt_producer multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
541 if ( multitrack != NULL )
542 {
543 // Inherit the producer's properties
544 mlt_properties properties = mlt_producer_properties( MLT_PRODUCER( tractor ) );
545 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
546 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
547 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
548 }
549
550 // See if the tractor should be added to a playlist or multitrack
551 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
552
553 // Otherwise, push the tractor back onto the stack
554 context_push_service( context, tractor );
555 }
556
557 static void on_end_property( deserialise_context context, const xmlChar *name )
558 {
559 // Tell parser to stop building a tree
560 context->is_value = 0;
561
562 // See if there is a xml tree for the value
563 if ( context->property != NULL && context->value_doc != NULL )
564 {
565 xmlChar *value;
566 int size;
567
568 // Serialise the tree to get value
569 xmlDocDumpMemory( context->value_doc, &value, &size );
570 mlt_properties_set( context->producer_properties, context->property, value );
571 xmlFree( value );
572 xmlFreeDoc( context->value_doc );
573 context->value_doc = NULL;
574 }
575
576 // Close this property handling
577 free( context->property );
578 context->property = NULL;
579 }
580
581 static void on_end_producer( deserialise_context context, const xmlChar *name )
582 {
583 mlt_properties properties = context->producer_properties;
584 mlt_service service = NULL;
585
586 if ( properties == NULL )
587 return;
588
589 char *resource = mlt_properties_get( properties, "resource" );
590 // Let Kino-SMIL src be a synonym for resource
591 if ( resource == NULL )
592 resource = mlt_properties_get( properties, "src" );
593
594 // Instantiate the producer
595 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
596 {
597 service = MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties, "mlt_service" ) ) );
598 }
599 if ( service == NULL && resource != NULL )
600 {
601 char *root = mlt_properties_get( context->producer_map, "_root" );
602 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
603 if ( resource[ 0 ] != '/' )
604 {
605 strcpy( full_resource, root );
606 strcat( full_resource, resource );
607 }
608 else
609 {
610 strcpy( full_resource, resource );
611 }
612 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
613 free( full_resource );
614 }
615 if ( service == NULL )
616 return;
617 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
618
619 // Add the producer to the producer map
620 if ( mlt_properties_get( properties, "id" ) != NULL )
621 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
622
623 // Handle in/out properties separately
624 mlt_position in = -1;
625 mlt_position out = -1;
626
627 // Get in
628 if ( mlt_properties_get( properties, "in" ) != NULL )
629 in = mlt_properties_get_position( properties, "in" );
630 // Let Kino-SMIL clipBegin be a synonym for in
631 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
632 in = mlt_properties_get_position( properties, "clipBegin" );
633 // Get out
634 if ( mlt_properties_get( properties, "out" ) != NULL )
635 out = mlt_properties_get_position( properties, "out" );
636 // Let Kino-SMIL clipEnd be a synonym for out
637 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
638 out = mlt_properties_get_position( properties, "clipEnd" );
639
640 // Remove in and out
641 mlt_properties_set( properties, "in", NULL );
642 mlt_properties_set( properties, "out", NULL );
643
644 mlt_properties_inherit( mlt_service_properties( service ), properties );
645 mlt_properties_close( properties );
646 context->producer_properties = NULL;
647
648 // See if the producer should be added to a playlist or multitrack
649 if ( add_producer( context, service, in, out ) == 0 )
650 {
651 // Otherwise, set in and out on...
652 if ( in != -1 || out != -1 )
653 {
654 // Get the parent service
655 mlt_service parent = context_pop_service( context );
656 if ( parent != NULL )
657 {
658 // Get the parent properties
659 properties = mlt_service_properties( parent );
660
661 char *resource = mlt_properties_get( properties, "resource" );
662
663 // Put the parent producer back
664 context_push_service( context, parent );
665
666 // If the parent is a track or entry
667 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
668 {
669 mlt_properties_set_position( properties, "in", in );
670 mlt_properties_set_position( properties, "out", out );
671 }
672 else
673 {
674 // Otherwise, set in and out on producer directly
675 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
676 }
677 }
678 else
679 {
680 // Otherwise, set in and out on producer directly
681 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
682 }
683 }
684
685 // Push the producer onto the stack
686 context_push_service( context, service );
687 }
688 }
689
690 static void on_end_filter( deserialise_context context, const xmlChar *name )
691 {
692 mlt_properties properties = context->producer_properties;
693 if ( properties == NULL )
694 return;
695
696 char *id;
697 char key[11];
698 key[ 10 ] = '\0';
699 mlt_service tractor = NULL;
700
701 // Get the producer from the stack
702 mlt_service producer = context_pop_service( context );
703 if ( producer == NULL )
704 return;
705
706 // See if the producer is a tractor
707 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
708 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
709 {
710 // If so, then get the next producer
711 tractor = producer;
712 producer = context_pop_service( context );
713 }
714
715 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
716
717 // Create the filter
718 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
719 if ( service == NULL )
720 {
721 context_push_service( context, producer );
722 return;
723 }
724 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
725
726 // Connect the filter to the producer
727 mlt_filter_connect( MLT_FILTER( service ), producer,
728 mlt_properties_get_int( properties, "track" ) );
729
730 // Set in and out from producer if non existant
731 if ( mlt_properties_get( properties, "in" ) == NULL )
732 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
733 if ( mlt_properties_get( properties, "out" ) == NULL )
734 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
735
736 // Propogate the properties
737 mlt_properties_inherit( mlt_service_properties( service ), properties );
738 mlt_properties_close( properties );
739 context->producer_properties = NULL;
740 properties = mlt_service_properties( service );
741
742 // Set in and out again due to inheritance
743 mlt_filter_set_in_and_out( MLT_FILTER( service ),
744 mlt_properties_get_position( properties, "in" ),
745 mlt_properties_get_position( properties, "out" ) );
746
747 // If a producer alias is in the producer_map, get it
748 snprintf( key, 10, "%p", producer );
749 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
750 producer = mlt_properties_get_data( context->producer_map, key, NULL );
751
752 // Put the producer in the producer map
753 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
754 if ( id != NULL )
755 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
756
757 // For filter chain support, add an alias to the producer map
758 snprintf( key, 10, "%p", service );
759 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
760
761 // Push the filter onto the stack
762 context_push_service( context, service );
763
764 if ( tractor != NULL )
765 {
766 // Connect the tractor to the filter
767 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
768
769 // Push the tractor back onto the stack
770 context_push_service( context, tractor );
771 }
772 }
773
774 static void on_end_transition( deserialise_context context, const xmlChar *name )
775 {
776 mlt_service tractor = NULL;
777 mlt_properties properties = context->producer_properties;
778 if ( properties == NULL )
779 return;
780
781 // Get the producer from the stack
782 mlt_service producer = context_pop_service( context );
783 if ( producer == NULL )
784 return;
785
786 // See if the producer is a tractor
787 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
788 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
789 {
790 // If so, then get the next producer
791 tractor = producer;
792 producer = context_pop_service( context );
793 }
794
795 // Create the transition
796 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
797 if ( service == NULL )
798 {
799 context_push_service( context, producer );
800 return;
801 }
802 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
803
804 // Propogate the properties
805 mlt_properties_inherit( mlt_service_properties( service ), properties );
806 mlt_properties_close( properties );
807 context->producer_properties = NULL;
808 properties = mlt_service_properties( service );
809
810 // Set in and out again due to inheritance
811 mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
812 mlt_properties_get_position( properties, "in" ),
813 mlt_properties_get_position( properties, "out" ) );
814
815 // Connect the transition to the producer
816 mlt_transition_connect( MLT_TRANSITION( service ), producer,
817 mlt_properties_get_int( properties, "a_track" ),
818 mlt_properties_get_int( properties, "b_track" ) );
819
820 // Push the transition onto the stack
821 context_push_service( context, service );
822
823 if ( tractor != NULL )
824 {
825 // Connect the tractor to the transition
826 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
827
828 // Push the tractor back onto the stack
829 context_push_service( context, tractor );
830 }
831 }
832
833 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
834 {
835 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
836 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
837
838 // Build a tree from nodes within a property value
839 if ( context->is_value == 1 )
840 {
841 xmlNodePtr node = xmlNewNode( NULL, name );
842
843 if ( context->value_doc == NULL )
844 {
845 // Start a new tree
846 context->value_doc = xmlNewDoc( "1.0" );
847 xmlDocSetRootElement( context->value_doc, node );
848 }
849 else
850 {
851 // Append child to tree
852 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
853 }
854 context_push_node( context, node );
855
856 // Set the attributes
857 for ( ; atts != NULL && *atts != NULL; atts += 2 )
858 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
859 }
860 else if ( strcmp( name, "tractor" ) == 0 )
861 on_start_tractor( context, name, atts );
862 else if ( strcmp( name, "multitrack" ) == 0 )
863 on_start_multitrack( context, name, atts );
864 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
865 on_start_playlist( context, name, atts );
866 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
867 on_start_producer( context, name, atts );
868 else if ( strcmp( name, "blank" ) == 0 )
869 on_start_blank( context, name, atts );
870 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
871 on_start_entry_track( context, name, atts );
872 else if ( strcmp( name, "filter" ) == 0 )
873 on_start_filter( context, name, atts );
874 else if ( strcmp( name, "transition" ) == 0 )
875 on_start_transition( context, name, atts );
876 else if ( strcmp( name, "property" ) == 0 )
877 on_start_property( context, name, atts );
878 }
879
880 static void on_end_element( void *ctx, const xmlChar *name )
881 {
882 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
883 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
884
885 if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
886 context_pop_node( context );
887 else if ( strcmp( name, "multitrack" ) == 0 )
888 on_end_multitrack( context, name );
889 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
890 on_end_playlist( context, name );
891 else if ( strcmp( name, "track" ) == 0 )
892 on_end_track( context, name );
893 else if ( strcmp( name, "entry" ) == 0 )
894 on_end_entry( context, name );
895 else if ( strcmp( name, "tractor" ) == 0 )
896 on_end_tractor( context, name );
897 else if ( strcmp( name, "property" ) == 0 )
898 on_end_property( context, name );
899 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
900 on_end_producer( context, name );
901 else if ( strcmp( name, "filter" ) == 0 )
902 on_end_filter( context, name );
903 else if ( strcmp( name, "transition" ) == 0 )
904 on_end_transition( context, name );
905 }
906
907 static void on_characters( void *ctx, const xmlChar *ch, int len )
908 {
909 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
910 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
911 char *value = calloc( len + 1, 1 );
912
913 value[ len ] = 0;
914 strncpy( value, (const char*) ch, len );
915
916 if ( context->stack_node_size > 0 )
917 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
918
919 else if ( context->property != NULL && context->producer_properties != NULL )
920 mlt_properties_set( context->producer_properties, context->property, value );
921
922 free( value);
923 }
924
925 // The following 3 facilitate entity substitution in the SAX parser
926 static void on_internal_subset( void *ctx, const xmlChar* name,
927 const xmlChar* publicId, const xmlChar* systemId )
928 {
929 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
930 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
931
932 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
933 }
934
935 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
936 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
937 {
938 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
939 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
940
941 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
942 }
943
944 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
945 {
946 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
947 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
948
949 return xmlGetDocEntity( context->entity_doc, name );
950 }
951
952
953 mlt_producer producer_westley_init( char *filename )
954 {
955 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
956 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
957 mlt_properties properties = NULL;
958 int i = 0;
959 struct _xmlParserCtxt *xmlcontext;
960 int well_formed = 0;
961
962 context->producer_map = mlt_properties_new();
963 context->destructors = mlt_properties_new();
964
965 // We need to track the number of registered filters
966 mlt_properties_set_int( context->destructors, "registered", 0 );
967
968 // We need the directory prefix which was used for the westley
969 mlt_properties_set( context->producer_map, "_root", "" );
970 if ( strchr( filename, '/' ) )
971 {
972 char *root = NULL;
973 mlt_properties_set( context->producer_map, "_root", filename );
974 root = mlt_properties_get( context->producer_map, "_root" );
975 *( strrchr( root, '/' ) + 1 ) = '\0';
976 }
977
978 // Setup SAX callbacks
979 sax->startElement = on_start_element;
980 sax->endElement = on_end_element;
981 sax->characters = on_characters;
982 sax->cdataBlock = on_characters;
983 sax->internalSubset = on_internal_subset;
984 sax->entityDecl = on_entity_declaration;
985 sax->getEntity = on_get_entity;
986
987 // Setup libxml2 SAX parsing
988 xmlInitParser();
989 xmlSubstituteEntitiesDefault( 1 );
990 // This is used to facilitate entity substitution in the SAX parser
991 context->entity_doc = xmlNewDoc( "1.0" );
992 xmlcontext = xmlCreateFileParserCtxt( filename );
993 xmlcontext->sax = sax;
994 xmlcontext->_private = ( void* )context;
995
996 // Parse
997 xmlParseDocument( xmlcontext );
998 well_formed = xmlcontext->wellFormed;
999
1000 // Cleanup after parsing
1001 xmlFreeDoc( context->entity_doc );
1002 free( sax );
1003 xmlcontext->sax = NULL;
1004 xmlcontext->_private = NULL;
1005 xmlFreeParserCtxt( xmlcontext );
1006 xmlMemoryDump( ); // for debugging
1007
1008 // Get the last producer on the stack
1009 mlt_service service = context_pop_service( context );
1010 if ( well_formed && service != NULL )
1011 {
1012 // Verify it is a producer service (mlt_type="mlt_producer")
1013 // (producer, playlist, multitrack)
1014 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
1015 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 ) )
1016 service = NULL;
1017 }
1018
1019 if ( well_formed && service != NULL )
1020 {
1021 // Need the complete producer list for various reasons
1022 properties = context->destructors;
1023
1024 // Now make sure we don't have a reference to the service in the properties
1025 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1026 {
1027 char *name = mlt_properties_get_name( properties, i );
1028 if ( mlt_properties_get_data( properties, name, NULL ) == service )
1029 {
1030 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1031 break;
1032 }
1033 }
1034
1035 // We are done referencing destructor property list
1036 // Set this var to service properties for convenience
1037 properties = mlt_service_properties( service );
1038
1039 // make the returned service destroy the connected services
1040 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1041
1042 // Now assign additional properties
1043 mlt_properties_set( properties, "resource", filename );
1044
1045 // This tells consumer_westley not to deep copy
1046 mlt_properties_set( properties, "westley", "was here" );
1047 }
1048 else
1049 {
1050 // Return null if not well formed
1051 service = NULL;
1052
1053 // Clean up
1054 mlt_properties_close( context->destructors );
1055 }
1056
1057 free( context->stack_service );
1058 mlt_properties_close( context->producer_map );
1059 //free( context );
1060
1061 return MLT_PRODUCER( service );
1062 }