8fd8783ad140ce3d05088d9bee172ed493f41263
[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 for ( ; atts != NULL && *atts != NULL; atts += 2 )
225 {
226 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
227
228 // Look for the producer attribute
229 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
230 {
231 if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) != NULL )
232 // Push the referenced producer onto the stack
233 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
234 }
235 }
236 }
237
238 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
239 {
240 mlt_properties properties = context->producer_properties = mlt_properties_new();
241
242 // Set the properties
243 for ( ; atts != NULL && *atts != NULL; atts += 2 )
244 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
245 }
246
247 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
248 {
249 mlt_properties properties = context->producer_properties = mlt_properties_new();
250
251 // Set the properties
252 for ( ; atts != NULL && *atts != NULL; atts += 2 )
253 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
254 }
255
256 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
257 {
258 mlt_properties properties = context->producer_properties;
259 char *value = NULL;
260
261 if ( properties == NULL )
262 return;
263
264 // Set the properties
265 for ( ; atts != NULL && *atts != NULL; atts += 2 )
266 {
267 if ( strcmp( atts[ 0 ], "name" ) == 0 )
268 {
269 context->property = strdup( atts[ 1 ] );
270 }
271 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
272 {
273 value = (char*) atts[ 1 ];
274 }
275 }
276
277 if ( context->property != NULL && value != NULL )
278 mlt_properties_set( properties, context->property, value );
279
280 // Tell parser to collect any further nodes for serialisation
281 context->is_value = 1;
282 }
283
284
285 /** This function adds a producer to a playlist or multitrack when
286 there is no entry or track element.
287 */
288
289 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
290 {
291 // Get the parent producer
292 mlt_service producer = context_pop_service( context );
293 if ( producer != NULL )
294 {
295 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
296
297 // Put the parent producer back
298 context_push_service( context, producer );
299
300 // If the parent producer is a multitrack or playlist (not a track or entry)
301 if ( resource && ( strcmp( resource, "<playlist>" ) == 0 ||
302 strcmp( resource, "<multitrack>" ) == 0 ) )
303 {
304 if ( strcmp( resource, "<playlist>" ) == 0 )
305 {
306 // Append this producer to the playlist
307 mlt_playlist_append_io( MLT_PLAYLIST( producer ),
308 MLT_PRODUCER( service ), in, out );
309 }
310 else
311 {
312 // Set this producer on the multitrack
313 mlt_multitrack_connect( MLT_MULTITRACK( producer ),
314 MLT_PRODUCER( service ),
315 mlt_multitrack_count( MLT_MULTITRACK( producer ) ) );
316 }
317 // Normally, the enclosing entry or track will pop this service off
318 // In its absence we do not push it on.
319 return 1;
320 }
321 }
322 return 0;
323 }
324
325 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
326 {
327 // Get the multitrack from the stack
328 mlt_service producer = context_pop_service( context );
329 if ( producer == NULL )
330 return;
331
332 // Get the tractor from the stack
333 mlt_service service = context_pop_service( context );
334
335 // Create a tractor if one does not exist
336 char *resource = NULL;
337 if ( service != NULL )
338 resource = mlt_properties_get( mlt_service_properties( service ), "resource" );
339 if ( service == NULL || resource == NULL || strcmp( resource, "<tractor>" ) )
340 {
341 // Put the anonymous service back onto the stack!
342 context_push_service( context, service );
343
344 service = mlt_tractor_service( mlt_tractor_init() );
345 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
346
347 // Inherit the producer's properties
348 mlt_properties properties = mlt_service_properties( service );
349 mlt_properties_set_position( properties, "length", mlt_producer_get_out( MLT_PRODUCER( producer ) ) + 1 );
350 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
351 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
352 }
353
354 // Connect the tractor to the multitrack
355 mlt_tractor_connect( MLT_TRACTOR( service ), producer );
356 mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
357 MLT_MULTITRACK( producer ), 0, NULL, NULL );
358
359 // See if the producer should be added to a playlist or multitrack
360 add_producer( context, service, 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
361
362 // Always push the multitrack back onto the stack for filters and transitions
363 context_push_service( context, producer );
364
365 // Always push the tractor back onto the stack for filters and transitions
366 context_push_service( context, service );
367 }
368
369 static void on_end_playlist( deserialise_context context, const xmlChar *name )
370 {
371 // Get the playlist from the stack
372 mlt_service producer = context_pop_service( context );
373 mlt_properties properties = mlt_service_properties( producer );
374
375 mlt_position in = mlt_properties_get_position( properties, "in" );
376 mlt_position out;
377
378 if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
379 out = mlt_properties_get_position( properties, "_westley.out" );
380 else
381 out = mlt_properties_get_position( properties, "length" ) - 1;
382
383 if ( mlt_properties_get_position( properties, "length" ) < out )
384 mlt_properties_set_position( properties, "length", out + 1 );
385
386 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
387
388 // See if the playlist should be added to a playlist or multitrack
389 if ( add_producer( context, producer, in, out ) == 0 )
390
391 // Otherwise, push the playlist back onto the stack
392 context_push_service( context, producer );
393 }
394
395 static void on_end_track( deserialise_context context, const xmlChar *name )
396 {
397 // Get the producer from the stack
398 mlt_service producer = context_pop_service( context );
399 if ( producer == NULL )
400 return;
401
402 // See if the producer is a tractor
403 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
404 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
405 // If so chomp its producer
406 context_pop_service( context );
407
408 // Get the dummy track service from the stack
409 mlt_service track = context_pop_service( context );
410 if ( track == NULL )
411 {
412 context_push_service( context, producer );
413 return;
414 }
415
416 // Get the multitrack from the stack
417 mlt_service service = context_pop_service( context );
418 if ( service == NULL )
419 {
420 context_push_service( context, producer );
421 return;
422 }
423
424 // Set the track on the multitrack
425 mlt_multitrack_connect( MLT_MULTITRACK( service ),
426 MLT_PRODUCER( producer ),
427 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
428
429 // Set producer i/o if specified
430 if ( mlt_properties_get( mlt_service_properties( track ), "in" ) != NULL ||
431 mlt_properties_get( mlt_service_properties( track ), "out" ) != NULL )
432 {
433 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
434 mlt_properties_get_position( mlt_service_properties( track ), "in" ),
435 mlt_properties_get_position( mlt_service_properties( track ), "out" ) );
436 }
437
438 // Push the multitrack back onto the stack
439 context_push_service( context, service );
440
441 mlt_service_close( track );
442 }
443
444 static void on_end_entry( deserialise_context context, const xmlChar *name )
445 {
446 // Get the producer from the stack
447 mlt_service producer = context_pop_service( context );
448 if ( producer == NULL )
449 return;
450
451 // See if the producer is a tractor
452 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
453 if ( resource && strcmp( resource, "<tractor>" ) == 0 )
454 // If so chomp its producer
455 context_pop_service( context );
456
457 // Get the dummy entry service from the stack
458 mlt_service entry = context_pop_service( context );
459 if ( entry == NULL )
460 {
461 context_push_service( context, producer );
462 return;
463 }
464
465 // Get the playlist from the stack
466 mlt_service service = context_pop_service( context );
467 if ( service == NULL )
468 {
469 context_push_service( context, producer );
470 return;
471 }
472
473 // Append the producer to the playlist
474 if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
475 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
476 {
477 mlt_playlist_append_io( MLT_PLAYLIST( service ),
478 MLT_PRODUCER( producer ),
479 mlt_properties_get_position( mlt_service_properties( entry ), "in" ),
480 mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
481 }
482 else
483 {
484 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
485 }
486
487 // Push the playlist back onto the stack
488 context_push_service( context, service );
489
490 mlt_service_close( entry );
491 }
492
493 static void on_end_tractor( deserialise_context context, const xmlChar *name )
494 {
495 // Get the tractor
496 mlt_service tractor = context_pop_service( context );
497 if ( tractor == NULL )
498 return;
499
500 // Get the tractor's multitrack
501 mlt_producer multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
502 if ( multitrack != NULL )
503 {
504 // Inherit the producer's properties
505 mlt_properties properties = mlt_producer_properties( MLT_PRODUCER( tractor ) );
506 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
507 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
508 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
509 }
510
511 // See if the tractor should be added to a playlist or multitrack
512 if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
513
514 // Otherwise, push the tractor back onto the stack
515 context_push_service( context, tractor );
516 }
517
518 static void on_end_property( deserialise_context context, const xmlChar *name )
519 {
520 // Tell parser to stop building a tree
521 context->is_value = 0;
522
523 // See if there is a xml tree for the value
524 if ( context->property != NULL && context->value_doc != NULL )
525 {
526 xmlChar *value;
527 int size;
528
529 // Serialise the tree to get value
530 xmlDocDumpMemory( context->value_doc, &value, &size );
531 mlt_properties_set( context->producer_properties, context->property, value );
532 xmlFree( value );
533 xmlFreeDoc( context->value_doc );
534 context->value_doc = NULL;
535 }
536
537 // Close this property handling
538 free( context->property );
539 context->property = NULL;
540 }
541
542 static void on_end_producer( deserialise_context context, const xmlChar *name )
543 {
544 mlt_properties properties = context->producer_properties;
545 mlt_service service = NULL;
546
547 if ( properties == NULL )
548 return;
549
550 char *resource = mlt_properties_get( properties, "resource" );
551 // Let Kino-SMIL src be a synonym for resource
552 if ( resource == NULL )
553 resource = mlt_properties_get( properties, "src" );
554
555 // Instantiate the producer
556 if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
557 {
558 service = MLT_SERVICE( mlt_factory_producer( "fezzik", mlt_properties_get( properties, "mlt_service" ) ) );
559 }
560 if ( service == NULL && resource != NULL )
561 {
562 char *root = mlt_properties_get( context->producer_map, "_root" );
563 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
564 if ( resource[ 0 ] != '/' )
565 {
566 strcpy( full_resource, root );
567 strcat( full_resource, resource );
568 }
569 else
570 {
571 strcpy( full_resource, resource );
572 }
573 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
574 free( full_resource );
575 }
576 if ( service == NULL )
577 return;
578 track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
579
580 // Add the producer to the producer map
581 if ( mlt_properties_get( properties, "id" ) != NULL )
582 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
583
584 // Handle in/out properties separately
585 mlt_position in = -1;
586 mlt_position out = -1;
587
588 // Get in
589 if ( mlt_properties_get( properties, "in" ) != NULL )
590 in = mlt_properties_get_position( properties, "in" );
591 // Let Kino-SMIL clipBegin be a synonym for in
592 if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
593 in = mlt_properties_get_position( properties, "clipBegin" );
594 // Get out
595 if ( mlt_properties_get( properties, "out" ) != NULL )
596 out = mlt_properties_get_position( properties, "out" );
597 // Let Kino-SMIL clipEnd be a synonym for out
598 if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
599 out = mlt_properties_get_position( properties, "clipEnd" );
600
601 // Remove in and out
602 mlt_properties_set( properties, "in", NULL );
603 mlt_properties_set( properties, "out", NULL );
604
605 mlt_properties_inherit( mlt_service_properties( service ), properties );
606 mlt_properties_close( properties );
607 context->producer_properties = NULL;
608
609 // See if the producer should be added to a playlist or multitrack
610 if ( add_producer( context, service, in, out ) == 0 )
611 {
612 // Otherwise, set in and out on...
613 if ( in != -1 || out != -1 )
614 {
615 // Get the parent service
616 mlt_service parent = context_pop_service( context );
617 if ( parent != NULL )
618 {
619 // Get the parent properties
620 properties = mlt_service_properties( parent );
621
622 char *resource = mlt_properties_get( properties, "resource" );
623
624 // Put the parent producer back
625 context_push_service( context, parent );
626
627 // If the parent is a track or entry
628 if ( resource && ( strcmp( resource, "<entry_track>" ) == 0 ) )
629 {
630 mlt_properties_set_position( properties, "in", in );
631 mlt_properties_set_position( properties, "out", out );
632 }
633 else
634 {
635 // Otherwise, set in and out on producer directly
636 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
637 }
638 }
639 else
640 {
641 // Otherwise, set in and out on producer directly
642 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
643 }
644 }
645
646 // Push the producer onto the stack
647 context_push_service( context, service );
648 }
649 }
650
651 static void on_end_filter( deserialise_context context, const xmlChar *name )
652 {
653 mlt_properties properties = context->producer_properties;
654 if ( properties == NULL )
655 return;
656
657 char *id;
658 char key[11];
659 key[ 10 ] = '\0';
660 mlt_service tractor = NULL;
661
662 // Get the producer from the stack
663 mlt_service producer = context_pop_service( context );
664 if ( producer == NULL )
665 return;
666
667 // See if the producer is a tractor
668 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
669 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
670 {
671 // If so, then get the next producer
672 tractor = producer;
673 producer = context_pop_service( context );
674 }
675
676 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
677
678 // Create the filter
679 mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
680 if ( service == NULL )
681 {
682 context_push_service( context, producer );
683 return;
684 }
685 track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
686
687 // Connect the filter to the producer
688 mlt_filter_connect( MLT_FILTER( service ), producer,
689 mlt_properties_get_int( properties, "track" ) );
690
691 // Set in and out from producer if non existant
692 if ( mlt_properties_get( properties, "in" ) == NULL )
693 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
694 if ( mlt_properties_get( properties, "out" ) == NULL )
695 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
696
697 // Propogate the properties
698 mlt_properties_inherit( mlt_service_properties( service ), properties );
699 mlt_properties_close( properties );
700 context->producer_properties = NULL;
701 properties = mlt_service_properties( service );
702
703 // Set in and out again due to inheritance
704 mlt_filter_set_in_and_out( MLT_FILTER( service ),
705 mlt_properties_get_position( properties, "in" ),
706 mlt_properties_get_position( properties, "out" ) );
707
708 // If a producer alias is in the producer_map, get it
709 snprintf( key, 10, "%p", producer );
710 if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
711 producer = mlt_properties_get_data( context->producer_map, key, NULL );
712
713 // Put the producer in the producer map
714 id = mlt_properties_get( mlt_service_properties( producer ), "id" );
715 if ( id != NULL )
716 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
717
718 // For filter chain support, add an alias to the producer map
719 snprintf( key, 10, "%p", service );
720 mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
721
722 // Push the filter onto the stack
723 context_push_service( context, service );
724
725 if ( tractor != NULL )
726 {
727 // Connect the tractor to the filter
728 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
729
730 // Push the tractor back onto the stack
731 context_push_service( context, tractor );
732 }
733 }
734
735 static void on_end_transition( deserialise_context context, const xmlChar *name )
736 {
737 mlt_service tractor = NULL;
738 mlt_properties properties = context->producer_properties;
739 if ( properties == NULL )
740 return;
741
742 // Get the producer from the stack
743 mlt_service producer = context_pop_service( context );
744 if ( producer == NULL )
745 return;
746
747 // See if the producer is a tractor
748 char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
749 if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
750 {
751 // If so, then get the next producer
752 tractor = producer;
753 producer = context_pop_service( context );
754 }
755
756 // Create the transition
757 mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
758 if ( service == NULL )
759 {
760 context_push_service( context, producer );
761 return;
762 }
763 track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
764
765 // Propogate the properties
766 mlt_properties_inherit( mlt_service_properties( service ), properties );
767 mlt_properties_close( properties );
768 context->producer_properties = NULL;
769 properties = mlt_service_properties( service );
770
771 // Set in and out again due to inheritance
772 mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
773 mlt_properties_get_position( properties, "in" ),
774 mlt_properties_get_position( properties, "out" ) );
775
776 // Connect the transition to the producer
777 mlt_transition_connect( MLT_TRANSITION( service ), producer,
778 mlt_properties_get_int( properties, "a_track" ),
779 mlt_properties_get_int( properties, "b_track" ) );
780
781 // Push the transition onto the stack
782 context_push_service( context, service );
783
784 if ( tractor != NULL )
785 {
786 // Connect the tractor to the transition
787 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
788
789 // Push the tractor back onto the stack
790 context_push_service( context, tractor );
791 }
792 }
793
794 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
795 {
796 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
797 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
798
799 // Build a tree from nodes within a property value
800 if ( context->is_value == 1 )
801 {
802 xmlNodePtr node = xmlNewNode( NULL, name );
803
804 if ( context->value_doc == NULL )
805 {
806 // Start a new tree
807 context->value_doc = xmlNewDoc( "1.0" );
808 xmlDocSetRootElement( context->value_doc, node );
809 }
810 else
811 {
812 // Append child to tree
813 xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
814 }
815 context_push_node( context, node );
816
817 // Set the attributes
818 for ( ; atts != NULL && *atts != NULL; atts += 2 )
819 xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
820 }
821 else if ( strcmp( name, "tractor" ) == 0 )
822 on_start_tractor( context, name, atts );
823 else if ( strcmp( name, "multitrack" ) == 0 )
824 on_start_multitrack( context, name, atts );
825 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
826 on_start_playlist( context, name, atts );
827 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
828 on_start_producer( context, name, atts );
829 else if ( strcmp( name, "blank" ) == 0 )
830 on_start_blank( context, name, atts );
831 else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
832 on_start_entry_track( context, name, atts );
833 else if ( strcmp( name, "filter" ) == 0 )
834 on_start_filter( context, name, atts );
835 else if ( strcmp( name, "transition" ) == 0 )
836 on_start_transition( context, name, atts );
837 else if ( strcmp( name, "property" ) == 0 )
838 on_start_property( context, name, atts );
839 }
840
841 static void on_end_element( void *ctx, const xmlChar *name )
842 {
843 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
844 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
845
846 if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
847 context_pop_node( context );
848 else if ( strcmp( name, "multitrack" ) == 0 )
849 on_end_multitrack( context, name );
850 else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
851 on_end_playlist( context, name );
852 else if ( strcmp( name, "track" ) == 0 )
853 on_end_track( context, name );
854 else if ( strcmp( name, "entry" ) == 0 )
855 on_end_entry( context, name );
856 else if ( strcmp( name, "tractor" ) == 0 )
857 on_end_tractor( context, name );
858 else if ( strcmp( name, "property" ) == 0 )
859 on_end_property( context, name );
860 else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
861 on_end_producer( context, name );
862 else if ( strcmp( name, "filter" ) == 0 )
863 on_end_filter( context, name );
864 else if ( strcmp( name, "transition" ) == 0 )
865 on_end_transition( context, name );
866 }
867
868 static void on_characters( void *ctx, const xmlChar *ch, int len )
869 {
870 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
871 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
872 char *value = calloc( len + 1, 1 );
873
874 value[ len ] = 0;
875 strncpy( value, (const char*) ch, len );
876
877 if ( context->stack_node_size > 0 )
878 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
879
880 else if ( context->property != NULL && context->producer_properties != NULL )
881 mlt_properties_set( context->producer_properties, context->property, value );
882
883 free( value);
884 }
885
886 // The following 3 facilitate entity substitution in the SAX parser
887 static void on_internal_subset( void *ctx, const xmlChar* name,
888 const xmlChar* publicId, const xmlChar* systemId )
889 {
890 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
891 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
892
893 xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
894 }
895
896 static void on_entity_declaration( void *ctx, const xmlChar* name, int type,
897 const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
898 {
899 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
900 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
901
902 xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
903 }
904
905 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
906 {
907 struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
908 deserialise_context context = ( deserialise_context )( xmlcontext->_private );
909
910 return xmlGetDocEntity( context->entity_doc, name );
911 }
912
913
914 mlt_producer producer_westley_init( char *filename )
915 {
916 xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
917 struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
918 mlt_properties properties = NULL;
919 int i = 0;
920 struct _xmlParserCtxt *xmlcontext;
921 int well_formed = 0;
922
923 context->producer_map = mlt_properties_new();
924 context->destructors = mlt_properties_new();
925
926 // We need to track the number of registered filters
927 mlt_properties_set_int( context->destructors, "registered", 0 );
928
929 // We need the directory prefix which was used for the westley
930 mlt_properties_set( context->producer_map, "_root", "" );
931 if ( strchr( filename, '/' ) )
932 {
933 char *root = NULL;
934 mlt_properties_set( context->producer_map, "_root", filename );
935 root = mlt_properties_get( context->producer_map, "_root" );
936 *( strrchr( root, '/' ) + 1 ) = '\0';
937 }
938
939 // Setup SAX callbacks
940 sax->startElement = on_start_element;
941 sax->endElement = on_end_element;
942 sax->characters = on_characters;
943 sax->cdataBlock = on_characters;
944 sax->internalSubset = on_internal_subset;
945 sax->entityDecl = on_entity_declaration;
946 sax->getEntity = on_get_entity;
947
948 // Setup libxml2 SAX parsing
949 xmlInitParser();
950 xmlSubstituteEntitiesDefault( 1 );
951 // This is used to facilitate entity substitution in the SAX parser
952 context->entity_doc = xmlNewDoc( "1.0" );
953 xmlcontext = xmlCreateFileParserCtxt( filename );
954 xmlcontext->sax = sax;
955 xmlcontext->_private = ( void* )context;
956
957 // Parse
958 xmlParseDocument( xmlcontext );
959 well_formed = xmlcontext->wellFormed;
960
961 // Cleanup after parsing
962 xmlFreeDoc( context->entity_doc );
963 free( sax );
964 xmlcontext->sax = NULL;
965 xmlcontext->_private = NULL;
966 xmlFreeParserCtxt( xmlcontext );
967 xmlMemoryDump( ); // for debugging
968
969 // Get the last producer on the stack
970 mlt_service service = context_pop_service( context );
971 if ( well_formed && service != NULL )
972 {
973 // Verify it is a producer service (mlt_type="mlt_producer")
974 // (producer, playlist, multitrack)
975 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
976 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 ) )
977 service = NULL;
978 }
979
980 if ( well_formed && service != NULL )
981 {
982 // Need the complete producer list for various reasons
983 properties = context->destructors;
984
985 // Now make sure we don't have a reference to the service in the properties
986 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
987 {
988 char *name = mlt_properties_get_name( properties, i );
989 if ( mlt_properties_get_data( properties, name, NULL ) == service )
990 {
991 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
992 break;
993 }
994 }
995
996 // We are done referencing destructor property list
997 // Set this var to service properties for convenience
998 properties = mlt_service_properties( service );
999
1000 // make the returned service destroy the connected services
1001 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1002
1003 // Now assign additional properties
1004 mlt_properties_set( properties, "resource", filename );
1005
1006 // This tells consumer_westley not to deep copy
1007 mlt_properties_set( properties, "westley", "was here" );
1008 }
1009 else
1010 {
1011 // Return null if not well formed
1012 service = NULL;
1013
1014 // Clean up
1015 mlt_properties_close( context->destructors );
1016 }
1017
1018 free( context->stack_service );
1019 mlt_properties_close( context->producer_map );
1020 //free( context );
1021
1022 return MLT_PRODUCER( service );
1023 }