]> git.sesse.net Git - mlt/blob - src/modules/xml/consumer_xml.c
Add LC_NUMERIC to MLT XML.
[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 <locale.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         xml_existing,
61         xml_producer,
62         xml_multitrack,
63         xml_playlist,
64         xml_tractor,
65         xml_filter,
66         xml_transition
67 }
68 xml_type;
69
70 /** Create or retrieve an id associated to this service.
71 */
72
73 static char *xml_get_id( serialise_context context, mlt_service service, xml_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 != xml_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 xml_producer:
100                                                 sprintf( temp, "producer%d", context->producer_count ++ );
101                                                 break;
102                                         case xml_multitrack:
103                                                 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
104                                                 break;
105                                         case xml_playlist:
106                                                 sprintf( temp, "playlist%d", context->playlist_count ++ );
107                                                 break;
108                                         case xml_tractor:
109                                                 sprintf( temp, "tractor%d", context->tractor_count ++ );
110                                                 break;
111                                         case xml_filter:
112                                                 sprintf( temp, "filter%d", context->filter_count ++ );
113                                                 break;
114                                         case xml_transition:
115                                                 sprintf( temp, "transition%d", context->transition_count ++ );
116                                                 break;
117                                         case xml_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 == xml_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_xml_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, "mlt" ) != 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                         int rootlen = strlen( context->root );
196                         if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
197                                 value += rootlen + 1;
198                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
199                         xmlNewProp( p, _x("name"), _x(name) );
200                 }
201         }
202 }
203
204 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
205 {
206         int i;
207         xmlNode *p;
208         
209         // Enumerate the properties
210         for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
211         {
212                 char *name = mlt_properties_get_name( properties, i );
213                 if ( !strncmp( name, store, strlen( store ) ) )
214                 {
215                         char *value = mlt_properties_get_value( properties, i );
216                         if ( value != NULL )
217                         {
218                                 int rootlen = strlen( context->root );
219                                 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
220                                         value += rootlen + 1;
221                                 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
222                                 xmlNewProp( p, _x("name"), _x(name) );
223                         }
224                 }
225         }
226 }
227
228 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
229 {
230         int i;
231         xmlNode *p;
232         mlt_filter filter = NULL;
233         
234         // Enumerate the filters
235         for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
236         {
237                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
238                 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
239                 {
240                         // Get a new id - if already allocated, do nothing
241                         char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
242                         if ( id != NULL )
243                         {
244                                 int in = mlt_properties_get_position( properties, "in" );
245                                 int out = mlt_properties_get_position( properties, "out" );
246                                 p = xmlNewChild( node, NULL, _x("filter"), NULL );
247                                 xmlNewProp( p, _x("id"), _x(id) );
248                                 if ( mlt_properties_get( properties, "title" ) )
249                                         xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
250                                 if ( in != 0 || out != 0 )
251                                 {
252                                         char temp[ 20 ];
253                                         sprintf( temp, "%d", in );
254                                         xmlNewProp( p, _x("in"), _x(temp) );
255                                         sprintf( temp, "%d", out );
256                                         xmlNewProp( p, _x("out"), _x(temp) );
257                                 }
258                                 serialise_properties( context, properties, p );
259                                 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
260                         }
261                 }
262         }
263 }
264
265 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
266 {
267         xmlNode *child = node;
268         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
269         
270         if ( context->pass == 0 )
271         {
272                 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
273                 // Get a new id - if already allocated, do nothing
274                 char *id = xml_get_id( context, parent, xml_producer );
275                 if ( id == NULL )
276                         return;
277
278                 child = xmlNewChild( node, NULL, _x("producer"), NULL );
279
280                 // Set the id
281                 xmlNewProp( child, _x("id"), _x(id) );
282                 if ( mlt_properties_get( properties, "title" ) )
283                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
284                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
285                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
286                 serialise_properties( context, properties, child );
287                 serialise_service_filters( context, service, child );
288
289                 // Add producer to the map
290                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
291         }
292         else
293         {
294                 char *id = xml_get_id( context, parent, xml_existing );
295                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
296                 xmlNewProp( node, _x("parent"), _x(id) );
297                 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
298                 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
299         }
300 }
301
302 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
303
304 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
305 {
306         int i;
307         
308         if ( context->pass == 0 )
309         {
310                 // Iterate over the tracks to collect the producers
311                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
312                 {
313                         mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
314                         serialise_service( context, MLT_SERVICE( producer ), node );
315                 }
316         }
317         else
318         {
319                 // Get a new id - if already allocated, do nothing
320                 char *id = xml_get_id( context, service, xml_multitrack );
321                 if ( id == NULL )
322                         return;
323
324                 // Serialise the tracks
325                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
326                 {
327                         xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
328                         int hide = 0;
329                         mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
330                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
331
332                         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
333
334                         char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
335                         xmlNewProp( track, _x("producer"), _x(id) );
336                         if ( mlt_producer_is_cut( producer ) )
337                         {
338                                 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
339                                 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
340                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
341                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
342                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
343                         }
344                         
345                         hide = mlt_properties_get_int( context->hide_map, id );
346                         if ( hide )
347                                 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
348                 }
349                 serialise_service_filters( context, service, node );
350         }
351 }
352
353 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
354 {
355         int i;
356         xmlNode *child = node;
357         mlt_playlist_clip_info info;
358         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
359         
360         if ( context->pass == 0 )
361         {
362                 // Get a new id - if already allocated, do nothing
363                 char *id = xml_get_id( context, service, xml_playlist );
364                 if ( id == NULL )
365                         return;
366
367                 // Iterate over the playlist entries to collect the producers
368                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
369                 {
370                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
371                         {
372                                 if ( info.producer != NULL )
373                                 {
374                                         mlt_producer producer = mlt_producer_cut_parent( info.producer );
375                                         char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
376                                         char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
377                                         if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
378                                                 serialise_playlist( context, MLT_SERVICE( producer ), node );
379                                         else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
380                                                 serialise_service( context, MLT_SERVICE( producer ), node );
381                                 }
382                         }
383                 }
384                 
385                 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
386
387                 // Set the id
388                 xmlNewProp( child, _x("id"), _x(id) );
389                 if ( mlt_properties_get( properties, "title" ) )
390                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
391
392                 // Store application specific properties
393                 serialise_store_properties( context, properties, child, context->store );
394                 serialise_store_properties( context, properties, child, "meta." );
395
396                 // Add producer to the map
397                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
398         
399                 // Iterate over the playlist entries
400                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
401                 {
402                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
403                         {
404                                 mlt_producer producer = mlt_producer_cut_parent( info.producer );
405                                 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
406                                 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
407                                 {
408                                         char length[ 20 ];
409                                         length[ 19 ] = '\0';
410                                         xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
411                                         snprintf( length, 19, "%d", (int)info.frame_count );
412                                         xmlNewProp( entry, _x("length"), _x(length) );
413                                 }
414                                 else
415                                 {
416                                         char temp[ 20 ];
417                                         xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
418                                         id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
419                                         xmlNewProp( entry, _x("producer"), _x(id) );
420                                         sprintf( temp, "%d", (int)info.frame_in );
421                                         xmlNewProp( entry, _x("in"), _x(temp) );
422                                         sprintf( temp, "%d", (int)info.frame_out );
423                                         xmlNewProp( entry, _x("out"), _x(temp) );
424                                         if ( info.repeat > 1 )
425                                         {
426                                                 sprintf( temp, "%d", info.repeat );
427                                                 xmlNewProp( entry, _x("repeat"), _x(temp) );
428                                         }
429                                         if ( mlt_producer_is_cut( info.cut ) )
430                                         {
431                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
432                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
433                                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
434                                         }
435                                 }
436                         }
437                 }
438
439                 serialise_service_filters( context, service, child );
440         }
441         else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
442         {
443                 char *id = xml_get_id( context, service, xml_existing );
444                 xmlNewProp( node, _x("producer"), _x(id) );
445         }
446 }
447
448 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
449 {
450         xmlNode *child = node;
451         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
452         
453         if ( context->pass == 0 )
454         {
455                 // Recurse on connected producer
456                 serialise_service( context, mlt_service_producer( service ), node );
457         }
458         else
459         {
460                 // Get a new id - if already allocated, do nothing
461                 char *id = xml_get_id( context, service, xml_tractor );
462                 if ( id == NULL )
463                         return;
464
465                 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
466
467                 // Set the id
468                 xmlNewProp( child, _x("id"), _x(id) );
469                 if ( mlt_properties_get( properties, "title" ) )
470                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
471                 if ( mlt_properties_get( properties, "global_feed" ) )
472                         xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
473                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
474                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
475
476                 // Store application specific properties
477                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
478                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
479
480                 // Recurse on connected producer
481                 serialise_service( context, mlt_service_producer( service ), child );
482                 serialise_service_filters( context, service, child );
483         }
484 }
485
486 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
487 {
488         xmlNode *child = node;
489         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
490         
491         // Recurse on connected producer
492         serialise_service( context, mlt_service_producer( service ), node );
493
494         if ( context->pass == 1 )
495         {
496                 // Get a new id - if already allocated, do nothing
497                 char *id = xml_get_id( context, service, xml_filter );
498                 if ( id == NULL )
499                         return;
500
501                 child = xmlNewChild( node, NULL, _x("filter"), NULL );
502
503                 // Set the id
504                 xmlNewProp( child, _x("id"), _x(id) );
505                 if ( mlt_properties_get( properties, "title" ) )
506                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
507                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
508                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
509
510                 serialise_properties( context, properties, child );
511                 serialise_service_filters( context, service, child );
512         }
513 }
514
515 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
516 {
517         xmlNode *child = node;
518         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
519         
520         // Recurse on connected producer
521         serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
522
523         if ( context->pass == 1 )
524         {
525                 // Get a new id - if already allocated, do nothing
526                 char *id = xml_get_id( context, service, xml_transition );
527                 if ( id == NULL )
528                         return;
529
530                 child = xmlNewChild( node, NULL, _x("transition"), NULL );
531         
532                 // Set the id
533                 xmlNewProp( child, _x("id"), _x(id) );
534                 if ( mlt_properties_get( properties, "title" ) )
535                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
536                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
537                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
538
539                 serialise_properties( context, properties, child );
540                 serialise_service_filters( context, service, child );
541         }
542 }
543
544 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
545 {
546         // Iterate over consumer/producer connections
547         while ( service != NULL )
548         {
549                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
550                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
551                 
552                 // Tell about the producer
553                 if ( strcmp( mlt_type, "producer" ) == 0 )
554                 {
555                         char *mlt_service = mlt_properties_get( properties, "mlt_service" );
556                         if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
557                         {
558                                 context->pass = 0;
559                                 serialise_tractor( context, service, node );
560                                 context->pass = 1;
561                                 serialise_tractor( context, service, node );
562                                 context->pass = 0;
563                                 break;
564                         }
565                         else
566                         {
567                                 serialise_producer( context, service, node );
568                         }
569                         if ( mlt_properties_get( properties, "xml" ) != NULL )
570                                 break;
571                 }
572
573                 // Tell about the framework container producers
574                 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
575                 {
576                         char *resource = mlt_properties_get( properties, "resource" );
577                         
578                         // Recurse on multitrack's tracks
579                         if ( strcmp( resource, "<multitrack>" ) == 0 )
580                         {
581                                 serialise_multitrack( context, service, node );
582                                 break;
583                         }
584                         
585                         // Recurse on playlist's clips
586                         else if ( strcmp( resource, "<playlist>" ) == 0 )
587                         {
588                                 serialise_playlist( context, service, node );
589                         }
590                         
591                         // Recurse on tractor's producer
592                         else if ( strcmp( resource, "<tractor>" ) == 0 )
593                         {
594                                 context->pass = 0;
595                                 serialise_tractor( context, service, node );
596                                 context->pass = 1;
597                                 serialise_tractor( context, service, node );
598                                 context->pass = 0;
599                                 break;
600                         }
601
602                         // Treat it as a normal producer
603                         else
604                         {
605                                 serialise_producer( context, service, node );
606                         }
607                 }
608                 
609                 // Tell about a filter
610                 else if ( strcmp( mlt_type, "filter" ) == 0 )
611                 {
612                         serialise_filter( context, service, node );
613                         break;
614                 }
615                 
616                 // Tell about a transition
617                 else if ( strcmp( mlt_type, "transition" ) == 0 )
618                 {
619                         serialise_transition( context, service, node );
620                         break;
621                 }
622                 
623                 // Get the next connected service
624                 service = mlt_service_producer( service );
625         }
626 }
627
628 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
629 {
630         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
631         xmlDocPtr doc = xmlNewDoc( _x("1.0") );
632         xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
633         struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
634         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
635         char tmpstr[ 32 ];
636
637         xmlDocSetRootElement( doc, root );
638
639         // Indicate the numeric locale
640         xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
641
642         // If we have root, then deal with it now
643         if ( mlt_properties_get( properties, "root" ) != NULL )
644         {
645                 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
646                 context->root = strdup( mlt_properties_get( properties, "root" ) );
647         }
648         else
649         {
650                 context->root = strdup( "" );
651         }
652
653         // Assign the additional 'storage' pattern for properties
654         context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
655
656         // Assign a title property
657         if ( mlt_properties_get( properties, "title" ) != NULL )
658                 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
659         mlt_properties_set_int( properties, "global_feed", 1 );
660
661         // Add a profile child element
662         if ( profile )
663         {
664                 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
665                 if ( profile->description )
666                         xmlNewProp( profile_node, _x("description"), _x(profile->description) );
667                 sprintf( tmpstr, "%d", profile->width );
668                 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
669                 sprintf( tmpstr, "%d", profile->height );
670                 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
671                 sprintf( tmpstr, "%d", profile->progressive );
672                 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
673                 sprintf( tmpstr, "%d", profile->sample_aspect_num );
674                 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
675                 sprintf( tmpstr, "%d", profile->sample_aspect_den );
676                 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
677                 sprintf( tmpstr, "%d", profile->display_aspect_num );
678                 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
679                 sprintf( tmpstr, "%d", profile->display_aspect_den );
680                 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
681                 sprintf( tmpstr, "%d", profile->frame_rate_num );
682                 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
683                 sprintf( tmpstr, "%d", profile->frame_rate_den );
684                 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
685                 sprintf( tmpstr, "%d", profile->colorspace );
686                 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
687         }
688
689         // Construct the context maps
690         context->id_map = mlt_properties_new();
691         context->hide_map = mlt_properties_new();
692         
693         // Ensure producer is a framework producer
694         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
695
696         // In pass one, we serialise the end producers and playlists,
697         // adding them to a map keyed by address.
698         serialise_service( context, service, root );
699
700         // In pass two, we serialise the tractor and reference the
701         // producers and playlists
702         context->pass++;
703         serialise_service( context, service, root );
704
705         // Cleanup resource
706         mlt_properties_close( context->id_map );
707         mlt_properties_close( context->hide_map );
708         free( context->root );
709         free( context );
710         
711         return doc;
712 }
713
714 static int consumer_start( mlt_consumer this )
715 {
716         xmlDocPtr doc = NULL;
717         
718         // Get the producer service
719         mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
720         if ( service != NULL )
721         {
722                 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
723                 char *resource =  mlt_properties_get( properties, "resource" );
724
725                 // Set the title if provided
726                 if ( mlt_properties_get( properties, "title" ) )
727                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
728                 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
729                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
730
731                 // Check for a root on the consumer properties and pass to service
732                 if ( mlt_properties_get( properties, "root" ) )
733                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
734
735                 // Specify roots in other cases...
736                 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
737                 {
738                         // Get the current working directory
739                         char *cwd = getcwd( NULL, 0 );
740                         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
741                         free( cwd );
742                 }
743
744                 // Make the document
745                 doc = xml_make_doc( this, service );
746
747                 // Handle the output
748                 if ( resource == NULL || !strcmp( resource, "" ) )
749                 {
750                         xmlDocFormatDump( stdout, doc, 1 );
751                 }
752                 else if ( strchr( resource, '.' ) == NULL )
753                 {
754                         xmlChar *buffer = NULL;
755                         int length = 0;
756                         xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
757                         mlt_properties_set( properties, resource, _s(buffer) );
758 #ifdef WIN32
759                         xmlFreeFunc xmlFree = NULL;
760                         xmlMemGet( &xmlFree, NULL, NULL, NULL);
761 #endif
762                         xmlFree( buffer );
763                 }
764                 else
765                 {
766                         xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
767                 }
768                 
769                 // Close the document
770                 xmlFreeDoc( doc );
771         }
772         
773         mlt_consumer_stop( this );
774
775         mlt_consumer_stopped( this );
776
777         return 0;
778 }
779
780 static int consumer_is_stopped( mlt_consumer this )
781 {
782         return 1;
783 }