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