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 _x (const xmlChar*)
33 #define _s (const char*)
35 // This maintains counters for adding ids to elements
36 struct serialise_context_s
38 mlt_properties id_map;
46 mlt_properties hide_map;
51 typedef struct serialise_context_s* serialise_context;
53 /** Forward references to static functions.
56 static int consumer_start( mlt_consumer parent );
57 static int consumer_stop( mlt_consumer parent );
58 static int consumer_is_stopped( mlt_consumer this );
59 static void *consumer_thread( void *arg );
60 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
74 /** Create or retrieve an id associated to this service.
77 static char *xml_get_id( serialise_context context, mlt_service service, xml_type type )
81 mlt_properties map = context->id_map;
83 // Search the map for the service
84 for ( i = 0; i < mlt_properties_count( map ); i ++ )
85 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
88 // If the service is not in the map, and the type indicates a new id is needed...
89 if ( i >= mlt_properties_count( map ) && type != xml_existing )
91 // Attempt to reuse existing id
92 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
94 // If no id, or the id is used in the map (for another service), then
96 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
104 sprintf( temp, "producer%d", context->producer_count ++ );
107 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
110 sprintf( temp, "playlist%d", context->playlist_count ++ );
113 sprintf( temp, "tractor%d", context->tractor_count ++ );
116 sprintf( temp, "filter%d", context->filter_count ++ );
119 sprintf( temp, "transition%d", context->transition_count ++ );
126 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
128 // Set the data at the generated name
129 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
131 // Get the pointer to the name (i is the end of the list)
132 id = mlt_properties_get_name( map, i );
136 // Store the existing id in the map
137 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
140 else if ( type == xml_existing )
142 id = mlt_properties_get_name( map, i );
148 /** This is what will be called by the factory - anything can be passed in
149 via the argument, but keep it simple.
152 mlt_consumer consumer_xml_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
154 // Create the consumer object
155 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
157 // If no malloc'd and consumer init ok
158 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
160 // Allow thread to be started/stopped
161 this->start = consumer_start;
162 this->stop = consumer_stop;
163 this->is_stopped = consumer_is_stopped;
165 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
166 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "real_time", -1 );
167 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "prefill", 1 );
168 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( this ), "terminate_on_pause", 1 );
170 // Return the consumer produced
174 // malloc or consumer init failed
181 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
186 // Enumerate the properties
187 for ( i = 0; i < mlt_properties_count( properties ); i++ )
189 char *name = mlt_properties_get_name( properties, i );
192 mlt_properties_get_value( properties, i ) != NULL &&
193 ( !context->no_meta || strncmp( name, "meta.", 5 ) ) &&
194 strcmp( name, "mlt" ) &&
195 strcmp( name, "in" ) &&
196 strcmp( name, "out" ) &&
197 strcmp( name, "id" ) &&
198 strcmp( name, "title" ) &&
199 strcmp( name, "root" ) &&
200 strcmp( name, "width" ) &&
201 strcmp( name, "height" ) )
203 char *value = mlt_properties_get_value( properties, i );
204 int rootlen = strlen( context->root );
205 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
206 value += rootlen + 1;
207 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
208 xmlNewProp( p, _x("name"), _x(name) );
213 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, const char *store )
218 // Enumerate the properties
219 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
221 char *name = mlt_properties_get_name( properties, i );
222 if ( !strncmp( name, store, strlen( store ) ) )
224 char *value = mlt_properties_get_value( properties, i );
227 int rootlen = strlen( context->root );
228 if ( rootlen && !strncmp( value, context->root, rootlen ) && value[ rootlen ] == '/' )
229 value += rootlen + 1;
230 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
231 xmlNewProp( p, _x("name"), _x(name) );
237 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
241 mlt_filter filter = NULL;
243 // Enumerate the filters
244 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
246 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
247 if ( mlt_properties_get_int( properties, "_loader" ) == 0 )
249 // Get a new id - if already allocated, do nothing
250 char *id = xml_get_id( context, MLT_FILTER_SERVICE( filter ), xml_filter );
253 int in = mlt_properties_get_position( properties, "in" );
254 int out = mlt_properties_get_position( properties, "out" );
255 p = xmlNewChild( node, NULL, _x("filter"), NULL );
256 xmlNewProp( p, _x("id"), _x(id) );
257 if ( mlt_properties_get( properties, "title" ) )
258 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
259 if ( in != 0 || out != 0 )
262 sprintf( temp, "%d", in );
263 xmlNewProp( p, _x("in"), _x(temp) );
264 sprintf( temp, "%d", out );
265 xmlNewProp( p, _x("out"), _x(temp) );
267 serialise_properties( context, properties, p );
268 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
274 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
276 xmlNode *child = node;
277 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
279 if ( context->pass == 0 )
281 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
282 // Get a new id - if already allocated, do nothing
283 char *id = xml_get_id( context, parent, xml_producer );
287 child = xmlNewChild( node, NULL, _x("producer"), NULL );
290 xmlNewProp( child, _x("id"), _x(id) );
291 if ( mlt_properties_get( properties, "title" ) )
292 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
293 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
294 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
295 serialise_properties( context, properties, child );
296 serialise_service_filters( context, service, child );
298 // Add producer to the map
299 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
303 char *id = xml_get_id( context, parent, xml_existing );
304 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
305 xmlNewProp( node, _x("parent"), _x(id) );
306 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
307 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
311 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
313 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
317 if ( context->pass == 0 )
319 // Iterate over the tracks to collect the producers
320 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
322 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
323 serialise_service( context, MLT_SERVICE( producer ), node );
328 // Get a new id - if already allocated, do nothing
329 char *id = xml_get_id( context, service, xml_multitrack );
333 // Serialise the tracks
334 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
336 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
338 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
339 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
341 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
343 char *id = xml_get_id( context, MLT_SERVICE( parent ), xml_existing );
344 xmlNewProp( track, _x("producer"), _x(id) );
345 if ( mlt_producer_is_cut( producer ) )
347 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
348 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
349 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
350 if ( !context->no_meta )
351 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
352 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
355 hide = mlt_properties_get_int( context->hide_map, id );
357 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
359 serialise_service_filters( context, service, node );
363 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
366 xmlNode *child = node;
367 mlt_playlist_clip_info info;
368 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
370 if ( context->pass == 0 )
372 // Get a new id - if already allocated, do nothing
373 char *id = xml_get_id( context, service, xml_playlist );
377 // Iterate over the playlist entries to collect the producers
378 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
380 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
382 if ( info.producer != NULL )
384 mlt_producer producer = mlt_producer_cut_parent( info.producer );
385 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
386 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
387 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
388 serialise_playlist( context, MLT_SERVICE( producer ), node );
389 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
390 serialise_service( context, MLT_SERVICE( producer ), node );
395 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
398 xmlNewProp( child, _x("id"), _x(id) );
399 if ( mlt_properties_get( properties, "title" ) )
400 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
402 // Store application specific properties
403 serialise_store_properties( context, properties, child, context->store );
404 if ( !context->no_meta )
405 serialise_store_properties( context, properties, child, "meta." );
407 // Add producer to the map
408 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
410 // Iterate over the playlist entries
411 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
413 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
415 mlt_producer producer = mlt_producer_cut_parent( info.producer );
416 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
417 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
421 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
422 snprintf( length, 19, "%d", (int)info.frame_count );
423 xmlNewProp( entry, _x("length"), _x(length) );
428 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
429 id = xml_get_id( context, MLT_SERVICE( producer ), xml_existing );
430 xmlNewProp( entry, _x("producer"), _x(id) );
431 sprintf( temp, "%d", (int)info.frame_in );
432 xmlNewProp( entry, _x("in"), _x(temp) );
433 sprintf( temp, "%d", (int)info.frame_out );
434 xmlNewProp( entry, _x("out"), _x(temp) );
435 if ( info.repeat > 1 )
437 sprintf( temp, "%d", info.repeat );
438 xmlNewProp( entry, _x("repeat"), _x(temp) );
440 if ( mlt_producer_is_cut( info.cut ) )
442 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
443 if ( !context->no_meta )
444 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
445 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
451 serialise_service_filters( context, service, child );
453 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
455 char *id = xml_get_id( context, service, xml_existing );
456 xmlNewProp( node, _x("producer"), _x(id) );
460 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
462 xmlNode *child = node;
463 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
465 if ( context->pass == 0 )
467 // Recurse on connected producer
468 serialise_service( context, mlt_service_producer( service ), node );
472 // Get a new id - if already allocated, do nothing
473 char *id = xml_get_id( context, service, xml_tractor );
477 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
480 xmlNewProp( child, _x("id"), _x(id) );
481 if ( mlt_properties_get( properties, "title" ) )
482 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
483 if ( mlt_properties_get( properties, "global_feed" ) )
484 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
485 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
486 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
488 // Store application specific properties
489 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
490 if ( !context->no_meta )
491 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
493 // Recurse on connected producer
494 serialise_service( context, mlt_service_producer( service ), child );
495 serialise_service_filters( context, service, child );
499 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
501 xmlNode *child = node;
502 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
504 // Recurse on connected producer
505 serialise_service( context, mlt_service_producer( service ), node );
507 if ( context->pass == 1 )
509 // Get a new id - if already allocated, do nothing
510 char *id = xml_get_id( context, service, xml_filter );
514 child = xmlNewChild( node, NULL, _x("filter"), NULL );
517 xmlNewProp( child, _x("id"), _x(id) );
518 if ( mlt_properties_get( properties, "title" ) )
519 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
520 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
521 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
523 serialise_properties( context, properties, child );
524 serialise_service_filters( context, service, child );
528 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
530 xmlNode *child = node;
531 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
533 // Recurse on connected producer
534 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
536 if ( context->pass == 1 )
538 // Get a new id - if already allocated, do nothing
539 char *id = xml_get_id( context, service, xml_transition );
543 child = xmlNewChild( node, NULL, _x("transition"), NULL );
546 xmlNewProp( child, _x("id"), _x(id) );
547 if ( mlt_properties_get( properties, "title" ) )
548 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
549 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
550 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
552 serialise_properties( context, properties, child );
553 serialise_service_filters( context, service, child );
557 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
559 // Iterate over consumer/producer connections
560 while ( service != NULL )
562 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
563 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
565 // Tell about the producer
566 if ( strcmp( mlt_type, "producer" ) == 0 )
568 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
569 if ( mlt_properties_get( properties, "xml" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
572 serialise_tractor( context, service, node );
574 serialise_tractor( context, service, node );
580 serialise_producer( context, service, node );
582 if ( mlt_properties_get( properties, "xml" ) != NULL )
586 // Tell about the framework container producers
587 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
589 char *resource = mlt_properties_get( properties, "resource" );
591 // Recurse on multitrack's tracks
592 if ( resource && strcmp( resource, "<multitrack>" ) == 0 )
594 serialise_multitrack( context, service, node );
598 // Recurse on playlist's clips
599 else if ( resource && strcmp( resource, "<playlist>" ) == 0 )
601 serialise_playlist( context, service, node );
604 // Recurse on tractor's producer
605 else if ( resource && strcmp( resource, "<tractor>" ) == 0 )
608 serialise_tractor( context, service, node );
610 serialise_tractor( context, service, node );
615 // Treat it as a normal producer
618 serialise_producer( context, service, node );
622 // Tell about a filter
623 else if ( strcmp( mlt_type, "filter" ) == 0 )
625 serialise_filter( context, service, node );
629 // Tell about a transition
630 else if ( strcmp( mlt_type, "transition" ) == 0 )
632 serialise_transition( context, service, node );
636 // Get the next connected service
637 service = mlt_service_producer( service );
641 xmlDocPtr xml_make_doc( mlt_consumer consumer, mlt_service service )
643 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
644 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
645 xmlNodePtr root = xmlNewNode( NULL, _x("mlt") );
646 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
647 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( consumer ) );
650 xmlDocSetRootElement( doc, root );
652 // Indicate the numeric locale
653 xmlNewProp( root, _x("LC_NUMERIC"), _x( setlocale( LC_NUMERIC, NULL ) ) );
655 // If we have root, then deal with it now
656 if ( mlt_properties_get( properties, "root" ) != NULL )
658 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
659 context->root = strdup( mlt_properties_get( properties, "root" ) );
663 context->root = strdup( "" );
666 // Assign the additional 'storage' pattern for properties
667 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
668 context->no_meta = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "no_meta" );
670 // Assign a title property
671 if ( mlt_properties_get( properties, "title" ) != NULL )
672 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
673 mlt_properties_set_int( properties, "global_feed", 1 );
675 // Add a profile child element
678 xmlNodePtr profile_node = xmlNewChild( root, NULL, _x("profile"), NULL );
679 if ( profile->description )
680 xmlNewProp( profile_node, _x("description"), _x(profile->description) );
681 sprintf( tmpstr, "%d", profile->width );
682 xmlNewProp( profile_node, _x("width"), _x(tmpstr) );
683 sprintf( tmpstr, "%d", profile->height );
684 xmlNewProp( profile_node, _x("height"), _x(tmpstr) );
685 sprintf( tmpstr, "%d", profile->progressive );
686 xmlNewProp( profile_node, _x("progressive"), _x(tmpstr) );
687 sprintf( tmpstr, "%d", profile->sample_aspect_num );
688 xmlNewProp( profile_node, _x("sample_aspect_num"), _x(tmpstr) );
689 sprintf( tmpstr, "%d", profile->sample_aspect_den );
690 xmlNewProp( profile_node, _x("sample_aspect_den"), _x(tmpstr) );
691 sprintf( tmpstr, "%d", profile->display_aspect_num );
692 xmlNewProp( profile_node, _x("display_aspect_num"), _x(tmpstr) );
693 sprintf( tmpstr, "%d", profile->display_aspect_den );
694 xmlNewProp( profile_node, _x("display_aspect_den"), _x(tmpstr) );
695 sprintf( tmpstr, "%d", profile->frame_rate_num );
696 xmlNewProp( profile_node, _x("frame_rate_num"), _x(tmpstr) );
697 sprintf( tmpstr, "%d", profile->frame_rate_den );
698 xmlNewProp( profile_node, _x("frame_rate_den"), _x(tmpstr) );
699 sprintf( tmpstr, "%d", profile->colorspace );
700 xmlNewProp( profile_node, _x("colorspace"), _x(tmpstr) );
703 // Construct the context maps
704 context->id_map = mlt_properties_new();
705 context->hide_map = mlt_properties_new();
707 // Ensure producer is a framework producer
708 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
710 // In pass one, we serialise the end producers and playlists,
711 // adding them to a map keyed by address.
712 serialise_service( context, service, root );
714 // In pass two, we serialise the tractor and reference the
715 // producers and playlists
717 serialise_service( context, service, root );
720 mlt_properties_close( context->id_map );
721 mlt_properties_close( context->hide_map );
722 free( context->root );
729 static void output_xml( mlt_consumer this )
731 // Get the producer service
732 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
733 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
734 char *resource = mlt_properties_get( properties, "resource" );
735 xmlDocPtr doc = NULL;
737 if ( !service ) return;
739 // Set the title if provided
740 if ( mlt_properties_get( properties, "title" ) )
741 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
742 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
743 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
745 // Check for a root on the consumer properties and pass to service
746 if ( mlt_properties_get( properties, "root" ) )
747 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
749 // Specify roots in other cases...
750 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
752 // Get the current working directory
753 char *cwd = getcwd( NULL, 0 );
754 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
759 doc = xml_make_doc( this, service );
762 if ( resource == NULL || !strcmp( resource, "" ) )
764 xmlDocFormatDump( stdout, doc, 1 );
766 else if ( strchr( resource, '.' ) == NULL )
768 xmlChar *buffer = NULL;
770 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
771 mlt_properties_set( properties, resource, _s(buffer) );
773 xmlFreeFunc xmlFree = NULL;
774 xmlMemGet( &xmlFree, NULL, NULL, NULL);
780 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
783 // Close the document
786 static int consumer_start( mlt_consumer this )
788 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
790 if ( mlt_properties_get_int( properties, "all" ) )
792 // Check that we're not already running
793 if ( !mlt_properties_get_int( properties, "running" ) )
796 pthread_t *thread = calloc( 1, sizeof( pthread_t ) );
798 // Assign the thread to properties
799 mlt_properties_set_data( properties, "thread", thread, sizeof( pthread_t ), free, NULL );
801 // Set the running state
802 mlt_properties_set_int( properties, "running", 1 );
803 mlt_properties_set_int( properties, "joined", 0 );
806 pthread_create( thread, NULL, consumer_thread, this );
812 mlt_consumer_stop( this );
813 mlt_consumer_stopped( this );
818 static int consumer_is_stopped( mlt_consumer this )
820 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
821 return !mlt_properties_get_int( properties, "running" );
824 static int consumer_stop( mlt_consumer this )
826 // Get the properties
827 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
829 // Check that we're running
830 if ( !mlt_properties_get_int( properties, "joined" ) )
833 pthread_t *thread = mlt_properties_get_data( properties, "thread", NULL );
836 mlt_properties_set_int( properties, "running", 0 );
837 mlt_properties_set_int( properties, "joined", 1 );
839 // Wait for termination
841 pthread_join( *thread, NULL );
847 static void *consumer_thread( void *arg )
849 // Map the argument to the object
850 mlt_consumer this = arg;
852 // Get the properties
853 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
855 // Convenience functionality
856 int terminate_on_pause = mlt_properties_get_int( properties, "terminate_on_pause" );
860 mlt_frame frame = NULL;
862 // Loop while running
863 while( !terminated && mlt_properties_get_int( properties, "running" ) )
866 frame = mlt_consumer_rt_frame( this );
868 // Check for termination
869 if ( terminate_on_pause && frame != NULL )
870 terminated = mlt_properties_get_double( MLT_FRAME_PROPERTIES( frame ), "_speed" ) == 0.0;
872 // Check that we have a frame to work with
875 int width = 0, height = 0;
876 int frequency = mlt_properties_get_int( properties, "frequency" );
877 int channels = mlt_properties_get_int( properties, "channels" );
879 mlt_image_format iformat = mlt_image_yuv422;
880 mlt_audio_format aformat = mlt_audio_s16;
883 mlt_frame_get_image( frame, &buffer, &iformat, &width, &height, 0 );
884 mlt_frame_get_audio( frame, (void**) &buffer, &aformat, &frequency, &channels, &samples );
887 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
888 mlt_frame_close( frame );
893 // Indicate that the consumer is stopped
894 mlt_properties_set_int( properties, "running", 0 );
895 mlt_consumer_stopped( this );