]> git.sesse.net Git - mlt/blobdiff - src/modules/xml/consumer_xml.c
If LC_NUMERIC unsupported on OS, serialize XML with LC_NUMERIC=C.
[mlt] / src / modules / xml / consumer_xml.c
index fae400b0a8170b2b391d25411307aa1a95cb3f16..4fa0904ba12e47fc188c0700a945b08ec32b8c95 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <locale.h>
 #include <libxml/tree.h>
+#include <pthread.h>
+#include <wchar.h>
 
 #define ID_SIZE 128
+#define TIME_PROPERTY "_consumer_xml"
 
 #define _x (const xmlChar*)
 #define _s (const char*)
@@ -44,6 +48,9 @@ struct serialise_context_s
        mlt_properties hide_map;
        char *root;
        char *store;
+       int no_meta;
+       mlt_profile profile;
+       mlt_time_format time_format;
 };
 typedef struct serialise_context_s* serialise_context;
 
@@ -51,10 +58,42 @@ typedef struct serialise_context_s* serialise_context;
 */
 
 static int consumer_start( mlt_consumer parent );
+static int consumer_stop( mlt_consumer parent );
 static int consumer_is_stopped( mlt_consumer this );
+static void *consumer_thread( void *arg );
 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
 
-typedef enum 
+static char* filter_restricted( const char *in )
+{
+       if ( !in ) return NULL;
+       size_t n = strlen( in );
+       char *out = calloc( 1, n + 1 );
+       char *p = out;
+       mbstate_t mbs;
+       memset( &mbs, 0, sizeof(mbs) );
+       while ( *in )
+       {
+               wchar_t w;
+               size_t c = mbrtowc( &w, in, n, &mbs );
+               if ( c <= 0 || c > n ) break;
+               n -= c;
+               in += c;
+               if ( w == 0x9 || w == 0xA || w == 0xD ||
+                               ( w >= 0x20 && w <= 0xD7FF ) ||
+                               ( w >= 0xE000 && w <= 0xFFFD ) ||
+                               ( w >= 0x10000 && w <= 0x10FFFF ) )
+               {
+                       mbstate_t ps;
+                       memset( &ps, 0, sizeof(ps) );
+                       c = wcrtomb( p, w, &ps );
+                       if ( c > 0 )
+                               p += c;
+               }
+       }
+       return out;
+}
+
+typedef enum
 {
        xml_existing,
        xml_producer,
@@ -86,7 +125,7 @@ static char *xml_get_id( serialise_context context, mlt_service service, xml_typ
                // Attempt to reuse existing id
                id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
 
-               // If no id, or the id is used in the map (for another service), then 
+               // If no id, or the id is used in the map (for another service), then
                // create a new one.
                if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
                {
@@ -147,16 +186,20 @@ static char *xml_get_id( serialise_context context, mlt_service service, xml_typ
 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
 {
        // Create the consumer object
-       mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
+       mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) );
 
        // If no malloc'd and consumer init ok
        if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
        {
                // Allow thread to be started/stopped
                this->start = consumer_start;
+               this->stop = consumer_stop;
                this->is_stopped = consumer_is_stopped;
 
                mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
+               mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
+               mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
+               mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
 
                // Return the consumer produced
                return this;
@@ -173,7 +216,7 @@ static void serialise_properties( serialise_context context, mlt_properties prop
 {
        int i;
        xmlNode *p;
-       
+
        // Enumerate the properties
        for ( i = 0; i < mlt_properties_count( properties ); i++ )
        {
@@ -181,21 +224,36 @@ static void serialise_properties( serialise_context context, mlt_properties prop
                if ( name != NULL &&
                         name[ 0 ] != '_' &&
                         mlt_properties_get_value( properties, i ) != NULL &&
-                        strcmp( name, "mlt" ) != 0 &&
-                        strcmp( name, "in" ) != 0 &&
-                        strcmp( name, "out" ) != 0 && 
-                        strcmp( name, "id" ) != 0 && 
-                        strcmp( name, "title" ) != 0 && 
-                        strcmp( name, "root" ) != 0 && 
-                        strcmp( name, "width" ) != 0 &&
-                        strcmp( name, "height" ) != 0 )
+                        ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
+                        strcmp( name, "mlt" ) &&
+                        strcmp( name, "in" ) &&
+                        strcmp( name, "out" ) &&
+                        strcmp( name, "id" ) &&
+                        strcmp( name, "title" ) &&
+                        strcmp( name, "root" ) &&
+                        strcmp( name, "width" ) &&
+                        strcmp( name, "height" ) )
                {
-                       char *value = mlt_properties_get_value( properties, i );
-                       int rootlen = strlen( context->root );
-                       if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
-                               value += rootlen + 1;
-                       p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
-                       xmlNewProp( p, _x("name"), _x(name) );
+                       char *value = NULL;
+                       if ( !strcmp( name, "length" ) )
+                       {
+                               char *time = mlt_properties_get_time( properties, name, context->time_format );
+                               if ( time )
+                                       value = strdup( time );
+                       }
+                       else
+                               value = filter_restricted( mlt_properties_get_value( properties, i ) );
+                       if ( value )
+                       {
+                               int rootlen = strlen( context->root );
+                               // convert absolute path to relative
+                               if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
+                                       p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1 ) );
+                               else
+                                       p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
+                               xmlNewProp( p, _x("name"), _x(name) );
+                               free( value );
+                       }
                }
        }
 }
@@ -204,21 +262,24 @@ static void serialise_store_properties( serialise_context context, mlt_propertie
 {
        int i;
        xmlNode *p;
-       
+
        // Enumerate the properties
        for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
        {
                char *name = mlt_properties_get_name( properties, i );
                if ( !strncmp( name, store, strlen( store ) ) )
                {
-                       char *value = mlt_properties_get_value( properties, i );
-                       if ( value != NULL )
+                       char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
+                       if ( value )
                        {
                                int rootlen = strlen( context->root );
+                               // convert absolute path to relative
                                if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
-                                       value += rootlen + 1;
-                               p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
+                                       p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
+                               else
+                                       p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
                                xmlNewProp( p, _x("name"), _x(name) );
+                               free( value );
                        }
                }
        }
@@ -229,7 +290,7 @@ static inline void serialise_service_filters( serialise_context context, mlt_ser
        int i;
        xmlNode *p;
        mlt_filter filter = NULL;
-       
+
        // Enumerate the filters
        for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
        {
@@ -240,20 +301,14 @@ static inline void serialise_service_filters( serialise_context context, mlt_ser
                        char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
                        if ( id != NULL )
                        {
-                               int in = mlt_properties_get_position( properties, "in" );
-                               int out = mlt_properties_get_position( properties, "out" );
                                p = xmlNewChild( node, NULL, _x("filter"), NULL );
                                xmlNewProp( p, _x("id"), _x(id) );
                                if ( mlt_properties_get( properties, "title" ) )
                                        xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
-                               if ( in != 0 || out != 0 )
-                               {
-                                       char temp[ 20 ];
-                                       sprintf( temp, "%d", in );
-                                       xmlNewProp( p, _x("in"), _x(temp) );
-                                       sprintf( temp, "%d", out );
-                                       xmlNewProp( p, _x("out"), _x(temp) );
-                               }
+                               if ( mlt_properties_get_position( properties, "in" ) )
+                                       xmlNewProp( p, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
+                               if ( mlt_properties_get_position( properties, "out" ) )
+                                       xmlNewProp( p, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
                                serialise_properties( context, properties, p );
                                serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
                        }
@@ -265,7 +320,7 @@ static void serialise_producer( serialise_context context, mlt_service service,
 {
        xmlNode *child = node;
        mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
-       
+
        if ( context->pass == 0 )
        {
                mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
@@ -280,8 +335,8 @@ static void serialise_producer( serialise_context context, mlt_service service,
                xmlNewProp( child, _x("id"), _x(id) );
                if ( mlt_properties_get( properties, "title" ) )
                        xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
-               xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-               xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+               xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
+               xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
                serialise_properties( context, properties, child );
                serialise_service_filters( context, service, child );
 
@@ -293,8 +348,8 @@ static void serialise_producer( serialise_context context, mlt_service service,
                char *id = xml_get_id( context, parent, xml_existing );
                mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
                xmlNewProp( node, _x("parent"), _x(id) );
-               xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-               xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+               xmlNewProp( node, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
+               xmlNewProp( node, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
        }
 }
 
@@ -303,7 +358,7 @@ static void serialise_tractor( serialise_context context, mlt_service service, x
 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
 {
        int i;
-       
+
        if ( context->pass == 0 )
        {
                // Iterate over the tracks to collect the producers
@@ -334,13 +389,15 @@ static void serialise_multitrack( serialise_context context, mlt_service service
                        xmlNewProp( track, _x("producer"), _x(id) );
                        if ( mlt_producer_is_cut( producer ) )
                        {
-                               xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-                               xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+                               xmlNewProp( track, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
+                               xmlNewProp( track, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
                                serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
-                               serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
+                               serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "xml_" );
+                               if ( !context->no_meta )
+                                       serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
                                serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
                        }
-                       
+
                        hide = mlt_properties_get_int( context->hide_map, id );
                        if ( hide )
                                xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
@@ -355,7 +412,7 @@ static void serialise_playlist( serialise_context context, mlt_service service,
        xmlNode *child = node;
        mlt_playlist_clip_info info;
        mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
-       
+
        if ( context->pass == 0 )
        {
                // Get a new id - if already allocated, do nothing
@@ -380,7 +437,7 @@ static void serialise_playlist( serialise_context context, mlt_service service,
                                }
                        }
                }
-               
+
                child = xmlNewChild( node, NULL, _x("playlist"), NULL );
 
                // Set the id
@@ -390,25 +447,27 @@ static void serialise_playlist( serialise_context context, mlt_service service,
 
                // Store application specific properties
                serialise_store_properties( context, properties, child, context->store );
-               serialise_store_properties( context, properties, child, "meta." );
+               serialise_store_properties( context, properties, child, "xml_" );
+               if ( !context->no_meta )
+                       serialise_store_properties( context, properties, child, "meta." );
 
                // Add producer to the map
                mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
-       
+
                // Iterate over the playlist entries
                for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
                {
                        if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
                        {
                                mlt_producer producer = mlt_producer_cut_parent( info.producer );
-                               char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
+                               mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
+                               char *service_s = mlt_properties_get( producer_props, "mlt_service" );
                                if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
                                {
-                                       char length[ 20 ];
-                                       length[ 19 ] = '\0';
                                        xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
-                                       snprintf( length, 19, "%d", (int)info.frame_count );
-                                       xmlNewProp( entry, _x("length"), _x(length) );
+                                       mlt_properties_set_data( producer_props, "_profile", context->profile, 0, NULL, NULL );
+                                       mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_count );
+                                       xmlNewProp( entry, _x("length"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
                                }
                                else
                                {
@@ -416,10 +475,10 @@ static void serialise_playlist( serialise_context context, mlt_service service,
                                        xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
                                        id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
                                        xmlNewProp( entry, _x("producer"), _x(id) );
-                                       sprintf( temp, "%d", (int)info.frame_in );
-                                       xmlNewProp( entry, _x("in"), _x(temp) );
-                                       sprintf( temp, "%d", (int)info.frame_out );
-                                       xmlNewProp( entry, _x("out"), _x(temp) );
+                                       mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_in );
+                                       xmlNewProp( entry, _x("in"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
+                                       mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_out );
+                                       xmlNewProp( entry, _x("out"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
                                        if ( info.repeat > 1 )
                                        {
                                                sprintf( temp, "%d", info.repeat );
@@ -428,7 +487,9 @@ static void serialise_playlist( serialise_context context, mlt_service service,
                                        if ( mlt_producer_is_cut( info.cut ) )
                                        {
                                                serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
-                                               serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
+                                               serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "xml_" );
+                                               if ( !context->no_meta )
+                                                       serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
                                                serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
                                        }
                                }
@@ -448,7 +509,7 @@ static void serialise_tractor( serialise_context context, mlt_service service, x
 {
        xmlNode *child = node;
        mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
-       
+
        if ( context->pass == 0 )
        {
                // Recurse on connected producer
@@ -469,12 +530,16 @@ static void serialise_tractor( serialise_context context, mlt_service service, x
                        xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
                if ( mlt_properties_get( properties, "global_feed" ) )
                        xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
-               xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-               xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+               if ( mlt_properties_get_position( properties, "in" ) >= 0 )
+                       xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
+               if ( mlt_properties_get_position( properties, "out" ) >= 0 )
+                       xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
 
                // Store application specific properties
                serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
-               serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
+               serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "xml_" );
+               if ( !context->no_meta )
+                       serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
 
                // Recurse on connected producer
                serialise_service( context, mlt_service_producer( service ), child );
@@ -486,7 +551,7 @@ static void serialise_filter( serialise_context context, mlt_service service, xm
 {
        xmlNode *child = node;
        mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
-       
+
        // Recurse on connected producer
        serialise_service( context, mlt_service_producer( service ), node );
 
@@ -503,8 +568,10 @@ static void serialise_filter( serialise_context context, mlt_service service, xm
                xmlNewProp( child, _x("id"), _x(id) );
                if ( mlt_properties_get( properties, "title" ) )
                        xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
-               xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-               xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+               if ( mlt_properties_get_position( properties, "in" ) )
+                       xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
+               if ( mlt_properties_get_position( properties, "out" ) )
+                       xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
 
                serialise_properties( context, properties, child );
                serialise_service_filters( context, service, child );
@@ -515,7 +582,7 @@ static void serialise_transition( serialise_context context, mlt_service service
 {
        xmlNode *child = node;
        mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
-       
+
        // Recurse on connected producer
        serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
 
@@ -527,13 +594,15 @@ static void serialise_transition( serialise_context context, mlt_service service
                        return;
 
                child = xmlNewChild( node, NULL, _x("transition"), NULL );
-       
+
                // Set the id
                xmlNewProp( child, _x("id"), _x(id) );
                if ( mlt_properties_get( properties, "title" ) )
                        xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
-               xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
-               xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
+               if ( mlt_properties_get_position( properties, "in" ) )
+                       xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
+               if ( mlt_properties_get_position( properties, "out" ) )
+                       xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
 
                serialise_properties( context, properties, child );
                serialise_service_filters( context, service, child );
@@ -547,7 +616,7 @@ static void serialise_service( serialise_context context, mlt_service service, x
        {
                mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
                char *mlt_type = mlt_properties_get( properties, "mlt_type" );
-               
+
                // Tell about the producer
                if ( strcmp( mlt_type, "producer" ) == 0 )
                {
@@ -573,22 +642,22 @@ static void serialise_service( serialise_context context, mlt_service service, x
                else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
                {
                        char *resource = mlt_properties_get( properties, "resource" );
-                       
+
                        // Recurse on multitrack's tracks
-                       if ( strcmp( resource, "<multitrack>" ) == 0 )
+                       if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
                        {
                                serialise_multitrack( context, service, node );
                                break;
                        }
-                       
+
                        // Recurse on playlist's clips
-                       else if ( strcmp( resource, "<playlist>" ) == 0 )
+                       else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
                        {
                                serialise_playlist( context, service, node );
                        }
-                       
+
                        // Recurse on tractor's producer
-                       else if ( strcmp( resource, "<tractor>" ) == 0 )
+                       else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
                        {
                                context->pass = 0;
                                serialise_tractor( context, service, node );
@@ -602,37 +671,65 @@ static void serialise_service( serialise_context context, mlt_service service, x
                        else
                        {
                                serialise_producer( context, service, node );
+                               if ( mlt_properties_get( properties, "xml" ) != NULL )
+                                       break;
                        }
                }
-               
+
                // Tell about a filter
                else if ( strcmp( mlt_type, "filter" ) == 0 )
                {
                        serialise_filter( context, service, node );
                        break;
                }
-               
+
                // Tell about a transition
                else if ( strcmp( mlt_type, "transition" ) == 0 )
                {
                        serialise_transition( context, service, node );
                        break;
                }
-               
+
                // Get the next connected service
                service = mlt_service_producer( service );
        }
 }
 
+static void serialise_other( mlt_properties properties, struct serialise_context_s *context, xmlNodePtr root )
+{
+       int i;
+       for ( i = 0; i < mlt_properties_count( properties ); i++ )
+       {
+               const char* name = mlt_properties_get_name( properties, i );
+               if ( strlen(name) > 10 && !strncmp( name, "xml_retain", 10 ) )
+               {
+                       mlt_service service = mlt_properties_get_data_at( properties, i, NULL );
+                       if ( service )
+                       {
+                               mlt_properties_set_int( MLT_SERVICE_PROPERTIES( service ), "xml_retain", 1 );
+                               serialise_service( context, service, root );
+                       }
+               }
+       }
+}
+
 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
 {
        mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
        xmlDocPtr doc = xmlNewDoc( _x("1.0") );
        xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
        struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
-       
+       mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
+       char tmpstr[ 32 ];
+
        xmlDocSetRootElement( doc, root );
 
+       // Indicate the numeric locale
+       xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
+
+       // Indicate the version
+       xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
+
        // If we have root, then deal with it now
        if ( mlt_properties_get( properties, "root" ) != NULL )
        {
@@ -646,26 +743,64 @@ xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
 
        // Assign the additional 'storage' pattern for properties
        context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
+       context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
+       const char *time_format = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "time_format" );
+       if ( time_format && ( !strcmp( time_format, "smpte" ) || !strcmp( time_format, "SMPTE" )
+                       || !strcmp( time_format, "timecode" ) ) )
+               context->time_format = mlt_time_smpte;
+       else if ( time_format && ( !strcmp( time_format, "clock" ) || !strcmp( time_format, "CLOCK" ) ) )
+               context->time_format = mlt_time_clock;
 
        // Assign a title property
        if ( mlt_properties_get( properties, "title" ) != NULL )
                xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
        mlt_properties_set_int( properties, "global_feed", 1 );
 
+       // Add a profile child element
+       if ( profile )
+       {
+               xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
+               if ( profile->description )
+                       xmlNewProp( profile_node, _x("description"), _x(profile->description) );
+               sprintf( tmpstr, "%d", profile->width );
+               xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->height );
+               xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->progressive );
+               xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->sample_aspect_num );
+               xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->sample_aspect_den );
+               xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->display_aspect_num );
+               xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->display_aspect_den );
+               xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->frame_rate_num );
+               xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->frame_rate_den );
+               xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
+               sprintf( tmpstr, "%d", profile->colorspace );
+               xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
+               context->profile = profile;
+       }
+
        // Construct the context maps
        context->id_map = mlt_properties_new();
        context->hide_map = mlt_properties_new();
-       
+
        // Ensure producer is a framework producer
        mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
 
        // In pass one, we serialise the end producers and playlists,
        // adding them to a map keyed by address.
+       serialise_other( MLT_SERVICE_PROPERTIES( service ), context, root );
        serialise_service( context, service, root );
 
        // In pass two, we serialise the tractor and reference the
        // producers and playlists
        context->pass++;
+       serialise_other( MLT_SERVICE_PROPERTIES( service ), context, root );
        serialise_service( context, service, root );
 
        // Cleanup resource
@@ -673,73 +808,195 @@ xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
        mlt_properties_close( context->hide_map );
        free( context->root );
        free( context );
-       
+
        return doc;
 }
 
-static int consumer_start( mlt_consumer this )
+
+static void output_xml( mlt_consumer this )
 {
-       xmlDocPtr doc = NULL;
-       
        // Get the producer service
        mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
-       if ( service != NULL )
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+       char *resource =  mlt_properties_get( properties, "resource" );
+       xmlDocPtr doc = NULL;
+
+       if ( !service ) return;
+
+       // Set the title if provided
+       if ( mlt_properties_get( properties, "title" ) )
+               mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
+       else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
+               mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
+
+       // Check for a root on the consumer properties and pass to service
+       if ( mlt_properties_get( properties, "root" ) )
+               mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
+
+       // Specify roots in other cases...
+       if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
        {
-               mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
-               char *resource =  mlt_properties_get( properties, "resource" );
+               // Get the current working directory
+               char *cwd = getcwd( NULL, 0 );
+               mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
+               free( cwd );
+       }
 
-               // Set the title if provided
-               if ( mlt_properties_get( properties, "title" ) )
-                       mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
-               else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
-                       mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
+#if !defined(__linux__) && !defined(__DARWIN__)
+       // Get the current locale
+       char *orig_localename = strdup( setlocale( LC_NUMERIC, NULL ) );
+       setlocale( LC_NUMERIC, "C" );
+#endif
 
-               // Check for a root on the consumer properties and pass to service
-               if ( mlt_properties_get( properties, "root" ) )
-                       mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
+       // Make the document
+       doc = xml_make_doc( this, service );
 
-               // Specify roots in other cases...
-               if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
-               {
-                       // Get the current working directory
-                       char *cwd = getcwd( NULL, 0 );
-                       mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
-                       free( cwd );
-               }
+       // Handle the output
+       if ( resource == NULL || !strcmp( resource, "" ) )
+       {
+               xmlDocFormatDump( stdout, doc, 1 );
+       }
+       else if ( strchr( resource, '.' ) == NULL )
+       {
+               xmlChar *buffer = NULL;
+               int length = 0;
+               xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
+               mlt_properties_set( properties, resource, _s(buffer) );
+#ifdef WIN32
+               xmlFreeFunc xmlFree = NULL;
+               xmlMemGet( &xmlFree, NULL, NULL, NULL);
+#endif
+               xmlFree( buffer );
+       }
+       else
+       {
+               xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
+       }
 
-               // Make the document
-               doc = xml_make_doc( this, service );
+#if !defined(__linux__) && !defined(__DARWIN__)
+       // Restore the current locale
+       setlocale( LC_NUMERIC, orig_localename );
+       free( orig_localename );
+#endif
 
-               // Handle the output
-               if ( resource == NULL || !strcmp( resource, "" ) )
-               {
-                       xmlDocFormatDump( stdout, doc, 1 );
-               }
-               else if ( strchr( resource, '.' ) == NULL )
-               {
-                       xmlChar *buffer = NULL;
-                       int length = 0;
-                       xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
-                       mlt_properties_set( properties, resource, _s(buffer) );
-                       xmlFree( buffer );
-               }
-               else
+       // Close the document
+       xmlFreeDoc( doc );
+}
+static int consumer_start( mlt_consumer this )
+{
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+
+       if ( mlt_properties_get_int( properties, "all" ) )
+       {
+               // Check that we're not already running
+               if ( !mlt_properties_get_int( properties, "running" ) )
                {
-                       xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
+                       // Allocate a thread
+                       pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
+
+                       // Assign the thread to properties
+                       mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
+
+                       // Set the running state
+                       mlt_properties_set_int( properties, "running", 1 );
+                       mlt_properties_set_int( properties, "joined", 0 );
+
+                       // Create the thread
+                       pthread_create( thread, NULL, consumer_thread, this );
                }
-               
-               // Close the document
-               xmlFreeDoc( doc );
        }
-       
-       mlt_consumer_stop( this );
+       else
+       {
+               output_xml( this );
+               mlt_consumer_stop( this );
+               mlt_consumer_stopped( this );
+       }
+       return 0;
+}
 
-       mlt_consumer_stopped( this );
+static int consumer_is_stopped( mlt_consumer this )
+{
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+       return !mlt_properties_get_int( properties, "running" );
+}
+
+static int consumer_stop( mlt_consumer this )
+{
+       // Get the properties
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+
+       // Check that we're running
+       if ( !mlt_properties_get_int( properties, "joined" ) )
+       {
+               // Get the thread
+               pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
+
+               // Stop the thread
+               mlt_properties_set_int( properties, "running", 0 );
+               mlt_properties_set_int( properties, "joined", 1 );
+
+               // Wait for termination
+               if ( thread )
+                       pthread_join( *thread, NULL );
+       }
 
        return 0;
 }
 
-static int consumer_is_stopped( mlt_consumer this )
+static void *consumer_thread( void *arg )
 {
-       return 1;
+       // Map the argument to the object
+       mlt_consumer this = arg;
+
+       // Get the properties
+       mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
+
+       // Convenience functionality
+       int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
+       int terminated = 0;
+
+       // Frame and size
+       mlt_frame frame = NULL;
+
+       int video_off = mlt_properties_get_int( properties, "video_off" );
+       int audio_off = mlt_properties_get_int( properties, "audio_off" );
+
+       // Loop while running
+       while( !terminated && mlt_properties_get_int( properties, "running" ) )
+       {
+               // Get the frame
+               frame = mlt_consumer_rt_frame( this );
+
+               // Check for termination
+               if ( terminate_on_pause && frame != NULL )
+                       terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
+
+               // Check that we have a frame to work with
+               if ( frame )
+               {
+                       int width = 0, height = 0;
+                       int frequency = mlt_properties_get_int( properties, "frequency" );
+                       int channels = mlt_properties_get_int( properties, "channels" );
+                       int samples = 0;
+                       mlt_image_format iformat = mlt_image_yuv422;
+                       mlt_audio_format aformat = mlt_audio_s16;
+                       uint8_t *buffer;
+
+                       if ( !video_off )
+                               mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
+                       if ( !audio_off )
+                               mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
+
+                       // Close the frame
+                       mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
+                       mlt_frame_close( frame );
+               }
+       }
+       output_xml( this );
+
+       // Indicate that the consumer is stopped
+       mlt_properties_set_int( properties, "running", 0 );
+       mlt_consumer_stopped( this );
+
+       return NULL;
 }