2 * consumer_westley.c -- a libxml2 serialiser of mlt service networks
3 * Copyright (C) 2003-2004 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>
31 #define _x (const xmlChar*)
32 #define _s (const char*)
34 // This maintains counters for adding ids to elements
35 struct serialise_context_s
37 mlt_properties id_map;
45 mlt_properties hide_map;
49 typedef struct serialise_context_s* serialise_context;
51 /** Forward references to static functions.
54 static int consumer_start( mlt_consumer parent );
55 static int consumer_is_stopped( mlt_consumer this );
56 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
70 /** Create or retrieve an id associated to this service.
73 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
77 mlt_properties map = context->id_map;
79 // Search the map for the service
80 for ( i = 0; i < mlt_properties_count( map ); i ++ )
81 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
84 // If the service is not in the map, and the type indicates a new id is needed...
85 if ( i >= mlt_properties_count( map ) && type != westley_existing )
87 // Attempt to reuse existing id
88 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
90 // If no id, or the id is used in the map (for another service), then
92 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
99 case westley_producer:
100 sprintf( temp, "producer%d", context->producer_count ++ );
102 case westley_multitrack:
103 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
105 case westley_playlist:
106 sprintf( temp, "playlist%d", context->playlist_count ++ );
108 case westley_tractor:
109 sprintf( temp, "tractor%d", context->tractor_count ++ );
112 sprintf( temp, "filter%d", context->filter_count ++ );
114 case westley_transition:
115 sprintf( temp, "transition%d", context->transition_count ++ );
117 case westley_existing:
122 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
124 // Set the data at the generated name
125 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
127 // Get the pointer to the name (i is the end of the list)
128 id = mlt_properties_get_name( map, i );
132 // Store the existing id in the map
133 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
136 else if ( type == westley_existing )
138 id = mlt_properties_get_name( map, i );
144 /** This is what will be called by the factory - anything can be passed in
145 via the argument, but keep it simple.
148 mlt_consumer consumer_westley_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
150 // Create the consumer object
151 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
153 // If no malloc'd and consumer init ok
154 if ( this != NULL && mlt_consumer_init( this, NULL, profile ) == 0 )
156 // Allow thread to be started/stopped
157 this->start = consumer_start;
158 this->is_stopped = consumer_is_stopped;
160 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
162 // Return the consumer produced
166 // malloc or consumer init failed
173 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
178 // Enumerate the properties
179 for ( i = 0; i < mlt_properties_count( properties ); i++ )
181 char *name = mlt_properties_get_name( properties, i );
184 mlt_properties_get_value( properties, i ) != NULL &&
185 strcmp( name, "westley" ) != 0 &&
186 strcmp( name, "in" ) != 0 &&
187 strcmp( name, "out" ) != 0 &&
188 strcmp( name, "id" ) != 0 &&
189 strcmp( name, "title" ) != 0 &&
190 strcmp( name, "root" ) != 0 &&
191 strcmp( name, "width" ) != 0 &&
192 strcmp( name, "height" ) != 0 )
194 char *value = mlt_properties_get_value( properties, i );
195 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
196 value += strlen( context->root ) + 1;
197 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
198 xmlNewProp( p, _x("name"), _x(name) );
203 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, char *store )
208 // Enumerate the properties
209 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
211 char *name = mlt_properties_get_name( properties, i );
212 if ( !strncmp( name, store, strlen( store ) ) )
214 char *value = mlt_properties_get_value( properties, i );
217 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
218 value += strlen( context->root ) + 1;
219 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
220 xmlNewProp( p, _x("name"), _x(name) );
226 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
230 mlt_filter filter = NULL;
232 // Enumerate the filters
233 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
235 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
236 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
238 // Get a new id - if already allocated, do nothing
239 char *id = westley_get_id( context, MLT_FILTER_SERVICE( filter ), westley_filter );
242 int in = mlt_properties_get_position( properties, "in" );
243 int out = mlt_properties_get_position( properties, "out" );
244 p = xmlNewChild( node, NULL, _x("filter"), NULL );
245 xmlNewProp( p, _x("id"), _x(id) );
246 if ( mlt_properties_get( properties, "title" ) )
247 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
248 if ( in != 0 || out != 0 )
251 sprintf( temp, "%d", in );
252 xmlNewProp( p, _x("in"), _x(temp) );
253 sprintf( temp, "%d", out );
254 xmlNewProp( p, _x("out"), _x(temp) );
256 serialise_properties( context, properties, p );
257 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
263 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
265 xmlNode *child = node;
266 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
268 if ( context->pass == 0 )
270 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
271 // Get a new id - if already allocated, do nothing
272 char *id = westley_get_id( context, parent, westley_producer );
276 child = xmlNewChild( node, NULL, _x("producer"), NULL );
279 xmlNewProp( child, _x("id"), _x(id) );
280 if ( mlt_properties_get( properties, "title" ) )
281 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
282 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
283 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
284 serialise_properties( context, properties, child );
285 serialise_service_filters( context, service, child );
287 // Add producer to the map
288 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
292 char *id = westley_get_id( context, parent, westley_existing );
293 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
294 xmlNewProp( node, _x("parent"), _x(id) );
295 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
296 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
300 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
302 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
306 if ( context->pass == 0 )
308 // Iterate over the tracks to collect the producers
309 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
311 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
312 serialise_service( context, MLT_SERVICE( producer ), node );
317 // Get a new id - if already allocated, do nothing
318 char *id = westley_get_id( context, service, westley_multitrack );
322 // Serialise the tracks
323 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
325 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
327 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
328 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
330 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
332 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
333 xmlNewProp( track, _x("producer"), _x(id) );
334 if ( mlt_producer_is_cut( producer ) )
336 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
337 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
338 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
339 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
340 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
343 hide = mlt_properties_get_int( context->hide_map, id );
345 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
347 serialise_service_filters( context, service, node );
351 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
354 xmlNode *child = node;
355 mlt_playlist_clip_info info;
356 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
358 if ( context->pass == 0 )
360 // Get a new id - if already allocated, do nothing
361 char *id = westley_get_id( context, service, westley_playlist );
365 // Iterate over the playlist entries to collect the producers
366 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
368 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
370 if ( info.producer != NULL )
372 mlt_producer producer = mlt_producer_cut_parent( info.producer );
373 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
374 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
375 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
376 serialise_playlist( context, MLT_SERVICE( producer ), node );
377 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
378 serialise_service( context, MLT_SERVICE( producer ), node );
383 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
386 xmlNewProp( child, _x("id"), _x(id) );
387 if ( mlt_properties_get( properties, "title" ) )
388 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
390 // Store application specific properties
391 serialise_store_properties( context, properties, child, context->store );
392 serialise_store_properties( context, properties, child, "meta." );
394 // Add producer to the map
395 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
397 // Iterate over the playlist entries
398 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
400 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
402 mlt_producer producer = mlt_producer_cut_parent( info.producer );
403 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
404 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
408 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
409 snprintf( length, 19, "%d", (int)info.frame_count );
410 xmlNewProp( entry, _x("length"), _x(length) );
415 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
416 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
417 xmlNewProp( entry, _x("producer"), _x(id) );
418 sprintf( temp, "%d", (int)info.frame_in );
419 xmlNewProp( entry, _x("in"), _x(temp) );
420 sprintf( temp, "%d", (int)info.frame_out );
421 xmlNewProp( entry, _x("out"), _x(temp) );
422 if ( info.repeat > 1 )
424 sprintf( temp, "%d", info.repeat );
425 xmlNewProp( entry, _x("repeat"), _x(temp) );
427 if ( mlt_producer_is_cut( info.cut ) )
429 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
430 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
431 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
437 serialise_service_filters( context, service, child );
439 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
441 char *id = westley_get_id( context, service, westley_existing );
442 xmlNewProp( node, _x("producer"), _x(id) );
446 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
448 xmlNode *child = node;
449 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
451 if ( context->pass == 0 )
453 // Recurse on connected producer
454 serialise_service( context, mlt_service_producer( service ), node );
458 // Get a new id - if already allocated, do nothing
459 char *id = westley_get_id( context, service, westley_tractor );
463 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
466 xmlNewProp( child, _x("id"), _x(id) );
467 if ( mlt_properties_get( properties, "title" ) )
468 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
469 if ( mlt_properties_get( properties, "global_feed" ) )
470 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
471 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
472 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
474 // Store application specific properties
475 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
476 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
478 // Recurse on connected producer
479 serialise_service( context, mlt_service_producer( service ), child );
480 serialise_service_filters( context, service, child );
484 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
486 xmlNode *child = node;
487 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
489 // Recurse on connected producer
490 serialise_service( context, mlt_service_producer( service ), node );
492 if ( context->pass == 1 )
494 // Get a new id - if already allocated, do nothing
495 char *id = westley_get_id( context, service, westley_filter );
499 child = xmlNewChild( node, NULL, _x("filter"), NULL );
502 xmlNewProp( child, _x("id"), _x(id) );
503 if ( mlt_properties_get( properties, "title" ) )
504 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
505 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
506 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
508 serialise_properties( context, properties, child );
509 serialise_service_filters( context, service, child );
513 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
515 xmlNode *child = node;
516 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
518 // Recurse on connected producer
519 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
521 if ( context->pass == 1 )
523 // Get a new id - if already allocated, do nothing
524 char *id = westley_get_id( context, service, westley_transition );
528 child = xmlNewChild( node, NULL, _x("transition"), NULL );
531 xmlNewProp( child, _x("id"), _x(id) );
532 if ( mlt_properties_get( properties, "title" ) )
533 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
534 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
535 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
537 serialise_properties( context, properties, child );
538 serialise_service_filters( context, service, child );
542 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
544 // Iterate over consumer/producer connections
545 while ( service != NULL )
547 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
548 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
550 // Tell about the producer
551 if ( strcmp( mlt_type, "producer" ) == 0 )
553 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
554 if ( mlt_properties_get( properties, "westley" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
557 serialise_tractor( context, service, node );
559 serialise_tractor( context, service, node );
565 serialise_producer( context, service, node );
567 if ( mlt_properties_get( properties, "westley" ) != NULL )
571 // Tell about the framework container producers
572 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
574 char *resource = mlt_properties_get( properties, "resource" );
576 // Recurse on multitrack's tracks
577 if ( strcmp( resource, "<multitrack>" ) == 0 )
579 serialise_multitrack( context, service, node );
583 // Recurse on playlist's clips
584 else if ( strcmp( resource, "<playlist>" ) == 0 )
586 serialise_playlist( context, service, node );
589 // Recurse on tractor's producer
590 else if ( strcmp( resource, "<tractor>" ) == 0 )
593 serialise_tractor( context, service, node );
595 serialise_tractor( context, service, node );
600 // Treat it as a normal producer
603 serialise_producer( context, service, node );
607 // Tell about a filter
608 else if ( strcmp( mlt_type, "filter" ) == 0 )
610 serialise_filter( context, service, node );
614 // Tell about a transition
615 else if ( strcmp( mlt_type, "transition" ) == 0 )
617 serialise_transition( context, service, node );
621 // Get the next connected service
622 service = mlt_service_producer( service );
626 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
628 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
629 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
630 xmlNodePtr root = xmlNewNode( NULL, _x("westley") );
631 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
633 xmlDocSetRootElement( doc, root );
635 // If we have root, then deal with it now
636 if ( mlt_properties_get( properties, "root" ) != NULL )
638 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
639 context->root = strdup( mlt_properties_get( properties, "root" ) );
643 context->root = strdup( "" );
646 // Assign the additional 'storage' pattern for properties
647 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
649 // Assign a title property
650 if ( mlt_properties_get( properties, "title" ) != NULL )
651 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
652 mlt_properties_set_int( properties, "global_feed", 1 );
654 // Construct the context maps
655 context->id_map = mlt_properties_new();
656 context->hide_map = mlt_properties_new();
658 // Ensure producer is a framework producer
659 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
661 // In pass one, we serialise the end producers and playlists,
662 // adding them to a map keyed by address.
663 serialise_service( context, service, root );
665 // In pass two, we serialise the tractor and reference the
666 // producers and playlists
668 serialise_service( context, service, root );
671 mlt_properties_close( context->id_map );
672 mlt_properties_close( context->hide_map );
673 free( context->root );
679 static int consumer_start( mlt_consumer this )
681 xmlDocPtr doc = NULL;
683 // Get the producer service
684 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
685 if ( service != NULL )
687 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
688 char *resource = mlt_properties_get( properties, "resource" );
690 // Set the title if provided
691 if ( mlt_properties_get( properties, "title" ) )
692 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
693 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
694 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
696 // Check for a root on the consumer properties and pass to service
697 if ( mlt_properties_get( properties, "root" ) )
698 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
700 // Specify roots in other cases...
701 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
703 // Get the current working directory
704 char *cwd = getcwd( NULL, 0 );
705 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
710 doc = westley_make_doc( this, service );
713 if ( resource == NULL || !strcmp( resource, "" ) )
715 xmlDocFormatDump( stdout, doc, 1 );
717 else if ( strchr( resource, '.' ) == NULL )
719 xmlChar *buffer = NULL;
721 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
722 mlt_properties_set( properties, resource, _s(buffer) );
727 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
730 // Close the document
734 mlt_consumer_stop( this );
736 mlt_consumer_stopped( this );
741 static int consumer_is_stopped( mlt_consumer this )