]> git.sesse.net Git - mlt/blob - src/modules/xml/consumer_xml.c
Rename inigo, fezzik, and westley.
[mlt] / src / modules / xml / consumer_xml.c
1 /*
2  * consumer_westley.c -- a libxml2 serialiser of mlt service networks
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <pthread.h>
26 #include <unistd.h>
27 #include <libxml/tree.h>
28
29 #define ID_SIZE 128
30
31 #define _x (const xmlChar*)
32 #define _s (const char*)
33
34 // This maintains counters for adding ids to elements
35 struct serialise_context_s
36 {
37         mlt_properties id_map;
38         int producer_count;
39         int multitrack_count;
40         int playlist_count;
41         int tractor_count;
42         int filter_count;
43         int transition_count;
44         int pass;
45         mlt_properties hide_map;
46         char *root;
47         char *store;
48 };
49 typedef struct serialise_context_s* serialise_context;
50
51 /** Forward references to static functions.
52 */
53
54 static int consumer_start( mlt_consumer parent );
55 static int consumer_is_stopped( mlt_consumer this );
56 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
57
58 typedef enum 
59 {
60         westley_existing,
61         westley_producer,
62         westley_multitrack,
63         westley_playlist,
64         westley_tractor,
65         westley_filter,
66         westley_transition
67 }
68 westley_type;
69
70 /** Create or retrieve an id associated to this service.
71 */
72
73 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
74 {
75         char *id = NULL;
76         int i = 0;
77         mlt_properties map = context->id_map;
78
79         // Search the map for the service
80         for ( i = 0; i < mlt_properties_count( map ); i ++ )
81                 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
82                         break;
83
84         // If the service is not in the map, and the type indicates a new id is needed...
85         if ( i >= mlt_properties_count( map ) && type != westley_existing )
86         {
87                 // Attempt to reuse existing id
88                 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
89
90                 // If no id, or the id is used in the map (for another service), then 
91                 // create a new one.
92                 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
93                 {
94                         char temp[ ID_SIZE ];
95                         do
96                         {
97                                 switch( type )
98                                 {
99                                         case westley_producer:
100                                                 sprintf( temp, "producer%d", context->producer_count ++ );
101                                                 break;
102                                         case westley_multitrack:
103                                                 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
104                                                 break;
105                                         case westley_playlist:
106                                                 sprintf( temp, "playlist%d", context->playlist_count ++ );
107                                                 break;
108                                         case westley_tractor:
109                                                 sprintf( temp, "tractor%d", context->tractor_count ++ );
110                                                 break;
111                                         case westley_filter:
112                                                 sprintf( temp, "filter%d", context->filter_count ++ );
113                                                 break;
114                                         case westley_transition:
115                                                 sprintf( temp, "transition%d", context->transition_count ++ );
116                                                 break;
117                                         case westley_existing:
118                                                 // Never gets here
119                                                 break;
120                                 }
121                         }
122                         while( mlt_properties_get_data( map, temp, NULL ) != NULL );
123
124                         // Set the data at the generated name
125                         mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
126
127                         // Get the pointer to the name (i is the end of the list)
128                         id = mlt_properties_get_name( map, i );
129                 }
130                 else
131                 {
132                         // Store the existing id in the map
133                         mlt_properties_set_data( map, id, service, 0, NULL, NULL );
134                 }
135         }
136         else if ( type == westley_existing )
137         {
138                 id = mlt_properties_get_name( map, i );
139         }
140
141         return id;
142 }
143
144 /** This is what will be called by the factory - anything can be passed in
145         via the argument, but keep it simple.
146 */
147
148 mlt_consumer consumer_westley_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
149 {
150         // Create the consumer object
151         mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
152
153         // If no malloc'd and consumer init ok
154         if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
155         {
156                 // Allow thread to be started/stopped
157                 this->start = consumer_start;
158                 this->is_stopped = consumer_is_stopped;
159
160                 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
161
162                 // Return the consumer produced
163                 return this;
164         }
165
166         // malloc or consumer init failed
167         free( this );
168
169         // Indicate failure
170         return NULL;
171 }
172
173 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
174 {
175         int i;
176         xmlNode *p;
177         
178         // Enumerate the properties
179         for ( i = 0; i < mlt_properties_count( properties ); i++ )
180         {
181                 char *name = mlt_properties_get_name( properties, i );
182                 if ( name != NULL &&
183                          name[ 0 ] != '_' &&
184                          mlt_properties_get_value( properties, i ) != NULL &&
185                          strcmp( name, "westley" ) != 0 &&
186                          strcmp( name, "in" ) != 0 &&
187                          strcmp( name, "out" ) != 0 && 
188                          strcmp( name, "id" ) != 0 && 
189                          strcmp( name, "title" ) != 0 && 
190                          strcmp( name, "root" ) != 0 && 
191                          strcmp( name, "width" ) != 0 &&
192                          strcmp( name, "height" ) != 0 )
193                 {
194                         char *value = mlt_properties_get_value( properties, i );
195                         if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
196                                 value += strlen( context->root ) + 1;
197                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
198                         xmlNewProp( p, _x("name"), _x(name) );
199                 }
200         }
201 }
202
203 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
204 {
205         int i;
206         xmlNode *p;
207         
208         // Enumerate the properties
209         for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
210         {
211                 char *name = mlt_properties_get_name( properties, i );
212                 if ( !strncmp( name, store, strlen( store ) ) )
213                 {
214                         char *value = mlt_properties_get_value( properties, i );
215                         if ( value != NULL )
216                         {
217                                 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
218                                         value += strlen( context->root ) + 1;
219                                 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
220                                 xmlNewProp( p, _x("name"), _x(name) );
221                         }
222                 }
223         }
224 }
225
226 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
227 {
228         int i;
229         xmlNode *p;
230         mlt_filter filter = NULL;
231         
232         // Enumerate the filters
233         for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
234         {
235                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
236                 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
237                 {
238                         // Get a new id - if already allocated, do nothing
239                         char *id = westley_get_id( context, MLT_FILTER_SERVICE( filter ), westley_filter );
240                         if ( id != NULL )
241                         {
242                                 int in = mlt_properties_get_position( properties, "in" );
243                                 int out = mlt_properties_get_position( properties, "out" );
244                                 p = xmlNewChild( node, NULL, _x("filter"), NULL );
245                                 xmlNewProp( p, _x("id"), _x(id) );
246                                 if ( mlt_properties_get( properties, "title" ) )
247                                         xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
248                                 if ( in != 0 || out != 0 )
249                                 {
250                                         char temp[ 20 ];
251                                         sprintf( temp, "%d", in );
252                                         xmlNewProp( p, _x("in"), _x(temp) );
253                                         sprintf( temp, "%d", out );
254                                         xmlNewProp( p, _x("out"), _x(temp) );
255                                 }
256                                 serialise_properties( context, properties, p );
257                                 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
258                         }
259                 }
260         }
261 }
262
263 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
264 {
265         xmlNode *child = node;
266         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
267         
268         if ( context->pass == 0 )
269         {
270                 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
271                 // Get a new id - if already allocated, do nothing
272                 char *id = westley_get_id( context, parent, westley_producer );
273                 if ( id == NULL )
274                         return;
275
276                 child = xmlNewChild( node, NULL, _x("producer"), NULL );
277
278                 // Set the id
279                 xmlNewProp( child, _x("id"), _x(id) );
280                 if ( mlt_properties_get( properties, "title" ) )
281                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
282                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
283                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
284                 serialise_properties( context, properties, child );
285                 serialise_service_filters( context, service, child );
286
287                 // Add producer to the map
288                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
289         }
290         else
291         {
292                 char *id = westley_get_id( context, parent, westley_existing );
293                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
294                 xmlNewProp( node, _x("parent"), _x(id) );
295                 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
296                 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
297         }
298 }
299
300 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
301
302 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
303 {
304         int i;
305         
306         if ( context->pass == 0 )
307         {
308                 // Iterate over the tracks to collect the producers
309                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
310                 {
311                         mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
312                         serialise_service( context, MLT_SERVICE( producer ), node );
313                 }
314         }
315         else
316         {
317                 // Get a new id - if already allocated, do nothing
318                 char *id = westley_get_id( context, service, westley_multitrack );
319                 if ( id == NULL )
320                         return;
321
322                 // Serialise the tracks
323                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
324                 {
325                         xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
326                         int hide = 0;
327                         mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
328                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
329
330                         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
331
332                         char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
333                         xmlNewProp( track, _x("producer"), _x(id) );
334                         if ( mlt_producer_is_cut( producer ) )
335                         {
336                                 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
337                                 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
338                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
339                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
340                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
341                         }
342                         
343                         hide = mlt_properties_get_int( context->hide_map, id );
344                         if ( hide )
345                                 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
346                 }
347                 serialise_service_filters( context, service, node );
348         }
349 }
350
351 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
352 {
353         int i;
354         xmlNode *child = node;
355         mlt_playlist_clip_info info;
356         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
357         
358         if ( context->pass == 0 )
359         {
360                 // Get a new id - if already allocated, do nothing
361                 char *id = westley_get_id( context, service, westley_playlist );
362                 if ( id == NULL )
363                         return;
364
365                 // Iterate over the playlist entries to collect the producers
366                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
367                 {
368                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
369                         {
370                                 if ( info.producer != NULL )
371                                 {
372                                         mlt_producer producer = mlt_producer_cut_parent( info.producer );
373                                         char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
374                                         char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
375                                         if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
376                                                 serialise_playlist( context, MLT_SERVICE( producer ), node );
377                                         else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
378                                                 serialise_service( context, MLT_SERVICE( producer ), node );
379                                 }
380                         }
381                 }
382                 
383                 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
384
385                 // Set the id
386                 xmlNewProp( child, _x("id"), _x(id) );
387                 if ( mlt_properties_get( properties, "title" ) )
388                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
389
390                 // Store application specific properties
391                 serialise_store_properties( context, properties, child, context->store );
392                 serialise_store_properties( context, properties, child, "meta." );
393
394                 // Add producer to the map
395                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
396         
397                 // Iterate over the playlist entries
398                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
399                 {
400                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
401                         {
402                                 mlt_producer producer = mlt_producer_cut_parent( info.producer );
403                                 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
404                                 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
405                                 {
406                                         char length[ 20 ];
407                                         length[ 19 ] = '\0';
408                                         xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
409                                         snprintf( length, 19, "%d", (int)info.frame_count );
410                                         xmlNewProp( entry, _x("length"), _x(length) );
411                                 }
412                                 else
413                                 {
414                                         char temp[ 20 ];
415                                         xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
416                                         id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
417                                         xmlNewProp( entry, _x("producer"), _x(id) );
418                                         sprintf( temp, "%d", (int)info.frame_in );
419                                         xmlNewProp( entry, _x("in"), _x(temp) );
420                                         sprintf( temp, "%d", (int)info.frame_out );
421                                         xmlNewProp( entry, _x("out"), _x(temp) );
422                                         if ( info.repeat > 1 )
423                                         {
424                                                 sprintf( temp, "%d", info.repeat );
425                                                 xmlNewProp( entry, _x("repeat"), _x(temp) );
426                                         }
427                                         if ( mlt_producer_is_cut( info.cut ) )
428                                         {
429                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
430                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
431                                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
432                                         }
433                                 }
434                         }
435                 }
436
437                 serialise_service_filters( context, service, child );
438         }
439         else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
440         {
441                 char *id = westley_get_id( context, service, westley_existing );
442                 xmlNewProp( node, _x("producer"), _x(id) );
443         }
444 }
445
446 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
447 {
448         xmlNode *child = node;
449         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
450         
451         if ( context->pass == 0 )
452         {
453                 // Recurse on connected producer
454                 serialise_service( context, mlt_service_producer( service ), node );
455         }
456         else
457         {
458                 // Get a new id - if already allocated, do nothing
459                 char *id = westley_get_id( context, service, westley_tractor );
460                 if ( id == NULL )
461                         return;
462
463                 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
464
465                 // Set the id
466                 xmlNewProp( child, _x("id"), _x(id) );
467                 if ( mlt_properties_get( properties, "title" ) )
468                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
469                 if ( mlt_properties_get( properties, "global_feed" ) )
470                         xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
471                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
472                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
473
474                 // Store application specific properties
475                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
476                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
477
478                 // Recurse on connected producer
479                 serialise_service( context, mlt_service_producer( service ), child );
480                 serialise_service_filters( context, service, child );
481         }
482 }
483
484 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
485 {
486         xmlNode *child = node;
487         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
488         
489         // Recurse on connected producer
490         serialise_service( context, mlt_service_producer( service ), node );
491
492         if ( context->pass == 1 )
493         {
494                 // Get a new id - if already allocated, do nothing
495                 char *id = westley_get_id( context, service, westley_filter );
496                 if ( id == NULL )
497                         return;
498
499                 child = xmlNewChild( node, NULL, _x("filter"), NULL );
500
501                 // Set the id
502                 xmlNewProp( child, _x("id"), _x(id) );
503                 if ( mlt_properties_get( properties, "title" ) )
504                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
505                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
506                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
507
508                 serialise_properties( context, properties, child );
509                 serialise_service_filters( context, service, child );
510         }
511 }
512
513 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
514 {
515         xmlNode *child = node;
516         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
517         
518         // Recurse on connected producer
519         serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
520
521         if ( context->pass == 1 )
522         {
523                 // Get a new id - if already allocated, do nothing
524                 char *id = westley_get_id( context, service, westley_transition );
525                 if ( id == NULL )
526                         return;
527
528                 child = xmlNewChild( node, NULL, _x("transition"), NULL );
529         
530                 // Set the id
531                 xmlNewProp( child, _x("id"), _x(id) );
532                 if ( mlt_properties_get( properties, "title" ) )
533                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
534                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
535                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
536
537                 serialise_properties( context, properties, child );
538                 serialise_service_filters( context, service, child );
539         }
540 }
541
542 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
543 {
544         // Iterate over consumer/producer connections
545         while ( service != NULL )
546         {
547                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
548                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
549                 
550                 // Tell about the producer
551                 if ( strcmp( mlt_type, "producer" ) == 0 )
552                 {
553                         char *mlt_service = mlt_properties_get( properties, "mlt_service" );
554                         if ( mlt_properties_get( properties, "westley" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
555                         {
556                                 context->pass = 0;
557                                 serialise_tractor( context, service, node );
558                                 context->pass = 1;
559                                 serialise_tractor( context, service, node );
560                                 context->pass = 0;
561                                 break;
562                         }
563                         else
564                         {
565                                 serialise_producer( context, service, node );
566                         }
567                         if ( mlt_properties_get( properties, "westley" ) != NULL )
568                                 break;
569                 }
570
571                 // Tell about the framework container producers
572                 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
573                 {
574                         char *resource = mlt_properties_get( properties, "resource" );
575                         
576                         // Recurse on multitrack's tracks
577                         if ( strcmp( resource, "<multitrack>" ) == 0 )
578                         {
579                                 serialise_multitrack( context, service, node );
580                                 break;
581                         }
582                         
583                         // Recurse on playlist's clips
584                         else if ( strcmp( resource, "<playlist>" ) == 0 )
585                         {
586                                 serialise_playlist( context, service, node );
587                         }
588                         
589                         // Recurse on tractor's producer
590                         else if ( strcmp( resource, "<tractor>" ) == 0 )
591                         {
592                                 context->pass = 0;
593                                 serialise_tractor( context, service, node );
594                                 context->pass = 1;
595                                 serialise_tractor( context, service, node );
596                                 context->pass = 0;
597                                 break;
598                         }
599
600                         // Treat it as a normal producer
601                         else
602                         {
603                                 serialise_producer( context, service, node );
604                         }
605                 }
606                 
607                 // Tell about a filter
608                 else if ( strcmp( mlt_type, "filter" ) == 0 )
609                 {
610                         serialise_filter( context, service, node );
611                         break;
612                 }
613                 
614                 // Tell about a transition
615                 else if ( strcmp( mlt_type, "transition" ) == 0 )
616                 {
617                         serialise_transition( context, service, node );
618                         break;
619                 }
620                 
621                 // Get the next connected service
622                 service = mlt_service_producer( service );
623         }
624 }
625
626 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
627 {
628         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
629         xmlDocPtr doc = xmlNewDoc( _x("1.0") );
630         xmlNodePtr root = xmlNewNode( NULL, _x("westley") );
631         struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
632         
633         xmlDocSetRootElement( doc, root );
634
635         // If we have root, then deal with it now
636         if ( mlt_properties_get( properties, "root" ) != NULL )
637         {
638                 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
639                 context->root = strdup( mlt_properties_get( properties, "root" ) );
640         }
641         else
642         {
643                 context->root = strdup( "" );
644         }
645
646         // Assign the additional 'storage' pattern for properties
647         context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
648
649         // Assign a title property
650         if ( mlt_properties_get( properties, "title" ) != NULL )
651                 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
652         mlt_properties_set_int( properties, "global_feed", 1 );
653
654         // Construct the context maps
655         context->id_map = mlt_properties_new();
656         context->hide_map = mlt_properties_new();
657         
658         // Ensure producer is a framework producer
659         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
660
661         // In pass one, we serialise the end producers and playlists,
662         // adding them to a map keyed by address.
663         serialise_service( context, service, root );
664
665         // In pass two, we serialise the tractor and reference the
666         // producers and playlists
667         context->pass++;
668         serialise_service( context, service, root );
669
670         // Cleanup resource
671         mlt_properties_close( context->id_map );
672         mlt_properties_close( context->hide_map );
673         free( context->root );
674         free( context );
675         
676         return doc;
677 }
678
679 static int consumer_start( mlt_consumer this )
680 {
681         xmlDocPtr doc = NULL;
682         
683         // Get the producer service
684         mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
685         if ( service != NULL )
686         {
687                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
688                 char *resource =  mlt_properties_get( properties, "resource" );
689
690                 // Set the title if provided
691                 if ( mlt_properties_get( properties, "title" ) )
692                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
693                 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
694                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
695
696                 // Check for a root on the consumer properties and pass to service
697                 if ( mlt_properties_get( properties, "root" ) )
698                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
699
700                 // Specify roots in other cases...
701                 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
702                 {
703                         // Get the current working directory
704                         char *cwd = getcwd( NULL, 0 );
705                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
706                         free( cwd );
707                 }
708
709                 // Make the document
710                 doc = westley_make_doc( this, service );
711
712                 // Handle the output
713                 if ( resource == NULL || !strcmp( resource, "" ) )
714                 {
715                         xmlDocFormatDump( stdout, doc, 1 );
716                 }
717                 else if ( strchr( resource, '.' ) == NULL )
718                 {
719                         xmlChar *buffer = NULL;
720                         int length = 0;
721                         xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
722                         mlt_properties_set( properties, resource, _s(buffer) );
723                         xmlFree( buffer );
724                 }
725                 else
726                 {
727                         xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
728                 }
729                 
730                 // Close the document
731                 xmlFreeDoc( doc );
732         }
733         
734         mlt_consumer_stop( this );
735
736         mlt_consumer_stopped( this );
737
738         return 0;
739 }
740
741 static int consumer_is_stopped( mlt_consumer this )
742 {
743         return 1;
744 }