]> git.sesse.net Git - mlt/blob - src/modules/westley/producer_westley.c
808d5ad56a87670098d1dd5e6a5bc73f3685ca34
[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
22  
23 #include "producer_westley.h"
24 #include <framework/mlt.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28
29 #include <libxml/parser.h>
30
31 #define STACK_SIZE 1000
32
33 struct deserialise_context_s
34 {
35         mlt_service stack_service[ STACK_SIZE ];
36         int stack_service_size;
37         mlt_properties producer_map;
38         mlt_properties destructors;
39         char *property;
40         mlt_properties producer_properties;
41 };
42 typedef struct deserialise_context_s *deserialise_context;
43
44
45 /** Push a service.
46 */
47
48 static int context_push_service( deserialise_context this, mlt_service that )
49 {
50         int ret = this->stack_service_size >= STACK_SIZE;
51         if ( ret == 0 )
52                 this->stack_service[ this->stack_service_size ++ ] = that;
53         return ret;
54 }
55
56 /** Pop a service.
57 */
58
59 static mlt_service context_pop_service( deserialise_context this )
60 {
61         mlt_service result = NULL;
62         if ( this->stack_service_size > 0 )
63                 result = this->stack_service[ -- this->stack_service_size ];
64         return result;
65 }
66
67 // Set the destructor on a new service
68 static void track_service( mlt_properties properties, void *service, mlt_destructor destructor )
69 {
70         int registered = mlt_properties_get_int( properties, "registered" );
71         char *key = mlt_properties_get( properties, "registered" );
72         mlt_properties_set_data( properties, key, service, 0, destructor, NULL );
73         mlt_properties_set_int( properties, "registered", ++ registered );
74 }
75
76 static void on_start_tractor( deserialise_context context, const xmlChar *name, const xmlChar **atts)
77 {
78         mlt_service service = mlt_tractor_service( mlt_tractor_init() );
79         mlt_properties properties = mlt_service_properties( service );
80
81         track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
82
83         mlt_properties_set_position( properties, "length", 0 );
84
85         for ( ; atts != NULL && *atts != NULL; atts += 2 )
86                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
87
88         if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
89         {
90                 mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
91                 mlt_properties_set_position( properties, "length", length );
92         }
93
94         context_push_service( context, service );
95 }
96
97 static void on_start_multitrack( deserialise_context context, const xmlChar *name, const xmlChar **atts)
98 {
99         mlt_service service = mlt_multitrack_service( mlt_multitrack_init() );
100         mlt_properties properties = mlt_service_properties( service );
101
102         track_service( context->destructors, service, (mlt_destructor) mlt_multitrack_close );
103
104         mlt_properties_set_position( properties, "length", 0 );
105
106         for ( ; atts != NULL && *atts != NULL; atts += 2 )
107                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
108
109         context_push_service( context, service );
110 }
111
112 static void on_start_playlist( deserialise_context context, const xmlChar *name, const xmlChar **atts)
113 {
114         mlt_service service = mlt_playlist_service( mlt_playlist_init() );
115         mlt_properties properties = mlt_service_properties( service );
116
117         track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
118
119         mlt_properties_set_position( properties, "length", 0 );
120
121         for ( ; atts != NULL && *atts != NULL; atts += 2 )
122         {
123                 mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
124
125                 // Out will be overwritten later as we append, so we need to save it
126                 if ( strcmp( atts[ 0 ], "out" ) == 0 )
127                         mlt_properties_set( properties, "_westley.out", ( char* )atts[ 1 ] );
128         }
129
130         if ( mlt_properties_get( properties, "id" ) != NULL )
131                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
132
133         context_push_service( context, service );
134 }
135
136 static void on_start_producer( deserialise_context context, const xmlChar *name, const xmlChar **atts)
137 {
138         mlt_properties properties = context->producer_properties = mlt_properties_new();
139
140         for ( ; atts != NULL && *atts != NULL; atts += 2 )
141         {
142                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
143         }
144 }
145
146 static void on_start_blank( deserialise_context context, const xmlChar *name, const xmlChar **atts)
147 {
148         // Get the playlist from the stack
149         mlt_service service = context_pop_service( context );
150         mlt_position length = 0;
151
152         // Look for the length attribute
153         for ( ; atts != NULL && *atts != NULL; atts += 2 )
154         {
155                 if ( strcmp( atts[0], "length" ) == 0 )
156                 {
157                         length = atoll( atts[1] );
158                         break;
159                 }
160         }
161
162         // Append a blank to the playlist
163         mlt_playlist_blank( MLT_PLAYLIST( service ), length - 1 );
164
165         // Push the playlist back onto the stack
166         context_push_service( context, service );
167 }
168
169 static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
170 {
171         // Use a dummy service to hold properties to allow arbitratry nesting
172         mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
173         mlt_service_init( service, NULL );
174
175         // Push the dummy service onto the stack
176         context_push_service( context, service );
177         
178         for ( ; atts != NULL && *atts != NULL; atts += 2 )
179         {
180                 mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
181                 
182                 // Look for the producer attribute
183                 if ( strcmp( atts[ 0 ], "producer" ) == 0 )
184                 {
185                         if ( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) !=  NULL )
186                                 // Push the referenced producer onto the stack
187                                 context_push_service( context, MLT_SERVICE( mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL ) ) );
188                 }
189         }
190 }
191
192 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
193 {
194         mlt_properties properties = context->producer_properties = mlt_properties_new();
195
196         // Set the properties
197         for ( ; atts != NULL && *atts != NULL; atts += 2 )
198                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
199 }
200
201 static void on_start_transition( deserialise_context context, const xmlChar *name, const xmlChar **atts)
202 {
203         mlt_properties properties = context->producer_properties = mlt_properties_new();
204
205         // Set the properties
206         for ( ; atts != NULL && *atts != NULL; atts += 2 )
207                 mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
208 }
209
210 static void on_start_property( deserialise_context context, const xmlChar *name, const xmlChar **atts)
211 {
212         mlt_properties properties = context->producer_properties;
213         char *value = NULL;
214
215         if ( properties == NULL )
216                 return;
217         
218         // Set the properties
219         for ( ; atts != NULL && *atts != NULL; atts += 2 )
220         {
221                 if ( strcmp( atts[ 0 ], "name" ) == 0 )
222                 {
223                         context->property = strdup( atts[ 1 ] );
224                 }
225                 else if ( strcmp( atts[ 0 ], "value" ) == 0 )
226                 {
227                         value = (char*) atts[ 1 ];
228                 }
229         }
230
231         if ( context->property != NULL && value != NULL )
232                 mlt_properties_set( properties, context->property, value );
233 }
234
235 static void on_end_multitrack( deserialise_context context, const xmlChar *name )
236 {
237         // Get the producer (multitrack) from the stack
238         mlt_service producer = context_pop_service( context );
239
240         // Get the tractor from the stack
241         mlt_service service = context_pop_service( context );
242
243         // Connect the tractor to the producer
244         mlt_tractor_connect( MLT_TRACTOR( service ), producer );
245         mlt_properties_set_data( mlt_service_properties( service ), "multitrack",
246                 MLT_MULTITRACK( producer ), 0, NULL, NULL );
247
248         // Push the tractor back onto the stack
249         context_push_service( context, service );
250
251         // Push the producer back onto the stack
252         context_push_service( context, producer );
253 }
254
255 static void on_end_playlist( deserialise_context context, const xmlChar *name )
256 {
257         // Get the playlist from the stack
258         mlt_service service = context_pop_service( context );
259         mlt_properties properties = mlt_service_properties( service );
260
261         mlt_position in = mlt_properties_get_position( properties, "in" );
262         mlt_position out;
263
264         if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
265                 out = mlt_properties_get_position( properties, "_westley.out" );
266         else
267                 out = mlt_properties_get_position( properties, "length" ) - 1;
268
269         if ( mlt_properties_get_position( properties, "length" ) < out )
270                 mlt_properties_set_position( properties, "length", out  + 1 );
271
272         mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
273
274         // Push the playlist back onto the stack
275         context_push_service( context, service );
276 }
277
278 static void on_end_track( deserialise_context context, const xmlChar *name )
279 {
280         // Get the producer from the stack
281         mlt_service producer = context_pop_service( context );
282
283         // Get the dummy track service from the stack
284         mlt_service track = context_pop_service( context );
285
286         // Get the multitrack from the stack
287         mlt_service service = context_pop_service( context );
288
289         // Set the track on the multitrack
290         mlt_multitrack_connect( MLT_MULTITRACK( service ),
291                 MLT_PRODUCER( producer ),
292                 mlt_multitrack_count( MLT_MULTITRACK( service ) ) );
293
294         // Set producer i/o if specified
295         if ( mlt_properties_get( mlt_service_properties( track ), "in" ) != NULL ||
296                 mlt_properties_get( mlt_service_properties( track ), "out" ) != NULL )
297         {
298                 mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
299                         mlt_properties_get_position( mlt_service_properties( track ), "in" ),
300                         mlt_properties_get_position( mlt_service_properties( track ), "out" ) );
301         }
302
303         // Push the multitrack back onto the stack
304         context_push_service( context, service );
305
306         mlt_service_close( track );
307 }
308
309 static void on_end_entry( deserialise_context context, const xmlChar *name )
310 {
311         // Get the producer from the stack
312         mlt_service producer = context_pop_service( context );
313
314         // Get the dummy entry service from the stack
315         mlt_service entry = context_pop_service( context );
316
317         // Get the playlist from the stack
318         mlt_service service = context_pop_service( context );
319
320         // Append the producer to the playlist
321         if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
322                 mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
323         {
324                 mlt_playlist_append_io( MLT_PLAYLIST( service ),
325                         MLT_PRODUCER( producer ),
326                         mlt_properties_get_position( mlt_service_properties( entry ), "in" ), 
327                         mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
328         }
329         else
330         {
331                 mlt_playlist_append( MLT_PLAYLIST( service ), MLT_PRODUCER( producer ) );
332         }
333
334         // Push the playlist back onto the stack
335         context_push_service( context, service );
336
337         mlt_service_close( entry );
338 }
339
340 static void on_end_tractor( deserialise_context context, const xmlChar *name )
341 {
342         // Get and discard the last producer
343         mlt_producer multitrack = MLT_PRODUCER( context_pop_service( context ) );
344
345         // Get the tractor
346         mlt_service tractor = context_pop_service( context );
347         multitrack = mlt_properties_get_data( mlt_service_properties( tractor ), "multitrack", NULL );
348
349         // Inherit the producer's properties
350         mlt_properties properties = mlt_producer_properties( multitrack );
351         mlt_properties_set_position( properties, "length", mlt_producer_get_out( multitrack ) + 1 );
352         mlt_producer_set_in_and_out( multitrack, 0, mlt_producer_get_out( multitrack ) );
353         mlt_properties_set_double( properties, "fps", mlt_producer_get_fps( multitrack ) );
354
355         // Push the playlist back onto the stack
356         context_push_service( context, tractor );
357 }
358
359 static void on_end_property( deserialise_context context, const xmlChar *name )
360 {
361         // Close this property handling
362         free( context->property );
363         context->property = NULL;
364 }
365
366 static void on_end_producer( deserialise_context context, const xmlChar *name )
367 {
368         mlt_properties properties = context->producer_properties;
369         mlt_service service = NULL;
370         
371         if ( properties == NULL )
372                 return;
373                 
374         // Instatiate the producer
375         if ( mlt_properties_get( properties, "resource" ) != NULL )
376         {
377                 char *root = mlt_properties_get( context->producer_map, "_root" );
378                 char *resource = mlt_properties_get( properties, "resource" );
379                 char *full_resource = malloc( strlen( root ) + strlen( resource ) + 1 );
380                 if ( resource[ 0 ] != '/' )
381                 {
382                         strcpy( full_resource, root );
383                         strcat( full_resource, resource );
384                 }
385                 else
386                 {
387                         strcpy( full_resource, resource );
388                 }
389                 service = MLT_SERVICE( mlt_factory_producer( "fezzik", full_resource ) );
390                 free( full_resource );
391         }
392         if ( service == NULL && mlt_properties_get( properties, "mlt_service" ) != NULL )
393         {
394                 service = MLT_SERVICE( mlt_factory_producer( mlt_properties_get( properties, "mlt_service" ),
395                         mlt_properties_get( properties, "resource" ) ) );
396         }
397
398         track_service( context->destructors, service, (mlt_destructor) mlt_producer_close );
399
400         // Add the producer to the producer map
401         if ( mlt_properties_get( properties, "id" ) != NULL )
402                 mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
403
404         mlt_properties_inherit( mlt_service_properties( service ), properties );
405         mlt_properties_close( properties );
406         context->producer_properties = NULL;
407         properties = mlt_service_properties( service );
408
409         // Set in and out
410         mlt_producer_set_in_and_out( MLT_PRODUCER( service ),
411                 mlt_properties_get_position( properties, "in" ),
412                 mlt_properties_get_position( properties, "out" ) );
413
414         // Push the new producer onto the stack
415         context_push_service( context, service );
416 }
417
418 static void on_end_filter( deserialise_context context, const xmlChar *name )
419 {
420         mlt_properties properties = context->producer_properties;
421         if ( properties == NULL )
422                 return;
423
424         char *id;
425         char key[11];
426         key[ 10 ] = '\0';
427
428         // Get the producer from the stack
429         mlt_service producer = context_pop_service( context );
430 //fprintf( stderr, "connecting filter to %s\n", mlt_properties_get( mlt_service_properties( producer ), "resource" ) );
431
432         // Create the filter
433         mlt_service service = MLT_SERVICE( mlt_factory_filter( mlt_properties_get( properties, "mlt_service" ), NULL ) );
434
435         track_service( context->destructors, service, (mlt_destructor) mlt_filter_close );
436
437         // Connect the filter to the producer
438         mlt_filter_connect( MLT_FILTER( service ), producer,
439                 mlt_properties_get_int( properties, "track" ) );
440
441         // Set in and out from producer if non existant
442         if ( mlt_properties_get( properties, "in" ) == NULL )
443                 mlt_properties_set_position( properties, "in", mlt_producer_get_in( MLT_PRODUCER( producer ) ) );
444         if ( mlt_properties_get( properties, "out" ) == NULL )
445                 mlt_properties_set_position( properties, "out", mlt_producer_get_out( MLT_PRODUCER( producer ) ) );
446
447         // Propogate the properties
448         mlt_properties_inherit( mlt_service_properties( service ), properties );
449         mlt_properties_close( properties );
450         context->producer_properties = NULL;
451         properties = mlt_service_properties( service );
452
453         // Set in and out
454 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
455         mlt_filter_set_in_and_out( MLT_FILTER( service ), 
456                 mlt_properties_get_position( properties, "in" ),
457                 mlt_properties_get_position( properties, "out" ) );
458
459         // Get the parent producer from the stack
460         mlt_service tractor = context_pop_service( context );
461
462         if ( tractor != NULL )
463         {
464 //fprintf( stderr, "connecting tractor %s to filter\n", mlt_properties_get( mlt_service_properties( tractor ), "resource" ) );
465                 // Connect the tractor to the filter
466                 if ( strcmp( mlt_properties_get( mlt_service_properties( tractor ), "resource" ), "<tractor>" ) == 0 )
467                         mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
468
469                 // Push the parent producer back onto the stack
470                 context_push_service( context, tractor );
471         }
472
473 //fprintf( stderr, "setting filter in %lld out %lld\n", mlt_properties_get_position( properties, "in" ), mlt_properties_get_position( properties, "out" ) );
474         // If a producer alias is in the producer_map, get it
475         snprintf( key, 10, "%p", producer );
476         if ( mlt_properties_get_data( context->producer_map, key, NULL ) != NULL )
477                 producer = mlt_properties_get_data( context->producer_map, key, NULL );
478
479         // Put the producer in the producer map
480         id = mlt_properties_get( mlt_service_properties( producer ), "id" );
481         if ( id != NULL )
482                 mlt_properties_set_data( context->producer_map, id, service, 0, NULL, NULL );
483
484         // For filter chain support, add an alias to the producer map
485         snprintf( key, 10, "%p", service );
486         mlt_properties_set_data( context->producer_map, key, producer, 0, NULL, NULL );
487
488         // Push the filter onto the stack
489         context_push_service( context, service );
490
491 }
492
493 static void on_end_transition( deserialise_context context, const xmlChar *name )
494 {
495         mlt_properties properties = context->producer_properties;
496         if ( properties == NULL )
497                 return;
498
499         // Get the producer from the stack
500         mlt_service producer = context_pop_service( context );
501
502         // Create the transition
503         mlt_service service = MLT_SERVICE( mlt_factory_transition( mlt_properties_get( properties, "mlt_service" ), NULL ) );
504
505         track_service( context->destructors, service, (mlt_destructor) mlt_transition_close );
506
507         // Propogate the properties
508         mlt_properties_inherit( mlt_service_properties( service ), properties );
509         mlt_properties_close( properties );
510         context->producer_properties = NULL;
511         properties = mlt_service_properties( service );
512
513         // Set in and out
514         mlt_transition_set_in_and_out( MLT_TRANSITION( service ),
515                 mlt_properties_get_position( properties, "in" ),
516                 mlt_properties_get_position( properties, "out" ) );
517
518         // Connect the filter to the producer
519         mlt_transition_connect( MLT_TRANSITION( service ), producer,
520                 mlt_properties_get_int( properties, "a_track" ),
521                 mlt_properties_get_int( properties, "b_track" ) );
522
523         // Get the tractor from the stack
524         mlt_service tractor = context_pop_service( context );
525
526         // Connect the tractor to the transition
527         mlt_tractor_connect( MLT_TRACTOR( tractor ), service );
528
529         // Push the tractor back onto the stack
530         context_push_service( context, tractor );
531
532         // Push the transition onto the stack
533         context_push_service( context, service );
534 }
535
536 static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **atts)
537 {
538         deserialise_context context = ( deserialise_context ) ctx;
539         
540         if ( strcmp( name, "tractor" ) == 0 )
541                 on_start_tractor( context, name, atts );
542         else if ( strcmp( name, "multitrack" ) == 0 )
543                 on_start_multitrack( context, name, atts );
544         else if ( strcmp( name, "playlist" ) == 0 )
545                 on_start_playlist( context, name, atts );
546         else if ( strcmp( name, "producer" ) == 0 )
547                 on_start_producer( context, name, atts );
548         else if ( strcmp( name, "blank" ) == 0 )
549                 on_start_blank( context, name, atts );
550         else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
551                 on_start_entry_track( context, name, atts );
552         else if ( strcmp( name, "filter" ) == 0 )
553                 on_start_filter( context, name, atts );
554         else if ( strcmp( name, "transition" ) == 0 )
555                 on_start_transition( context, name, atts );
556         else if ( strcmp( name, "property" ) == 0 )
557                 on_start_property( context, name, atts );
558 }
559
560 static void on_end_element( void *ctx, const xmlChar *name )
561 {
562         deserialise_context context = ( deserialise_context ) ctx;
563         
564         if ( strcmp( name, "multitrack" ) == 0 )
565                 on_end_multitrack( context, name );
566         else if ( strcmp( name, "playlist" ) == 0 )
567                 on_end_playlist( context, name );
568         else if ( strcmp( name, "track" ) == 0 )
569                 on_end_track( context, name );
570         else if ( strcmp( name, "entry" ) == 0 )
571                 on_end_entry( context, name );
572         else if ( strcmp( name, "tractor" ) == 0 )
573                 on_end_tractor( context, name );
574         else if ( strcmp( name, "property" ) == 0 )
575                 on_end_property( context, name );
576         else if ( strcmp( name, "producer" ) == 0 )
577                 on_end_producer( context, name );
578         else if ( strcmp( name, "filter" ) == 0 )
579                 on_end_filter( context, name );
580         else if ( strcmp( name, "transition" ) == 0 )
581                 on_end_transition( context, name );
582 }
583
584 static void on_characters( void *ctx, const xmlChar *ch, int len )
585 {
586         deserialise_context context = ( deserialise_context ) ctx;
587         char *value = calloc( len + 1, 1 );
588
589         value[ len ] = 0;
590         strncpy( value, (const char*) ch, len );
591
592         if ( context->property != NULL && context->producer_properties != NULL )
593                 mlt_properties_set( context->producer_properties, context->property, value );
594                 
595         free( value);
596 }
597
598 mlt_producer producer_westley_init( char *filename )
599 {
600         xmlSAXHandler *sax = calloc( 1, sizeof( xmlSAXHandler ) );
601         struct deserialise_context_s *context = calloc( 1, sizeof( struct deserialise_context_s ) );
602         mlt_properties properties = NULL;
603         int i = 0;
604
605         context->producer_map = mlt_properties_new();
606         context->destructors = mlt_properties_new();
607
608         // We need to track the number of registered filters
609         mlt_properties_set_int( context->destructors, "registered", 0 );
610
611         // We need the directory prefix which was used for the westley
612         mlt_properties_set( context->producer_map, "_root", "" );
613         if ( strchr( filename, '/' ) )
614         {
615                 char *root = NULL;
616                 mlt_properties_set( context->producer_map, "_root", filename );
617                 root = mlt_properties_get( context->producer_map, "_root" );
618                 *( strrchr( root, '/' ) + 1 ) = '\0';
619         }
620
621         sax->startElement = on_start_element;
622         sax->endElement = on_end_element;
623         sax->characters = on_characters;
624
625         // I REALLY DON'T GET THIS - HOW THE HELL CAN YOU REFERENCE A WESTLEY IN A WESTLEY???
626         xmlInitParser();
627
628         xmlSAXUserParseFile( sax, context, filename );
629
630         // Need the complete producer list for various reasons
631         properties = context->destructors;
632
633         // Get the last producer on the stack
634         mlt_service service = context_pop_service( context );
635
636         // Do we actually have a producer here?
637         if ( service != NULL )
638         {
639                 // Now make sure we don't have a reference to the service in the properties
640                 for ( i = mlt_properties_count( properties ) - 1; i >= 1; i -- )
641                 {
642                         char *name = mlt_properties_get_name( properties, i );
643                         if ( mlt_properties_get_data( properties, name, NULL ) == service )
644                         {
645                                 mlt_properties_set_data( properties, name, service, 0, NULL, NULL );
646                                 break;
647                         }
648                 }
649
650                 // We are done referencing destructor property list
651                 // Set this var to service properties for convenience
652                 properties = mlt_service_properties( service );
653         
654                 // make the returned service destroy the connected services
655                 mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
656
657                 // Now assign additional properties
658                 mlt_properties_set( properties, "resource", filename );
659
660                 // This tells consumer_westley not to deep copy
661                 mlt_properties_set( properties, "westley", "was here" );
662         }
663         else
664         {
665                 // Clean up
666                 mlt_properties_close( properties );
667         }
668
669         free( context->stack_service );
670         mlt_properties_close( context->producer_map );
671         //free( context );
672         free( sax );
673         xmlCleanupParser();
674         xmlMemoryDump( );
675
676
677         return MLT_PRODUCER( service );
678 }
679