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 inline 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, "root" ) != 0 &&
188 strcmp( name, "width" ) != 0 &&
189 strcmp( name, "height" ) != 0 )
191 char *value = mlt_properties_get_value( properties, i );
192 p = xmlNewChild( node, NULL, "property", NULL );
193 xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
194 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
195 xmlNodeSetContent( p, value + strlen( context->root ) + 1 );
197 xmlNodeSetContent( p, value );
202 static inline void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node )
207 // Enumerate the properties
208 for ( i = 0; context->store != NULL && i < mlt_properties_count( properties ); i++ )
210 char *name = mlt_properties_get_name( properties, i );
211 if ( !strncmp( name, context->store, strlen( context->store ) ) )
213 char *value = mlt_properties_get_value( properties, i );
216 p = xmlNewChild( node, NULL, "property", NULL );
217 xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
218 if ( context->root != NULL && strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
219 xmlNodeSetContent( p, value + strlen( context->root ) + 1 );
221 xmlNodeSetContent( p, value );
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, "filter", NULL );
246 xmlNewProp( p, "id", id );
247 if ( in != 0 || out != 0 )
250 sprintf( temp, "%d", in );
251 xmlNewProp( p, "in", temp );
252 sprintf( temp, "%d", out );
253 xmlNewProp( p, "out", temp );
255 serialise_properties( context, properties, p );
256 serialise_service_filters( context, mlt_filter_service( filter ), p );
262 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
264 xmlNode *child = node;
265 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
267 if ( context->pass == 0 )
269 mlt_properties properties = mlt_service_properties( parent );
270 // Get a new id - if already allocated, do nothing
271 char *id = westley_get_id( context, parent, westley_producer );
275 child = xmlNewChild( node, NULL, "producer", NULL );
278 xmlNewProp( child, "id", id );
279 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
280 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
281 serialise_properties( context, properties, child );
282 serialise_service_filters( context, service, child );
284 // Add producer to the map
285 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
289 char *id = westley_get_id( context, parent, westley_existing );
290 mlt_properties properties = mlt_service_properties( service );
291 xmlNewProp( node, "parent", id );
292 xmlNewProp( node, "in", mlt_properties_get( properties, "in" ) );
293 xmlNewProp( node, "out", mlt_properties_get( properties, "out" ) );
297 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
299 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
303 if ( context->pass == 0 )
305 // Iterate over the tracks to collect the producers
306 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
308 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
309 serialise_service( context, MLT_SERVICE( producer ), node );
314 // Get a new id - if already allocated, do nothing
315 char *id = westley_get_id( context, service, westley_multitrack );
319 // Serialise the tracks
320 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
322 xmlNode *track = xmlNewChild( node, NULL, "track", NULL );
324 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
325 mlt_properties properties = mlt_producer_properties( producer );
327 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
329 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
330 xmlNewProp( track, "producer", id );
331 if ( mlt_properties_get_int( properties, "_cut" ) == 1 )
333 xmlNewProp( track, "in", mlt_properties_get( properties, "in" ) );
334 xmlNewProp( track, "out", mlt_properties_get( properties, "out" ) );
337 hide = mlt_properties_get_int( context->hide_map, id );
339 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
341 serialise_service_filters( context, service, node );
345 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
348 xmlNode *child = node;
349 mlt_playlist_clip_info info;
350 mlt_properties properties = mlt_service_properties( service );
352 if ( context->pass == 0 )
354 // Get a new id - if already allocated, do nothing
355 char *id = westley_get_id( context, service, westley_playlist );
359 // Iterate over the playlist entries to collect the producers
360 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
362 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
364 if ( info.producer != NULL )
366 mlt_producer producer = mlt_producer_cut_parent( info.producer );
367 char *service_s = mlt_properties_get( mlt_producer_properties( producer ), "mlt_service" );
368 char *resource_s = mlt_properties_get( mlt_producer_properties( producer ), "resource" );
369 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
370 serialise_playlist( context, MLT_SERVICE( producer ), node );
371 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
372 serialise_service( context, MLT_SERVICE( producer ), node );
377 child = xmlNewChild( node, NULL, "playlist", NULL );
380 xmlNewProp( child, "id", id );
382 // Store application specific properties
383 serialise_store_properties( context, properties, child );
385 // Add producer to the map
386 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
388 // Iterate over the playlist entries
389 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
391 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
393 mlt_producer producer = mlt_producer_cut_parent( info.producer );
394 char *service_s = mlt_properties_get( mlt_producer_properties( producer ), "mlt_service" );
395 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
399 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
400 snprintf( length, 19, "%d", info.frame_count );
401 xmlNewProp( entry, "length", length );
406 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
407 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
408 xmlNewProp( entry, "producer", id );
409 sprintf( temp, "%d", info.frame_in );
410 xmlNewProp( entry, "in", temp );
411 sprintf( temp, "%d", info.frame_out );
412 xmlNewProp( entry, "out", temp );
413 if ( info.repeat > 1 )
415 sprintf( temp, "%d", info.repeat );
416 xmlNewProp( entry, "repeat", temp );
418 if ( mlt_producer_is_cut( info.cut ) )
419 serialise_service_filters( context, mlt_producer_service( info.cut ), entry );
424 serialise_service_filters( context, service, child );
426 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
428 char *id = westley_get_id( context, service, westley_existing );
429 xmlNewProp( node, "producer", id );
433 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
435 xmlNode *child = node;
436 mlt_properties properties = mlt_service_properties( service );
438 if ( context->pass == 0 )
440 // Recurse on connected producer
441 serialise_service( context, mlt_service_producer( service ), node );
445 // Get a new id - if already allocated, do nothing
446 char *id = westley_get_id( context, service, westley_tractor );
450 child = xmlNewChild( node, NULL, "tractor", NULL );
453 xmlNewProp( child, "id", id );
454 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
455 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
457 // Store application specific properties
458 serialise_store_properties( context, mlt_service_properties( service ), child );
460 // Recurse on connected producer
461 serialise_service( context, mlt_service_producer( service ), child );
462 serialise_service_filters( context, service, child );
466 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
468 xmlNode *child = node;
469 mlt_properties properties = mlt_service_properties( service );
471 // Recurse on connected producer
472 serialise_service( context, mlt_service_producer( service ), node );
474 if ( context->pass == 1 )
476 // Get a new id - if already allocated, do nothing
477 char *id = westley_get_id( context, service, westley_filter );
481 child = xmlNewChild( node, NULL, "filter", NULL );
484 xmlNewProp( child, "id", id );
485 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
486 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
488 serialise_properties( context, properties, child );
489 serialise_service_filters( context, service, child );
493 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
495 xmlNode *child = node;
496 mlt_properties properties = mlt_service_properties( service );
498 // Recurse on connected producer
499 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
501 if ( context->pass == 1 )
503 // Get a new id - if already allocated, do nothing
504 char *id = westley_get_id( context, service, westley_transition );
508 child = xmlNewChild( node, NULL, "transition", NULL );
511 xmlNewProp( child, "id", id );
512 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
513 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
515 serialise_properties( context, properties, child );
516 serialise_service_filters( context, service, child );
520 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
522 // Iterate over consumer/producer connections
523 while ( service != NULL )
525 mlt_properties properties = mlt_service_properties( service );
526 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
528 // Tell about the producer
529 if ( strcmp( mlt_type, "producer" ) == 0 )
531 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
532 if ( mlt_properties_get( properties, "westley" ) == NULL && !strcmp( mlt_service, "tractor" ) )
535 serialise_tractor( context, service, node );
537 serialise_tractor( context, service, node );
543 serialise_producer( context, service, node );
545 if ( mlt_properties_get( properties, "westley" ) != NULL )
549 // Tell about the framework container producers
550 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
552 char *resource = mlt_properties_get( properties, "resource" );
554 // Recurse on multitrack's tracks
555 if ( strcmp( resource, "<multitrack>" ) == 0 )
557 serialise_multitrack( context, service, node );
561 // Recurse on playlist's clips
562 else if ( strcmp( resource, "<playlist>" ) == 0 )
564 serialise_playlist( context, service, node );
567 // Recurse on tractor's producer
568 else if ( strcmp( resource, "<tractor>" ) == 0 )
571 serialise_tractor( context, service, node );
573 serialise_tractor( context, service, node );
578 // Treat it as a normal producer
581 serialise_producer( context, service, node );
585 // Tell about a filter
586 else if ( strcmp( mlt_type, "filter" ) == 0 )
588 serialise_filter( context, service, node );
592 // Tell about a transition
593 else if ( strcmp( mlt_type, "transition" ) == 0 )
595 serialise_transition( context, service, node );
599 // Get the next connected service
600 service = mlt_service_producer( service );
604 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
606 mlt_properties properties = mlt_service_properties( service );
607 xmlDocPtr doc = xmlNewDoc( "1.0" );
608 xmlNodePtr root = xmlNewNode( NULL, "westley" );
609 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
611 xmlDocSetRootElement( doc, root );
613 // If we have root, then deal with it now
614 if ( mlt_properties_get( properties, "root" ) != NULL )
616 xmlNewProp( root, "root", mlt_properties_get( properties, "root" ) );
617 context->root = strdup( mlt_properties_get( properties, "root" ) );
621 context->root = strdup( "" );
624 // Assign the additional 'storage' pattern for properties
625 context->store = mlt_properties_get( mlt_consumer_properties( consumer ), "store" );
627 // Assign a title property
628 if ( mlt_properties_get( properties, "title" ) != NULL )
629 xmlNewProp( root, "title", mlt_properties_get( properties, "title" ) );
631 // Construct the context maps
632 context->id_map = mlt_properties_new();
633 context->hide_map = mlt_properties_new();
635 // Ensure producer is a framework producer
636 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
638 // In pass one, we serialise the end producers and playlists,
639 // adding them to a map keyed by address.
640 serialise_service( context, service, root );
642 // In pass two, we serialise the tractor and reference the
643 // producers and playlists
645 serialise_service( context, service, root );
648 mlt_properties_close( context->id_map );
649 mlt_properties_close( context->hide_map );
650 free( context->root );
656 static int consumer_start( mlt_consumer this )
658 xmlDocPtr doc = NULL;
660 // Get the producer service
661 mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
662 if ( service != NULL )
664 mlt_properties properties = mlt_consumer_properties( this );
665 char *resource = mlt_properties_get( properties, "resource" );
667 // Set the title if provided
668 if ( mlt_properties_get( properties, "title" ) )
669 mlt_properties_set( mlt_service_properties( service ), "title", mlt_properties_get( properties, "title" ) );
670 else if ( mlt_properties_get( mlt_service_properties( service ), "title" ) == NULL )
671 mlt_properties_set( mlt_service_properties( service ), "title", "Anonymous Submission" );
673 // Check for a root on the consumer properties and pass to service
674 if ( mlt_properties_get( properties, "root" ) )
675 mlt_properties_set( mlt_service_properties( service ), "root", mlt_properties_get( properties, "root" ) );
677 // Specify roots in other cases...
678 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
680 // Get the current working directory
681 char *cwd = getcwd( NULL, 0 );
682 mlt_properties_set( mlt_service_properties( service ), "root", cwd );
687 doc = westley_make_doc( this, service );
690 if ( resource == NULL || !strcmp( resource, "" ) )
692 xmlDocFormatDump( stdout, doc, 1 );
694 else if ( strchr( resource, '.' ) == NULL )
696 xmlChar *buffer = NULL;
698 xmlDocDumpMemory( doc, &buffer, &length );
699 mlt_properties_set( properties, resource, buffer );
704 xmlSaveFormatFile( resource, doc, 1 );
707 // Close the document
711 mlt_consumer_stop( this );
713 mlt_consumer_stopped( this );
718 static int consumer_is_stopped( mlt_consumer this )