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