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>
35 #define _x (const xmlChar*)
36 #define _s (const char*)
38 // This maintains counters for adding ids to elements
39 struct serialise_context_s
41 mlt_properties id_map;
49 mlt_properties hide_map;
54 typedef struct serialise_context_s* serialise_context;
56 /** Forward references to static functions.
59 static int consumer_start( mlt_consumer parent );
60 static int consumer_stop( mlt_consumer parent );
61 static int consumer_is_stopped( mlt_consumer this );
62 static void *consumer_thread( void *arg );
63 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
67 void* filter_restricted( const wchar_t *in )
69 if ( !in ) return NULL;
70 wchar_t *out = calloc( 1, strlen( (const char*) in ) );
71 size_t i, j, n = wcslen( in );
72 for ( i = 0, j = 0; i < n; i++ )
75 if ( w == 0x9 || w == 0xA || w == 0xD ||
76 ( w >= 0x20 && w <= 0xD7FF ) ||
77 ( w >= 0xE000 && w <= 0xFFFD ) ||
78 ( w >= 0x10000 && w <= 0x10FFFF ) )
86 void* filter_restricted( const char *in )
88 if ( !in ) return NULL;
89 char *out = calloc( 1, strlen( in ) );
90 size_t i, j, n = strlen( in );
91 for ( i = 0, j = 0; i < n; i++ )
94 if ( c == 0x9 || c == 0xA || c == 0xD || ( c >= 0x20 && c <= 0xFF ) )
114 /** Create or retrieve an id associated to this service.
117 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
121 mlt_properties map = context->id_map;
123 // Search the map for the service
124 for ( i = 0; i < mlt_properties_count( map ); i ++ )
125 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
128 // If the service is not in the map, and the type indicates a new id is needed...
129 if ( i >= mlt_properties_count( map ) && type != xml_existing )
131 // Attempt to reuse existing id
132 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
134 // If no id, or the id is used in the map (for another service), then
136 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
138 char temp[ ID_SIZE ];
144 sprintf( temp, "producer%d", context->producer_count ++ );
147 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
150 sprintf( temp, "playlist%d", context->playlist_count ++ );
153 sprintf( temp, "tractor%d", context->tractor_count ++ );
156 sprintf( temp, "filter%d", context->filter_count ++ );
159 sprintf( temp, "transition%d", context->transition_count ++ );
166 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
168 // Set the data at the generated name
169 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
171 // Get the pointer to the name (i is the end of the list)
172 id = mlt_properties_get_name( map, i );
176 // Store the existing id in the map
177 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
180 else if ( type == xml_existing )
182 id = mlt_properties_get_name( map, i );
188 /** This is what will be called by the factory - anything can be passed in
189 via the argument, but keep it simple.
192 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
194 // Create the consumer object
195 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
197 // If no malloc'd and consumer init ok
198 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
200 // Allow thread to be started/stopped
201 this->start = consumer_start;
202 this->stop = consumer_stop;
203 this->is_stopped = consumer_is_stopped;
205 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
206 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
207 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
208 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
210 // Return the consumer produced
214 // malloc or consumer init failed
221 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
226 // Enumerate the properties
227 for ( i = 0; i < mlt_properties_count( properties ); i++ )
229 char *name = mlt_properties_get_name( properties, i );
232 mlt_properties_get_value( properties, i ) != NULL &&
233 ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
234 strcmp( name, "mlt" ) &&
235 strcmp( name, "in" ) &&
236 strcmp( name, "out" ) &&
237 strcmp( name, "id" ) &&
238 strcmp( name, "title" ) &&
239 strcmp( name, "root" ) &&
240 strcmp( name, "width" ) &&
241 strcmp( name, "height" ) )
243 char *value = filter_restricted( mlt_properties_get_value( properties, i ) );
246 int rootlen = strlen( context->root );
247 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
248 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 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
273 value += rootlen + 1;
274 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
275 xmlNewProp( p, _x("name"), _x(name) );
282 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
286 mlt_filter filter = NULL;
288 // Enumerate the filters
289 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
291 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
292 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
294 // Get a new id - if already allocated, do nothing
295 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
298 int in = mlt_properties_get_position( properties, "in" );
299 int out = mlt_properties_get_position( properties, "out" );
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 ( in != 0 || out != 0 )
307 sprintf( temp, "%d", in );
308 xmlNewProp( p, _x("in"), _x(temp) );
309 sprintf( temp, "%d", out );
310 xmlNewProp( p, _x("out"), _x(temp) );
312 serialise_properties( context, properties, p );
313 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
319 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
321 xmlNode *child = node;
322 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
324 if ( context->pass == 0 )
326 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
327 // Get a new id - if already allocated, do nothing
328 char *id = xml_get_id( context, parent, xml_producer );
332 child = xmlNewChild( node, NULL, _x("producer"), NULL );
335 xmlNewProp( child, _x("id"), _x(id) );
336 if ( mlt_properties_get( properties, "title" ) )
337 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
338 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
339 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
340 serialise_properties( context, properties, child );
341 serialise_service_filters( context, service, child );
343 // Add producer to the map
344 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
348 char *id = xml_get_id( context, parent, xml_existing );
349 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
350 xmlNewProp( node, _x("parent"), _x(id) );
351 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
352 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
356 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
358 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
362 if ( context->pass == 0 )
364 // Iterate over the tracks to collect the producers
365 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
367 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
368 serialise_service( context, MLT_SERVICE( producer ), node );
373 // Get a new id - if already allocated, do nothing
374 char *id = xml_get_id( context, service, xml_multitrack );
378 // Serialise the tracks
379 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
381 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
383 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
384 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
386 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
388 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
389 xmlNewProp( track, _x("producer"), _x(id) );
390 if ( mlt_producer_is_cut( producer ) )
392 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
393 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
394 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
395 if ( !context->no_meta )
396 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
397 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
400 hide = mlt_properties_get_int( context->hide_map, id );
402 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
404 serialise_service_filters( context, service, node );
408 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
411 xmlNode *child = node;
412 mlt_playlist_clip_info info;
413 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
415 if ( context->pass == 0 )
417 // Get a new id - if already allocated, do nothing
418 char *id = xml_get_id( context, service, xml_playlist );
422 // Iterate over the playlist entries to collect the producers
423 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
425 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
427 if ( info.producer != NULL )
429 mlt_producer producer = mlt_producer_cut_parent( info.producer );
430 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
431 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
432 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
433 serialise_playlist( context, MLT_SERVICE( producer ), node );
434 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
435 serialise_service( context, MLT_SERVICE( producer ), node );
440 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
443 xmlNewProp( child, _x("id"), _x(id) );
444 if ( mlt_properties_get( properties, "title" ) )
445 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
447 // Store application specific properties
448 serialise_store_properties( context, properties, child, context->store );
449 if ( !context->no_meta )
450 serialise_store_properties( context, properties, child, "meta." );
452 // Add producer to the map
453 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
455 // Iterate over the playlist entries
456 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
458 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
460 mlt_producer producer = mlt_producer_cut_parent( info.producer );
461 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
462 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
466 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
467 snprintf( length, 19, "%d", (int)info.frame_count );
468 xmlNewProp( entry, _x("length"), _x(length) );
473 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
474 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
475 xmlNewProp( entry, _x("producer"), _x(id) );
476 sprintf( temp, "%d", (int)info.frame_in );
477 xmlNewProp( entry, _x("in"), _x(temp) );
478 sprintf( temp, "%d", (int)info.frame_out );
479 xmlNewProp( entry, _x("out"), _x(temp) );
480 if ( info.repeat > 1 )
482 sprintf( temp, "%d", info.repeat );
483 xmlNewProp( entry, _x("repeat"), _x(temp) );
485 if ( mlt_producer_is_cut( info.cut ) )
487 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
488 if ( !context->no_meta )
489 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
490 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
496 serialise_service_filters( context, service, child );
498 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
500 char *id = xml_get_id( context, service, xml_existing );
501 xmlNewProp( node, _x("producer"), _x(id) );
505 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
507 xmlNode *child = node;
508 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
510 if ( context->pass == 0 )
512 // Recurse on connected producer
513 serialise_service( context, mlt_service_producer( service ), node );
517 // Get a new id - if already allocated, do nothing
518 char *id = xml_get_id( context, service, xml_tractor );
522 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
525 xmlNewProp( child, _x("id"), _x(id) );
526 if ( mlt_properties_get( properties, "title" ) )
527 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
528 if ( mlt_properties_get( properties, "global_feed" ) )
529 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
530 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
531 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
533 // Store application specific properties
534 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
535 if ( !context->no_meta )
536 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
538 // Recurse on connected producer
539 serialise_service( context, mlt_service_producer( service ), child );
540 serialise_service_filters( context, service, child );
544 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
546 xmlNode *child = node;
547 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
549 // Recurse on connected producer
550 serialise_service( context, mlt_service_producer( service ), node );
552 if ( context->pass == 1 )
554 // Get a new id - if already allocated, do nothing
555 char *id = xml_get_id( context, service, xml_filter );
559 child = xmlNewChild( node, NULL, _x("filter"), NULL );
562 xmlNewProp( child, _x("id"), _x(id) );
563 if ( mlt_properties_get( properties, "title" ) )
564 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
565 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
566 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
568 serialise_properties( context, properties, child );
569 serialise_service_filters( context, service, child );
573 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
575 xmlNode *child = node;
576 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
578 // Recurse on connected producer
579 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
581 if ( context->pass == 1 )
583 // Get a new id - if already allocated, do nothing
584 char *id = xml_get_id( context, service, xml_transition );
588 child = xmlNewChild( node, NULL, _x("transition"), NULL );
591 xmlNewProp( child, _x("id"), _x(id) );
592 if ( mlt_properties_get( properties, "title" ) )
593 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
594 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
595 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
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" );
718 // Assign a title property
719 if ( mlt_properties_get( properties, "title" ) != NULL )
720 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
721 mlt_properties_set_int( properties, "global_feed", 1 );
723 // Add a profile child element
726 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
727 if ( profile->description )
728 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
729 sprintf( tmpstr, "%d", profile->width );
730 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
731 sprintf( tmpstr, "%d", profile->height );
732 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
733 sprintf( tmpstr, "%d", profile->progressive );
734 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
735 sprintf( tmpstr, "%d", profile->sample_aspect_num );
736 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
737 sprintf( tmpstr, "%d", profile->sample_aspect_den );
738 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
739 sprintf( tmpstr, "%d", profile->display_aspect_num );
740 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
741 sprintf( tmpstr, "%d", profile->display_aspect_den );
742 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
743 sprintf( tmpstr, "%d", profile->frame_rate_num );
744 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
745 sprintf( tmpstr, "%d", profile->frame_rate_den );
746 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
747 sprintf( tmpstr, "%d", profile->colorspace );
748 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
751 // Construct the context maps
752 context->id_map = mlt_properties_new();
753 context->hide_map = mlt_properties_new();
755 // Ensure producer is a framework producer
756 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
758 // In pass one, we serialise the end producers and playlists,
759 // adding them to a map keyed by address.
760 serialise_service( context, service, root );
762 // In pass two, we serialise the tractor and reference the
763 // producers and playlists
765 serialise_service( context, service, root );
768 mlt_properties_close( context->id_map );
769 mlt_properties_close( context->hide_map );
770 free( context->root );
777 static void output_xml( mlt_consumer this )
779 // Get the producer service
780 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
781 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
782 char *resource = mlt_properties_get( properties, "resource" );
783 xmlDocPtr doc = NULL;
785 if ( !service ) return;
787 // Set the title if provided
788 if ( mlt_properties_get( properties, "title" ) )
789 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
790 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
791 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
793 // Check for a root on the consumer properties and pass to service
794 if ( mlt_properties_get( properties, "root" ) )
795 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
797 // Specify roots in other cases...
798 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
800 // Get the current working directory
801 char *cwd = getcwd( NULL, 0 );
802 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
807 doc = xml_make_doc( this, service );
810 if ( resource == NULL || !strcmp( resource, "" ) )
812 xmlDocFormatDump( stdout, doc, 1 );
814 else if ( strchr( resource, '.' ) == NULL )
816 xmlChar *buffer = NULL;
818 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
819 mlt_properties_set( properties, resource, _s(buffer) );
821 xmlFreeFunc xmlFree = NULL;
822 xmlMemGet( &xmlFree, NULL, NULL, NULL);
828 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
831 // Close the document
834 static int consumer_start( mlt_consumer this )
836 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
838 if ( mlt_properties_get_int( properties, "all" ) )
840 // Check that we're not already running
841 if ( !mlt_properties_get_int( properties, "running" ) )
844 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
846 // Assign the thread to properties
847 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
849 // Set the running state
850 mlt_properties_set_int( properties, "running", 1 );
851 mlt_properties_set_int( properties, "joined", 0 );
854 pthread_create( thread, NULL, consumer_thread, this );
860 mlt_consumer_stop( this );
861 mlt_consumer_stopped( this );
866 static int consumer_is_stopped( mlt_consumer this )
868 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
869 return !mlt_properties_get_int( properties, "running" );
872 static int consumer_stop( mlt_consumer this )
874 // Get the properties
875 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
877 // Check that we're running
878 if ( !mlt_properties_get_int( properties, "joined" ) )
881 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
884 mlt_properties_set_int( properties, "running", 0 );
885 mlt_properties_set_int( properties, "joined", 1 );
887 // Wait for termination
889 pthread_join( *thread, NULL );
895 static void *consumer_thread( void *arg )
897 // Map the argument to the object
898 mlt_consumer this = arg;
900 // Get the properties
901 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
903 // Convenience functionality
904 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
908 mlt_frame frame = NULL;
910 // Loop while running
911 while( !terminated && mlt_properties_get_int( properties, "running" ) )
914 frame = mlt_consumer_rt_frame( this );
916 // Check for termination
917 if ( terminate_on_pause && frame != NULL )
918 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
920 // Check that we have a frame to work with
923 int width = 0, height = 0;
924 int frequency = mlt_properties_get_int( properties, "frequency" );
925 int channels = mlt_properties_get_int( properties, "channels" );
927 mlt_image_format iformat = mlt_image_yuv422;
928 mlt_audio_format aformat = mlt_audio_s16;
931 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
932 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
935 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
936 mlt_frame_close( frame );
941 // Indicate that the consumer is stopped
942 mlt_properties_set_int( properties, "running", 0 );
943 mlt_consumer_stopped( this );