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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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>
32 // This maintains counters for adding ids to elements
33 struct serialise_context_s
35 mlt_properties id_map;
43 mlt_properties hide_map;
47 typedef struct serialise_context_s* serialise_context;
49 /** Forward references to static functions.
52 static int consumer_start( mlt_consumer parent );
53 static int consumer_is_stopped( mlt_consumer this );
54 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
68 /** Create or retrieve an id associated to this service.
71 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
75 mlt_properties map = context->id_map;
77 // Search the map for the service
78 for ( i = 0; i < mlt_properties_count( map ); i ++ )
79 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
82 // If the service is not in the map, and the type indicates a new id is needed...
83 if ( i >= mlt_properties_count( map ) && type != westley_existing )
85 // Attempt to reuse existing id
86 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
88 // If no id, or the id is used in the map (for another service), then
90 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
97 case westley_producer:
98 sprintf( temp, "producer%d", context->producer_count ++ );
100 case westley_multitrack:
101 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
103 case westley_playlist:
104 sprintf( temp, "playlist%d", context->playlist_count ++ );
106 case westley_tractor:
107 sprintf( temp, "tractor%d", context->tractor_count ++ );
110 sprintf( temp, "filter%d", context->filter_count ++ );
112 case westley_transition:
113 sprintf( temp, "transition%d", context->transition_count ++ );
115 case westley_existing:
120 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
122 // Set the data at the generated name
123 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
125 // Get the pointer to the name (i is the end of the list)
126 id = mlt_properties_get_name( map, i );
130 // Store the existing id in the map
131 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
134 else if ( type == westley_existing )
136 id = mlt_properties_get_name( map, i );
142 /** This is what will be called by the factory - anything can be passed in
143 via the argument, but keep it simple.
146 mlt_consumer consumer_westley_init( char *arg )
148 // Create the consumer object
149 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
151 // If no malloc'd and consumer init ok
152 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
154 // Allow thread to be started/stopped
155 this->start = consumer_start;
156 this->is_stopped = consumer_is_stopped;
158 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
160 // Return the consumer produced
164 // malloc or consumer init failed
171 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
176 // Enumerate the properties
177 for ( i = 0; i < mlt_properties_count( properties ); i++ )
179 char *name = mlt_properties_get_name( properties, i );
182 mlt_properties_get_value( properties, i ) != NULL &&
183 strcmp( name, "westley" ) != 0 &&
184 strcmp( name, "in" ) != 0 &&
185 strcmp( name, "out" ) != 0 &&
186 strcmp( name, "id" ) != 0 &&
187 strcmp( name, "title" ) != 0 &&
188 strcmp( name, "root" ) != 0 &&
189 strcmp( name, "width" ) != 0 &&
190 strcmp( name, "height" ) != 0 )
192 char *value = mlt_properties_get_value( properties, i );
193 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
194 value += strlen( context->root ) + 1;
195 p = xmlNewTextChild( node, NULL, "property", value );
196 xmlNewProp( p, "name", name );
201 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, char *store )
206 // Enumerate the properties
207 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
209 char *name = mlt_properties_get_name( properties, i );
210 if ( !strncmp( name, store, strlen( store ) ) )
212 char *value = mlt_properties_get_value( properties, i );
215 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
216 value += strlen( context->root ) + 1;
217 p = xmlNewTextChild( node, NULL, "property", value );
218 xmlNewProp( p, "name", name );
224 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
228 mlt_filter filter = NULL;
230 // Enumerate the filters
231 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
233 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
234 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
236 // Get a new id - if already allocated, do nothing
237 char *id = westley_get_id( context, MLT_FILTER_SERVICE( filter ), westley_filter );
240 int in = mlt_properties_get_position( properties, "in" );
241 int out = mlt_properties_get_position( properties, "out" );
242 p = xmlNewChild( node, NULL, "filter", NULL );
243 xmlNewProp( p, "id", id );
244 if ( mlt_properties_get( properties, "title" ) )
245 xmlNewProp( p, "title", mlt_properties_get( properties, "title" ) );
246 if ( in != 0 || out != 0 )
249 sprintf( temp, "%d", in );
250 xmlNewProp( p, "in", temp );
251 sprintf( temp, "%d", out );
252 xmlNewProp( p, "out", temp );
254 serialise_properties( context, properties, p );
255 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
261 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
263 xmlNode *child = node;
264 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
266 if ( context->pass == 0 )
268 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
269 // Get a new id - if already allocated, do nothing
270 char *id = westley_get_id( context, parent, westley_producer );
274 child = xmlNewChild( node, NULL, "producer", NULL );
277 xmlNewProp( child, "id", id );
278 if ( mlt_properties_get( properties, "title" ) )
279 xmlNewProp( child, "title", mlt_properties_get( properties, "title" ) );
280 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
281 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
282 serialise_properties( context, properties, child );
283 serialise_service_filters( context, service, child );
285 // Add producer to the map
286 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
290 char *id = westley_get_id( context, parent, westley_existing );
291 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
292 xmlNewProp( node, "parent", id );
293 xmlNewProp( node, "in", mlt_properties_get( properties, "in" ) );
294 xmlNewProp( node, "out", mlt_properties_get( properties, "out" ) );
298 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
300 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
304 if ( context->pass == 0 )
306 // Iterate over the tracks to collect the producers
307 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
309 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
310 serialise_service( context, MLT_SERVICE( producer ), node );
315 // Get a new id - if already allocated, do nothing
316 char *id = westley_get_id( context, service, westley_multitrack );
320 // Serialise the tracks
321 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
323 xmlNode *track = xmlNewChild( node, NULL, "track", NULL );
325 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
326 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
328 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
330 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
331 xmlNewProp( track, "producer", id );
332 if ( mlt_producer_is_cut( producer ) )
334 xmlNewProp( track, "in", mlt_properties_get( properties, "in" ) );
335 xmlNewProp( track, "out", mlt_properties_get( properties, "out" ) );
336 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
337 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
338 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
341 hide = mlt_properties_get_int( context->hide_map, id );
343 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
345 serialise_service_filters( context, service, node );
349 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
352 xmlNode *child = node;
353 mlt_playlist_clip_info info;
354 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
356 if ( context->pass == 0 )
358 // Get a new id - if already allocated, do nothing
359 char *id = westley_get_id( context, service, westley_playlist );
363 // Iterate over the playlist entries to collect the producers
364 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
366 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
368 if ( info.producer != NULL )
370 mlt_producer producer = mlt_producer_cut_parent( info.producer );
371 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
372 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
373 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
374 serialise_playlist( context, MLT_SERVICE( producer ), node );
375 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
376 serialise_service( context, MLT_SERVICE( producer ), node );
381 child = xmlNewChild( node, NULL, "playlist", NULL );
384 xmlNewProp( child, "id", id );
385 if ( mlt_properties_get( properties, "title" ) )
386 xmlNewProp( child, "title", mlt_properties_get( properties, "title" ) );
388 // Store application specific properties
389 serialise_store_properties( context, properties, child, context->store );
390 serialise_store_properties( context, properties, child, "meta." );
392 // Add producer to the map
393 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
395 // Iterate over the playlist entries
396 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
398 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
400 mlt_producer producer = mlt_producer_cut_parent( info.producer );
401 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
402 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
406 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
407 snprintf( length, 19, "%d", info.frame_count );
408 xmlNewProp( entry, "length", length );
413 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
414 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
415 xmlNewProp( entry, "producer", id );
416 sprintf( temp, "%d", info.frame_in );
417 xmlNewProp( entry, "in", temp );
418 sprintf( temp, "%d", info.frame_out );
419 xmlNewProp( entry, "out", temp );
420 if ( info.repeat > 1 )
422 sprintf( temp, "%d", info.repeat );
423 xmlNewProp( entry, "repeat", temp );
425 if ( mlt_producer_is_cut( info.cut ) )
427 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
428 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
429 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
435 serialise_service_filters( context, service, child );
437 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
439 char *id = westley_get_id( context, service, westley_existing );
440 xmlNewProp( node, "producer", id );
444 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
446 xmlNode *child = node;
447 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
449 if ( context->pass == 0 )
451 // Recurse on connected producer
452 serialise_service( context, mlt_service_producer( service ), node );
456 // Get a new id - if already allocated, do nothing
457 char *id = westley_get_id( context, service, westley_tractor );
461 child = xmlNewChild( node, NULL, "tractor", NULL );
464 xmlNewProp( child, "id", id );
465 if ( mlt_properties_get( properties, "title" ) )
466 xmlNewProp( child, "title", mlt_properties_get( properties, "title" ) );
467 if ( mlt_properties_get( properties, "global_feed" ) )
468 xmlNewProp( child, "global_feed", mlt_properties_get( properties, "global_feed" ) );
469 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
470 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
472 // Store application specific properties
473 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
474 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
476 // Recurse on connected producer
477 serialise_service( context, mlt_service_producer( service ), child );
478 serialise_service_filters( context, service, child );
482 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
484 xmlNode *child = node;
485 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
487 // Recurse on connected producer
488 serialise_service( context, mlt_service_producer( service ), node );
490 if ( context->pass == 1 )
492 // Get a new id - if already allocated, do nothing
493 char *id = westley_get_id( context, service, westley_filter );
497 child = xmlNewChild( node, NULL, "filter", NULL );
500 xmlNewProp( child, "id", id );
501 if ( mlt_properties_get( properties, "title" ) )
502 xmlNewProp( child, "title", mlt_properties_get( properties, "title" ) );
503 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
504 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
506 serialise_properties( context, properties, child );
507 serialise_service_filters( context, service, child );
511 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
513 xmlNode *child = node;
514 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
516 // Recurse on connected producer
517 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
519 if ( context->pass == 1 )
521 // Get a new id - if already allocated, do nothing
522 char *id = westley_get_id( context, service, westley_transition );
526 child = xmlNewChild( node, NULL, "transition", NULL );
529 xmlNewProp( child, "id", id );
530 if ( mlt_properties_get( properties, "title" ) )
531 xmlNewProp( child, "title", mlt_properties_get( properties, "title" ) );
532 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
533 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
535 serialise_properties( context, properties, child );
536 serialise_service_filters( context, service, child );
540 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
542 // Iterate over consumer/producer connections
543 while ( service != NULL )
545 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
546 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
548 // Tell about the producer
549 if ( strcmp( mlt_type, "producer" ) == 0 )
551 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
552 if ( mlt_properties_get( properties, "westley" ) == NULL && !strcmp( mlt_service, "tractor" ) )
555 serialise_tractor( context, service, node );
557 serialise_tractor( context, service, node );
563 serialise_producer( context, service, node );
565 if ( mlt_properties_get( properties, "westley" ) != NULL )
569 // Tell about the framework container producers
570 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
572 char *resource = mlt_properties_get( properties, "resource" );
574 // Recurse on multitrack's tracks
575 if ( strcmp( resource, "<multitrack>" ) == 0 )
577 serialise_multitrack( context, service, node );
581 // Recurse on playlist's clips
582 else if ( strcmp( resource, "<playlist>" ) == 0 )
584 serialise_playlist( context, service, node );
587 // Recurse on tractor's producer
588 else if ( strcmp( resource, "<tractor>" ) == 0 )
591 serialise_tractor( context, service, node );
593 serialise_tractor( context, service, node );
598 // Treat it as a normal producer
601 serialise_producer( context, service, node );
605 // Tell about a filter
606 else if ( strcmp( mlt_type, "filter" ) == 0 )
608 serialise_filter( context, service, node );
612 // Tell about a transition
613 else if ( strcmp( mlt_type, "transition" ) == 0 )
615 serialise_transition( context, service, node );
619 // Get the next connected service
620 service = mlt_service_producer( service );
624 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
626 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
627 xmlDocPtr doc = xmlNewDoc( "1.0" );
628 xmlNodePtr root = xmlNewNode( NULL, "westley" );
629 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
631 xmlDocSetRootElement( doc, root );
633 // If we have root, then deal with it now
634 if ( mlt_properties_get( properties, "root" ) != NULL )
636 xmlNewProp( root, "root", mlt_properties_get( properties, "root" ) );
637 context->root = strdup( mlt_properties_get( properties, "root" ) );
641 context->root = strdup( "" );
644 // Assign the additional 'storage' pattern for properties
645 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
647 // Assign a title property
648 if ( mlt_properties_get( properties, "title" ) != NULL )
649 xmlNewProp( root, "title", mlt_properties_get( properties, "title" ) );
650 mlt_properties_set_int( properties, "global_feed", 1 );
652 // Construct the context maps
653 context->id_map = mlt_properties_new();
654 context->hide_map = mlt_properties_new();
656 // Ensure producer is a framework producer
657 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
659 // In pass one, we serialise the end producers and playlists,
660 // adding them to a map keyed by address.
661 serialise_service( context, service, root );
663 // In pass two, we serialise the tractor and reference the
664 // producers and playlists
666 serialise_service( context, service, root );
669 mlt_properties_close( context->id_map );
670 mlt_properties_close( context->hide_map );
671 free( context->root );
677 static int consumer_start( mlt_consumer this )
679 xmlDocPtr doc = NULL;
681 // Get the producer service
682 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
683 if ( service != NULL )
685 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
686 char *resource = mlt_properties_get( properties, "resource" );
688 // Set the title if provided
689 if ( mlt_properties_get( properties, "title" ) )
690 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
691 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
692 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
694 // Check for a root on the consumer properties and pass to service
695 if ( mlt_properties_get( properties, "root" ) )
696 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
698 // Specify roots in other cases...
699 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
701 // Get the current working directory
702 char *cwd = getcwd( NULL, 0 );
703 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
708 doc = westley_make_doc( this, service );
711 if ( resource == NULL || !strcmp( resource, "" ) )
713 xmlDocFormatDump( stdout, doc, 1 );
715 else if ( strchr( resource, '.' ) == NULL )
717 xmlChar *buffer = NULL;
719 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
720 mlt_properties_set( properties, resource, buffer );
725 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
728 // Close the document
732 mlt_consumer_stop( this );
734 mlt_consumer_stopped( this );
739 static int consumer_is_stopped( mlt_consumer this )