]> git.sesse.net Git - mlt/blob - src/modules/xml/consumer_xml.c
quick fix for xml containing bad characters
[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 #include <pthread.h>
29 #ifdef FILTER_WCHAR
30 #include <wchar.h>
31 #endif
32
33 #define ID_SIZE 128
34
35 #define _x (const xmlChar*)
36 #define _s (const char*)
37
38 // This maintains counters for adding ids to elements
39 struct serialise_context_s
40 {
41         mlt_properties id_map;
42         int producer_count;
43         int multitrack_count;
44         int playlist_count;
45         int tractor_count;
46         int filter_count;
47         int transition_count;
48         int pass;
49         mlt_properties hide_map;
50         char *root;
51         char *store;
52         int no_meta;
53 };
54 typedef struct serialise_context_s* serialise_context;
55
56 /** Forward references to static functions.
57 */
58
59 static int consumer_start( mlt_consumer parent );
60 static int consumer_stop( mlt_consumer parent );
61 static int consumer_is_stopped( mlt_consumer this );
62 static void *consumer_thread( void *arg );
63 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
64
65 #ifdef FILTER_WCHAR
66
67 void* filter_restricted( const wchar_t *in )
68 {
69         if ( !in ) return NULL;
70         wchar_t *out = calloc( 1, strlen( (const char*) in ) );
71         size_t i, j, n = wcslen( in );
72         for ( i = 0, j = 0; i < n; i++ )
73         {
74                 wchar_t w = in[i];
75                 if ( w == 0x9 || w == 0xA || w == 0xD ||
76                                 ( w >= 0x20 && w <= 0xD7FF ) ||
77                                 ( w >= 0xE000 && w <= 0xFFFD ) ||
78                                 ( w >= 0x10000 && w <= 0x10FFFF ) )
79                         out[ j++ ] = w;
80         }
81         return out;
82 }
83
84 #else
85
86 void* filter_restricted( const char *in )
87 {
88         if ( !in ) return NULL;
89         char *out = calloc( 1, strlen( in ) );
90         size_t i, j, n = strlen( in );
91         for ( i = 0, j = 0; i < n; i++ )
92         {
93                 char c = in[i];
94                 if ( c == 0x9 || c == 0xA || c == 0xD || ( c >= 0x20 && c <= 0xFF ) )
95                         out[ j++ ] = c;
96         }
97         return out;
98 }
99
100 #endif
101
102 typedef enum 
103 {
104         xml_existing,
105         xml_producer,
106         xml_multitrack,
107         xml_playlist,
108         xml_tractor,
109         xml_filter,
110         xml_transition
111 }
112 xml_type;
113
114 /** Create or retrieve an id associated to this service.
115 */
116
117 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
118 {
119         char *id = NULL;
120         int i = 0;
121         mlt_properties map = context->id_map;
122
123         // Search the map for the service
124         for ( i = 0; i < mlt_properties_count( map ); i ++ )
125                 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
126                         break;
127
128         // If the service is not in the map, and the type indicates a new id is needed...
129         if ( i >= mlt_properties_count( map ) && type != xml_existing )
130         {
131                 // Attempt to reuse existing id
132                 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
133
134                 // If no id, or the id is used in the map (for another service), then 
135                 // create a new one.
136                 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
137                 {
138                         char temp[ ID_SIZE ];
139                         do
140                         {
141                                 switch( type )
142                                 {
143                                         case xml_producer:
144                                                 sprintf( temp, "producer%d", context->producer_count ++ );
145                                                 break;
146                                         case xml_multitrack:
147                                                 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
148                                                 break;
149                                         case xml_playlist:
150                                                 sprintf( temp, "playlist%d", context->playlist_count ++ );
151                                                 break;
152                                         case xml_tractor:
153                                                 sprintf( temp, "tractor%d", context->tractor_count ++ );
154                                                 break;
155                                         case xml_filter:
156                                                 sprintf( temp, "filter%d", context->filter_count ++ );
157                                                 break;
158                                         case xml_transition:
159                                                 sprintf( temp, "transition%d", context->transition_count ++ );
160                                                 break;
161                                         case xml_existing:
162                                                 // Never gets here
163                                                 break;
164                                 }
165                         }
166                         while( mlt_properties_get_data( map, temp, NULL ) != NULL );
167
168                         // Set the data at the generated name
169                         mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
170
171                         // Get the pointer to the name (i is the end of the list)
172                         id = mlt_properties_get_name( map, i );
173                 }
174                 else
175                 {
176                         // Store the existing id in the map
177                         mlt_properties_set_data( map, id, service, 0, NULL, NULL );
178                 }
179         }
180         else if ( type == xml_existing )
181         {
182                 id = mlt_properties_get_name( map, i );
183         }
184
185         return id;
186 }
187
188 /** This is what will be called by the factory - anything can be passed in
189         via the argument, but keep it simple.
190 */
191
192 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
193 {
194         // Create the consumer object
195         mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
196
197         // If no malloc'd and consumer init ok
198         if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
199         {
200                 // Allow thread to be started/stopped
201                 this->start = consumer_start;
202                 this->stop = consumer_stop;
203                 this->is_stopped = consumer_is_stopped;
204
205                 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
206                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
207                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
208                 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
209
210                 // Return the consumer produced
211                 return this;
212         }
213
214         // malloc or consumer init failed
215         free( this );
216
217         // Indicate failure
218         return NULL;
219 }
220
221 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
222 {
223         int i;
224         xmlNode *p;
225         
226         // Enumerate the properties
227         for ( i = 0; i < mlt_properties_count( properties ); i++ )
228         {
229                 char *name = mlt_properties_get_name( properties, i );
230                 if ( name != NULL &&
231                          name[ 0 ] != '_' &&
232                          mlt_properties_get_value( properties, i ) != NULL &&
233                          ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
234                          strcmp( name, "mlt" ) &&
235                          strcmp( name, "in" ) &&
236                          strcmp( name, "out" ) &&
237                          strcmp( name, "id" ) &&
238                          strcmp( name, "title" ) &&
239                          strcmp( name, "root" ) &&
240                          strcmp( name, "width" ) &&
241                          strcmp( name, "height" ) )
242                 {
243                         char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
244                         if ( value )
245                         {
246                                 int rootlen = strlen( context->root );
247                                 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
248                                         value += rootlen + 1;
249                                 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
250                                 xmlNewProp( p, _x("name"), _x(name) );
251                                 free( value );
252                         }
253                 }
254         }
255 }
256
257 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
258 {
259         int i;
260         xmlNode *p;
261         
262         // Enumerate the properties
263         for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
264         {
265                 char *name = mlt_properties_get_name( properties, i );
266                 if ( !strncmp( name, store, strlen( store ) ) )
267                 {
268                         char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
269                         if ( value )
270                         {
271                                 int rootlen = strlen( context->root );
272                                 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
273                                         value += rootlen + 1;
274                                 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
275                                 xmlNewProp( p, _x("name"), _x(name) );
276                                 free( value );
277                         }
278                 }
279         }
280 }
281
282 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
283 {
284         int i;
285         xmlNode *p;
286         mlt_filter filter = NULL;
287         
288         // Enumerate the filters
289         for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
290         {
291                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
292                 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
293                 {
294                         // Get a new id - if already allocated, do nothing
295                         char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
296                         if ( id != NULL )
297                         {
298                                 int in = mlt_properties_get_position( properties, "in" );
299                                 int out = mlt_properties_get_position( properties, "out" );
300                                 p = xmlNewChild( node, NULL, _x("filter"), NULL );
301                                 xmlNewProp( p, _x("id"), _x(id) );
302                                 if ( mlt_properties_get( properties, "title" ) )
303                                         xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
304                                 if ( in != 0 || out != 0 )
305                                 {
306                                         char temp[ 20 ];
307                                         sprintf( temp, "%d", in );
308                                         xmlNewProp( p, _x("in"), _x(temp) );
309                                         sprintf( temp, "%d", out );
310                                         xmlNewProp( p, _x("out"), _x(temp) );
311                                 }
312                                 serialise_properties( context, properties, p );
313                                 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
314                         }
315                 }
316         }
317 }
318
319 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
320 {
321         xmlNode *child = node;
322         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
323         
324         if ( context->pass == 0 )
325         {
326                 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
327                 // Get a new id - if already allocated, do nothing
328                 char *id = xml_get_id( context, parent, xml_producer );
329                 if ( id == NULL )
330                         return;
331
332                 child = xmlNewChild( node, NULL, _x("producer"), NULL );
333
334                 // Set the id
335                 xmlNewProp( child, _x("id"), _x(id) );
336                 if ( mlt_properties_get( properties, "title" ) )
337                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
338                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
339                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
340                 serialise_properties( context, properties, child );
341                 serialise_service_filters( context, service, child );
342
343                 // Add producer to the map
344                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
345         }
346         else
347         {
348                 char *id = xml_get_id( context, parent, xml_existing );
349                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
350                 xmlNewProp( node, _x("parent"), _x(id) );
351                 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
352                 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
353         }
354 }
355
356 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
357
358 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
359 {
360         int i;
361         
362         if ( context->pass == 0 )
363         {
364                 // Iterate over the tracks to collect the producers
365                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
366                 {
367                         mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
368                         serialise_service( context, MLT_SERVICE( producer ), node );
369                 }
370         }
371         else
372         {
373                 // Get a new id - if already allocated, do nothing
374                 char *id = xml_get_id( context, service, xml_multitrack );
375                 if ( id == NULL )
376                         return;
377
378                 // Serialise the tracks
379                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
380                 {
381                         xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
382                         int hide = 0;
383                         mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
384                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
385
386                         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
387
388                         char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
389                         xmlNewProp( track, _x("producer"), _x(id) );
390                         if ( mlt_producer_is_cut( producer ) )
391                         {
392                                 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
393                                 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
394                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
395                                 if ( !context->no_meta )
396                                         serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
397                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
398                         }
399                         
400                         hide = mlt_properties_get_int( context->hide_map, id );
401                         if ( hide )
402                                 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
403                 }
404                 serialise_service_filters( context, service, node );
405         }
406 }
407
408 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
409 {
410         int i;
411         xmlNode *child = node;
412         mlt_playlist_clip_info info;
413         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
414         
415         if ( context->pass == 0 )
416         {
417                 // Get a new id - if already allocated, do nothing
418                 char *id = xml_get_id( context, service, xml_playlist );
419                 if ( id == NULL )
420                         return;
421
422                 // Iterate over the playlist entries to collect the producers
423                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
424                 {
425                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
426                         {
427                                 if ( info.producer != NULL )
428                                 {
429                                         mlt_producer producer = mlt_producer_cut_parent( info.producer );
430                                         char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
431                                         char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
432                                         if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
433                                                 serialise_playlist( context, MLT_SERVICE( producer ), node );
434                                         else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
435                                                 serialise_service( context, MLT_SERVICE( producer ), node );
436                                 }
437                         }
438                 }
439                 
440                 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
441
442                 // Set the id
443                 xmlNewProp( child, _x("id"), _x(id) );
444                 if ( mlt_properties_get( properties, "title" ) )
445                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
446
447                 // Store application specific properties
448                 serialise_store_properties( context, properties, child, context->store );
449                 if ( !context->no_meta )
450                         serialise_store_properties( context, properties, child, "meta." );
451
452                 // Add producer to the map
453                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
454         
455                 // Iterate over the playlist entries
456                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
457                 {
458                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
459                         {
460                                 mlt_producer producer = mlt_producer_cut_parent( info.producer );
461                                 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
462                                 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
463                                 {
464                                         char length[ 20 ];
465                                         length[ 19 ] = '\0';
466                                         xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
467                                         snprintf( length, 19, "%d", (int)info.frame_count );
468                                         xmlNewProp( entry, _x("length"), _x(length) );
469                                 }
470                                 else
471                                 {
472                                         char temp[ 20 ];
473                                         xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
474                                         id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
475                                         xmlNewProp( entry, _x("producer"), _x(id) );
476                                         sprintf( temp, "%d", (int)info.frame_in );
477                                         xmlNewProp( entry, _x("in"), _x(temp) );
478                                         sprintf( temp, "%d", (int)info.frame_out );
479                                         xmlNewProp( entry, _x("out"), _x(temp) );
480                                         if ( info.repeat > 1 )
481                                         {
482                                                 sprintf( temp, "%d", info.repeat );
483                                                 xmlNewProp( entry, _x("repeat"), _x(temp) );
484                                         }
485                                         if ( mlt_producer_is_cut( info.cut ) )
486                                         {
487                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
488                                                 if ( !context->no_meta )
489                                                         serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
490                                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
491                                         }
492                                 }
493                         }
494                 }
495
496                 serialise_service_filters( context, service, child );
497         }
498         else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
499         {
500                 char *id = xml_get_id( context, service, xml_existing );
501                 xmlNewProp( node, _x("producer"), _x(id) );
502         }
503 }
504
505 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
506 {
507         xmlNode *child = node;
508         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
509         
510         if ( context->pass == 0 )
511         {
512                 // Recurse on connected producer
513                 serialise_service( context, mlt_service_producer( service ), node );
514         }
515         else
516         {
517                 // Get a new id - if already allocated, do nothing
518                 char *id = xml_get_id( context, service, xml_tractor );
519                 if ( id == NULL )
520                         return;
521
522                 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
523
524                 // Set the id
525                 xmlNewProp( child, _x("id"), _x(id) );
526                 if ( mlt_properties_get( properties, "title" ) )
527                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
528                 if ( mlt_properties_get( properties, "global_feed" ) )
529                         xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
530                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
531                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
532
533                 // Store application specific properties
534                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
535                 if ( !context->no_meta )
536                         serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
537
538                 // Recurse on connected producer
539                 serialise_service( context, mlt_service_producer( service ), child );
540                 serialise_service_filters( context, service, child );
541         }
542 }
543
544 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
545 {
546         xmlNode *child = node;
547         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
548         
549         // Recurse on connected producer
550         serialise_service( context, mlt_service_producer( service ), node );
551
552         if ( context->pass == 1 )
553         {
554                 // Get a new id - if already allocated, do nothing
555                 char *id = xml_get_id( context, service, xml_filter );
556                 if ( id == NULL )
557                         return;
558
559                 child = xmlNewChild( node, NULL, _x("filter"), NULL );
560
561                 // Set the id
562                 xmlNewProp( child, _x("id"), _x(id) );
563                 if ( mlt_properties_get( properties, "title" ) )
564                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
565                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
566                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
567
568                 serialise_properties( context, properties, child );
569                 serialise_service_filters( context, service, child );
570         }
571 }
572
573 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
574 {
575         xmlNode *child = node;
576         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
577         
578         // Recurse on connected producer
579         serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
580
581         if ( context->pass == 1 )
582         {
583                 // Get a new id - if already allocated, do nothing
584                 char *id = xml_get_id( context, service, xml_transition );
585                 if ( id == NULL )
586                         return;
587
588                 child = xmlNewChild( node, NULL, _x("transition"), NULL );
589         
590                 // Set the id
591                 xmlNewProp( child, _x("id"), _x(id) );
592                 if ( mlt_properties_get( properties, "title" ) )
593                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
594                 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
595                 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
596
597                 serialise_properties( context, properties, child );
598                 serialise_service_filters( context, service, child );
599         }
600 }
601
602 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
603 {
604         // Iterate over consumer/producer connections
605         while ( service != NULL )
606         {
607                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
608                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
609                 
610                 // Tell about the producer
611                 if ( strcmp( mlt_type, "producer" ) == 0 )
612                 {
613                         char *mlt_service = mlt_properties_get( properties, "mlt_service" );
614                         if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
615                         {
616                                 context->pass = 0;
617                                 serialise_tractor( context, service, node );
618                                 context->pass = 1;
619                                 serialise_tractor( context, service, node );
620                                 context->pass = 0;
621                                 break;
622                         }
623                         else
624                         {
625                                 serialise_producer( context, service, node );
626                         }
627                         if ( mlt_properties_get( properties, "xml" ) != NULL )
628                                 break;
629                 }
630
631                 // Tell about the framework container producers
632                 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
633                 {
634                         char *resource = mlt_properties_get( properties, "resource" );
635                         
636                         // Recurse on multitrack's tracks
637                         if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
638                         {
639                                 serialise_multitrack( context, service, node );
640                                 break;
641                         }
642                         
643                         // Recurse on playlist's clips
644                         else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
645                         {
646                                 serialise_playlist( context, service, node );
647                         }
648                         
649                         // Recurse on tractor's producer
650                         else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
651                         {
652                                 context->pass = 0;
653                                 serialise_tractor( context, service, node );
654                                 context->pass = 1;
655                                 serialise_tractor( context, service, node );
656                                 context->pass = 0;
657                                 break;
658                         }
659
660                         // Treat it as a normal producer
661                         else
662                         {
663                                 serialise_producer( context, service, node );
664                         }
665                 }
666                 
667                 // Tell about a filter
668                 else if ( strcmp( mlt_type, "filter" ) == 0 )
669                 {
670                         serialise_filter( context, service, node );
671                         break;
672                 }
673                 
674                 // Tell about a transition
675                 else if ( strcmp( mlt_type, "transition" ) == 0 )
676                 {
677                         serialise_transition( context, service, node );
678                         break;
679                 }
680                 
681                 // Get the next connected service
682                 service = mlt_service_producer( service );
683         }
684 }
685
686 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
687 {
688         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
689         xmlDocPtr doc = xmlNewDoc( _x("1.0") );
690         xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
691         struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
692         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
693         char tmpstr[ 32 ];
694
695         xmlDocSetRootElement( doc, root );
696
697         // Indicate the numeric locale
698         xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
699
700         // Indicate the version
701         xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
702
703         // If we have root, then deal with it now
704         if ( mlt_properties_get( properties, "root" ) != NULL )
705         {
706                 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
707                 context->root = strdup( mlt_properties_get( properties, "root" ) );
708         }
709         else
710         {
711                 context->root = strdup( "" );
712         }
713
714         // Assign the additional 'storage' pattern for properties
715         context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
716         context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
717
718         // Assign a title property
719         if ( mlt_properties_get( properties, "title" ) != NULL )
720                 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
721         mlt_properties_set_int( properties, "global_feed", 1 );
722
723         // Add a profile child element
724         if ( profile )
725         {
726                 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
727                 if ( profile->description )
728                         xmlNewProp( profile_node, _x("description"), _x(profile->description) );
729                 sprintf( tmpstr, "%d", profile->width );
730                 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
731                 sprintf( tmpstr, "%d", profile->height );
732                 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
733                 sprintf( tmpstr, "%d", profile->progressive );
734                 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
735                 sprintf( tmpstr, "%d", profile->sample_aspect_num );
736                 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
737                 sprintf( tmpstr, "%d", profile->sample_aspect_den );
738                 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
739                 sprintf( tmpstr, "%d", profile->display_aspect_num );
740                 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
741                 sprintf( tmpstr, "%d", profile->display_aspect_den );
742                 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
743                 sprintf( tmpstr, "%d", profile->frame_rate_num );
744                 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
745                 sprintf( tmpstr, "%d", profile->frame_rate_den );
746                 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
747                 sprintf( tmpstr, "%d", profile->colorspace );
748                 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
749         }
750
751         // Construct the context maps
752         context->id_map = mlt_properties_new();
753         context->hide_map = mlt_properties_new();
754         
755         // Ensure producer is a framework producer
756         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
757
758         // In pass one, we serialise the end producers and playlists,
759         // adding them to a map keyed by address.
760         serialise_service( context, service, root );
761
762         // In pass two, we serialise the tractor and reference the
763         // producers and playlists
764         context->pass++;
765         serialise_service( context, service, root );
766
767         // Cleanup resource
768         mlt_properties_close( context->id_map );
769         mlt_properties_close( context->hide_map );
770         free( context->root );
771         free( context );
772         
773         return doc;
774 }
775
776
777 static void output_xml( mlt_consumer this )
778 {
779         // Get the producer service
780         mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
781         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
782         char *resource =  mlt_properties_get( properties, "resource" );
783         xmlDocPtr doc = NULL;
784
785         if ( !service ) return;
786
787         // Set the title if provided
788         if ( mlt_properties_get( properties, "title" ) )
789                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
790         else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
791                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
792
793         // Check for a root on the consumer properties and pass to service
794         if ( mlt_properties_get( properties, "root" ) )
795                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
796
797         // Specify roots in other cases...
798         if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
799         {
800                 // Get the current working directory
801                 char *cwd = getcwd( NULL, 0 );
802                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
803                 free( cwd );
804         }
805
806         // Make the document
807         doc = xml_make_doc( this, service );
808
809         // Handle the output
810         if ( resource == NULL || !strcmp( resource, "" ) )
811         {
812                 xmlDocFormatDump( stdout, doc, 1 );
813         }
814         else if ( strchr( resource, '.' ) == NULL )
815         {
816                 xmlChar *buffer = NULL;
817                 int length = 0;
818                 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
819                 mlt_properties_set( properties, resource, _s(buffer) );
820 #ifdef WIN32
821                 xmlFreeFunc xmlFree = NULL;
822                 xmlMemGet( &xmlFree, NULL, NULL, NULL);
823 #endif
824                 xmlFree( buffer );
825         }
826         else
827         {
828                 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
829         }
830
831         // Close the document
832         xmlFreeDoc( doc );
833 }
834 static int consumer_start( mlt_consumer this )
835 {
836         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
837
838         if ( mlt_properties_get_int( properties, "all" ) )
839         {
840                 // Check that we're not already running
841                 if ( !mlt_properties_get_int( properties, "running" ) )
842                 {
843                         // Allocate a thread
844                         pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
845
846                         // Assign the thread to properties
847                         mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
848
849                         // Set the running state
850                         mlt_properties_set_int( properties, "running", 1 );
851                         mlt_properties_set_int( properties, "joined", 0 );
852
853                         // Create the thread
854                         pthread_create( thread, NULL, consumer_thread, this );
855                 }
856         }
857         else
858         {
859                 output_xml( this );
860                 mlt_consumer_stop( this );
861                 mlt_consumer_stopped( this );
862         }
863         return 0;
864 }
865
866 static int consumer_is_stopped( mlt_consumer this )
867 {
868         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
869         return !mlt_properties_get_int( properties, "running" );
870 }
871
872 static int consumer_stop( mlt_consumer this )
873 {
874         // Get the properties
875         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
876
877         // Check that we're running
878         if ( !mlt_properties_get_int( properties, "joined" ) )
879         {
880                 // Get the thread
881                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
882
883                 // Stop the thread
884                 mlt_properties_set_int( properties, "running", 0 );
885                 mlt_properties_set_int( properties, "joined", 1 );
886
887                 // Wait for termination
888                 if ( thread )
889                         pthread_join( *thread, NULL );
890         }
891
892         return 0;
893 }
894
895 static void *consumer_thread( void *arg )
896 {
897         // Map the argument to the object
898         mlt_consumer this = arg;
899
900         // Get the properties
901         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
902
903         // Convenience functionality
904         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
905         int terminated = 0;
906
907         // Frame and size
908         mlt_frame frame = NULL;
909
910         // Loop while running
911         while( !terminated && mlt_properties_get_int( properties, "running" ) )
912         {
913                 // Get the frame
914                 frame = mlt_consumer_rt_frame( this );
915
916                 // Check for termination
917                 if ( terminate_on_pause && frame != NULL )
918                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
919
920                 // Check that we have a frame to work with
921                 if ( frame )
922                 {
923                         int width = 0, height = 0;
924                         int frequency = mlt_properties_get_int( properties, "frequency" );
925                         int channels = mlt_properties_get_int( properties, "channels" );
926                         int samples = 0;
927                         mlt_image_format iformat = mlt_image_yuv422;
928                         mlt_audio_format aformat = mlt_audio_s16;
929                         uint8_t *buffer;
930
931                         mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
932                         mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
933
934                         // Close the frame
935                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
936                         mlt_frame_close( frame );
937                 }
938         }
939         output_xml( this );
940
941         // Indicate that the consumer is stopped
942         mlt_properties_set_int( properties, "running", 0 );
943         mlt_consumer_stopped( this );
944
945         return NULL;
946 }