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