]> git.sesse.net Git - mlt/blob - src/modules/westley/producer_westley.c
d2acdbb7ff8e5111ba4578783d5f892c1fc76b02
[mlt] / 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
24 #include "producer_westley.h"
25 #include <framework/mlt.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <ctype.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 #define BRANCH_SIG_LEN 4000
37
38 #undef DEBUG
39 #ifdef DEBUG
40 extern xmlDocPtr westley_make_doc( mlt_service service );
41 #endif
42
43 struct deserialise_context_s
44 {
45         mlt_service stack_service[ STACK_SIZE ];
46         int stack_service_size;
47         mlt_properties producer_map;
48         mlt_properties destructors;
49         char *property;
50         mlt_properties producer_properties;
51         int is_value;
52         xmlDocPtr value_doc;
53         xmlNodePtr stack_node[ STACK_SIZE ];
54         int stack_node_size;
55         xmlDocPtr entity_doc;
56         int entity_is_replace;
57         int depth;
58         int branch[ STACK_SIZE ];
59         const xmlChar *publicId;
60         const xmlChar *systemId;
61         mlt_properties params;
62         mlt_deque filter_queue;
63         int in_producer;
64 };
65 typedef struct deserialise_context_s *deserialise_context;
66
67 /** Convert the numerical current branch address to a dot-delimited string.
68 */
69 static char *serialise_branch( deserialise_context this, char *s )
70 {
71         int i;
72         
73         s[0] = 0;
74         for ( i = 0; i < this->depth; i++ )
75         {
76                 int len = strlen( s );
77                 snprintf( s + len, BRANCH_SIG_LEN - len, "%d.", this->branch[ i ] );
78         }
79         return s;
80 }
81
82 /** Push a service.
83 */
84
85 static int context_push_service( deserialise_context this, mlt_service that )
86 {
87         int ret = this->stack_service_size >= STACK_SIZE - 1;
88         if ( ret == 0 )
89         {
90                 this->stack_service[ this->stack_service_size++ ] = that;
91                 
92                 // Record the tree branch on which this service lives
93                 if ( mlt_properties_get( mlt_service_properties( that ), "_westley_branch" ) == NULL )
94                 {
95                         char s[ BRANCH_SIG_LEN ];
96                         mlt_properties_set( mlt_service_properties( that ), "_westley_branch", serialise_branch( this, s ) );
97                 }
98         }
99         return ret;
100 }
101
102 /** Pop a service.
103 */
104
105 static mlt_service context_pop_service( deserialise_context this )
106 {
107         mlt_service result = NULL;
108         if ( this->stack_service_size > 0 )
109                 result = this->stack_service[ -- this->stack_service_size ];
110         return result;
111 }
112
113 /** Push a node.
114 */
115
116 static int context_push_node( deserialise_context this, xmlNodePtr node )
117 {
118         int ret = this->stack_node_size >= STACK_SIZE - 1;
119         if ( ret == 0 )
120                 this->stack_node[ this->stack_node_size ++ ] = node;
121         return ret;
122 }
123
124 /** Pop a node.
125 */
126
127 static xmlNodePtr context_pop_node( deserialise_context this )
128 {
129         xmlNodePtr result = NULL;
130         if ( this->stack_node_size > 0 )
131                 result = this->stack_node[ -- this->stack_node_size ];
132         return result;
133 }
134
135
136 // Set the destructor on a new service
137 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
138 {
139         int registered = mlt_properties_get_int( properties, "registered" );
140         char *key = mlt_properties_get( properties, "registered" );
141         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
142         mlt_properties_set_int( properties, "registered", ++ registered );
143 }
144
145
146 // Prepend the property value with the document root
147 static inline void qualify_property( deserialise_context context, mlt_properties properties, char *name )
148 {
149         char *resource = mlt_properties_get( properties, name );
150         if ( resource != NULL )
151         {
152                 // Qualify file name properties 
153                 char *root = mlt_properties_get( context->producer_map, "_root" );
154                 if ( root != NULL )
155                 {
156                         char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
157                         if ( resource[ 0 ] != '/' )
158                         {
159                                 strcpy( full_resource, root );
160                                 strcat( full_resource, resource );
161                         }
162                         else
163                         {
164                                 strcpy( full_resource, resource );
165                         }
166                         mlt_properties_set( properties, name, full_resource );
167                         free( full_resource );
168                 }
169         }
170 }
171
172
173 // Forward declarations
174 static void on_end_track( deserialise_context context, const xmlChar *name );
175 static void on_end_entry( deserialise_context context, const xmlChar *name );
176
177 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
178 {
179         mlt_service service = mlt_tractor_service( mlt_tractor_init() );
180         mlt_properties properties = mlt_service_properties( service );
181
182         track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
183
184         mlt_properties_set_position( properties, "length", 0 );
185
186         for ( ; atts != NULL && *atts != NULL; atts += 2 )
187                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
188
189         if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
190         {
191                 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
192                 mlt_properties_set_position( properties, "length", length );
193         }
194
195         if ( mlt_properties_get( properties, "id" ) != NULL )
196                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
197         
198         context_push_service( context, service );
199 }
200
201 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
202 {
203         mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
204         mlt_properties properties = mlt_service_properties( service );
205
206         track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
207
208         mlt_properties_set_position( properties, "length", 0 );
209
210         for ( ; atts != NULL && *atts != NULL; atts += 2 )
211                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
212
213         if ( mlt_properties_get( properties, "id" ) != NULL )
214                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
215
216         context_push_service( context, service );
217 }
218
219 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
220 {
221         mlt_service service = mlt_playlist_service( mlt_playlist_init() );
222         mlt_properties properties = mlt_service_properties( service );
223
224         track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
225
226         mlt_properties_set_position( properties, "length", 0 );
227
228         for ( ; atts != NULL && *atts != NULL; atts += 2 )
229         {
230                 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
231
232                 // Out will be overwritten later as we append, so we need to save it
233                 if ( strcmp( atts[ 0 ], "out" ) == 0 )
234                         mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
235         }
236
237         if ( mlt_properties_get( properties, "id" ) != NULL )
238                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
239
240         context_push_service( context, service );
241 }
242
243 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
244 {
245         mlt_properties properties = context->producer_properties = mlt_properties_new();
246
247         context->in_producer ++;
248
249         for ( ; atts != NULL && *atts != NULL; atts += 2 )
250         {
251                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
252         }
253 }
254
255 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
256 {
257         // Get the playlist from the stack
258         mlt_service service = context_pop_service( context );
259         mlt_position length = 0;
260         
261         if ( service == NULL )
262                 return;
263         
264         // Look for the length attribute
265         for ( ; atts != NULL && *atts != NULL; atts += 2 )
266         {
267                 if ( strcmp( atts[0], "length" ) == 0 )
268                 {
269                         length = atoll( atts[1] );
270                         break;
271                 }
272         }
273
274         // Append a blank to the playlist
275         mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
276
277         // Push the playlist back onto the stack
278         context_push_service( context, service );
279 }
280
281 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
282 {
283         // Use a dummy service to hold properties to allow arbitrary nesting
284         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
285         mlt_service_init( service, NULL );
286
287         // Push the dummy service onto the stack
288         context_push_service( context, service );
289         
290         if ( strcmp( name, "entry" ) == 0 )
291                 mlt_properties_set( mlt_service_properties( service ), "resource", "<entry>" );
292         else
293                 mlt_properties_set( mlt_service_properties( service ), "resource", "<track>" );
294         
295         for ( ; atts != NULL && *atts != NULL; atts += 2 )
296         {
297                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
298                 
299                 // Look for the producer attribute
300                 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
301                 {
302                         if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) !=  NULL )
303                                 // Push the referenced producer onto the stack
304                                 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
305                         else
306                                 // Remove the dummy service to cause end element failure
307                                 context_pop_service( context );
308                 }
309         }
310 }
311
312 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
313 {
314         mlt_properties properties = mlt_properties_new();
315
316         if ( context->in_producer != 0 )
317                 mlt_deque_push_front( context->filter_queue, context->producer_properties );
318
319         context->producer_properties = properties;
320
321         // Set the properties
322         for ( ; atts != NULL && *atts != NULL; atts += 2 )
323                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
324 }
325
326 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
327 {
328         mlt_properties properties = context->producer_properties = mlt_properties_new();
329
330         // Set the properties
331         for ( ; atts != NULL && *atts != NULL; atts += 2 )
332                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
333 }
334
335 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
336 {
337         mlt_properties properties = context->producer_properties;
338         char *value = NULL;
339
340         if ( properties == NULL )
341                 return;
342         
343         // Set the properties
344         for ( ; atts != NULL && *atts != NULL; atts += 2 )
345         {
346                 if ( strcmp( atts[ 0 ], "name" ) == 0 )
347                 {
348                         context->property = strdup( atts[ 1 ] );
349                 }
350                 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
351                 {
352                         value = (char*) atts[ 1 ];
353                 }
354         }
355
356         if ( context->property != NULL && value != NULL )
357                 mlt_properties_set( properties, context->property, value );
358         
359         // Tell parser to collect any further nodes for serialisation
360         context->is_value = 1;
361 }
362
363
364 /** This function adds a producer to a playlist or multitrack when
365     there is no entry or track element.
366 */
367
368 static int add_producer( deserialise_context context, mlt_service service, mlt_position in, mlt_position out )
369 {
370         // Get the parent producer
371         mlt_service producer = context_pop_service( context );
372
373         if ( producer != NULL )
374         {
375                 char current_branch[ BRANCH_SIG_LEN ];
376                 char *service_branch = mlt_properties_get( mlt_service_properties( producer ), "_westley_branch" );
377
378                 // Validate the producer from the stack is an ancestor and not predecessor
379                 serialise_branch( context, current_branch );
380                 if ( service_branch != NULL && strncmp( service_branch, current_branch, strlen( service_branch ) ) == 0 )
381                 {
382                         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
383                         
384                         // Put the parent producer back
385                         context_push_service( context, producer );
386                                 
387                         // If the parent producer is a multitrack or playlist (not a track or entry)
388                         if ( resource && ( strcmp( resource, "<playlist>" ) == 0 ||
389                                 strcmp( resource, "<multitrack>" ) == 0 ) )
390                         {
391 //printf( "add_producer: current branch %s service branch %s (%d)\n", current_branch, service_branch, strncmp( service_branch, current_branch, strlen( service_branch ) ) );
392                                 if ( strcmp( resource, "<playlist>" ) == 0 )
393                                 {
394                                         // Append this producer to the playlist
395                                         mlt_playlist_append_io( MLT_PLAYLIST( producer ), 
396                                                 MLT_PRODUCER( service ), in, out );
397                                 }
398                                 else
399                                 {
400                                         mlt_properties properties = mlt_service_properties( service );
401                                         
402                                         // Set this producer on the multitrack
403                                         mlt_multitrack_connect( MLT_MULTITRACK( producer ),
404                                                 MLT_PRODUCER( service ),
405                                                 mlt_multitrack_count( MLT_MULTITRACK( producer ) ) );
406                                         
407                                         // Set the hide state of the track producer
408                                         char *hide_s = mlt_properties_get( properties, "hide" );
409                                         if ( hide_s != NULL )
410                                         {
411                                                 if ( strcmp( hide_s, "video" ) == 0 )
412                                                         mlt_properties_set_int( properties, "hide", 1 );
413                                                 else if ( strcmp( hide_s, "audio" ) == 0 )
414                                                         mlt_properties_set_int( properties, "hide", 2 );
415                                                 else if ( strcmp( hide_s, "both" ) == 0 )
416                                                         mlt_properties_set_int( properties, "hide", 3 );
417                                         }
418         
419                                 }
420                                 // Normally, the enclosing entry or track will pop this service off
421                                 // In its absence we do not push it on.
422                                 return 1;
423                         }
424                 }
425         }
426         return 0;
427 }
428
429 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
430 {
431         // Get the multitrack from the stack
432         mlt_service producer = context_pop_service( context );
433         if ( producer == NULL )
434                 return;
435         
436         // Get the tractor from the stack
437         mlt_service service = context_pop_service( context );
438         
439         // Create a tractor if one does not exist
440         char *resource = NULL;
441         if ( service != NULL )
442                 resource = mlt_properties_get( mlt_service_properties( service ), "resource" );
443         if ( service == NULL || resource == NULL || strcmp( resource, "<tractor>" ) )
444         {
445 //printf("creating a tractor\n");
446                 char current_branch[ BRANCH_SIG_LEN ];
447                 
448                 // Put the anonymous service back onto the stack!
449                 if ( service != NULL )
450                         context_push_service( context, service );
451                 
452                 // Fabricate the tractor
453                 service = mlt_tractor_service( mlt_tractor_init() );
454                 track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
455                 
456                 // Inherit the producer's properties
457                 mlt_properties properties = mlt_service_properties( service );
458                 mlt_properties_set_position( properties, "length", mlt_producer_get_out( MLT_PRODUCER( producer ) ) + 1 );
459                 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
460                 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( MLT_PRODUCER( producer ) ) );
461                 
462                 mlt_properties_set( properties, "_westley_branch", serialise_branch( context, current_branch ) );
463         }
464         
465         // Connect the tractor to the multitrack
466         mlt_tractor_connect( MLT_TRACTOR( service ), producer );
467         mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
468                 MLT_MULTITRACK( producer ), 0, NULL, NULL );
469
470         // See if the tractor should be added to a playlist or multitrack
471         add_producer( context, service, 0, mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
472         
473         // Always push the multitrack back onto the stack for filters and transitions
474         context_push_service( context, producer );
475         
476         // Always push the tractor back onto the stack for filters and transitions
477         context_push_service( context, service );
478 }
479
480 static void on_end_playlist( deserialise_context context, const xmlChar *name )
481 {
482         // Get the playlist from the stack
483         mlt_service producer = context_pop_service( context );
484         if ( producer == NULL )
485                 return;
486         mlt_properties properties = mlt_service_properties( producer );
487
488         mlt_position in = mlt_properties_get_position( properties, "in" );
489         mlt_position out;
490
491         if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
492                 out = mlt_properties_get_position( properties, "_westley.out" );
493         else
494                 out = mlt_properties_get_position( properties, "length" ) - 1;
495
496         if ( mlt_properties_get_position( properties, "length" ) < out )
497                 mlt_properties_set_position( properties, "length", out  + 1 );
498
499         mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
500         
501         // See if the playlist should be added to a playlist or multitrack
502         if ( add_producer( context, producer, in, out ) == 0 )
503                 
504                 // Otherwise, push the playlist back onto the stack
505                 context_push_service( context, producer );
506 }
507
508 static void on_end_track( deserialise_context context, const xmlChar *name )
509 {
510         // Get the producer from the stack
511         mlt_service producer = context_pop_service( context );
512         if ( producer == NULL )
513                 return;
514         mlt_properties producer_props = mlt_service_properties( producer );
515
516         // See if the producer is a tractor
517         char *resource = mlt_properties_get( producer_props, "resource" );
518         if ( resource && strcmp( resource, "<tractor>" ) == 0 )
519                 // If so chomp its producer
520                 context_pop_service( context );
521
522         // Get the dummy track service from the stack
523         mlt_service track = context_pop_service( context );
524         if ( track == NULL || strcmp( mlt_properties_get( mlt_service_properties( track ), "resource" ), "<track>" ) )
525         {
526                 context_push_service( context, producer );
527                 return;
528         }
529         mlt_properties track_props = mlt_service_properties( track );
530
531         // Get the multitrack from the stack
532         mlt_service service = context_pop_service( context );
533         if ( service == NULL )
534         {
535                 context_push_service( context, producer );
536                 return;
537         }
538         
539         // Set the track on the multitrack
540         mlt_multitrack_connect( MLT_MULTITRACK( service ),
541                 MLT_PRODUCER( producer ),
542                 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
543
544         // Set producer i/o if specified
545         if ( mlt_properties_get( track_props, "in" ) != NULL ||
546                 mlt_properties_get( track_props, "out" ) != NULL )
547         {
548                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
549                         mlt_properties_get_position( track_props, "in" ),
550                         mlt_properties_get_position( track_props, "out" ) );
551         }
552         
553         // Set the hide state of the track producer
554         char *hide_s = mlt_properties_get( track_props, "hide" );
555         if ( hide_s != NULL )
556         {
557                 if ( strcmp( hide_s, "video" ) == 0 )
558                         mlt_properties_set_int( producer_props, "hide", 1 );
559                 else if ( strcmp( hide_s, "audio" ) == 0 )
560                         mlt_properties_set_int( producer_props, "hide", 2 );
561                 else if ( strcmp( hide_s, "both" ) == 0 )
562                         mlt_properties_set_int( producer_props, "hide", 3 );
563         }
564
565         // Push the multitrack back onto the stack
566         context_push_service( context, service );
567
568         mlt_service_close( track );
569 }
570
571 static void on_end_entry( deserialise_context context, const xmlChar *name )
572 {
573         // Get the producer from the stack
574         mlt_service producer = context_pop_service( context );
575         if ( producer == NULL )
576                 return;
577         
578         // See if the producer is a tractor
579         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
580         if ( resource && strcmp( resource, "<tractor>" ) == 0 )
581                 // If so chomp its producer
582                 context_pop_service( context );
583
584         // Get the dummy entry service from the stack
585         mlt_service entry = context_pop_service( context );
586         if ( entry == NULL || strcmp( mlt_properties_get( mlt_service_properties( entry ), "resource" ), "<entry>" ) )
587         {
588                 context_push_service( context, producer );
589                 return;
590         }
591
592         // Get the playlist from the stack
593         mlt_service service = context_pop_service( context );
594         if ( service == NULL )
595         {
596                 context_push_service( context, producer );
597                 return;
598         }
599
600         // Append the producer to the playlist
601         if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
602                 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
603         {
604                 mlt_playlist_append_io( MLT_PLAYLIST( service ),
605                         MLT_PRODUCER( producer ),
606                         mlt_properties_get_position( mlt_service_properties( entry ), "in" ), 
607                         mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
608         }
609         else
610         {
611                 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
612         }
613
614         // Push the playlist back onto the stack
615         context_push_service( context, service );
616
617         mlt_service_close( entry );
618 }
619
620 static void on_end_tractor( deserialise_context context, const xmlChar *name )
621 {
622         // Get the tractor
623         mlt_service tractor = context_pop_service( context );
624         if ( tractor == NULL )
625                 return;
626         
627         // Get the tractor's multitrack
628         mlt_producer multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
629         if ( multitrack != NULL )
630         {
631                 // Inherit the producer's properties
632                 mlt_properties properties = mlt_producer_properties( MLT_PRODUCER( tractor ) );
633                 mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
634                 mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
635                 mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
636         }
637
638         // See if the tractor should be added to a playlist or multitrack
639         if ( add_producer( context, tractor, 0, mlt_producer_get_out( MLT_PRODUCER( tractor ) ) ) == 0 )
640                 
641                 // Otherwise, push the tractor back onto the stack
642                 context_push_service( context, tractor );
643 }
644
645 static void on_end_property( deserialise_context context, const xmlChar *name )
646 {
647         // Tell parser to stop building a tree
648         context->is_value = 0;
649         
650         // See if there is a xml tree for the value
651         if ( context->property != NULL && context->value_doc != NULL )
652         {
653                 xmlChar *value;
654                 int size;
655                 
656                 // Serialise the tree to get value
657                 xmlDocDumpMemory( context->value_doc, &value, &size );
658                 mlt_properties_set( context->producer_properties, context->property, value );
659                 xmlFree( value );
660                 xmlFreeDoc( context->value_doc );
661                 context->value_doc = NULL;
662         }
663         
664         // Close this property handling
665         free( context->property );
666         context->property = NULL;
667 }
668
669 static void on_end_producer( deserialise_context context, const xmlChar *name )
670 {
671         mlt_properties properties = context->producer_properties;
672         mlt_service service = NULL;
673         
674         context->in_producer --;
675
676         if ( properties == NULL )
677                 return;
678                 
679         qualify_property( context, properties, "resource" );
680         char *resource = mlt_properties_get( properties, "resource" );
681         // Let Kino-SMIL src be a synonym for resource
682         if ( resource == NULL )
683         {
684                 qualify_property( context, properties, "src" );
685                 resource = mlt_properties_get( properties, "src" );
686         }
687
688         // Instantiate the producer
689         if ( mlt_properties_get( properties, "mlt_service" ) != NULL )
690         {
691                 char temp[ 1024 ];
692                 strncpy( temp, mlt_properties_get( properties, "mlt_service" ), 1024 );
693                 if ( resource != NULL )
694                 {
695                         strcat( temp, ":" );
696                         strncat( temp, resource, 1023 - strlen( temp ) );
697                 }
698                 service = MLT_SERVICE( mlt_factory_producer( "fezzik", temp ) );
699         }
700         if ( service == NULL && resource != NULL )
701         {
702                 service = MLT_SERVICE( mlt_factory_producer( "fezzik", resource ) );
703         }
704         
705         if ( service == NULL )
706                 return;
707         track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
708
709         // Add the producer to the producer map
710         if ( mlt_properties_get( properties, "id" ) != NULL )
711                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
712
713         // Handle in/out properties separately
714         mlt_position in = -1;
715         mlt_position out = -1;
716         
717         // Get in
718         if ( mlt_properties_get( properties, "in" ) != NULL )
719                 in = mlt_properties_get_position( properties, "in" );
720         // Let Kino-SMIL clipBegin be a synonym for in
721         if ( mlt_properties_get( properties, "clipBegin" ) != NULL )
722                 in = mlt_properties_get_position( properties, "clipBegin" );
723         // Get out
724         if ( mlt_properties_get( properties, "out" ) != NULL )
725                 out = mlt_properties_get_position( properties, "out" );
726         // Let Kino-SMIL clipEnd be a synonym for out
727         if ( mlt_properties_get( properties, "clipEnd" ) != NULL )
728                 out = mlt_properties_get_position( properties, "clipEnd" );
729         
730         // Remove in and out
731         mlt_properties_set( properties, "in", NULL );
732         mlt_properties_set( properties, "out", NULL );
733         
734         mlt_properties_inherit( mlt_service_properties( service ), properties );
735         mlt_properties_close( properties );
736         context->producer_properties = NULL;
737
738         // See if the producer should be added to a playlist or multitrack
739         if ( add_producer( context, service, in, out ) == 0 )
740         {
741                 // Otherwise, set in and out on...
742                 if ( in != -1 || out != -1 )
743                 {
744                         // Get the parent service
745                         mlt_service parent = context_pop_service( context );
746                         if ( parent != NULL )
747                         {
748                                 // Get the parent properties
749                                 properties = mlt_service_properties( parent );
750                                 
751                                 char *resource = mlt_properties_get( properties, "resource" );
752                                 
753                                 // Put the parent producer back
754                                 context_push_service( context, parent );
755                                         
756                                 // If the parent is a track or entry
757                                 if ( resource && ( strcmp( resource, "<entry>" ) == 0 ) )
758                                 {
759                                         mlt_properties_set_position( properties, "in", in );
760                                         mlt_properties_set_position( properties, "out", out );
761                                 }
762                                 else
763                                 {
764                                         // Otherwise, set in and out on producer directly
765                                         mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
766                                 }
767                         }
768                         else
769                         {
770                                 // Otherwise, set in and out on producer directly
771                                 mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
772                         }
773                 }
774         
775                 // Allow for embedded filters
776                 while( mlt_deque_count( context->filter_queue ) )
777                 {
778                         mlt_properties filter_properties = mlt_deque_pop_front( context->filter_queue );
779                         mlt_filter filter = mlt_factory_filter( mlt_properties_get( filter_properties, "mlt_service" ), NULL );
780                         if ( filter != NULL )
781                         {
782                                 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
783                                 qualify_property( context, filter_properties, "resource" );
784                                 qualify_property( context, filter_properties, "luma" );
785                                 qualify_property( context, filter_properties, "luma.resource" );
786                                 qualify_property( context, filter_properties, "composite.luma" );
787                                 qualify_property( context, filter_properties, "producer.resource" );
788                                 mlt_properties_inherit( mlt_filter_properties( filter ), filter_properties );
789                                 mlt_properties_close( filter_properties );
790                                 mlt_producer_attach( MLT_PRODUCER( service ), filter );
791                         }
792                 }
793
794                 // Push the producer onto the stack
795                 context_push_service( context, service );
796         }
797 }
798
799 static void on_end_filter( deserialise_context context, const xmlChar *name )
800 {
801         mlt_properties properties = context->producer_properties;
802         if ( context->in_producer )
803         {
804                 mlt_deque_push_back( context->filter_queue, properties );
805                 context->producer_properties = mlt_deque_pop_front( context->filter_queue );
806                 return;
807         }
808
809         if ( properties == NULL )
810                 return;
811
812         char *id;
813         char key[11];
814         key[ 10 ] = '\0';
815         mlt_service tractor = NULL;
816         
817         // Get the producer from the stack
818         mlt_service producer = context_pop_service( context );
819         if ( producer == NULL )
820                 return;
821         
822         // See if the producer is a tractor
823         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
824         if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
825         {
826                 // If so, then get the next producer
827                 tractor = producer;
828                 producer = context_pop_service( context );
829         }
830         
831 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
832
833         // Create the filter
834         mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
835         if ( service == NULL )
836         {
837                 context_push_service( context, producer );
838                 return;
839         }
840         track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
841
842         // Connect the filter to the producer
843         mlt_filter_connect( MLT_FILTER( service ), producer,
844                 mlt_properties_get_int( properties, "track" ) );
845
846         // Set in and out from producer if non existant
847         if ( mlt_properties_get( properties, "in" ) == NULL )
848                 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
849         if ( mlt_properties_get( properties, "out" ) == NULL )
850                 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
851
852         // Propogate the properties
853         qualify_property( context, properties, "resource" );
854         qualify_property( context, properties, "luma" );
855         qualify_property( context, properties, "luma.resource" );
856         qualify_property( context, properties, "composite.luma" );
857         qualify_property( context, properties, "producer.resource" );
858         mlt_properties_inherit( mlt_service_properties( service ), properties );
859         mlt_properties_close( properties );
860         context->producer_properties = NULL;
861         properties = mlt_service_properties( service );
862
863         // Set in and out again due to inheritance
864         mlt_filter_set_in_and_out( MLT_FILTER( service ), 
865                 mlt_properties_get_position( properties, "in" ),
866                 mlt_properties_get_position( properties, "out" ) );
867
868         // If a producer alias is in the producer_map, get it
869         snprintf( key, 10, "%p", producer );
870         if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
871                 producer = mlt_properties_get_data( context->producer_map, key, NULL );
872
873         // Put the producer in the producer map
874         id = mlt_properties_get( mlt_service_properties( producer ), "id" );
875         if ( id != NULL )
876                 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
877
878         // For filter chain support, add an alias to the producer map
879         snprintf( key, 10, "%p", service );
880         mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
881         
882         // Push the filter onto the stack
883         context_push_service( context, service );
884         
885         if ( tractor != NULL )
886         {
887                 // Connect the tractor to the filter
888                 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
889
890                 // Push the tractor back onto the stack
891                 context_push_service( context, tractor );
892         }
893 }
894
895 static void on_end_transition( deserialise_context context, const xmlChar *name )
896 {
897         mlt_service tractor = NULL;
898         mlt_properties properties = context->producer_properties;
899         if ( properties == NULL )
900                 return;
901
902         // Get the producer from the stack
903         mlt_service producer = context_pop_service( context );
904         if ( producer == NULL )
905                 return;
906
907         // See if the producer is a tractor
908         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
909         if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
910         {
911                 // If so, then get the next producer
912                 tractor = producer;
913                 producer = context_pop_service( context );
914         }
915         
916         // Create the transition
917         mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
918         if ( service == NULL )
919         {
920                 context_push_service( context, producer );
921                 return;
922         }
923         track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
924
925         // Propogate the properties
926         qualify_property( context, properties, "resource" );
927         qualify_property( context, properties, "luma" );
928         qualify_property( context, properties, "luma.resource" );
929         qualify_property( context, properties, "composite.luma" );
930         qualify_property( context, properties, "producer.resource" );
931         mlt_properties_inherit( mlt_service_properties( service ), properties );
932         mlt_properties_close( properties );
933         context->producer_properties = NULL;
934         properties = mlt_service_properties( service );
935
936         // Set in and out again due to inheritance
937         mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
938                 mlt_properties_get_position( properties, "in" ),
939                 mlt_properties_get_position( properties, "out" ) );
940
941         // Connect the transition to the producer
942         mlt_transition_connect( MLT_TRANSITION( service ), producer,
943                 mlt_properties_get_int( properties, "a_track" ),
944                 mlt_properties_get_int( properties, "b_track" ) );
945
946         // Push the transition onto the stack
947         context_push_service( context, service );
948         
949         if ( tractor != NULL )
950         {
951                 // Connect the tractor to the transition
952                 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
953
954                 // Push the tractor back onto the stack
955                 context_push_service( context, tractor );
956         }
957 }
958
959 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
960 {
961         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
962         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
963         
964 //printf("on_start_element: %s\n", name );
965         context->branch[ context->depth ] ++;
966         context->depth ++;
967         
968         // Build a tree from nodes within a property value
969         if ( context->is_value == 1 )
970         {
971                 xmlNodePtr node = xmlNewNode( NULL, name );
972                 
973                 if ( context->value_doc == NULL )
974                 {
975                         // Start a new tree
976                         context->value_doc = xmlNewDoc( "1.0" );
977                         xmlDocSetRootElement( context->value_doc, node );
978                 }
979                 else
980                 {
981                         // Append child to tree
982                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
983                 }
984                 context_push_node( context, node );
985                 
986                 // Set the attributes
987                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
988                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
989         }
990         else if ( strcmp( name, "tractor" ) == 0 )
991                 on_start_tractor( context, name, atts );
992         else if ( strcmp( name, "multitrack" ) == 0 )
993                 on_start_multitrack( context, name, atts );
994         else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
995                 on_start_playlist( context, name, atts );
996         else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
997                 on_start_producer( context, name, atts );
998         else if ( strcmp( name, "blank" ) == 0 )
999                 on_start_blank( context, name, atts );
1000         else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
1001                 on_start_entry_track( context, name, atts );
1002         else if ( strcmp( name, "filter" ) == 0 )
1003                 on_start_filter( context, name, atts );
1004         else if ( strcmp( name, "transition" ) == 0 )
1005                 on_start_transition( context, name, atts );
1006         else if ( strcmp( name, "property" ) == 0 )
1007                 on_start_property( context, name, atts );
1008 }
1009
1010 static void on_end_element( void *ctx, const xmlChar *name )
1011 {
1012         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1013         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1014         
1015 //printf("on_end_element: %s\n", name );
1016         if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
1017                 context_pop_node( context );
1018         else if ( strcmp( name, "multitrack" ) == 0 )
1019                 on_end_multitrack( context, name );
1020         else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
1021                 on_end_playlist( context, name );
1022         else if ( strcmp( name, "track" ) == 0 )
1023                 on_end_track( context, name );
1024         else if ( strcmp( name, "entry" ) == 0 )
1025                 on_end_entry( context, name );
1026         else if ( strcmp( name, "tractor" ) == 0 )
1027                 on_end_tractor( context, name );
1028         else if ( strcmp( name, "property" ) == 0 )
1029                 on_end_property( context, name );
1030         else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
1031                 on_end_producer( context, name );
1032         else if ( strcmp( name, "filter" ) == 0 )
1033                 on_end_filter( context, name );
1034         else if ( strcmp( name, "transition" ) == 0 )
1035                 on_end_transition( context, name );
1036
1037         context->branch[ context->depth ] = 0;
1038         context->depth --;
1039 }
1040
1041 static void on_characters( void *ctx, const xmlChar *ch, int len )
1042 {
1043         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1044         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1045         char *value = calloc( len + 1, 1 );
1046
1047         value[ len ] = 0;
1048         strncpy( value, (const char*) ch, len );
1049
1050         if ( context->stack_node_size > 0 )
1051                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1052
1053         // libxml2 generates an on_characters immediately after a get_entity within
1054         // an element value, and we ignore it because it is called again during
1055         // actual substitution.
1056         else if ( context->property != NULL && context->producer_properties != NULL
1057                 && context->entity_is_replace == 0 )
1058         {
1059                 char *s = mlt_properties_get( context->producer_properties, context->property );
1060                 if ( s != NULL )
1061                 {
1062                         // Append new text to existing content
1063                         char *new = calloc( strlen( s ) + len + 1, 1 );
1064                         strcat( new, s );
1065                         strcat( new, value );
1066                         mlt_properties_set( context->producer_properties, context->property, new );
1067                         free( new );
1068                 }
1069                 else
1070                         mlt_properties_set( context->producer_properties, context->property, value );
1071         }
1072         context->entity_is_replace = 0;
1073                 
1074         free( value);
1075 }
1076
1077 /** Convert parameters parsed from resource into entity declarations.
1078 */
1079 static void params_to_entities( deserialise_context context )
1080 {
1081         if ( context->params != NULL )
1082         {       
1083                 int i;
1084                 
1085                 // Add our params as entitiy declarations
1086                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1087                 {
1088                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1089                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1090                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, name ) );
1091                 }
1092
1093                 // Flag completion
1094                 mlt_properties_close( context->params );
1095                 context->params = NULL;
1096         }
1097 }
1098
1099 // The following 3 facilitate entity substitution in the SAX parser
1100 static void on_internal_subset( void *ctx, const xmlChar* name,
1101         const xmlChar* publicId, const xmlChar* systemId )
1102 {
1103         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1104         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1105         
1106         context->publicId = publicId;
1107         context->systemId = systemId;
1108         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1109         
1110         // Override default entities with our parameters
1111         params_to_entities( context );
1112 }
1113
1114 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1115         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1116 {
1117         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1118         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1119         
1120         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1121 }
1122
1123 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1124 {
1125         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1126         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1127         xmlEntityPtr e = NULL;
1128
1129         // Setup for entity declarations if not ready
1130         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1131         {
1132                 xmlCreateIntSubset( context->entity_doc, "westley", "", "" );
1133                 context->publicId = "";
1134                 context->systemId = "";
1135         }
1136
1137         // Add our parameters if not already
1138         params_to_entities( context );
1139         
1140         e = xmlGetDocEntity( context->entity_doc, name );
1141         
1142         // Send signal to on_characters that an entity substitutin is pending
1143         if ( e != NULL )
1144                 context->entity_is_replace = 1;
1145         
1146         return e;
1147 }
1148
1149 /** Convert a hexadecimal character to its value.
1150 */
1151 static int tohex( char p )
1152 {
1153         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1154 }
1155
1156 /** Decode a url-encoded string containing hexadecimal character sequences.
1157 */
1158 static char *url_decode( char *dest, char *src )
1159 {
1160         char *p = dest;
1161         
1162         while ( *src )
1163         {
1164                 if ( *src == '%' )
1165                 {
1166                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1167                         src += 3;
1168                 }
1169                 else
1170                 {
1171                         *p ++ = *src ++;
1172                 }
1173         }
1174
1175         *p = *src;
1176         return dest;
1177 }
1178
1179 /** Extract the filename from a URL attaching parameters to a properties list.
1180 */
1181 static void parse_url( mlt_properties properties, char *url )
1182 {
1183         int i;
1184         int n = strlen( url );
1185         char *name = NULL;
1186         char *value = NULL;
1187         
1188         for ( i = 0; i < n; i++ )
1189         {
1190                 switch ( url[ i ] )
1191                 {
1192                         case '?':
1193                                 url[ i++ ] = '\0';
1194                                 name = &url[ i ];
1195                                 break;
1196                         
1197                         case ':':
1198                         case '=':
1199                                 url[ i++ ] = '\0';
1200                                 value = &url[ i ];
1201                                 break;
1202                         
1203                         case '&':
1204                                 url[ i++ ] = '\0';
1205                                 if ( name != NULL && value != NULL )
1206                                         mlt_properties_set( properties, name, value );
1207                                 name = &url[ i ];
1208                                 value = NULL;
1209                                 break;
1210                 }
1211         }
1212         if ( name != NULL && value != NULL )
1213                 mlt_properties_set( properties, name, value );
1214 }
1215
1216 mlt_producer producer_westley_init( char *url )
1217 {
1218         if ( url == NULL )
1219                 return NULL;
1220         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1221         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1222         mlt_properties properties = NULL;
1223         int i = 0;
1224         struct _xmlParserCtxt *xmlcontext;
1225         int well_formed = 0;
1226         char *filename = strdup( url );
1227         
1228         context->producer_map = mlt_properties_new();
1229         context->destructors = mlt_properties_new();
1230         context->params = mlt_properties_new();
1231         context->filter_queue = mlt_deque_init();
1232
1233         // Decode URL and parse parameters      
1234         parse_url( context->params, url_decode( filename, url ) );
1235
1236         // We need to track the number of registered filters
1237         mlt_properties_set_int( context->destructors, "registered", 0 );
1238
1239         // We need the directory prefix which was used for the westley
1240         mlt_properties_set( context->producer_map, "_root", "" );
1241         if ( strchr( filename, '/' ) )
1242         {
1243                 char *root = NULL;
1244                 mlt_properties_set( context->producer_map, "_root", filename );
1245                 root = mlt_properties_get( context->producer_map, "_root" );
1246                 *( strrchr( root, '/' ) + 1 ) = '\0';
1247         }
1248
1249         // Setup SAX callbacks
1250         sax->startElement = on_start_element;
1251         sax->endElement = on_end_element;
1252         sax->characters = on_characters;
1253         sax->cdataBlock = on_characters;
1254         sax->internalSubset = on_internal_subset;
1255         sax->entityDecl = on_entity_declaration;
1256         sax->getEntity = on_get_entity;
1257
1258         // Setup libxml2 SAX parsing
1259         xmlInitParser(); 
1260         xmlSubstituteEntitiesDefault( 1 );
1261         // This is used to facilitate entity substitution in the SAX parser
1262         context->entity_doc = xmlNewDoc( "1.0" );
1263         xmlcontext = xmlCreateFileParserCtxt( filename );
1264         xmlcontext->sax = sax;
1265         xmlcontext->_private = ( void* )context;
1266         
1267         // Parse
1268         xmlParseDocument( xmlcontext );
1269         well_formed = xmlcontext->wellFormed;
1270         
1271         // Cleanup after parsing
1272         xmlFreeDoc( context->entity_doc );
1273         free( sax );
1274         xmlcontext->sax = NULL;
1275         xmlcontext->_private = NULL;
1276         xmlFreeParserCtxt( xmlcontext );
1277         xmlMemoryDump( ); // for debugging
1278
1279         // Get the last producer on the stack
1280         mlt_service service = context_pop_service( context );
1281         if ( well_formed && service != NULL )
1282         {
1283                 // Verify it is a producer service (mlt_type="mlt_producer")
1284                 // (producer, playlist, multitrack)
1285                 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
1286                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1287                         service = NULL;
1288         }
1289
1290 #ifdef DEBUG
1291         xmlDocPtr doc = westley_make_doc( service );
1292         xmlDocFormatDump( stdout, doc, 1 );
1293         xmlFreeDoc( doc );
1294         service = NULL;
1295 #endif
1296         
1297         if ( well_formed && service != NULL )
1298         {
1299                 
1300                 // Need the complete producer list for various reasons
1301                 properties = context->destructors;
1302
1303                 // Now make sure we don't have a reference to the service in the properties
1304                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1305                 {
1306                         char *name = mlt_properties_get_name( properties, i );
1307                         if ( mlt_properties_get_data( properties, name, NULL ) == service )
1308                         {
1309                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1310                                 break;
1311                         }
1312                 }
1313
1314                 // We are done referencing destructor property list
1315                 // Set this var to service properties for convenience
1316                 properties = mlt_service_properties( service );
1317         
1318                 // make the returned service destroy the connected services
1319                 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1320
1321                 // Now assign additional properties
1322                 mlt_properties_set( properties, "resource", url );
1323
1324                 // This tells consumer_westley not to deep copy
1325                 mlt_properties_set( properties, "westley", "was here" );
1326         }
1327         else
1328         {
1329                 // Return null if not well formed
1330                 service = NULL;
1331                 
1332                 // Clean up
1333                 mlt_properties_close( context->destructors );
1334         }
1335
1336         // Clean up
1337         mlt_properties_close( context->producer_map );
1338         if ( context->params != NULL )
1339                 mlt_properties_close( context->params );
1340         mlt_deque_close( context->filter_queue );
1341         free( context );
1342         free( filename );
1343
1344         return MLT_PRODUCER( service );
1345 }