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 fprintf(stderr, "serializing %s=%s (%d)\n", name, mlt_properties_get( properties, name ),
239 context->time_format);
240 if ( !strcmp( name, "length" ) )
242 char *time = mlt_properties_get_time( properties, name, context->time_format );
244 value = strdup( time );
247 value = filter_restricted( mlt_properties_get_value( properties, i ) );
250 int rootlen = strlen( context->root );
251 // convert absolute path to relative
252 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
253 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1 ) );
255 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
256 xmlNewProp( p, _x("name"), _x(name) );
263 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
268 // Enumerate the properties
269 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
271 char *name = mlt_properties_get_name( properties, i );
272 if ( !strncmp( name, store, strlen( store ) ) )
274 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
277 int rootlen = strlen( context->root );
278 // convert absolute path to relative
279 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
280 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
282 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
283 xmlNewProp( p, _x("name"), _x(name) );
290 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
294 mlt_filter filter = NULL;
296 // Enumerate the filters
297 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
299 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
300 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
302 // Get a new id - if already allocated, do nothing
303 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
306 p = xmlNewChild( node, NULL, _x("filter"), NULL );
307 xmlNewProp( p, _x("id"), _x(id) );
308 if ( mlt_properties_get( properties, "title" ) )
309 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
310 if ( mlt_properties_get_position( properties, "in" ) )
311 xmlNewProp( p, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
312 if ( mlt_properties_get_position( properties, "out" ) )
313 xmlNewProp( p, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
314 serialise_properties( context, properties, p );
315 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
321 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
323 xmlNode *child = node;
324 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
326 if ( context->pass == 0 )
328 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
329 // Get a new id - if already allocated, do nothing
330 char *id = xml_get_id( context, parent, xml_producer );
334 child = xmlNewChild( node, NULL, _x("producer"), NULL );
337 xmlNewProp( child, _x("id"), _x(id) );
338 if ( mlt_properties_get( properties, "title" ) )
339 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
340 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
341 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
342 serialise_properties( context, properties, child );
343 serialise_service_filters( context, service, child );
345 // Add producer to the map
346 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
350 char *id = xml_get_id( context, parent, xml_existing );
351 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
352 xmlNewProp( node, _x("parent"), _x(id) );
353 xmlNewProp( node, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
354 xmlNewProp( node, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
358 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
360 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
364 if ( context->pass == 0 )
366 // Iterate over the tracks to collect the producers
367 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
369 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
370 serialise_service( context, MLT_SERVICE( producer ), node );
375 // Get a new id - if already allocated, do nothing
376 char *id = xml_get_id( context, service, xml_multitrack );
380 // Serialise the tracks
381 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
383 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
385 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
386 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
388 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
390 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
391 xmlNewProp( track, _x("producer"), _x(id) );
392 if ( mlt_producer_is_cut( producer ) )
394 xmlNewProp( track, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
395 xmlNewProp( track, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
396 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
397 if ( !context->no_meta )
398 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
399 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
402 hide = mlt_properties_get_int( context->hide_map, id );
404 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
406 serialise_service_filters( context, service, node );
410 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
413 xmlNode *child = node;
414 mlt_playlist_clip_info info;
415 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
417 if ( context->pass == 0 )
419 // Get a new id - if already allocated, do nothing
420 char *id = xml_get_id( context, service, xml_playlist );
424 // Iterate over the playlist entries to collect the producers
425 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
427 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
429 if ( info.producer != NULL )
431 mlt_producer producer = mlt_producer_cut_parent( info.producer );
432 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
433 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
434 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
435 serialise_playlist( context, MLT_SERVICE( producer ), node );
436 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
437 serialise_service( context, MLT_SERVICE( producer ), node );
442 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
445 xmlNewProp( child, _x("id"), _x(id) );
446 if ( mlt_properties_get( properties, "title" ) )
447 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
449 // Store application specific properties
450 serialise_store_properties( context, properties, child, context->store );
451 if ( !context->no_meta )
452 serialise_store_properties( context, properties, child, "meta." );
454 // Add producer to the map
455 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
457 // Iterate over the playlist entries
458 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
460 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
462 mlt_producer producer = mlt_producer_cut_parent( info.producer );
463 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
464 char *service_s = mlt_properties_get( producer_props, "mlt_service" );
465 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
467 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
468 mlt_properties_set_data( producer_props, "_profile", context->profile, 0, NULL, NULL );
469 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_count );
470 xmlNewProp( entry, _x("length"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
475 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
476 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
477 xmlNewProp( entry, _x("producer"), _x(id) );
478 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_in );
479 xmlNewProp( entry, _x("in"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
480 mlt_properties_set_position( producer_props, TIME_PROPERTY, info.frame_out );
481 xmlNewProp( entry, _x("out"), _x( mlt_properties_get_time( producer_props, TIME_PROPERTY, context->time_format ) ) );
482 if ( info.repeat > 1 )
484 sprintf( temp, "%d", info.repeat );
485 xmlNewProp( entry, _x("repeat"), _x(temp) );
487 if ( mlt_producer_is_cut( info.cut ) )
489 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
490 if ( !context->no_meta )
491 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
492 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
498 serialise_service_filters( context, service, child );
500 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
502 char *id = xml_get_id( context, service, xml_existing );
503 xmlNewProp( node, _x("producer"), _x(id) );
507 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
509 xmlNode *child = node;
510 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
512 if ( context->pass == 0 )
514 // Recurse on connected producer
515 serialise_service( context, mlt_service_producer( service ), node );
519 // Get a new id - if already allocated, do nothing
520 char *id = xml_get_id( context, service, xml_tractor );
524 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
527 xmlNewProp( child, _x("id"), _x(id) );
528 if ( mlt_properties_get( properties, "title" ) )
529 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
530 if ( mlt_properties_get( properties, "global_feed" ) )
531 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
532 xmlNewProp( child, _x("in"), _x(mlt_properties_get_time( properties, "in", context->time_format )) );
533 xmlNewProp( child, _x("out"), _x(mlt_properties_get_time( properties, "out", context->time_format )) );
535 // Store application specific properties
536 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
537 if ( !context->no_meta )
538 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
540 // Recurse on connected producer
541 serialise_service( context, mlt_service_producer( service ), child );
542 serialise_service_filters( context, service, child );
546 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
548 xmlNode *child = node;
549 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
551 // Recurse on connected producer
552 serialise_service( context, mlt_service_producer( service ), node );
554 if ( context->pass == 1 )
556 // Get a new id - if already allocated, do nothing
557 char *id = xml_get_id( context, service, xml_filter );
561 child = xmlNewChild( node, NULL, _x("filter"), NULL );
564 xmlNewProp( child, _x("id"), _x(id) );
565 if ( mlt_properties_get( properties, "title" ) )
566 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
567 if ( mlt_properties_get_position( properties, "in" ) )
568 xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
569 if ( mlt_properties_get_position( properties, "out" ) )
570 xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
572 serialise_properties( context, properties, child );
573 serialise_service_filters( context, service, child );
577 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
579 xmlNode *child = node;
580 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
582 // Recurse on connected producer
583 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
585 if ( context->pass == 1 )
587 // Get a new id - if already allocated, do nothing
588 char *id = xml_get_id( context, service, xml_transition );
592 child = xmlNewChild( node, NULL, _x("transition"), NULL );
595 xmlNewProp( child, _x("id"), _x(id) );
596 if ( mlt_properties_get( properties, "title" ) )
597 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
598 if ( mlt_properties_get_position( properties, "in" ) )
599 xmlNewProp( child, _x("in"), _x( mlt_properties_get_time( properties, "in", context->time_format ) ) );
600 if ( mlt_properties_get_position( properties, "out" ) )
601 xmlNewProp( child, _x("out"), _x( mlt_properties_get_time( properties, "out", context->time_format ) ) );
603 serialise_properties( context, properties, child );
604 serialise_service_filters( context, service, child );
608 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
610 // Iterate over consumer/producer connections
611 while ( service != NULL )
613 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
614 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
616 // Tell about the producer
617 if ( strcmp( mlt_type, "producer" ) == 0 )
619 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
620 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
623 serialise_tractor( context, service, node );
625 serialise_tractor( context, service, node );
631 serialise_producer( context, service, node );
633 if ( mlt_properties_get( properties, "xml" ) != NULL )
637 // Tell about the framework container producers
638 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
640 char *resource = mlt_properties_get( properties, "resource" );
642 // Recurse on multitrack's tracks
643 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
645 serialise_multitrack( context, service, node );
649 // Recurse on playlist's clips
650 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
652 serialise_playlist( context, service, node );
655 // Recurse on tractor's producer
656 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
659 serialise_tractor( context, service, node );
661 serialise_tractor( context, service, node );
666 // Treat it as a normal producer
669 serialise_producer( context, service, node );
673 // Tell about a filter
674 else if ( strcmp( mlt_type, "filter" ) == 0 )
676 serialise_filter( context, service, node );
680 // Tell about a transition
681 else if ( strcmp( mlt_type, "transition" ) == 0 )
683 serialise_transition( context, service, node );
687 // Get the next connected service
688 service = mlt_service_producer( service );
692 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
694 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
695 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
696 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
697 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
698 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
701 xmlDocSetRootElement( doc, root );
703 // Indicate the numeric locale
704 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
706 // Indicate the version
707 xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
709 // If we have root, then deal with it now
710 if ( mlt_properties_get( properties, "root" ) != NULL )
712 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
713 context->root = strdup( mlt_properties_get( properties, "root" ) );
717 context->root = strdup( "" );
720 // Assign the additional 'storage' pattern for properties
721 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
722 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
723 const char *time_format = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "time_format" );
724 if ( time_format && ( !strcmp( time_format, "smpte" ) || !strcmp( time_format, "SMPTE" )
725 || !strcmp( time_format, "timecode" ) ) )
726 context->time_format = mlt_time_smpte;
727 else if ( time_format && ( !strcmp( time_format, "clock" ) || !strcmp( time_format, "CLOCK" ) ) )
728 context->time_format = mlt_time_clock;
730 // Assign a title property
731 if ( mlt_properties_get( properties, "title" ) != NULL )
732 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
733 mlt_properties_set_int( properties, "global_feed", 1 );
735 // Add a profile child element
738 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
739 if ( profile->description )
740 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
741 sprintf( tmpstr, "%d", profile->width );
742 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
743 sprintf( tmpstr, "%d", profile->height );
744 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
745 sprintf( tmpstr, "%d", profile->progressive );
746 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
747 sprintf( tmpstr, "%d", profile->sample_aspect_num );
748 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
749 sprintf( tmpstr, "%d", profile->sample_aspect_den );
750 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
751 sprintf( tmpstr, "%d", profile->display_aspect_num );
752 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
753 sprintf( tmpstr, "%d", profile->display_aspect_den );
754 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
755 sprintf( tmpstr, "%d", profile->frame_rate_num );
756 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
757 sprintf( tmpstr, "%d", profile->frame_rate_den );
758 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
759 sprintf( tmpstr, "%d", profile->colorspace );
760 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
761 context->profile = profile;
764 // Construct the context maps
765 context->id_map = mlt_properties_new();
766 context->hide_map = mlt_properties_new();
768 // Ensure producer is a framework producer
769 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
771 // In pass one, we serialise the end producers and playlists,
772 // adding them to a map keyed by address.
773 serialise_service( context, service, root );
775 // In pass two, we serialise the tractor and reference the
776 // producers and playlists
778 serialise_service( context, service, root );
781 mlt_properties_close( context->id_map );
782 mlt_properties_close( context->hide_map );
783 free( context->root );
790 static void output_xml( mlt_consumer this )
792 // Get the producer service
793 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
794 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
795 char *resource = mlt_properties_get( properties, "resource" );
796 xmlDocPtr doc = NULL;
798 if ( !service ) return;
800 // Set the title if provided
801 if ( mlt_properties_get( properties, "title" ) )
802 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
803 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
804 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
806 // Check for a root on the consumer properties and pass to service
807 if ( mlt_properties_get( properties, "root" ) )
808 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
810 // Specify roots in other cases...
811 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
813 // Get the current working directory
814 char *cwd = getcwd( NULL, 0 );
815 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
820 doc = xml_make_doc( this, service );
823 if ( resource == NULL || !strcmp( resource, "" ) )
825 xmlDocFormatDump( stdout, doc, 1 );
827 else if ( strchr( resource, '.' ) == NULL )
829 xmlChar *buffer = NULL;
831 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
832 mlt_properties_set( properties, resource, _s(buffer) );
834 xmlFreeFunc xmlFree = NULL;
835 xmlMemGet( &xmlFree, NULL, NULL, NULL);
841 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
844 // Close the document
847 static int consumer_start( mlt_consumer this )
849 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
851 if ( mlt_properties_get_int( properties, "all" ) )
853 // Check that we're not already running
854 if ( !mlt_properties_get_int( properties, "running" ) )
857 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
859 // Assign the thread to properties
860 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
862 // Set the running state
863 mlt_properties_set_int( properties, "running", 1 );
864 mlt_properties_set_int( properties, "joined", 0 );
867 pthread_create( thread, NULL, consumer_thread, this );
873 mlt_consumer_stop( this );
874 mlt_consumer_stopped( this );
879 static int consumer_is_stopped( mlt_consumer this )
881 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
882 return !mlt_properties_get_int( properties, "running" );
885 static int consumer_stop( mlt_consumer this )
887 // Get the properties
888 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
890 // Check that we're running
891 if ( !mlt_properties_get_int( properties, "joined" ) )
894 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
897 mlt_properties_set_int( properties, "running", 0 );
898 mlt_properties_set_int( properties, "joined", 1 );
900 // Wait for termination
902 pthread_join( *thread, NULL );
908 static void *consumer_thread( void *arg )
910 // Map the argument to the object
911 mlt_consumer this = arg;
913 // Get the properties
914 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
916 // Convenience functionality
917 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
921 mlt_frame frame = NULL;
923 // Loop while running
924 while( !terminated && mlt_properties_get_int( properties, "running" ) )
927 frame = mlt_consumer_rt_frame( this );
929 // Check for termination
930 if ( terminate_on_pause && frame != NULL )
931 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
933 // Check that we have a frame to work with
936 int width = 0, height = 0;
937 int frequency = mlt_properties_get_int( properties, "frequency" );
938 int channels = mlt_properties_get_int( properties, "channels" );
940 mlt_image_format iformat = mlt_image_yuv422;
941 mlt_audio_format aformat = mlt_audio_s16;
944 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
945 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
948 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
949 mlt_frame_close( frame );
954 // Indicate that the consumer is stopped
955 mlt_properties_set_int( properties, "running", 0 );
956 mlt_consumer_stopped( this );