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 "consumer_westley.h"
22 #include <framework/mlt.h>
28 #include <libxml/tree.h>
35 // This maintains counters for adding ids to elements
36 struct serialise_context_s
38 mlt_properties id_map;
46 mlt_properties hide_map;
50 typedef struct serialise_context_s* serialise_context;
52 /** Forward references to static functions.
55 static int consumer_start( mlt_consumer parent );
56 static int consumer_is_stopped( mlt_consumer this );
57 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
71 /** Create or retrieve an id associated to this service.
74 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
78 mlt_properties map = context->id_map;
80 // Search the map for the service
81 for ( i = 0; i < mlt_properties_count( map ); i ++ )
82 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
85 // If the service is not in the map, and the type indicates a new id is needed...
86 if ( i >= mlt_properties_count( map ) && type != westley_existing )
88 // Attempt to reuse existing id
89 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
91 // If no id, or the id is used in the map (for another service), then
93 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
100 case westley_producer:
101 sprintf( temp, "producer%d", context->producer_count ++ );
103 case westley_multitrack:
104 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
106 case westley_playlist:
107 sprintf( temp, "playlist%d", context->playlist_count ++ );
109 case westley_tractor:
110 sprintf( temp, "tractor%d", context->tractor_count ++ );
113 sprintf( temp, "filter%d", context->filter_count ++ );
115 case westley_transition:
116 sprintf( temp, "transition%d", context->transition_count ++ );
118 case westley_existing:
123 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
125 // Set the data at the generated name
126 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
128 // Get the pointer to the name (i is the end of the list)
129 id = mlt_properties_get_name( map, i );
133 // Store the existing id in the map
134 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
137 else if ( type == westley_existing )
139 id = mlt_properties_get_name( map, i );
145 /** This is what will be called by the factory - anything can be passed in
146 via the argument, but keep it simple.
149 mlt_consumer consumer_westley_init( char *arg )
151 // Create the consumer object
152 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
154 // If no malloc'd and consumer init ok
155 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
157 // Allow thread to be started/stopped
158 this->start = consumer_start;
159 this->is_stopped = consumer_is_stopped;
161 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
163 // Return the consumer produced
167 // malloc or consumer init failed
174 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
179 // Enumerate the properties
180 for ( i = 0; i < mlt_properties_count( properties ); i++ )
182 char *name = mlt_properties_get_name( properties, i );
185 mlt_properties_get_value( properties, i ) != NULL &&
186 strcmp( name, "westley" ) != 0 &&
187 strcmp( name, "in" ) != 0 &&
188 strcmp( name, "out" ) != 0 &&
189 strcmp( name, "id" ) != 0 &&
190 strcmp( name, "title" ) != 0 &&
191 strcmp( name, "root" ) != 0 &&
192 strcmp( name, "width" ) != 0 &&
193 strcmp( name, "height" ) != 0 )
195 char *value = mlt_properties_get_value( properties, i );
196 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
197 value += strlen( context->root ) + 1;
198 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
199 xmlNewProp( p, _x("name"), _x(name) );
204 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, char *store )
209 // Enumerate the properties
210 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
212 char *name = mlt_properties_get_name( properties, i );
213 if ( !strncmp( name, store, strlen( store ) ) )
215 char *value = mlt_properties_get_value( properties, i );
218 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
219 value += strlen( context->root ) + 1;
220 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
221 xmlNewProp( p, _x("name"), _x(name) );
227 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
231 mlt_filter filter = NULL;
233 // Enumerate the filters
234 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
236 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
237 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
239 // Get a new id - if already allocated, do nothing
240 char *id = westley_get_id( context, MLT_FILTER_SERVICE( filter ), westley_filter );
243 int in = mlt_properties_get_position( properties, "in" );
244 int out = mlt_properties_get_position( properties, "out" );
245 p = xmlNewChild( node, NULL, _x("filter"), NULL );
246 xmlNewProp( p, _x("id"), _x(id) );
247 if ( mlt_properties_get( properties, "title" ) )
248 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
249 if ( in != 0 || out != 0 )
252 sprintf( temp, "%d", in );
253 xmlNewProp( p, _x("in"), _x(temp) );
254 sprintf( temp, "%d", out );
255 xmlNewProp( p, _x("out"), _x(temp) );
257 serialise_properties( context, properties, p );
258 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
264 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
266 xmlNode *child = node;
267 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
269 if ( context->pass == 0 )
271 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
272 // Get a new id - if already allocated, do nothing
273 char *id = westley_get_id( context, parent, westley_producer );
277 child = xmlNewChild( node, NULL, _x("producer"), NULL );
280 xmlNewProp( child, _x("id"), _x(id) );
281 if ( mlt_properties_get( properties, "title" ) )
282 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
283 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
284 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
285 serialise_properties( context, properties, child );
286 serialise_service_filters( context, service, child );
288 // Add producer to the map
289 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
293 char *id = westley_get_id( context, parent, westley_existing );
294 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
295 xmlNewProp( node, _x("parent"), _x(id) );
296 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
297 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
301 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
303 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
307 if ( context->pass == 0 )
309 // Iterate over the tracks to collect the producers
310 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
312 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
313 serialise_service( context, MLT_SERVICE( producer ), node );
318 // Get a new id - if already allocated, do nothing
319 char *id = westley_get_id( context, service, westley_multitrack );
323 // Serialise the tracks
324 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
326 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
328 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
329 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
331 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
333 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
334 xmlNewProp( track, _x("producer"), _x(id) );
335 if ( mlt_producer_is_cut( producer ) )
337 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
338 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
339 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
340 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
341 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
344 hide = mlt_properties_get_int( context->hide_map, id );
346 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
348 serialise_service_filters( context, service, node );
352 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
355 xmlNode *child = node;
356 mlt_playlist_clip_info info;
357 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
359 if ( context->pass == 0 )
361 // Get a new id - if already allocated, do nothing
362 char *id = westley_get_id( context, service, westley_playlist );
366 // Iterate over the playlist entries to collect the producers
367 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
369 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
371 if ( info.producer != NULL )
373 mlt_producer producer = mlt_producer_cut_parent( info.producer );
374 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
375 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
376 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
377 serialise_playlist( context, MLT_SERVICE( producer ), node );
378 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
379 serialise_service( context, MLT_SERVICE( producer ), node );
384 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
387 xmlNewProp( child, _x("id"), _x(id) );
388 if ( mlt_properties_get( properties, "title" ) )
389 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
391 // Store application specific properties
392 serialise_store_properties( context, properties, child, context->store );
393 serialise_store_properties( context, properties, child, "meta." );
395 // Add producer to the map
396 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
398 // Iterate over the playlist entries
399 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
401 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
403 mlt_producer producer = mlt_producer_cut_parent( info.producer );
404 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
405 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
409 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
410 snprintf( length, 19, "%d", (int)info.frame_count );
411 xmlNewProp( entry, _x("length"), _x(length) );
416 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
417 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
418 xmlNewProp( entry, _x("producer"), _x(id) );
419 sprintf( temp, "%d", (int)info.frame_in );
420 xmlNewProp( entry, _x("in"), _x(temp) );
421 sprintf( temp, "%d", (int)info.frame_out );
422 xmlNewProp( entry, _x("out"), _x(temp) );
423 if ( info.repeat > 1 )
425 sprintf( temp, "%d", info.repeat );
426 xmlNewProp( entry, _x("repeat"), _x(temp) );
428 if ( mlt_producer_is_cut( info.cut ) )
430 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
431 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
432 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
438 serialise_service_filters( context, service, child );
440 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
442 char *id = westley_get_id( context, service, westley_existing );
443 xmlNewProp( node, _x("producer"), _x(id) );
447 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
449 xmlNode *child = node;
450 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
452 if ( context->pass == 0 )
454 // Recurse on connected producer
455 serialise_service( context, mlt_service_producer( service ), node );
459 // Get a new id - if already allocated, do nothing
460 char *id = westley_get_id( context, service, westley_tractor );
464 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
467 xmlNewProp( child, _x("id"), _x(id) );
468 if ( mlt_properties_get( properties, "title" ) )
469 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
470 if ( mlt_properties_get( properties, "global_feed" ) )
471 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
472 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
473 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
475 // Store application specific properties
476 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
477 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
479 // Recurse on connected producer
480 serialise_service( context, mlt_service_producer( service ), child );
481 serialise_service_filters( context, service, child );
485 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
487 xmlNode *child = node;
488 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
490 // Recurse on connected producer
491 serialise_service( context, mlt_service_producer( service ), node );
493 if ( context->pass == 1 )
495 // Get a new id - if already allocated, do nothing
496 char *id = westley_get_id( context, service, westley_filter );
500 child = xmlNewChild( node, NULL, _x("filter"), NULL );
503 xmlNewProp( child, _x("id"), _x(id) );
504 if ( mlt_properties_get( properties, "title" ) )
505 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
506 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
507 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
509 serialise_properties( context, properties, child );
510 serialise_service_filters( context, service, child );
514 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
516 xmlNode *child = node;
517 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
519 // Recurse on connected producer
520 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
522 if ( context->pass == 1 )
524 // Get a new id - if already allocated, do nothing
525 char *id = westley_get_id( context, service, westley_transition );
529 child = xmlNewChild( node, NULL, _x("transition"), NULL );
532 xmlNewProp( child, _x("id"), _x(id) );
533 if ( mlt_properties_get( properties, "title" ) )
534 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
535 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
536 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
538 serialise_properties( context, properties, child );
539 serialise_service_filters( context, service, child );
543 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
545 // Iterate over consumer/producer connections
546 while ( service != NULL )
548 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
549 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
551 // Tell about the producer
552 if ( strcmp( mlt_type, "producer" ) == 0 )
554 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
555 if ( mlt_properties_get( properties, "westley" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
558 serialise_tractor( context, service, node );
560 serialise_tractor( context, service, node );
566 serialise_producer( context, service, node );
568 if ( mlt_properties_get( properties, "westley" ) != NULL )
572 // Tell about the framework container producers
573 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
575 char *resource = mlt_properties_get( properties, "resource" );
577 // Recurse on multitrack's tracks
578 if ( strcmp( resource, "<multitrack>" ) == 0 )
580 serialise_multitrack( context, service, node );
584 // Recurse on playlist's clips
585 else if ( strcmp( resource, "<playlist>" ) == 0 )
587 serialise_playlist( context, service, node );
590 // Recurse on tractor's producer
591 else if ( strcmp( resource, "<tractor>" ) == 0 )
594 serialise_tractor( context, service, node );
596 serialise_tractor( context, service, node );
601 // Treat it as a normal producer
604 serialise_producer( context, service, node );
608 // Tell about a filter
609 else if ( strcmp( mlt_type, "filter" ) == 0 )
611 serialise_filter( context, service, node );
615 // Tell about a transition
616 else if ( strcmp( mlt_type, "transition" ) == 0 )
618 serialise_transition( context, service, node );
622 // Get the next connected service
623 service = mlt_service_producer( service );
627 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
629 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
630 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
631 xmlNodePtr root = xmlNewNode( NULL, _x("westley") );
632 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
634 xmlDocSetRootElement( doc, root );
636 // If we have root, then deal with it now
637 if ( mlt_properties_get( properties, "root" ) != NULL )
639 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
640 context->root = strdup( mlt_properties_get( properties, "root" ) );
644 context->root = strdup( "" );
647 // Assign the additional 'storage' pattern for properties
648 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
650 // Assign a title property
651 if ( mlt_properties_get( properties, "title" ) != NULL )
652 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
653 mlt_properties_set_int( properties, "global_feed", 1 );
655 // Construct the context maps
656 context->id_map = mlt_properties_new();
657 context->hide_map = mlt_properties_new();
659 // Ensure producer is a framework producer
660 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
662 // In pass one, we serialise the end producers and playlists,
663 // adding them to a map keyed by address.
664 serialise_service( context, service, root );
666 // In pass two, we serialise the tractor and reference the
667 // producers and playlists
669 serialise_service( context, service, root );
672 mlt_properties_close( context->id_map );
673 mlt_properties_close( context->hide_map );
674 free( context->root );
680 static int consumer_start( mlt_consumer this )
682 xmlDocPtr doc = NULL;
684 // Get the producer service
685 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
686 if ( service != NULL )
688 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
689 char *resource = mlt_properties_get( properties, "resource" );
691 // Set the title if provided
692 if ( mlt_properties_get( properties, "title" ) )
693 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
694 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
695 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
697 // Check for a root on the consumer properties and pass to service
698 if ( mlt_properties_get( properties, "root" ) )
699 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
701 // Specify roots in other cases...
702 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
704 // Get the current working directory
705 char *cwd = getcwd( NULL, 0 );
706 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
711 doc = westley_make_doc( this, service );
714 if ( resource == NULL || !strcmp( resource, "" ) )
716 xmlDocFormatDump( stdout, doc, 1 );
718 else if ( strchr( resource, '.' ) == NULL )
720 xmlChar *buffer = NULL;
722 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
723 mlt_properties_set( properties, resource, _s(buffer) );
728 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
731 // Close the document
735 mlt_consumer_stop( this );
737 mlt_consumer_stopped( this );
742 static int consumer_is_stopped( mlt_consumer this )