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