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>
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.
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.
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
21 #include <framework/mlt.h>
27 #include <libxml/tree.h>
33 #define _x (const xmlChar*)
34 #define _s (const char*)
36 // This maintains counters for adding ids to elements
37 struct serialise_context_s
39 mlt_properties id_map;
47 mlt_properties hide_map;
52 typedef struct serialise_context_s* serialise_context;
54 /** Forward references to static functions.
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 );
63 static void* filter_restricted( const char *in )
65 if ( !in ) return NULL;
66 size_t n = strlen( in );
67 char *out = calloc( 1, n + 1 );
70 memset( &mbs, 0, sizeof(mbs) );
74 size_t c = mbrtowc( &w, in, n, &mbs );
78 if ( w == 0x9 || w == 0xA || w == 0xD ||
79 ( w >= 0x20 && w <= 0xD7FF ) ||
80 ( w >= 0xE000 && w <= 0xFFFD ) ||
81 ( w >= 0x10000 && w <= 0x10FFFF ) )
84 memset( &ps, 0, sizeof(ps) );
85 c = wcrtomb( p, w, &ps );
105 /** Create or retrieve an id associated to this service.
108 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
112 mlt_properties map = context->id_map;
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 )
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 )
122 // Attempt to reuse existing id
123 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
125 // If no id, or the id is used in the map (for another service), then
127 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
129 char temp[ ID_SIZE ];
135 sprintf( temp, "producer%d", context->producer_count ++ );
138 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
141 sprintf( temp, "playlist%d", context->playlist_count ++ );
144 sprintf( temp, "tractor%d", context->tractor_count ++ );
147 sprintf( temp, "filter%d", context->filter_count ++ );
150 sprintf( temp, "transition%d", context->transition_count ++ );
157 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
159 // Set the data at the generated name
160 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
162 // Get the pointer to the name (i is the end of the list)
163 id = mlt_properties_get_name( map, i );
167 // Store the existing id in the map
168 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
171 else if ( type == xml_existing )
173 id = mlt_properties_get_name( map, i );
179 /** This is what will be called by the factory - anything can be passed in
180 via the argument, but keep it simple.
183 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
185 // Create the consumer object
186 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
188 // If no malloc'd and consumer init ok
189 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
191 // Allow thread to be started/stopped
192 this->start = consumer_start;
193 this->stop = consumer_stop;
194 this->is_stopped = consumer_is_stopped;
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 );
201 // Return the consumer produced
205 // malloc or consumer init failed
212 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
217 // Enumerate the properties
218 for ( i = 0; i < mlt_properties_count( properties ); i++ )
220 char *name = mlt_properties_get_name( properties, i );
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" ) )
234 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
237 int rootlen = strlen( context->root );
238 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
239 value += rootlen + 1;
240 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
241 xmlNewProp( p, _x("name"), _x(name) );
248 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
253 // Enumerate the properties
254 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
256 char *name = mlt_properties_get_name( properties, i );
257 if ( !strncmp( name, store, strlen( store ) ) )
259 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
262 int rootlen = strlen( context->root );
263 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
264 value += rootlen + 1;
265 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
266 xmlNewProp( p, _x("name"), _x(name) );
273 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
277 mlt_filter filter = NULL;
279 // Enumerate the filters
280 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
282 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
283 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
285 // Get a new id - if already allocated, do nothing
286 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
289 int in = mlt_properties_get_position( properties, "in" );
290 int out = mlt_properties_get_position( properties, "out" );
291 p = xmlNewChild( node, NULL, _x("filter"), NULL );
292 xmlNewProp( p, _x("id"), _x(id) );
293 if ( mlt_properties_get( properties, "title" ) )
294 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
295 if ( in != 0 || out != 0 )
298 sprintf( temp, "%d", in );
299 xmlNewProp( p, _x("in"), _x(temp) );
300 sprintf( temp, "%d", out );
301 xmlNewProp( p, _x("out"), _x(temp) );
303 serialise_properties( context, properties, p );
304 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
310 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
312 xmlNode *child = node;
313 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
315 if ( context->pass == 0 )
317 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
318 // Get a new id - if already allocated, do nothing
319 char *id = xml_get_id( context, parent, xml_producer );
323 child = xmlNewChild( node, NULL, _x("producer"), NULL );
326 xmlNewProp( child, _x("id"), _x(id) );
327 if ( mlt_properties_get( properties, "title" ) )
328 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
329 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
330 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
331 serialise_properties( context, properties, child );
332 serialise_service_filters( context, service, child );
334 // Add producer to the map
335 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
339 char *id = xml_get_id( context, parent, xml_existing );
340 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
341 xmlNewProp( node, _x("parent"), _x(id) );
342 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
343 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
347 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
349 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
353 if ( context->pass == 0 )
355 // Iterate over the tracks to collect the producers
356 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
358 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
359 serialise_service( context, MLT_SERVICE( producer ), node );
364 // Get a new id - if already allocated, do nothing
365 char *id = xml_get_id( context, service, xml_multitrack );
369 // Serialise the tracks
370 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
372 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
374 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
375 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
377 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
379 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
380 xmlNewProp( track, _x("producer"), _x(id) );
381 if ( mlt_producer_is_cut( producer ) )
383 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
384 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
385 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
386 if ( !context->no_meta )
387 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
388 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
391 hide = mlt_properties_get_int( context->hide_map, id );
393 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
395 serialise_service_filters( context, service, node );
399 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
402 xmlNode *child = node;
403 mlt_playlist_clip_info info;
404 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
406 if ( context->pass == 0 )
408 // Get a new id - if already allocated, do nothing
409 char *id = xml_get_id( context, service, xml_playlist );
413 // Iterate over the playlist entries to collect the producers
414 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
416 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
418 if ( info.producer != NULL )
420 mlt_producer producer = mlt_producer_cut_parent( info.producer );
421 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
422 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
423 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
424 serialise_playlist( context, MLT_SERVICE( producer ), node );
425 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
426 serialise_service( context, MLT_SERVICE( producer ), node );
431 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
434 xmlNewProp( child, _x("id"), _x(id) );
435 if ( mlt_properties_get( properties, "title" ) )
436 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
438 // Store application specific properties
439 serialise_store_properties( context, properties, child, context->store );
440 if ( !context->no_meta )
441 serialise_store_properties( context, properties, child, "meta." );
443 // Add producer to the map
444 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
446 // Iterate over the playlist entries
447 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
449 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
451 mlt_producer producer = mlt_producer_cut_parent( info.producer );
452 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
453 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
457 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
458 snprintf( length, 19, "%d", (int)info.frame_count );
459 xmlNewProp( entry, _x("length"), _x(length) );
464 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
465 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
466 xmlNewProp( entry, _x("producer"), _x(id) );
467 sprintf( temp, "%d", (int)info.frame_in );
468 xmlNewProp( entry, _x("in"), _x(temp) );
469 sprintf( temp, "%d", (int)info.frame_out );
470 xmlNewProp( entry, _x("out"), _x(temp) );
471 if ( info.repeat > 1 )
473 sprintf( temp, "%d", info.repeat );
474 xmlNewProp( entry, _x("repeat"), _x(temp) );
476 if ( mlt_producer_is_cut( info.cut ) )
478 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
479 if ( !context->no_meta )
480 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
481 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
487 serialise_service_filters( context, service, child );
489 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
491 char *id = xml_get_id( context, service, xml_existing );
492 xmlNewProp( node, _x("producer"), _x(id) );
496 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
498 xmlNode *child = node;
499 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
501 if ( context->pass == 0 )
503 // Recurse on connected producer
504 serialise_service( context, mlt_service_producer( service ), node );
508 // Get a new id - if already allocated, do nothing
509 char *id = xml_get_id( context, service, xml_tractor );
513 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
516 xmlNewProp( child, _x("id"), _x(id) );
517 if ( mlt_properties_get( properties, "title" ) )
518 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
519 if ( mlt_properties_get( properties, "global_feed" ) )
520 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
521 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
522 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
524 // Store application specific properties
525 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
526 if ( !context->no_meta )
527 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
529 // Recurse on connected producer
530 serialise_service( context, mlt_service_producer( service ), child );
531 serialise_service_filters( context, service, child );
535 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
537 xmlNode *child = node;
538 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
540 // Recurse on connected producer
541 serialise_service( context, mlt_service_producer( service ), node );
543 if ( context->pass == 1 )
545 // Get a new id - if already allocated, do nothing
546 char *id = xml_get_id( context, service, xml_filter );
550 child = xmlNewChild( node, NULL, _x("filter"), NULL );
553 xmlNewProp( child, _x("id"), _x(id) );
554 if ( mlt_properties_get( properties, "title" ) )
555 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
556 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
557 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
559 serialise_properties( context, properties, child );
560 serialise_service_filters( context, service, child );
564 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
566 xmlNode *child = node;
567 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
569 // Recurse on connected producer
570 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
572 if ( context->pass == 1 )
574 // Get a new id - if already allocated, do nothing
575 char *id = xml_get_id( context, service, xml_transition );
579 child = xmlNewChild( node, NULL, _x("transition"), NULL );
582 xmlNewProp( child, _x("id"), _x(id) );
583 if ( mlt_properties_get( properties, "title" ) )
584 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
585 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
586 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
588 serialise_properties( context, properties, child );
589 serialise_service_filters( context, service, child );
593 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
595 // Iterate over consumer/producer connections
596 while ( service != NULL )
598 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
599 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
601 // Tell about the producer
602 if ( strcmp( mlt_type, "producer" ) == 0 )
604 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
605 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
608 serialise_tractor( context, service, node );
610 serialise_tractor( context, service, node );
616 serialise_producer( context, service, node );
618 if ( mlt_properties_get( properties, "xml" ) != NULL )
622 // Tell about the framework container producers
623 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
625 char *resource = mlt_properties_get( properties, "resource" );
627 // Recurse on multitrack's tracks
628 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
630 serialise_multitrack( context, service, node );
634 // Recurse on playlist's clips
635 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
637 serialise_playlist( context, service, node );
640 // Recurse on tractor's producer
641 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
644 serialise_tractor( context, service, node );
646 serialise_tractor( context, service, node );
651 // Treat it as a normal producer
654 serialise_producer( context, service, node );
658 // Tell about a filter
659 else if ( strcmp( mlt_type, "filter" ) == 0 )
661 serialise_filter( context, service, node );
665 // Tell about a transition
666 else if ( strcmp( mlt_type, "transition" ) == 0 )
668 serialise_transition( context, service, node );
672 // Get the next connected service
673 service = mlt_service_producer( service );
677 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
679 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
680 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
681 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
682 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
683 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
686 xmlDocSetRootElement( doc, root );
688 // Indicate the numeric locale
689 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
691 // Indicate the version
692 xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
694 // If we have root, then deal with it now
695 if ( mlt_properties_get( properties, "root" ) != NULL )
697 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
698 context->root = strdup( mlt_properties_get( properties, "root" ) );
702 context->root = strdup( "" );
705 // Assign the additional 'storage' pattern for properties
706 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
707 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
709 // Assign a title property
710 if ( mlt_properties_get( properties, "title" ) != NULL )
711 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
712 mlt_properties_set_int( properties, "global_feed", 1 );
714 // Add a profile child element
717 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
718 if ( profile->description )
719 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
720 sprintf( tmpstr, "%d", profile->width );
721 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
722 sprintf( tmpstr, "%d", profile->height );
723 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
724 sprintf( tmpstr, "%d", profile->progressive );
725 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
726 sprintf( tmpstr, "%d", profile->sample_aspect_num );
727 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
728 sprintf( tmpstr, "%d", profile->sample_aspect_den );
729 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
730 sprintf( tmpstr, "%d", profile->display_aspect_num );
731 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
732 sprintf( tmpstr, "%d", profile->display_aspect_den );
733 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
734 sprintf( tmpstr, "%d", profile->frame_rate_num );
735 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
736 sprintf( tmpstr, "%d", profile->frame_rate_den );
737 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
738 sprintf( tmpstr, "%d", profile->colorspace );
739 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
742 // Construct the context maps
743 context->id_map = mlt_properties_new();
744 context->hide_map = mlt_properties_new();
746 // Ensure producer is a framework producer
747 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
749 // In pass one, we serialise the end producers and playlists,
750 // adding them to a map keyed by address.
751 serialise_service( context, service, root );
753 // In pass two, we serialise the tractor and reference the
754 // producers and playlists
756 serialise_service( context, service, root );
759 mlt_properties_close( context->id_map );
760 mlt_properties_close( context->hide_map );
761 free( context->root );
768 static void output_xml( mlt_consumer this )
770 // Get the producer service
771 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
772 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
773 char *resource = mlt_properties_get( properties, "resource" );
774 xmlDocPtr doc = NULL;
776 if ( !service ) return;
778 // Set the title if provided
779 if ( mlt_properties_get( properties, "title" ) )
780 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
781 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
782 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
784 // Check for a root on the consumer properties and pass to service
785 if ( mlt_properties_get( properties, "root" ) )
786 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
788 // Specify roots in other cases...
789 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
791 // Get the current working directory
792 char *cwd = getcwd( NULL, 0 );
793 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
798 doc = xml_make_doc( this, service );
801 if ( resource == NULL || !strcmp( resource, "" ) )
803 xmlDocFormatDump( stdout, doc, 1 );
805 else if ( strchr( resource, '.' ) == NULL )
807 xmlChar *buffer = NULL;
809 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
810 mlt_properties_set( properties, resource, _s(buffer) );
812 xmlFreeFunc xmlFree = NULL;
813 xmlMemGet( &xmlFree, NULL, NULL, NULL);
819 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
822 // Close the document
825 static int consumer_start( mlt_consumer this )
827 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
829 if ( mlt_properties_get_int( properties, "all" ) )
831 // Check that we're not already running
832 if ( !mlt_properties_get_int( properties, "running" ) )
835 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
837 // Assign the thread to properties
838 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
840 // Set the running state
841 mlt_properties_set_int( properties, "running", 1 );
842 mlt_properties_set_int( properties, "joined", 0 );
845 pthread_create( thread, NULL, consumer_thread, this );
851 mlt_consumer_stop( this );
852 mlt_consumer_stopped( this );
857 static int consumer_is_stopped( mlt_consumer this )
859 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
860 return !mlt_properties_get_int( properties, "running" );
863 static int consumer_stop( mlt_consumer this )
865 // Get the properties
866 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
868 // Check that we're running
869 if ( !mlt_properties_get_int( properties, "joined" ) )
872 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
875 mlt_properties_set_int( properties, "running", 0 );
876 mlt_properties_set_int( properties, "joined", 1 );
878 // Wait for termination
880 pthread_join( *thread, NULL );
886 static void *consumer_thread( void *arg )
888 // Map the argument to the object
889 mlt_consumer this = arg;
891 // Get the properties
892 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
894 // Convenience functionality
895 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
899 mlt_frame frame = NULL;
901 // Loop while running
902 while( !terminated && mlt_properties_get_int( properties, "running" ) )
905 frame = mlt_consumer_rt_frame( this );
907 // Check for termination
908 if ( terminate_on_pause && frame != NULL )
909 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
911 // Check that we have a frame to work with
914 int width = 0, height = 0;
915 int frequency = mlt_properties_get_int( properties, "frequency" );
916 int channels = mlt_properties_get_int( properties, "channels" );
918 mlt_image_format iformat = mlt_image_yuv422;
919 mlt_audio_format aformat = mlt_audio_s16;
922 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
923 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
926 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
927 mlt_frame_close( frame );
932 // Indicate that the consumer is stopped
933 mlt_properties_set_int( properties, "running", 0 );
934 mlt_consumer_stopped( this );