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