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>
36 #define _x (const xmlChar*)
37 #define _s (const char*)
39 // This maintains counters for adding ids to elements
40 struct serialise_context_s
42 mlt_properties id_map;
50 mlt_properties hide_map;
55 typedef struct serialise_context_s* serialise_context;
57 /** Forward references to static functions.
60 static int consumer_start( mlt_consumer parent );
61 static int consumer_stop( mlt_consumer parent );
62 static int consumer_is_stopped( mlt_consumer this );
63 static void *consumer_thread( void *arg );
64 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
68 static void* filter_restricted( const char *in )
70 if ( !in ) return NULL;
71 size_t n = strlen( in );
72 char *out = calloc( 1, n );
75 memset( &mbs, 0, sizeof(mbs) );
79 size_t c = mbrtowc( &w, in, n, &mbs );
83 if ( w == 0x9 || w == 0xA || w == 0xD ||
84 ( w >= 0x20 && w <= 0xD7FF ) ||
85 ( w >= 0xE000 && w <= 0xFFFD ) ||
86 ( w >= 0x10000 && w <= 0x10FFFF ) )
89 memset( &ps, 0, sizeof(ps) );
90 c = wcrtomb( p, w, &ps );
100 static void* filter_restricted( const char *in )
102 if ( !in ) return NULL;
103 char *out = calloc( 1, strlen( in ) );
104 size_t i, j, n = strlen( in );
105 for ( i = 0, j = 0; i < n; i++ )
108 if ( c == 0x9 || c == 0xA || c == 0xD || ( c >= 0x20 && c <= 0xFF ) )
128 /** Create or retrieve an id associated to this service.
131 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
135 mlt_properties map = context->id_map;
137 // Search the map for the service
138 for ( i = 0; i < mlt_properties_count( map ); i ++ )
139 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
142 // If the service is not in the map, and the type indicates a new id is needed...
143 if ( i >= mlt_properties_count( map ) && type != xml_existing )
145 // Attempt to reuse existing id
146 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
148 // If no id, or the id is used in the map (for another service), then
150 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
152 char temp[ ID_SIZE ];
158 sprintf( temp, "producer%d", context->producer_count ++ );
161 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
164 sprintf( temp, "playlist%d", context->playlist_count ++ );
167 sprintf( temp, "tractor%d", context->tractor_count ++ );
170 sprintf( temp, "filter%d", context->filter_count ++ );
173 sprintf( temp, "transition%d", context->transition_count ++ );
180 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
182 // Set the data at the generated name
183 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
185 // Get the pointer to the name (i is the end of the list)
186 id = mlt_properties_get_name( map, i );
190 // Store the existing id in the map
191 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
194 else if ( type == xml_existing )
196 id = mlt_properties_get_name( map, i );
202 /** This is what will be called by the factory - anything can be passed in
203 via the argument, but keep it simple.
206 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
208 // Create the consumer object
209 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
211 // If no malloc'd and consumer init ok
212 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
214 // Allow thread to be started/stopped
215 this->start = consumer_start;
216 this->stop = consumer_stop;
217 this->is_stopped = consumer_is_stopped;
219 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
220 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
221 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
222 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
224 // Return the consumer produced
228 // malloc or consumer init failed
235 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
240 // Enumerate the properties
241 for ( i = 0; i < mlt_properties_count( properties ); i++ )
243 char *name = mlt_properties_get_name( properties, i );
246 mlt_properties_get_value( properties, i ) != NULL &&
247 ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
248 strcmp( name, "mlt" ) &&
249 strcmp( name, "in" ) &&
250 strcmp( name, "out" ) &&
251 strcmp( name, "id" ) &&
252 strcmp( name, "title" ) &&
253 strcmp( name, "root" ) &&
254 strcmp( name, "width" ) &&
255 strcmp( name, "height" ) )
257 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
260 int rootlen = strlen( context->root );
261 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
262 value += rootlen + 1;
263 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
264 xmlNewProp( p, _x("name"), _x(name) );
271 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
276 // Enumerate the properties
277 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
279 char *name = mlt_properties_get_name( properties, i );
280 if ( !strncmp( name, store, strlen( store ) ) )
282 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
285 int rootlen = strlen( context->root );
286 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
287 value += rootlen + 1;
288 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
289 xmlNewProp( p, _x("name"), _x(name) );
296 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
300 mlt_filter filter = NULL;
302 // Enumerate the filters
303 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
305 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
306 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
308 // Get a new id - if already allocated, do nothing
309 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
312 int in = mlt_properties_get_position( properties, "in" );
313 int out = mlt_properties_get_position( properties, "out" );
314 p = xmlNewChild( node, NULL, _x("filter"), NULL );
315 xmlNewProp( p, _x("id"), _x(id) );
316 if ( mlt_properties_get( properties, "title" ) )
317 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
318 if ( in != 0 || out != 0 )
321 sprintf( temp, "%d", in );
322 xmlNewProp( p, _x("in"), _x(temp) );
323 sprintf( temp, "%d", out );
324 xmlNewProp( p, _x("out"), _x(temp) );
326 serialise_properties( context, properties, p );
327 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
333 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
335 xmlNode *child = node;
336 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
338 if ( context->pass == 0 )
340 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
341 // Get a new id - if already allocated, do nothing
342 char *id = xml_get_id( context, parent, xml_producer );
346 child = xmlNewChild( node, NULL, _x("producer"), NULL );
349 xmlNewProp( child, _x("id"), _x(id) );
350 if ( mlt_properties_get( properties, "title" ) )
351 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
352 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
353 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
354 serialise_properties( context, properties, child );
355 serialise_service_filters( context, service, child );
357 // Add producer to the map
358 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
362 char *id = xml_get_id( context, parent, xml_existing );
363 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
364 xmlNewProp( node, _x("parent"), _x(id) );
365 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
366 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
370 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
372 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
376 if ( context->pass == 0 )
378 // Iterate over the tracks to collect the producers
379 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
381 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
382 serialise_service( context, MLT_SERVICE( producer ), node );
387 // Get a new id - if already allocated, do nothing
388 char *id = xml_get_id( context, service, xml_multitrack );
392 // Serialise the tracks
393 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
395 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
397 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
398 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
400 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
402 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
403 xmlNewProp( track, _x("producer"), _x(id) );
404 if ( mlt_producer_is_cut( producer ) )
406 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
407 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
408 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
409 if ( !context->no_meta )
410 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
411 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
414 hide = mlt_properties_get_int( context->hide_map, id );
416 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
418 serialise_service_filters( context, service, node );
422 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
425 xmlNode *child = node;
426 mlt_playlist_clip_info info;
427 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
429 if ( context->pass == 0 )
431 // Get a new id - if already allocated, do nothing
432 char *id = xml_get_id( context, service, xml_playlist );
436 // Iterate over the playlist entries to collect the producers
437 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
439 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
441 if ( info.producer != NULL )
443 mlt_producer producer = mlt_producer_cut_parent( info.producer );
444 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
445 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
446 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
447 serialise_playlist( context, MLT_SERVICE( producer ), node );
448 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
449 serialise_service( context, MLT_SERVICE( producer ), node );
454 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
457 xmlNewProp( child, _x("id"), _x(id) );
458 if ( mlt_properties_get( properties, "title" ) )
459 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
461 // Store application specific properties
462 serialise_store_properties( context, properties, child, context->store );
463 if ( !context->no_meta )
464 serialise_store_properties( context, properties, child, "meta." );
466 // Add producer to the map
467 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
469 // Iterate over the playlist entries
470 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
472 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
474 mlt_producer producer = mlt_producer_cut_parent( info.producer );
475 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
476 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
480 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
481 snprintf( length, 19, "%d", (int)info.frame_count );
482 xmlNewProp( entry, _x("length"), _x(length) );
487 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
488 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
489 xmlNewProp( entry, _x("producer"), _x(id) );
490 sprintf( temp, "%d", (int)info.frame_in );
491 xmlNewProp( entry, _x("in"), _x(temp) );
492 sprintf( temp, "%d", (int)info.frame_out );
493 xmlNewProp( entry, _x("out"), _x(temp) );
494 if ( info.repeat > 1 )
496 sprintf( temp, "%d", info.repeat );
497 xmlNewProp( entry, _x("repeat"), _x(temp) );
499 if ( mlt_producer_is_cut( info.cut ) )
501 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
502 if ( !context->no_meta )
503 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
504 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
510 serialise_service_filters( context, service, child );
512 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
514 char *id = xml_get_id( context, service, xml_existing );
515 xmlNewProp( node, _x("producer"), _x(id) );
519 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
521 xmlNode *child = node;
522 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
524 if ( context->pass == 0 )
526 // Recurse on connected producer
527 serialise_service( context, mlt_service_producer( service ), node );
531 // Get a new id - if already allocated, do nothing
532 char *id = xml_get_id( context, service, xml_tractor );
536 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
539 xmlNewProp( child, _x("id"), _x(id) );
540 if ( mlt_properties_get( properties, "title" ) )
541 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
542 if ( mlt_properties_get( properties, "global_feed" ) )
543 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
544 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
545 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
547 // Store application specific properties
548 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
549 if ( !context->no_meta )
550 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
552 // Recurse on connected producer
553 serialise_service( context, mlt_service_producer( service ), child );
554 serialise_service_filters( context, service, child );
558 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
560 xmlNode *child = node;
561 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
563 // Recurse on connected producer
564 serialise_service( context, mlt_service_producer( service ), node );
566 if ( context->pass == 1 )
568 // Get a new id - if already allocated, do nothing
569 char *id = xml_get_id( context, service, xml_filter );
573 child = xmlNewChild( node, NULL, _x("filter"), NULL );
576 xmlNewProp( child, _x("id"), _x(id) );
577 if ( mlt_properties_get( properties, "title" ) )
578 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
579 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
580 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
582 serialise_properties( context, properties, child );
583 serialise_service_filters( context, service, child );
587 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
589 xmlNode *child = node;
590 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
592 // Recurse on connected producer
593 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
595 if ( context->pass == 1 )
597 // Get a new id - if already allocated, do nothing
598 char *id = xml_get_id( context, service, xml_transition );
602 child = xmlNewChild( node, NULL, _x("transition"), NULL );
605 xmlNewProp( child, _x("id"), _x(id) );
606 if ( mlt_properties_get( properties, "title" ) )
607 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
608 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
609 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
611 serialise_properties( context, properties, child );
612 serialise_service_filters( context, service, child );
616 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
618 // Iterate over consumer/producer connections
619 while ( service != NULL )
621 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
622 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
624 // Tell about the producer
625 if ( strcmp( mlt_type, "producer" ) == 0 )
627 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
628 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
631 serialise_tractor( context, service, node );
633 serialise_tractor( context, service, node );
639 serialise_producer( context, service, node );
641 if ( mlt_properties_get( properties, "xml" ) != NULL )
645 // Tell about the framework container producers
646 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
648 char *resource = mlt_properties_get( properties, "resource" );
650 // Recurse on multitrack's tracks
651 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
653 serialise_multitrack( context, service, node );
657 // Recurse on playlist's clips
658 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
660 serialise_playlist( context, service, node );
663 // Recurse on tractor's producer
664 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
667 serialise_tractor( context, service, node );
669 serialise_tractor( context, service, node );
674 // Treat it as a normal producer
677 serialise_producer( context, service, node );
681 // Tell about a filter
682 else if ( strcmp( mlt_type, "filter" ) == 0 )
684 serialise_filter( context, service, node );
688 // Tell about a transition
689 else if ( strcmp( mlt_type, "transition" ) == 0 )
691 serialise_transition( context, service, node );
695 // Get the next connected service
696 service = mlt_service_producer( service );
700 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
702 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
703 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
704 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
705 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
706 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
709 xmlDocSetRootElement( doc, root );
711 // Indicate the numeric locale
712 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
714 // Indicate the version
715 xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
717 // If we have root, then deal with it now
718 if ( mlt_properties_get( properties, "root" ) != NULL )
720 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
721 context->root = strdup( mlt_properties_get( properties, "root" ) );
725 context->root = strdup( "" );
728 // Assign the additional 'storage' pattern for properties
729 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
730 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
732 // Assign a title property
733 if ( mlt_properties_get( properties, "title" ) != NULL )
734 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
735 mlt_properties_set_int( properties, "global_feed", 1 );
737 // Add a profile child element
740 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
741 if ( profile->description )
742 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
743 sprintf( tmpstr, "%d", profile->width );
744 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
745 sprintf( tmpstr, "%d", profile->height );
746 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
747 sprintf( tmpstr, "%d", profile->progressive );
748 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
749 sprintf( tmpstr, "%d", profile->sample_aspect_num );
750 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
751 sprintf( tmpstr, "%d", profile->sample_aspect_den );
752 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
753 sprintf( tmpstr, "%d", profile->display_aspect_num );
754 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
755 sprintf( tmpstr, "%d", profile->display_aspect_den );
756 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
757 sprintf( tmpstr, "%d", profile->frame_rate_num );
758 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
759 sprintf( tmpstr, "%d", profile->frame_rate_den );
760 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
761 sprintf( tmpstr, "%d", profile->colorspace );
762 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
765 // Construct the context maps
766 context->id_map = mlt_properties_new();
767 context->hide_map = mlt_properties_new();
769 // Ensure producer is a framework producer
770 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
772 // In pass one, we serialise the end producers and playlists,
773 // adding them to a map keyed by address.
774 serialise_service( context, service, root );
776 // In pass two, we serialise the tractor and reference the
777 // producers and playlists
779 serialise_service( context, service, root );
782 mlt_properties_close( context->id_map );
783 mlt_properties_close( context->hide_map );
784 free( context->root );
791 static void output_xml( mlt_consumer this )
793 // Get the producer service
794 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
795 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
796 char *resource = mlt_properties_get( properties, "resource" );
797 xmlDocPtr doc = NULL;
799 if ( !service ) return;
801 // Set the title if provided
802 if ( mlt_properties_get( properties, "title" ) )
803 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
804 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
805 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
807 // Check for a root on the consumer properties and pass to service
808 if ( mlt_properties_get( properties, "root" ) )
809 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
811 // Specify roots in other cases...
812 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
814 // Get the current working directory
815 char *cwd = getcwd( NULL, 0 );
816 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
821 doc = xml_make_doc( this, service );
824 if ( resource == NULL || !strcmp( resource, "" ) )
826 xmlDocFormatDump( stdout, doc, 1 );
828 else if ( strchr( resource, '.' ) == NULL )
830 xmlChar *buffer = NULL;
832 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
833 mlt_properties_set( properties, resource, _s(buffer) );
835 xmlFreeFunc xmlFree = NULL;
836 xmlMemGet( &xmlFree, NULL, NULL, NULL);
842 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
845 // Close the document
848 static int consumer_start( mlt_consumer this )
850 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
852 if ( mlt_properties_get_int( properties, "all" ) )
854 // Check that we're not already running
855 if ( !mlt_properties_get_int( properties, "running" ) )
858 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
860 // Assign the thread to properties
861 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
863 // Set the running state
864 mlt_properties_set_int( properties, "running", 1 );
865 mlt_properties_set_int( properties, "joined", 0 );
868 pthread_create( thread, NULL, consumer_thread, this );
874 mlt_consumer_stop( this );
875 mlt_consumer_stopped( this );
880 static int consumer_is_stopped( mlt_consumer this )
882 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
883 return !mlt_properties_get_int( properties, "running" );
886 static int consumer_stop( mlt_consumer this )
888 // Get the properties
889 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
891 // Check that we're running
892 if ( !mlt_properties_get_int( properties, "joined" ) )
895 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
898 mlt_properties_set_int( properties, "running", 0 );
899 mlt_properties_set_int( properties, "joined", 1 );
901 // Wait for termination
903 pthread_join( *thread, NULL );
909 static void *consumer_thread( void *arg )
911 // Map the argument to the object
912 mlt_consumer this = arg;
914 // Get the properties
915 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
917 // Convenience functionality
918 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
922 mlt_frame frame = NULL;
924 // Loop while running
925 while( !terminated && mlt_properties_get_int( properties, "running" ) )
928 frame = mlt_consumer_rt_frame( this );
930 // Check for termination
931 if ( terminate_on_pause && frame != NULL )
932 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
934 // Check that we have a frame to work with
937 int width = 0, height = 0;
938 int frequency = mlt_properties_get_int( properties, "frequency" );
939 int channels = mlt_properties_get_int( properties, "channels" );
941 mlt_image_format iformat = mlt_image_yuv422;
942 mlt_audio_format aformat = mlt_audio_s16;
945 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
946 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
949 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
950 mlt_frame_close( frame );
955 // Indicate that the consumer is stopped
956 mlt_properties_set_int( properties, "running", 0 );
957 mlt_consumer_stopped( this );