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