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