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 char* 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 );
75 if ( c <= 0 || c > n ) break;
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 // convert absolute path to relative
239 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
240 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1 ) );
242 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
243 xmlNewProp( p, _x("name"), _x(name) );
250 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
255 // Enumerate the properties
256 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
258 char *name = mlt_properties_get_name( properties, i );
259 if ( !strncmp( name, store, strlen( store ) ) )
261 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
264 int rootlen = strlen( context->root );
265 // convert absolute path to relative
266 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
267 p = xmlNewTextChild( node, NULL, _x("property"), _x(value + rootlen + 1) );
269 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
270 xmlNewProp( p, _x("name"), _x(name) );
277 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
281 mlt_filter filter = NULL;
283 // Enumerate the filters
284 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
286 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
287 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
289 // Get a new id - if already allocated, do nothing
290 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
293 int in = mlt_properties_get_position( properties, "in" );
294 int out = mlt_properties_get_position( properties, "out" );
295 p = xmlNewChild( node, NULL, _x("filter"), NULL );
296 xmlNewProp( p, _x("id"), _x(id) );
297 if ( mlt_properties_get( properties, "title" ) )
298 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
299 if ( in != 0 || out != 0 )
302 sprintf( temp, "%d", in );
303 xmlNewProp( p, _x("in"), _x(temp) );
304 sprintf( temp, "%d", out );
305 xmlNewProp( p, _x("out"), _x(temp) );
307 serialise_properties( context, properties, p );
308 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
314 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
316 xmlNode *child = node;
317 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
319 if ( context->pass == 0 )
321 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
322 // Get a new id - if already allocated, do nothing
323 char *id = xml_get_id( context, parent, xml_producer );
327 child = xmlNewChild( node, NULL, _x("producer"), NULL );
330 xmlNewProp( child, _x("id"), _x(id) );
331 if ( mlt_properties_get( properties, "title" ) )
332 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
333 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
334 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
335 serialise_properties( context, properties, child );
336 serialise_service_filters( context, service, child );
338 // Add producer to the map
339 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
343 char *id = xml_get_id( context, parent, xml_existing );
344 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
345 xmlNewProp( node, _x("parent"), _x(id) );
346 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
347 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
351 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
353 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
357 if ( context->pass == 0 )
359 // Iterate over the tracks to collect the producers
360 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
362 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
363 serialise_service( context, MLT_SERVICE( producer ), node );
368 // Get a new id - if already allocated, do nothing
369 char *id = xml_get_id( context, service, xml_multitrack );
373 // Serialise the tracks
374 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
376 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
378 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
379 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
381 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
383 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
384 xmlNewProp( track, _x("producer"), _x(id) );
385 if ( mlt_producer_is_cut( producer ) )
387 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
388 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
389 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
390 if ( !context->no_meta )
391 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
392 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
395 hide = mlt_properties_get_int( context->hide_map, id );
397 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
399 serialise_service_filters( context, service, node );
403 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
406 xmlNode *child = node;
407 mlt_playlist_clip_info info;
408 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
410 if ( context->pass == 0 )
412 // Get a new id - if already allocated, do nothing
413 char *id = xml_get_id( context, service, xml_playlist );
417 // Iterate over the playlist entries to collect the producers
418 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
420 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
422 if ( info.producer != NULL )
424 mlt_producer producer = mlt_producer_cut_parent( info.producer );
425 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
426 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
427 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
428 serialise_playlist( context, MLT_SERVICE( producer ), node );
429 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
430 serialise_service( context, MLT_SERVICE( producer ), node );
435 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
438 xmlNewProp( child, _x("id"), _x(id) );
439 if ( mlt_properties_get( properties, "title" ) )
440 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
442 // Store application specific properties
443 serialise_store_properties( context, properties, child, context->store );
444 if ( !context->no_meta )
445 serialise_store_properties( context, properties, child, "meta." );
447 // Add producer to the map
448 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
450 // Iterate over the playlist entries
451 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
453 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
455 mlt_producer producer = mlt_producer_cut_parent( info.producer );
456 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
457 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
461 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
462 snprintf( length, 19, "%d", (int)info.frame_count );
463 xmlNewProp( entry, _x("length"), _x(length) );
468 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
469 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
470 xmlNewProp( entry, _x("producer"), _x(id) );
471 sprintf( temp, "%d", (int)info.frame_in );
472 xmlNewProp( entry, _x("in"), _x(temp) );
473 sprintf( temp, "%d", (int)info.frame_out );
474 xmlNewProp( entry, _x("out"), _x(temp) );
475 if ( info.repeat > 1 )
477 sprintf( temp, "%d", info.repeat );
478 xmlNewProp( entry, _x("repeat"), _x(temp) );
480 if ( mlt_producer_is_cut( info.cut ) )
482 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
483 if ( !context->no_meta )
484 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
485 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
491 serialise_service_filters( context, service, child );
493 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
495 char *id = xml_get_id( context, service, xml_existing );
496 xmlNewProp( node, _x("producer"), _x(id) );
500 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
502 xmlNode *child = node;
503 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
505 if ( context->pass == 0 )
507 // Recurse on connected producer
508 serialise_service( context, mlt_service_producer( service ), node );
512 // Get a new id - if already allocated, do nothing
513 char *id = xml_get_id( context, service, xml_tractor );
517 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
520 xmlNewProp( child, _x("id"), _x(id) );
521 if ( mlt_properties_get( properties, "title" ) )
522 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
523 if ( mlt_properties_get( properties, "global_feed" ) )
524 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
525 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
526 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
528 // Store application specific properties
529 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
530 if ( !context->no_meta )
531 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
533 // Recurse on connected producer
534 serialise_service( context, mlt_service_producer( service ), child );
535 serialise_service_filters( context, service, child );
539 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
541 xmlNode *child = node;
542 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
544 // Recurse on connected producer
545 serialise_service( context, mlt_service_producer( service ), node );
547 if ( context->pass == 1 )
549 // Get a new id - if already allocated, do nothing
550 char *id = xml_get_id( context, service, xml_filter );
554 child = xmlNewChild( node, NULL, _x("filter"), NULL );
557 xmlNewProp( child, _x("id"), _x(id) );
558 if ( mlt_properties_get( properties, "title" ) )
559 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
560 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
561 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
563 serialise_properties( context, properties, child );
564 serialise_service_filters( context, service, child );
568 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
570 xmlNode *child = node;
571 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
573 // Recurse on connected producer
574 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
576 if ( context->pass == 1 )
578 // Get a new id - if already allocated, do nothing
579 char *id = xml_get_id( context, service, xml_transition );
583 child = xmlNewChild( node, NULL, _x("transition"), NULL );
586 xmlNewProp( child, _x("id"), _x(id) );
587 if ( mlt_properties_get( properties, "title" ) )
588 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
589 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
590 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
592 serialise_properties( context, properties, child );
593 serialise_service_filters( context, service, child );
597 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
599 // Iterate over consumer/producer connections
600 while ( service != NULL )
602 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
603 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
605 // Tell about the producer
606 if ( strcmp( mlt_type, "producer" ) == 0 )
608 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
609 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
612 serialise_tractor( context, service, node );
614 serialise_tractor( context, service, node );
620 serialise_producer( context, service, node );
622 if ( mlt_properties_get( properties, "xml" ) != NULL )
626 // Tell about the framework container producers
627 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
629 char *resource = mlt_properties_get( properties, "resource" );
631 // Recurse on multitrack's tracks
632 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
634 serialise_multitrack( context, service, node );
638 // Recurse on playlist's clips
639 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
641 serialise_playlist( context, service, node );
644 // Recurse on tractor's producer
645 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
648 serialise_tractor( context, service, node );
650 serialise_tractor( context, service, node );
655 // Treat it as a normal producer
658 serialise_producer( context, service, node );
662 // Tell about a filter
663 else if ( strcmp( mlt_type, "filter" ) == 0 )
665 serialise_filter( context, service, node );
669 // Tell about a transition
670 else if ( strcmp( mlt_type, "transition" ) == 0 )
672 serialise_transition( context, service, node );
676 // Get the next connected service
677 service = mlt_service_producer( service );
681 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
683 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
684 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
685 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
686 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
687 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
690 xmlDocSetRootElement( doc, root );
692 // Indicate the numeric locale
693 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
695 // Indicate the version
696 xmlNewProp( root, _x("version"), _x( mlt_version_get_string() ) );
698 // If we have root, then deal with it now
699 if ( mlt_properties_get( properties, "root" ) != NULL )
701 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
702 context->root = strdup( mlt_properties_get( properties, "root" ) );
706 context->root = strdup( "" );
709 // Assign the additional 'storage' pattern for properties
710 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
711 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
713 // Assign a title property
714 if ( mlt_properties_get( properties, "title" ) != NULL )
715 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
716 mlt_properties_set_int( properties, "global_feed", 1 );
718 // Add a profile child element
721 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
722 if ( profile->description )
723 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
724 sprintf( tmpstr, "%d", profile->width );
725 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
726 sprintf( tmpstr, "%d", profile->height );
727 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
728 sprintf( tmpstr, "%d", profile->progressive );
729 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
730 sprintf( tmpstr, "%d", profile->sample_aspect_num );
731 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
732 sprintf( tmpstr, "%d", profile->sample_aspect_den );
733 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
734 sprintf( tmpstr, "%d", profile->display_aspect_num );
735 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
736 sprintf( tmpstr, "%d", profile->display_aspect_den );
737 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
738 sprintf( tmpstr, "%d", profile->frame_rate_num );
739 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
740 sprintf( tmpstr, "%d", profile->frame_rate_den );
741 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
742 sprintf( tmpstr, "%d", profile->colorspace );
743 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
746 // Construct the context maps
747 context->id_map = mlt_properties_new();
748 context->hide_map = mlt_properties_new();
750 // Ensure producer is a framework producer
751 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
753 // In pass one, we serialise the end producers and playlists,
754 // adding them to a map keyed by address.
755 serialise_service( context, service, root );
757 // In pass two, we serialise the tractor and reference the
758 // producers and playlists
760 serialise_service( context, service, root );
763 mlt_properties_close( context->id_map );
764 mlt_properties_close( context->hide_map );
765 free( context->root );
772 static void output_xml( mlt_consumer this )
774 // Get the producer service
775 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
776 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
777 char *resource = mlt_properties_get( properties, "resource" );
778 xmlDocPtr doc = NULL;
780 if ( !service ) return;
782 // Set the title if provided
783 if ( mlt_properties_get( properties, "title" ) )
784 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
785 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
786 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
788 // Check for a root on the consumer properties and pass to service
789 if ( mlt_properties_get( properties, "root" ) )
790 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
792 // Specify roots in other cases...
793 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
795 // Get the current working directory
796 char *cwd = getcwd( NULL, 0 );
797 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
802 doc = xml_make_doc( this, service );
805 if ( resource == NULL || !strcmp( resource, "" ) )
807 xmlDocFormatDump( stdout, doc, 1 );
809 else if ( strchr( resource, '.' ) == NULL )
811 xmlChar *buffer = NULL;
813 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
814 mlt_properties_set( properties, resource, _s(buffer) );
816 xmlFreeFunc xmlFree = NULL;
817 xmlMemGet( &xmlFree, NULL, NULL, NULL);
823 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
826 // Close the document
829 static int consumer_start( mlt_consumer this )
831 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
833 if ( mlt_properties_get_int( properties, "all" ) )
835 // Check that we're not already running
836 if ( !mlt_properties_get_int( properties, "running" ) )
839 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
841 // Assign the thread to properties
842 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
844 // Set the running state
845 mlt_properties_set_int( properties, "running", 1 );
846 mlt_properties_set_int( properties, "joined", 0 );
849 pthread_create( thread, NULL, consumer_thread, this );
855 mlt_consumer_stop( this );
856 mlt_consumer_stopped( this );
861 static int consumer_is_stopped( mlt_consumer this )
863 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
864 return !mlt_properties_get_int( properties, "running" );
867 static int consumer_stop( mlt_consumer this )
869 // Get the properties
870 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
872 // Check that we're running
873 if ( !mlt_properties_get_int( properties, "joined" ) )
876 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
879 mlt_properties_set_int( properties, "running", 0 );
880 mlt_properties_set_int( properties, "joined", 1 );
882 // Wait for termination
884 pthread_join( *thread, NULL );
890 static void *consumer_thread( void *arg )
892 // Map the argument to the object
893 mlt_consumer this = arg;
895 // Get the properties
896 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
898 // Convenience functionality
899 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
903 mlt_frame frame = NULL;
905 // Loop while running
906 while( !terminated && mlt_properties_get_int( properties, "running" ) )
909 frame = mlt_consumer_rt_frame( this );
911 // Check for termination
912 if ( terminate_on_pause && frame != NULL )
913 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
915 // Check that we have a frame to work with
918 int width = 0, height = 0;
919 int frequency = mlt_properties_get_int( properties, "frequency" );
920 int channels = mlt_properties_get_int( properties, "channels" );
922 mlt_image_format iformat = mlt_image_yuv422;
923 mlt_audio_format aformat = mlt_audio_s16;
926 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
927 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
930 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
931 mlt_frame_close( frame );
936 // Indicate that the consumer is stopped
937 mlt_properties_set_int( properties, "running", 0 );
938 mlt_consumer_stopped( this );