]> git.sesse.net Git - mlt/blob - src/modules/westley/producer_westley.c
Producer filter attach/detach methods; major rework on westley consumer, minor on...
[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_properties_debug( filter_properties, "Filter?", stderr );
780                         mlt_filter filter = mlt_factory_filter( mlt_properties_get( filter_properties, "mlt_service" ), NULL );
781                         if ( filter != NULL )
782                         {
783                                 track_service( context->destructors, filter, (mlt_destructor) mlt_filter_close );
784                                 qualify_property( context, filter_properties, "resource" );
785                                 qualify_property( context, filter_properties, "luma" );
786                                 qualify_property( context, filter_properties, "luma.resource" );
787                                 qualify_property( context, filter_properties, "composite.luma" );
788                                 qualify_property( context, filter_properties, "producer.resource" );
789                                 mlt_properties_inherit( mlt_filter_properties( filter ), filter_properties );
790                                 mlt_properties_close( filter_properties );
791                                 mlt_producer_attach( MLT_PRODUCER( service ), filter );
792                         }
793                 }
794
795                 // Push the producer onto the stack
796                 context_push_service( context, service );
797         }
798 }
799
800 static void on_end_filter( deserialise_context context, const xmlChar *name )
801 {
802         mlt_properties properties = context->producer_properties;
803         if ( context->in_producer )
804         {
805                 mlt_deque_push_back( context->filter_queue, properties );
806                 context->producer_properties = mlt_deque_pop_front( context->filter_queue );
807                 return;
808         }
809
810         if ( properties == NULL )
811                 return;
812
813         char *id;
814         char key[11];
815         key[ 10 ] = '\0';
816         mlt_service tractor = NULL;
817         
818         // Get the producer from the stack
819         mlt_service producer = context_pop_service( context );
820         if ( producer == NULL )
821                 return;
822         
823         // See if the producer is a tractor
824         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
825         if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
826         {
827                 // If so, then get the next producer
828                 tractor = producer;
829                 producer = context_pop_service( context );
830         }
831         
832 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
833
834         // Create the filter
835         mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
836         if ( service == NULL )
837         {
838                 context_push_service( context, producer );
839                 return;
840         }
841         track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
842
843         // Connect the filter to the producer
844         mlt_filter_connect( MLT_FILTER( service ), producer,
845                 mlt_properties_get_int( properties, "track" ) );
846
847         // Set in and out from producer if non existant
848         if ( mlt_properties_get( properties, "in" ) == NULL )
849                 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
850         if ( mlt_properties_get( properties, "out" ) == NULL )
851                 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
852
853         // Propogate the properties
854         qualify_property( context, properties, "resource" );
855         qualify_property( context, properties, "luma" );
856         qualify_property( context, properties, "luma.resource" );
857         qualify_property( context, properties, "composite.luma" );
858         qualify_property( context, properties, "producer.resource" );
859         mlt_properties_inherit( mlt_service_properties( service ), properties );
860         mlt_properties_close( properties );
861         context->producer_properties = NULL;
862         properties = mlt_service_properties( service );
863
864         // Set in and out again due to inheritance
865         mlt_filter_set_in_and_out( MLT_FILTER( service ), 
866                 mlt_properties_get_position( properties, "in" ),
867                 mlt_properties_get_position( properties, "out" ) );
868
869         // If a producer alias is in the producer_map, get it
870         snprintf( key, 10, "%p", producer );
871         if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
872                 producer = mlt_properties_get_data( context->producer_map, key, NULL );
873
874         // Put the producer in the producer map
875         id = mlt_properties_get( mlt_service_properties( producer ), "id" );
876         if ( id != NULL )
877                 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
878
879         // For filter chain support, add an alias to the producer map
880         snprintf( key, 10, "%p", service );
881         mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
882         
883         // Push the filter onto the stack
884         context_push_service( context, service );
885         
886         if ( tractor != NULL )
887         {
888                 // Connect the tractor to the filter
889                 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
890
891                 // Push the tractor back onto the stack
892                 context_push_service( context, tractor );
893         }
894 }
895
896 static void on_end_transition( deserialise_context context, const xmlChar *name )
897 {
898         mlt_service tractor = NULL;
899         mlt_properties properties = context->producer_properties;
900         if ( properties == NULL )
901                 return;
902
903         // Get the producer from the stack
904         mlt_service producer = context_pop_service( context );
905         if ( producer == NULL )
906                 return;
907
908         // See if the producer is a tractor
909         char *resource = mlt_properties_get( mlt_service_properties( producer ), "resource" );
910         if ( resource != NULL && strcmp( resource, "<tractor>" ) == 0 )
911         {
912                 // If so, then get the next producer
913                 tractor = producer;
914                 producer = context_pop_service( context );
915         }
916         
917         // Create the transition
918         mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
919         if ( service == NULL )
920         {
921                 context_push_service( context, producer );
922                 return;
923         }
924         track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
925
926         // Propogate the properties
927         qualify_property( context, properties, "resource" );
928         qualify_property( context, properties, "luma" );
929         qualify_property( context, properties, "luma.resource" );
930         qualify_property( context, properties, "composite.luma" );
931         qualify_property( context, properties, "producer.resource" );
932         mlt_properties_inherit( mlt_service_properties( service ), properties );
933         mlt_properties_close( properties );
934         context->producer_properties = NULL;
935         properties = mlt_service_properties( service );
936
937         // Set in and out again due to inheritance
938         mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
939                 mlt_properties_get_position( properties, "in" ),
940                 mlt_properties_get_position( properties, "out" ) );
941
942         // Connect the transition to the producer
943         mlt_transition_connect( MLT_TRANSITION( service ), producer,
944                 mlt_properties_get_int( properties, "a_track" ),
945                 mlt_properties_get_int( properties, "b_track" ) );
946
947         // Push the transition onto the stack
948         context_push_service( context, service );
949         
950         if ( tractor != NULL )
951         {
952                 // Connect the tractor to the transition
953                 mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
954
955                 // Push the tractor back onto the stack
956                 context_push_service( context, tractor );
957         }
958 }
959
960 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
961 {
962         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
963         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
964         
965 //printf("on_start_element: %s\n", name );
966         context->branch[ context->depth ] ++;
967         context->depth ++;
968         
969         // Build a tree from nodes within a property value
970         if ( context->is_value == 1 )
971         {
972                 xmlNodePtr node = xmlNewNode( NULL, name );
973                 
974                 if ( context->value_doc == NULL )
975                 {
976                         // Start a new tree
977                         context->value_doc = xmlNewDoc( "1.0" );
978                         xmlDocSetRootElement( context->value_doc, node );
979                 }
980                 else
981                 {
982                         // Append child to tree
983                         xmlAddChild( context->stack_node[ context->stack_node_size - 1 ], node );
984                 }
985                 context_push_node( context, node );
986                 
987                 // Set the attributes
988                 for ( ; atts != NULL && *atts != NULL; atts += 2 )
989                         xmlSetProp( node, atts[ 0 ], atts[ 1 ] );
990         }
991         else if ( strcmp( name, "tractor" ) == 0 )
992                 on_start_tractor( context, name, atts );
993         else if ( strcmp( name, "multitrack" ) == 0 )
994                 on_start_multitrack( context, name, atts );
995         else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
996                 on_start_playlist( context, name, atts );
997         else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
998                 on_start_producer( context, name, atts );
999         else if ( strcmp( name, "blank" ) == 0 )
1000                 on_start_blank( context, name, atts );
1001         else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
1002                 on_start_entry_track( context, name, atts );
1003         else if ( strcmp( name, "filter" ) == 0 )
1004                 on_start_filter( context, name, atts );
1005         else if ( strcmp( name, "transition" ) == 0 )
1006                 on_start_transition( context, name, atts );
1007         else if ( strcmp( name, "property" ) == 0 )
1008                 on_start_property( context, name, atts );
1009 }
1010
1011 static void on_end_element( void *ctx, const xmlChar *name )
1012 {
1013         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1014         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1015         
1016 //printf("on_end_element: %s\n", name );
1017         if ( context->is_value == 1 && strcmp( name, "property" ) != 0 )
1018                 context_pop_node( context );
1019         else if ( strcmp( name, "multitrack" ) == 0 )
1020                 on_end_multitrack( context, name );
1021         else if ( strcmp( name, "playlist" ) == 0 || strcmp( name, "seq" ) == 0 || strcmp( name, "smil" ) == 0 )
1022                 on_end_playlist( context, name );
1023         else if ( strcmp( name, "track" ) == 0 )
1024                 on_end_track( context, name );
1025         else if ( strcmp( name, "entry" ) == 0 )
1026                 on_end_entry( context, name );
1027         else if ( strcmp( name, "tractor" ) == 0 )
1028                 on_end_tractor( context, name );
1029         else if ( strcmp( name, "property" ) == 0 )
1030                 on_end_property( context, name );
1031         else if ( strcmp( name, "producer" ) == 0 || strcmp( name, "video" ) == 0 )
1032                 on_end_producer( context, name );
1033         else if ( strcmp( name, "filter" ) == 0 )
1034                 on_end_filter( context, name );
1035         else if ( strcmp( name, "transition" ) == 0 )
1036                 on_end_transition( context, name );
1037
1038         context->branch[ context->depth ] = 0;
1039         context->depth --;
1040 }
1041
1042 static void on_characters( void *ctx, const xmlChar *ch, int len )
1043 {
1044         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1045         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1046         char *value = calloc( len + 1, 1 );
1047
1048         value[ len ] = 0;
1049         strncpy( value, (const char*) ch, len );
1050
1051         if ( context->stack_node_size > 0 )
1052                 xmlNodeAddContent( context->stack_node[ context->stack_node_size - 1 ], ( xmlChar* )value );
1053
1054         // libxml2 generates an on_characters immediately after a get_entity within
1055         // an element value, and we ignore it because it is called again during
1056         // actual substitution.
1057         else if ( context->property != NULL && context->producer_properties != NULL
1058                 && context->entity_is_replace == 0 )
1059         {
1060                 char *s = mlt_properties_get( context->producer_properties, context->property );
1061                 if ( s != NULL )
1062                 {
1063                         // Append new text to existing content
1064                         char *new = calloc( strlen( s ) + len + 1, 1 );
1065                         strcat( new, s );
1066                         strcat( new, value );
1067                         mlt_properties_set( context->producer_properties, context->property, new );
1068                         free( new );
1069                 }
1070                 else
1071                         mlt_properties_set( context->producer_properties, context->property, value );
1072         }
1073         context->entity_is_replace = 0;
1074                 
1075         free( value);
1076 }
1077
1078 /** Convert parameters parsed from resource into entity declarations.
1079 */
1080 static void params_to_entities( deserialise_context context )
1081 {
1082         if ( context->params != NULL )
1083         {       
1084                 int i;
1085                 
1086                 // Add our params as entitiy declarations
1087                 for ( i = 0; i < mlt_properties_count( context->params ); i++ )
1088                 {
1089                         xmlChar *name = ( xmlChar* )mlt_properties_get_name( context->params, i );
1090                         xmlAddDocEntity( context->entity_doc, name, XML_INTERNAL_GENERAL_ENTITY,
1091                                 context->publicId, context->systemId, ( xmlChar* )mlt_properties_get( context->params, name ) );
1092                 }
1093
1094                 // Flag completion
1095                 mlt_properties_close( context->params );
1096                 context->params = NULL;
1097         }
1098 }
1099
1100 // The following 3 facilitate entity substitution in the SAX parser
1101 static void on_internal_subset( void *ctx, const xmlChar* name,
1102         const xmlChar* publicId, const xmlChar* systemId )
1103 {
1104         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1105         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1106         
1107         context->publicId = publicId;
1108         context->systemId = systemId;
1109         xmlCreateIntSubset( context->entity_doc, name, publicId, systemId );
1110         
1111         // Override default entities with our parameters
1112         params_to_entities( context );
1113 }
1114
1115 static void on_entity_declaration( void *ctx, const xmlChar* name, int type, 
1116         const xmlChar* publicId, const xmlChar* systemId, xmlChar* content)
1117 {
1118         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1119         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1120         
1121         xmlAddDocEntity( context->entity_doc, name, type, publicId, systemId, content );
1122 }
1123
1124 xmlEntityPtr on_get_entity( void *ctx, const xmlChar* name )
1125 {
1126         struct _xmlParserCtxt *xmlcontext = ( struct _xmlParserCtxt* )ctx;
1127         deserialise_context context = ( deserialise_context )( xmlcontext->_private );
1128         xmlEntityPtr e = NULL;
1129
1130         // Setup for entity declarations if not ready
1131         if ( xmlGetIntSubset( context->entity_doc ) == NULL )
1132         {
1133                 xmlCreateIntSubset( context->entity_doc, "westley", "", "" );
1134                 context->publicId = "";
1135                 context->systemId = "";
1136         }
1137
1138         // Add our parameters if not already
1139         params_to_entities( context );
1140         
1141         e = xmlGetDocEntity( context->entity_doc, name );
1142         
1143         // Send signal to on_characters that an entity substitutin is pending
1144         if ( e != NULL )
1145                 context->entity_is_replace = 1;
1146         
1147         return e;
1148 }
1149
1150 /** Convert a hexadecimal character to its value.
1151 */
1152 static int tohex( char p )
1153 {
1154         return isdigit( p ) ? p - '0' : tolower( p ) - 'a' + 10;
1155 }
1156
1157 /** Decode a url-encoded string containing hexadecimal character sequences.
1158 */
1159 static char *url_decode( char *dest, char *src )
1160 {
1161         char *p = dest;
1162         
1163         while ( *src )
1164         {
1165                 if ( *src == '%' )
1166                 {
1167                         *p ++ = ( tohex( *( src + 1 ) ) << 4 ) | tohex( *( src + 2 ) );
1168                         src += 3;
1169                 }
1170                 else
1171                 {
1172                         *p ++ = *src ++;
1173                 }
1174         }
1175
1176         *p = *src;
1177         return dest;
1178 }
1179
1180 /** Extract the filename from a URL attaching parameters to a properties list.
1181 */
1182 static void parse_url( mlt_properties properties, char *url )
1183 {
1184         int i;
1185         int n = strlen( url );
1186         char *name = NULL;
1187         char *value = NULL;
1188         
1189         for ( i = 0; i < n; i++ )
1190         {
1191                 switch ( url[ i ] )
1192                 {
1193                         case '?':
1194                                 url[ i++ ] = '\0';
1195                                 name = &url[ i ];
1196                                 break;
1197                         
1198                         case ':':
1199                         case '=':
1200                                 url[ i++ ] = '\0';
1201                                 value = &url[ i ];
1202                                 break;
1203                         
1204                         case '&':
1205                                 url[ i++ ] = '\0';
1206                                 if ( name != NULL && value != NULL )
1207                                         mlt_properties_set( properties, name, value );
1208                                 name = &url[ i ];
1209                                 value = NULL;
1210                                 break;
1211                 }
1212         }
1213         if ( name != NULL && value != NULL )
1214                 mlt_properties_set( properties, name, value );
1215 }
1216
1217 mlt_producer producer_westley_init( char *url )
1218 {
1219         if ( url == NULL )
1220                 return NULL;
1221         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
1222         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
1223         mlt_properties properties = NULL;
1224         int i = 0;
1225         struct _xmlParserCtxt *xmlcontext;
1226         int well_formed = 0;
1227         char *filename = strdup( url );
1228         
1229         context->producer_map = mlt_properties_new();
1230         context->destructors = mlt_properties_new();
1231         context->params = mlt_properties_new();
1232         context->filter_queue = mlt_deque_init();
1233
1234         // Decode URL and parse parameters      
1235         parse_url( context->params, url_decode( filename, url ) );
1236
1237         // We need to track the number of registered filters
1238         mlt_properties_set_int( context->destructors, "registered", 0 );
1239
1240         // We need the directory prefix which was used for the westley
1241         mlt_properties_set( context->producer_map, "_root", "" );
1242         if ( strchr( filename, '/' ) )
1243         {
1244                 char *root = NULL;
1245                 mlt_properties_set( context->producer_map, "_root", filename );
1246                 root = mlt_properties_get( context->producer_map, "_root" );
1247                 *( strrchr( root, '/' ) + 1 ) = '\0';
1248         }
1249
1250         // Setup SAX callbacks
1251         sax->startElement = on_start_element;
1252         sax->endElement = on_end_element;
1253         sax->characters = on_characters;
1254         sax->cdataBlock = on_characters;
1255         sax->internalSubset = on_internal_subset;
1256         sax->entityDecl = on_entity_declaration;
1257         sax->getEntity = on_get_entity;
1258
1259         // Setup libxml2 SAX parsing
1260         xmlInitParser(); 
1261         xmlSubstituteEntitiesDefault( 1 );
1262         // This is used to facilitate entity substitution in the SAX parser
1263         context->entity_doc = xmlNewDoc( "1.0" );
1264         xmlcontext = xmlCreateFileParserCtxt( filename );
1265         xmlcontext->sax = sax;
1266         xmlcontext->_private = ( void* )context;
1267         
1268         // Parse
1269         xmlParseDocument( xmlcontext );
1270         well_formed = xmlcontext->wellFormed;
1271         
1272         // Cleanup after parsing
1273         xmlFreeDoc( context->entity_doc );
1274         free( sax );
1275         xmlcontext->sax = NULL;
1276         xmlcontext->_private = NULL;
1277         xmlFreeParserCtxt( xmlcontext );
1278         xmlMemoryDump( ); // for debugging
1279
1280         // Get the last producer on the stack
1281         mlt_service service = context_pop_service( context );
1282         if ( well_formed && service != NULL )
1283         {
1284                 // Verify it is a producer service (mlt_type="mlt_producer")
1285                 // (producer, playlist, multitrack)
1286                 char *type = mlt_properties_get( mlt_service_properties( service ), "mlt_type" );
1287                 if ( type == NULL || ( strcmp( type, "mlt_producer" ) != 0 && strcmp( type, "producer" ) != 0 ) )
1288                         service = NULL;
1289         }
1290
1291 #ifdef DEBUG
1292         xmlDocPtr doc = westley_make_doc( service );
1293         xmlDocFormatDump( stdout, doc, 1 );
1294         xmlFreeDoc( doc );
1295         service = NULL;
1296 #endif
1297         
1298         if ( well_formed && service != NULL )
1299         {
1300                 
1301                 // Need the complete producer list for various reasons
1302                 properties = context->destructors;
1303
1304                 // Now make sure we don't have a reference to the service in the properties
1305                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
1306                 {
1307                         char *name = mlt_properties_get_name( properties, i );
1308                         if ( mlt_properties_get_data( properties, name, NULL ) == service )
1309                         {
1310                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
1311                                 break;
1312                         }
1313                 }
1314
1315                 // We are done referencing destructor property list
1316                 // Set this var to service properties for convenience
1317                 properties = mlt_service_properties( service );
1318         
1319                 // make the returned service destroy the connected services
1320                 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
1321
1322                 // Now assign additional properties
1323                 mlt_properties_set( properties, "resource", url );
1324
1325                 // This tells consumer_westley not to deep copy
1326                 mlt_properties_set( properties, "westley", "was here" );
1327         }
1328         else
1329         {
1330                 // Return null if not well formed
1331                 service = NULL;
1332                 
1333                 // Clean up
1334                 mlt_properties_close( context->destructors );
1335         }
1336
1337         // Clean up
1338         mlt_properties_close( context->producer_map );
1339         if ( context->params != NULL )
1340                 mlt_properties_close( context->params );
1341         mlt_deque_close( context->filter_queue );
1342         free( context );
1343         free( filename );
1344
1345         return MLT_PRODUCER( service );
1346 }