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>
32 #define TIME_PROPERTY "_consumer_xml"
34 #define _x (const xmlChar*)
35 #define _s (const char*)
37 // This maintains counters for adding ids to elements
38 struct serialise_context_s
40 mlt_properties id_map;
48 mlt_properties hide_map;
53 mlt_time_format time_format;
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 );
66 static char* filter_restricted( const char *in )
68 if ( !in ) return NULL;
69 size_t n = strlen( in );
70 char *out = calloc( 1, n + 1 );
73 memset( &mbs, 0, sizeof(mbs) );
77 size_t c = mbrtowc( &w, in, n, &mbs );
78 if ( c <= 0 || c > n ) break;
81 if ( w == 0x9 || w == 0xA || w == 0xD ||
82 ( w >= 0x20 && w <= 0xD7FF ) ||
83 ( w >= 0xE000 && w <= 0xFFFD ) ||
84 ( w >= 0x10000 && w <= 0x10FFFF ) )
87 memset( &ps, 0, sizeof(ps) );
88 c = wcrtomb( p, w, &ps );
108 /** Create or retrieve an id associated to this service.
111 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
115 mlt_properties map = context->id_map;
117 // Search the map for the service
118 for ( i = 0; i < mlt_properties_count( map ); i ++ )
119 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
122 // If the service is not in the map, and the type indicates a new id is needed...
123 if ( i >= mlt_properties_count( map ) && type != xml_existing )
125 // Attempt to reuse existing id
126 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
128 // If no id, or the id is used in the map (for another service), then
130 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
132 char temp[ ID_SIZE ];
138 sprintf( temp, "producer%d", context->producer_count ++ );
141 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
144 sprintf( temp, "playlist%d", context->playlist_count ++ );
147 sprintf( temp, "tractor%d", context->tractor_count ++ );
150 sprintf( temp, "filter%d", context->filter_count ++ );
153 sprintf( temp, "transition%d", context->transition_count ++ );
160 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
162 // Set the data at the generated name
163 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
165 // Get the pointer to the name (i is the end of the list)
166 id = mlt_properties_get_name( map, i );
170 // Store the existing id in the map
171 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
174 else if ( type == xml_existing )
176 id = mlt_properties_get_name( map, i );
182 /** This is what will be called by the factory - anything can be passed in
183 via the argument, but keep it simple.
186 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
188 // Create the consumer object
189 mlt_consumer this = calloc( 1, sizeof( struct mlt_consumer_s ) );
191 // If no malloc'd and consumer init ok
192 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
194 // Allow thread to be started/stopped
195 this->start = consumer_start;
196 this->stop = consumer_stop;
197 this->is_stopped = consumer_is_stopped;
199 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
200 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
201 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
202 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
204 // Return the consumer produced
208 // malloc or consumer init failed
215 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
220 // Enumerate the properties
221 for ( i = 0; i < mlt_properties_count( properties ); i++ )
223 char *name = mlt_properties_get_name( properties, i );
226 mlt_properties_get_value( properties, i ) != NULL &&
227 ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
228 strcmp( name, "mlt" ) &&
229 strcmp( name, "in" ) &&
230 strcmp( name, "out" ) &&
231 strcmp( name, "id" ) &&
232 strcmp( name, "title" ) &&
233 strcmp( name, "root" ) &&
234 strcmp( name, "width" ) &&
235 strcmp( name, "height" ) )
238 if ( !strcmp( name, "length" ) )
239 value = strdup( mlt_properties_get_time( properties, name, context->time_format ) );
241 value = filter_restricted( mlt_properties_get_value( properties, i ) );
244 int rootlen = strlen( context->root );
245 // convert absolute path to relative
246 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
247 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1 ) );
249 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
250 xmlNewProp( p, _x("name"), _x(name) );
257 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
262 // Enumerate the properties
263 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
265 char *name = mlt_properties_get_name( properties, i );
266 if ( !strncmp( name, store, strlen( store ) ) )
268 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
271 int rootlen = strlen( context->root );
272 // convert absolute path to relative
273 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
274 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
276 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
277 xmlNewProp( p, _x("name"), _x(name) );
284 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
288 mlt_filter filter = NULL;
290 // Enumerate the filters
291 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
293 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
294 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
296 // Get a new id - if already allocated, do nothing
297 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
300 p = xmlNewChild( node, NULL, _x("filter"), NULL );
301 xmlNewProp( p, _x("id"), _x(id) );
302 if ( mlt_properties_get( properties, "title" ) )
303 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
304 if ( mlt_properties_get_position( properties, "in" ) )
305 xmlNewProp( p, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
306 if ( mlt_properties_get_position( properties, "out" ) )
307 xmlNewProp( p, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
308 serialise_properties( context, properties, p );
309 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
315 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
317 xmlNode *child = node;
318 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
320 if ( context->pass == 0 )
322 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
323 // Get a new id - if already allocated, do nothing
324 char *id = xml_get_id( context, parent, xml_producer );
328 child = xmlNewChild( node, NULL, _x("producer"), NULL );
331 xmlNewProp( child, _x("id"), _x(id) );
332 if ( mlt_properties_get( properties, "title" ) )
333 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
334 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
335 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
336 serialise_properties( context, properties, child );
337 serialise_service_filters( context, service, child );
339 // Add producer to the map
340 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
344 char *id = xml_get_id( context, parent, xml_existing );
345 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
346 xmlNewProp( node, _x("parent"), _x(id) );
347 xmlNewProp( node, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
348 xmlNewProp( node, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
352 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
354 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
358 if ( context->pass == 0 )
360 // Iterate over the tracks to collect the producers
361 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
363 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
364 serialise_service( context, MLT_SERVICE( producer ), node );
369 // Get a new id - if already allocated, do nothing
370 char *id = xml_get_id( context, service, xml_multitrack );
374 // Serialise the tracks
375 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
377 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
379 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
380 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
382 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
384 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
385 xmlNewProp( track, _x("producer"), _x(id) );
386 if ( mlt_producer_is_cut( producer ) )
388 xmlNewProp( track, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
389 xmlNewProp( track, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
390 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
391 if ( !context->no_meta )
392 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
393 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
396 hide = mlt_properties_get_int( context->hide_map, id );
398 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
400 serialise_service_filters( context, service, node );
404 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
407 xmlNode *child = node;
408 mlt_playlist_clip_info info;
409 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
411 if ( context->pass == 0 )
413 // Get a new id - if already allocated, do nothing
414 char *id = xml_get_id( context, service, xml_playlist );
418 // Iterate over the playlist entries to collect the producers
419 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
421 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
423 if ( info.producer != NULL )
425 mlt_producer producer = mlt_producer_cut_parent( info.producer );
426 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
427 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
428 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
429 serialise_playlist( context, MLT_SERVICE( producer ), node );
430 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
431 serialise_service( context, MLT_SERVICE( producer ), node );
436 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
439 xmlNewProp( child, _x("id"), _x(id) );
440 if ( mlt_properties_get( properties, "title" ) )
441 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
443 // Store application specific properties
444 serialise_store_properties( context, properties, child, context->store );
445 if ( !context->no_meta )
446 serialise_store_properties( context, properties, child, "meta." );
448 // Add producer to the map
449 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
451 // Iterate over the playlist entries
452 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
454 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
456 mlt_producer producer = mlt_producer_cut_parent( info.producer );
457 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
458 char *service_s = mlt_properties_get( producer_props, "mlt_service" );
459 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
461 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
462 mlt_properties_set_data( producer_props, "_profile", context->profile, 0, NULL, NULL );
463 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_count );
464 xmlNewProp( entry, _x("length"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
469 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
470 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
471 xmlNewProp( entry, _x("producer"), _x(id) );
472 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_in );
473 xmlNewProp( entry, _x("in"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
474 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_out );
475 xmlNewProp( entry, _x("out"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
476 if ( info.repeat > 1 )
478 sprintf( temp, "%d", info.repeat );
479 xmlNewProp( entry, _x("repeat"), _x(temp) );
481 if ( mlt_producer_is_cut( info.cut ) )
483 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
484 if ( !context->no_meta )
485 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
486 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
492 serialise_service_filters( context, service, child );
494 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
496 char *id = xml_get_id( context, service, xml_existing );
497 xmlNewProp( node, _x("producer"), _x(id) );
501 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
503 xmlNode *child = node;
504 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
506 if ( context->pass == 0 )
508 // Recurse on connected producer
509 serialise_service( context, mlt_service_producer( service ), node );
513 // Get a new id - if already allocated, do nothing
514 char *id = xml_get_id( context, service, xml_tractor );
518 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
521 xmlNewProp( child, _x("id"), _x(id) );
522 if ( mlt_properties_get( properties, "title" ) )
523 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
524 if ( mlt_properties_get( properties, "global_feed" ) )
525 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
526 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
527 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
529 // Store application specific properties
530 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
531 if ( !context->no_meta )
532 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
534 // Recurse on connected producer
535 serialise_service( context, mlt_service_producer( service ), child );
536 serialise_service_filters( context, service, child );
540 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
542 xmlNode *child = node;
543 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
545 // Recurse on connected producer
546 serialise_service( context, mlt_service_producer( service ), node );
548 if ( context->pass == 1 )
550 // Get a new id - if already allocated, do nothing
551 char *id = xml_get_id( context, service, xml_filter );
555 child = xmlNewChild( node, NULL, _x("filter"), NULL );
558 xmlNewProp( child, _x("id"), _x(id) );
559 if ( mlt_properties_get( properties, "title" ) )
560 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
561 if ( mlt_properties_get_position( properties, "in" ) )
562 xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
563 if ( mlt_properties_get_position( properties, "out" ) )
564 xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
566 serialise_properties( context, properties, child );
567 serialise_service_filters( context, service, child );
571 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
573 xmlNode *child = node;
574 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
576 // Recurse on connected producer
577 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
579 if ( context->pass == 1 )
581 // Get a new id - if already allocated, do nothing
582 char *id = xml_get_id( context, service, xml_transition );
586 child = xmlNewChild( node, NULL, _x("transition"), NULL );
589 xmlNewProp( child, _x("id"), _x(id) );
590 if ( mlt_properties_get( properties, "title" ) )
591 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
592 if ( mlt_properties_get_position( properties, "in" ) )
593 xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
594 if ( mlt_properties_get_position( properties, "out" ) )
595 xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
597 serialise_properties( context, properties, child );
598 serialise_service_filters( context, service, child );
602 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
604 // Iterate over consumer/producer connections
605 while ( service != NULL )
607 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
608 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
610 // Tell about the producer
611 if ( strcmp( mlt_type, "producer" ) == 0 )
613 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
614 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
617 serialise_tractor( context, service, node );
619 serialise_tractor( context, service, node );
625 serialise_producer( context, service, node );
627 if ( mlt_properties_get( properties, "xml" ) != NULL )
631 // Tell about the framework container producers
632 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
634 char *resource = mlt_properties_get( properties, "resource" );
636 // Recurse on multitrack's tracks
637 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
639 serialise_multitrack( context, service, node );
643 // Recurse on playlist's clips
644 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
646 serialise_playlist( context, service, node );
649 // Recurse on tractor's producer
650 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
653 serialise_tractor( context, service, node );
655 serialise_tractor( context, service, node );
660 // Treat it as a normal producer
663 serialise_producer( context, service, node );
667 // Tell about a filter
668 else if ( strcmp( mlt_type, "filter" ) == 0 )
670 serialise_filter( context, service, node );
674 // Tell about a transition
675 else if ( strcmp( mlt_type, "transition" ) == 0 )
677 serialise_transition( context, service, node );
681 // Get the next connected service
682 service = mlt_service_producer( service );
686 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
688 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
689 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
690 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
691 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
692 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
695 xmlDocSetRootElement( doc, root );
697 // Indicate the numeric locale
698 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
700 // Indicate the version
701 xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
703 // If we have root, then deal with it now
704 if ( mlt_properties_get( properties, "root" ) != NULL )
706 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
707 context->root = strdup( mlt_properties_get( properties, "root" ) );
711 context->root = strdup( "" );
714 // Assign the additional 'storage' pattern for properties
715 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
716 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
717 const char *time_format = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "time_format" );
718 if ( time_format && ( !strcmp( time_format, "smpte" ) || !strcmp( time_format, "SMPTE" )
719 || !strcmp( time_format, "timecode" ) ) )
720 context->time_format = mlt_time_smpte;
721 else if ( time_format && ( !strcmp( time_format, "clock" ) || !strcmp( time_format, "CLOCK" ) ) )
722 context->time_format = mlt_time_clock;
724 // Assign a title property
725 if ( mlt_properties_get( properties, "title" ) != NULL )
726 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
727 mlt_properties_set_int( properties, "global_feed", 1 );
729 // Add a profile child element
732 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
733 if ( profile->description )
734 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
735 sprintf( tmpstr, "%d", profile->width );
736 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
737 sprintf( tmpstr, "%d", profile->height );
738 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
739 sprintf( tmpstr, "%d", profile->progressive );
740 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
741 sprintf( tmpstr, "%d", profile->sample_aspect_num );
742 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
743 sprintf( tmpstr, "%d", profile->sample_aspect_den );
744 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
745 sprintf( tmpstr, "%d", profile->display_aspect_num );
746 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
747 sprintf( tmpstr, "%d", profile->display_aspect_den );
748 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
749 sprintf( tmpstr, "%d", profile->frame_rate_num );
750 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
751 sprintf( tmpstr, "%d", profile->frame_rate_den );
752 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
753 sprintf( tmpstr, "%d", profile->colorspace );
754 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
755 context->profile = profile;
758 // Construct the context maps
759 context->id_map = mlt_properties_new();
760 context->hide_map = mlt_properties_new();
762 // Ensure producer is a framework producer
763 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
765 // In pass one, we serialise the end producers and playlists,
766 // adding them to a map keyed by address.
767 serialise_service( context, service, root );
769 // In pass two, we serialise the tractor and reference the
770 // producers and playlists
772 serialise_service( context, service, root );
775 mlt_properties_close( context->id_map );
776 mlt_properties_close( context->hide_map );
777 free( context->root );
784 static void output_xml( mlt_consumer this )
786 // Get the producer service
787 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
788 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
789 char *resource = mlt_properties_get( properties, "resource" );
790 xmlDocPtr doc = NULL;
792 if ( !service ) return;
794 // Set the title if provided
795 if ( mlt_properties_get( properties, "title" ) )
796 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
797 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
798 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
800 // Check for a root on the consumer properties and pass to service
801 if ( mlt_properties_get( properties, "root" ) )
802 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
804 // Specify roots in other cases...
805 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
807 // Get the current working directory
808 char *cwd = getcwd( NULL, 0 );
809 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
814 doc = xml_make_doc( this, service );
817 if ( resource == NULL || !strcmp( resource, "" ) )
819 xmlDocFormatDump( stdout, doc, 1 );
821 else if ( strchr( resource, '.' ) == NULL )
823 xmlChar *buffer = NULL;
825 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
826 mlt_properties_set( properties, resource, _s(buffer) );
828 xmlFreeFunc xmlFree = NULL;
829 xmlMemGet( &xmlFree, NULL, NULL, NULL);
835 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
838 // Close the document
841 static int consumer_start( mlt_consumer this )
843 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
845 if ( mlt_properties_get_int( properties, "all" ) )
847 // Check that we're not already running
848 if ( !mlt_properties_get_int( properties, "running" ) )
851 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
853 // Assign the thread to properties
854 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
856 // Set the running state
857 mlt_properties_set_int( properties, "running", 1 );
858 mlt_properties_set_int( properties, "joined", 0 );
861 pthread_create( thread, NULL, consumer_thread, this );
867 mlt_consumer_stop( this );
868 mlt_consumer_stopped( this );
873 static int consumer_is_stopped( mlt_consumer this )
875 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
876 return !mlt_properties_get_int( properties, "running" );
879 static int consumer_stop( mlt_consumer this )
881 // Get the properties
882 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
884 // Check that we're running
885 if ( !mlt_properties_get_int( properties, "joined" ) )
888 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
891 mlt_properties_set_int( properties, "running", 0 );
892 mlt_properties_set_int( properties, "joined", 1 );
894 // Wait for termination
896 pthread_join( *thread, NULL );
902 static void *consumer_thread( void *arg )
904 // Map the argument to the object
905 mlt_consumer this = arg;
907 // Get the properties
908 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
910 // Convenience functionality
911 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
915 mlt_frame frame = NULL;
917 // Loop while running
918 while( !terminated && mlt_properties_get_int( properties, "running" ) )
921 frame = mlt_consumer_rt_frame( this );
923 // Check for termination
924 if ( terminate_on_pause && frame != NULL )
925 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
927 // Check that we have a frame to work with
930 int width = 0, height = 0;
931 int frequency = mlt_properties_get_int( properties, "frequency" );
932 int channels = mlt_properties_get_int( properties, "channels" );
934 mlt_image_format iformat = mlt_image_yuv422;
935 mlt_audio_format aformat = mlt_audio_s16;
938 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
939 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
942 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
943 mlt_frame_close( frame );
948 // Indicate that the consumer is stopped
949 mlt_properties_set_int( properties, "running", 0 );
950 mlt_consumer_stopped( this );