]> git.sesse.net Git - mlt/blob - src/modules/xml/consumer_xml.c
add time_format property to xml consumer
[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( sizeof( struct mlt_consumer_s ), 1 );
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 = filter_restricted( mlt_properties_get_value( properties, i ) );
238                         if ( !strcmp( name, "length" ) )
239                                 value = strdup( mlt_properties_get_time( properties, name, context->time_format ) );
240                         if ( value )
241                         {
242                                 int rootlen = strlen( context->root );
243                                 // convert absolute path to relative
244                                 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
245                                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1 ) );
246                                 else
247                                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
248                                 xmlNewProp( p, _x("name"), _x(name) );
249                                 free( value );
250                         }
251                 }
252         }
253 }
254
255 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
256 {
257         int i;
258         xmlNode *p;
259
260         // Enumerate the properties
261         for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
262         {
263                 char *name = mlt_properties_get_name( properties, i );
264                 if ( !strncmp( name, store, strlen( store ) ) )
265                 {
266                         char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
267                         if ( value )
268                         {
269                                 int rootlen = strlen( context->root );
270                                 // convert absolute path to relative
271                                 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
272                                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
273                                 else
274                                         p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
275                                 xmlNewProp( p, _x("name"), _x(name) );
276                                 free( value );
277                         }
278                 }
279         }
280 }
281
282 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
283 {
284         int i;
285         xmlNode *p;
286         mlt_filter filter = NULL;
287
288         // Enumerate the filters
289         for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
290         {
291                 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
292                 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
293                 {
294                         // Get a new id - if already allocated, do nothing
295                         char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
296                         if ( id != NULL )
297                         {
298                                 p = xmlNewChild( node, NULL, _x("filter"), NULL );
299                                 xmlNewProp( p, _x("id"), _x(id) );
300                                 if ( mlt_properties_get( properties, "title" ) )
301                                         xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
302                                 if ( mlt_properties_get_position( properties, "in" ) )
303                                         xmlNewProp( p, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
304                                 if ( mlt_properties_get_position( properties, "out" ) )
305                                         xmlNewProp( p, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
306                                 serialise_properties( context, properties, p );
307                                 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
308                         }
309                 }
310         }
311 }
312
313 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
314 {
315         xmlNode *child = node;
316         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
317
318         if ( context->pass == 0 )
319         {
320                 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
321                 // Get a new id - if already allocated, do nothing
322                 char *id = xml_get_id( context, parent, xml_producer );
323                 if ( id == NULL )
324                         return;
325
326                 child = xmlNewChild( node, NULL, _x("producer"), NULL );
327
328                 // Set the id
329                 xmlNewProp( child, _x("id"), _x(id) );
330                 if ( mlt_properties_get( properties, "title" ) )
331                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
332                 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
333                 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
334                 serialise_properties( context, properties, child );
335                 serialise_service_filters( context, service, child );
336
337                 // Add producer to the map
338                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
339         }
340         else
341         {
342                 char *id = xml_get_id( context, parent, xml_existing );
343                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
344                 xmlNewProp( node, _x("parent"), _x(id) );
345                 xmlNewProp( node, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
346                 xmlNewProp( node, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
347         }
348 }
349
350 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
351
352 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
353 {
354         int i;
355
356         if ( context->pass == 0 )
357         {
358                 // Iterate over the tracks to collect the producers
359                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
360                 {
361                         mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
362                         serialise_service( context, MLT_SERVICE( producer ), node );
363                 }
364         }
365         else
366         {
367                 // Get a new id - if already allocated, do nothing
368                 char *id = xml_get_id( context, service, xml_multitrack );
369                 if ( id == NULL )
370                         return;
371
372                 // Serialise the tracks
373                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
374                 {
375                         xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
376                         int hide = 0;
377                         mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
378                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
379
380                         mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
381
382                         char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
383                         xmlNewProp( track, _x("producer"), _x(id) );
384                         if ( mlt_producer_is_cut( producer ) )
385                         {
386                                 xmlNewProp( track, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
387                                 xmlNewProp( track, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
388                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
389                                 if ( !context->no_meta )
390                                         serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
391                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
392                         }
393
394                         hide = mlt_properties_get_int( context->hide_map, id );
395                         if ( hide )
396                                 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
397                 }
398                 serialise_service_filters( context, service, node );
399         }
400 }
401
402 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
403 {
404         int i;
405         xmlNode *child = node;
406         mlt_playlist_clip_info info;
407         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
408
409         if ( context->pass == 0 )
410         {
411                 // Get a new id - if already allocated, do nothing
412                 char *id = xml_get_id( context, service, xml_playlist );
413                 if ( id == NULL )
414                         return;
415
416                 // Iterate over the playlist entries to collect the producers
417                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
418                 {
419                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
420                         {
421                                 if ( info.producer != NULL )
422                                 {
423                                         mlt_producer producer = mlt_producer_cut_parent( info.producer );
424                                         char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
425                                         char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
426                                         if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
427                                                 serialise_playlist( context, MLT_SERVICE( producer ), node );
428                                         else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
429                                                 serialise_service( context, MLT_SERVICE( producer ), node );
430                                 }
431                         }
432                 }
433
434                 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
435
436                 // Set the id
437                 xmlNewProp( child, _x("id"), _x(id) );
438                 if ( mlt_properties_get( properties, "title" ) )
439                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
440
441                 // Store application specific properties
442                 serialise_store_properties( context, properties, child, context->store );
443                 if ( !context->no_meta )
444                         serialise_store_properties( context, properties, child, "meta." );
445
446                 // Add producer to the map
447                 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
448
449                 // Iterate over the playlist entries
450                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
451                 {
452                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
453                         {
454                                 mlt_producer producer = mlt_producer_cut_parent( info.producer );
455                                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
456                                 char *service_s = mlt_properties_get( producer_props, "mlt_service" );
457                                 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
458                                 {
459                                         xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
460                                         mlt_properties_set_data( producer_props, "_profile", context->profile, 0, NULL, NULL );
461                                         mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_count );
462                                         xmlNewProp( entry, _x("length"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
463                                 }
464                                 else
465                                 {
466                                         char temp[ 20 ];
467                                         xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
468                                         id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
469                                         xmlNewProp( entry, _x("producer"), _x(id) );
470                                         mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_in );
471                                         xmlNewProp( entry, _x("in"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
472                                         mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_out );
473                                         xmlNewProp( entry, _x("out"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
474                                         if ( info.repeat > 1 )
475                                         {
476                                                 sprintf( temp, "%d", info.repeat );
477                                                 xmlNewProp( entry, _x("repeat"), _x(temp) );
478                                         }
479                                         if ( mlt_producer_is_cut( info.cut ) )
480                                         {
481                                                 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
482                                                 if ( !context->no_meta )
483                                                         serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
484                                                 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
485                                         }
486                                 }
487                         }
488                 }
489
490                 serialise_service_filters( context, service, child );
491         }
492         else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
493         {
494                 char *id = xml_get_id( context, service, xml_existing );
495                 xmlNewProp( node, _x("producer"), _x(id) );
496         }
497 }
498
499 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
500 {
501         xmlNode *child = node;
502         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
503
504         if ( context->pass == 0 )
505         {
506                 // Recurse on connected producer
507                 serialise_service( context, mlt_service_producer( service ), node );
508         }
509         else
510         {
511                 // Get a new id - if already allocated, do nothing
512                 char *id = xml_get_id( context, service, xml_tractor );
513                 if ( id == NULL )
514                         return;
515
516                 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
517
518                 // Set the id
519                 xmlNewProp( child, _x("id"), _x(id) );
520                 if ( mlt_properties_get( properties, "title" ) )
521                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
522                 if ( mlt_properties_get( properties, "global_feed" ) )
523                         xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
524                 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
525                 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
526
527                 // Store application specific properties
528                 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
529                 if ( !context->no_meta )
530                         serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
531
532                 // Recurse on connected producer
533                 serialise_service( context, mlt_service_producer( service ), child );
534                 serialise_service_filters( context, service, child );
535         }
536 }
537
538 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
539 {
540         xmlNode *child = node;
541         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
542
543         // Recurse on connected producer
544         serialise_service( context, mlt_service_producer( service ), node );
545
546         if ( context->pass == 1 )
547         {
548                 // Get a new id - if already allocated, do nothing
549                 char *id = xml_get_id( context, service, xml_filter );
550                 if ( id == NULL )
551                         return;
552
553                 child = xmlNewChild( node, NULL, _x("filter"), NULL );
554
555                 // Set the id
556                 xmlNewProp( child, _x("id"), _x(id) );
557                 if ( mlt_properties_get( properties, "title" ) )
558                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
559                 if ( mlt_properties_get_position( properties, "in" ) )
560                         xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
561                 if ( mlt_properties_get_position( properties, "out" ) )
562                         xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
563
564                 serialise_properties( context, properties, child );
565                 serialise_service_filters( context, service, child );
566         }
567 }
568
569 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
570 {
571         xmlNode *child = node;
572         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
573
574         // Recurse on connected producer
575         serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
576
577         if ( context->pass == 1 )
578         {
579                 // Get a new id - if already allocated, do nothing
580                 char *id = xml_get_id( context, service, xml_transition );
581                 if ( id == NULL )
582                         return;
583
584                 child = xmlNewChild( node, NULL, _x("transition"), NULL );
585
586                 // Set the id
587                 xmlNewProp( child, _x("id"), _x(id) );
588                 if ( mlt_properties_get( properties, "title" ) )
589                         xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
590                 if ( mlt_properties_get_position( properties, "in" ) )
591                         xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
592                 if ( mlt_properties_get_position( properties, "out" ) )
593                         xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
594
595                 serialise_properties( context, properties, child );
596                 serialise_service_filters( context, service, child );
597         }
598 }
599
600 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
601 {
602         // Iterate over consumer/producer connections
603         while ( service != NULL )
604         {
605                 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
606                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
607
608                 // Tell about the producer
609                 if ( strcmp( mlt_type, "producer" ) == 0 )
610                 {
611                         char *mlt_service = mlt_properties_get( properties, "mlt_service" );
612                         if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
613                         {
614                                 context->pass = 0;
615                                 serialise_tractor( context, service, node );
616                                 context->pass = 1;
617                                 serialise_tractor( context, service, node );
618                                 context->pass = 0;
619                                 break;
620                         }
621                         else
622                         {
623                                 serialise_producer( context, service, node );
624                         }
625                         if ( mlt_properties_get( properties, "xml" ) != NULL )
626                                 break;
627                 }
628
629                 // Tell about the framework container producers
630                 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
631                 {
632                         char *resource = mlt_properties_get( properties, "resource" );
633
634                         // Recurse on multitrack's tracks
635                         if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
636                         {
637                                 serialise_multitrack( context, service, node );
638                                 break;
639                         }
640
641                         // Recurse on playlist's clips
642                         else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
643                         {
644                                 serialise_playlist( context, service, node );
645                         }
646
647                         // Recurse on tractor's producer
648                         else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
649                         {
650                                 context->pass = 0;
651                                 serialise_tractor( context, service, node );
652                                 context->pass = 1;
653                                 serialise_tractor( context, service, node );
654                                 context->pass = 0;
655                                 break;
656                         }
657
658                         // Treat it as a normal producer
659                         else
660                         {
661                                 serialise_producer( context, service, node );
662                         }
663                 }
664
665                 // Tell about a filter
666                 else if ( strcmp( mlt_type, "filter" ) == 0 )
667                 {
668                         serialise_filter( context, service, node );
669                         break;
670                 }
671
672                 // Tell about a transition
673                 else if ( strcmp( mlt_type, "transition" ) == 0 )
674                 {
675                         serialise_transition( context, service, node );
676                         break;
677                 }
678
679                 // Get the next connected service
680                 service = mlt_service_producer( service );
681         }
682 }
683
684 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
685 {
686         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
687         xmlDocPtr doc = xmlNewDoc( _x("1.0") );
688         xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
689         struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
690         mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
691         char tmpstr[ 32 ];
692
693         xmlDocSetRootElement( doc, root );
694
695         // Indicate the numeric locale
696         xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
697
698         // Indicate the version
699         xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
700
701         // If we have root, then deal with it now
702         if ( mlt_properties_get( properties, "root" ) != NULL )
703         {
704                 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
705                 context->root = strdup( mlt_properties_get( properties, "root" ) );
706         }
707         else
708         {
709                 context->root = strdup( "" );
710         }
711
712         // Assign the additional 'storage' pattern for properties
713         context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
714         context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
715         const char *time_format = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "time_format" );
716         if ( time_format && ( !strcmp( time_format, "smpte" ) || !strcmp( time_format, "SMPTE" )
717                         || !strcmp( time_format, "timecode" ) ) )
718                 context->time_format = mlt_time_smpte;
719         else if ( time_format && ( !strcmp( time_format, "clock" ) || !strcmp( time_format, "CLOCK" ) ) )
720                 context->time_format = mlt_time_clock;
721
722         // Assign a title property
723         if ( mlt_properties_get( properties, "title" ) != NULL )
724                 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
725         mlt_properties_set_int( properties, "global_feed", 1 );
726
727         // Add a profile child element
728         if ( profile )
729         {
730                 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
731                 if ( profile->description )
732                         xmlNewProp( profile_node, _x("description"), _x(profile->description) );
733                 sprintf( tmpstr, "%d", profile->width );
734                 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
735                 sprintf( tmpstr, "%d", profile->height );
736                 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
737                 sprintf( tmpstr, "%d", profile->progressive );
738                 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
739                 sprintf( tmpstr, "%d", profile->sample_aspect_num );
740                 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
741                 sprintf( tmpstr, "%d", profile->sample_aspect_den );
742                 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
743                 sprintf( tmpstr, "%d", profile->display_aspect_num );
744                 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
745                 sprintf( tmpstr, "%d", profile->display_aspect_den );
746                 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
747                 sprintf( tmpstr, "%d", profile->frame_rate_num );
748                 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
749                 sprintf( tmpstr, "%d", profile->frame_rate_den );
750                 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
751                 sprintf( tmpstr, "%d", profile->colorspace );
752                 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
753                 context->profile = profile;
754         }
755
756         // Construct the context maps
757         context->id_map = mlt_properties_new();
758         context->hide_map = mlt_properties_new();
759
760         // Ensure producer is a framework producer
761         mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
762
763         // In pass one, we serialise the end producers and playlists,
764         // adding them to a map keyed by address.
765         serialise_service( context, service, root );
766
767         // In pass two, we serialise the tractor and reference the
768         // producers and playlists
769         context->pass++;
770         serialise_service( context, service, root );
771
772         // Cleanup resource
773         mlt_properties_close( context->id_map );
774         mlt_properties_close( context->hide_map );
775         free( context->root );
776         free( context );
777
778         return doc;
779 }
780
781
782 static void output_xml( mlt_consumer this )
783 {
784         // Get the producer service
785         mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
786         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
787         char *resource =  mlt_properties_get( properties, "resource" );
788         xmlDocPtr doc = NULL;
789
790         if ( !service ) return;
791
792         // Set the title if provided
793         if ( mlt_properties_get( properties, "title" ) )
794                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
795         else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
796                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
797
798         // Check for a root on the consumer properties and pass to service
799         if ( mlt_properties_get( properties, "root" ) )
800                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
801
802         // Specify roots in other cases...
803         if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
804         {
805                 // Get the current working directory
806                 char *cwd = getcwd( NULL, 0 );
807                 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
808                 free( cwd );
809         }
810
811         // Make the document
812         doc = xml_make_doc( this, service );
813
814         // Handle the output
815         if ( resource == NULL || !strcmp( resource, "" ) )
816         {
817                 xmlDocFormatDump( stdout, doc, 1 );
818         }
819         else if ( strchr( resource, '.' ) == NULL )
820         {
821                 xmlChar *buffer = NULL;
822                 int length = 0;
823                 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
824                 mlt_properties_set( properties, resource, _s(buffer) );
825 #ifdef WIN32
826                 xmlFreeFunc xmlFree = NULL;
827                 xmlMemGet( &xmlFree, NULL, NULL, NULL);
828 #endif
829                 xmlFree( buffer );
830         }
831         else
832         {
833                 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
834         }
835
836         // Close the document
837         xmlFreeDoc( doc );
838 }
839 static int consumer_start( mlt_consumer this )
840 {
841         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
842
843         if ( mlt_properties_get_int( properties, "all" ) )
844         {
845                 // Check that we're not already running
846                 if ( !mlt_properties_get_int( properties, "running" ) )
847                 {
848                         // Allocate a thread
849                         pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
850
851                         // Assign the thread to properties
852                         mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
853
854                         // Set the running state
855                         mlt_properties_set_int( properties, "running", 1 );
856                         mlt_properties_set_int( properties, "joined", 0 );
857
858                         // Create the thread
859                         pthread_create( thread, NULL, consumer_thread, this );
860                 }
861         }
862         else
863         {
864                 output_xml( this );
865                 mlt_consumer_stop( this );
866                 mlt_consumer_stopped( this );
867         }
868         return 0;
869 }
870
871 static int consumer_is_stopped( mlt_consumer this )
872 {
873         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
874         return !mlt_properties_get_int( properties, "running" );
875 }
876
877 static int consumer_stop( mlt_consumer this )
878 {
879         // Get the properties
880         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
881
882         // Check that we're running
883         if ( !mlt_properties_get_int( properties, "joined" ) )
884         {
885                 // Get the thread
886                 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
887
888                 // Stop the thread
889                 mlt_properties_set_int( properties, "running", 0 );
890                 mlt_properties_set_int( properties, "joined", 1 );
891
892                 // Wait for termination
893                 if ( thread )
894                         pthread_join( *thread, NULL );
895         }
896
897         return 0;
898 }
899
900 static void *consumer_thread( void *arg )
901 {
902         // Map the argument to the object
903         mlt_consumer this = arg;
904
905         // Get the properties
906         mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
907
908         // Convenience functionality
909         int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
910         int terminated = 0;
911
912         // Frame and size
913         mlt_frame frame = NULL;
914
915         // Loop while running
916         while( !terminated && mlt_properties_get_int( properties, "running" ) )
917         {
918                 // Get the frame
919                 frame = mlt_consumer_rt_frame( this );
920
921                 // Check for termination
922                 if ( terminate_on_pause && frame != NULL )
923                         terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
924
925                 // Check that we have a frame to work with
926                 if ( frame )
927                 {
928                         int width = 0, height = 0;
929                         int frequency = mlt_properties_get_int( properties, "frequency" );
930                         int channels = mlt_properties_get_int( properties, "channels" );
931                         int samples = 0;
932                         mlt_image_format iformat = mlt_image_yuv422;
933                         mlt_audio_format aformat = mlt_audio_s16;
934                         uint8_t *buffer;
935
936                         mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
937                         mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
938
939                         // Close the frame
940                         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
941                         mlt_frame_close( frame );
942                 }
943         }
944         output_xml( this );
945
946         // Indicate that the consumer is stopped
947         mlt_properties_set_int( properties, "running", 0 );
948         mlt_consumer_stopped( this );
949
950         return NULL;
951 }