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