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;
46 typedef struct serialise_context_s* serialise_context;
48 /** Forward references to static functions.
51 static int consumer_start( mlt_consumer parent );
52 static int consumer_is_stopped( mlt_consumer this );
53 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
67 /** Create or retrieve an id associated to this service.
70 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
74 mlt_properties map = context->id_map;
76 // Search the map for the service
77 for ( i = 0; i < mlt_properties_count( map ); i ++ )
78 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
81 // If the service is not in the map, and the type indicates a new id is needed...
82 if ( i >= mlt_properties_count( map ) && type != westley_existing )
84 // Attempt to reuse existing id
85 id = mlt_properties_get( mlt_service_properties( service ), "id" );
87 // If no id, or the id is used in the map (for another service), then
89 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
96 case westley_producer:
97 sprintf( temp, "producer%d", context->producer_count ++ );
99 case westley_multitrack:
100 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
102 case westley_playlist:
103 sprintf( temp, "playlist%d", context->playlist_count ++ );
105 case westley_tractor:
106 sprintf( temp, "tractor%d", context->tractor_count ++ );
109 sprintf( temp, "filter%d", context->filter_count ++ );
111 case westley_transition:
112 sprintf( temp, "transition%d", context->transition_count ++ );
114 case westley_existing:
119 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
121 // Set the data at the generated name
122 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
124 // Get the pointer to the name (i is the end of the list)
125 id = mlt_properties_get_name( map, i );
129 // Store the existing id in the map
130 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
133 else if ( type == westley_existing )
135 id = mlt_properties_get_name( map, i );
141 /** This is what will be called by the factory - anything can be passed in
142 via the argument, but keep it simple.
145 mlt_consumer consumer_westley_init( char *arg )
147 // Create the consumer object
148 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
150 // If no malloc'd and consumer init ok
151 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
153 // Allow thread to be started/stopped
154 this->start = consumer_start;
155 this->is_stopped = consumer_is_stopped;
157 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg );
159 // Return the consumer produced
163 // malloc or consumer init failed
170 static inline void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
175 // Enumerate the properties
176 for ( i = 0; i < mlt_properties_count( properties ); i++ )
178 char *name = mlt_properties_get_name( properties, i );
181 mlt_properties_get_value( properties, i ) != NULL &&
182 strcmp( name, "westley" ) != 0 &&
183 strcmp( name, "in" ) != 0 &&
184 strcmp( name, "out" ) != 0 &&
185 strcmp( name, "id" ) != 0 &&
186 strcmp( name, "root" ) != 0 &&
187 strcmp( name, "width" ) != 0 &&
188 strcmp( name, "height" ) != 0 )
190 char *value = mlt_properties_get_value( properties, i );
191 p = xmlNewChild( node, NULL, "property", NULL );
192 xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
193 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
194 xmlNodeSetContent( p, value + strlen( context->root ) + 1 );
196 xmlNodeSetContent( p, value );
201 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
205 mlt_filter filter = NULL;
207 // Enumerate the filters
208 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
210 mlt_properties properties = mlt_filter_properties( filter );
211 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
213 // Get a new id - if already allocated, do nothing
214 char *id = westley_get_id( context, mlt_filter_service( filter ), westley_filter );
217 int in = mlt_properties_get_position( properties, "in" );
218 int out = mlt_properties_get_position( properties, "out" );
219 p = xmlNewChild( node, NULL, "filter", NULL );
220 xmlNewProp( p, "id", id );
221 if ( in != 0 || out != 0 )
224 sprintf( temp, "%d", in );
225 xmlNewProp( p, "in", temp );
226 sprintf( temp, "%d", out );
227 xmlNewProp( p, "out", temp );
229 serialise_properties( context, properties, p );
230 serialise_service_filters( context, mlt_filter_service( filter ), p );
236 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
238 xmlNode *child = node;
239 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
241 if ( context->pass == 0 )
243 mlt_properties properties = mlt_service_properties( parent );
244 // Get a new id - if already allocated, do nothing
245 char *id = westley_get_id( context, parent, westley_producer );
249 child = xmlNewChild( node, NULL, "producer", NULL );
252 xmlNewProp( child, "id", id );
253 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
254 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
255 serialise_properties( context, properties, child );
256 serialise_service_filters( context, service, child );
258 // Add producer to the map
259 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
263 char *id = westley_get_id( context, parent, westley_existing );
264 mlt_properties properties = mlt_service_properties( service );
265 xmlNewProp( node, "parent", id );
266 xmlNewProp( node, "in", mlt_properties_get( properties, "in" ) );
267 xmlNewProp( node, "out", mlt_properties_get( properties, "out" ) );
271 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
273 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
277 if ( context->pass == 0 )
279 // Iterate over the tracks to collect the producers
280 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
282 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
283 char *resource_s = mlt_properties_get( mlt_producer_properties( producer ), "resource" );
284 if ( resource_s != NULL && !strcmp( resource_s, "<tractor>" ) )
286 serialise_tractor( context, MLT_SERVICE( producer ), node );
288 serialise_tractor( context, MLT_SERVICE( producer ), node );
293 serialise_service( context, MLT_SERVICE( producer ), node );
299 // Get a new id - if already allocated, do nothing
300 char *id = westley_get_id( context, service, westley_multitrack );
304 // Serialise the tracks
305 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
307 xmlNode *track = xmlNewChild( node, NULL, "track", NULL );
309 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
310 mlt_properties properties = mlt_producer_properties( producer );
312 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
314 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
315 xmlNewProp( track, "producer", id );
316 if ( mlt_properties_get_int( properties, "_cut" ) == 1 )
318 xmlNewProp( track, "in", mlt_properties_get( properties, "in" ) );
319 xmlNewProp( track, "out", mlt_properties_get( properties, "out" ) );
322 hide = mlt_properties_get_int( context->hide_map, id );
324 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
326 serialise_service_filters( context, service, node );
330 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
333 xmlNode *child = node;
334 mlt_playlist_clip_info info;
335 mlt_properties properties = mlt_service_properties( service );
337 if ( context->pass == 0 )
339 // Get a new id - if already allocated, do nothing
340 char *id = westley_get_id( context, service, westley_playlist );
344 // Iterate over the playlist entries to collect the producers
345 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
347 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
349 if ( info.producer != NULL )
351 mlt_producer producer = mlt_producer_cut_parent( info.producer );
352 char *service_s = mlt_properties_get( mlt_producer_properties( producer ), "mlt_service" );
353 char *resource_s = mlt_properties_get( mlt_producer_properties( producer ), "resource" );
354 if ( resource_s != NULL && !strcmp( resource_s, "<tractor>" ) )
356 serialise_tractor( context, MLT_SERVICE( producer ), node );
358 serialise_tractor( context, MLT_SERVICE( producer ), node );
361 else if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
362 serialise_playlist( context, MLT_SERVICE( producer ), node );
363 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
364 serialise_service( context, MLT_SERVICE( producer ), node );
369 child = xmlNewChild( node, NULL, "playlist", NULL );
372 xmlNewProp( child, "id", id );
374 // Add producer to the map
375 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
377 // Iterate over the playlist entries
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 mlt_producer producer = mlt_producer_cut_parent( info.producer );
383 char *service_s = mlt_properties_get( mlt_producer_properties( producer ), "mlt_service" );
384 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
388 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
389 snprintf( length, 19, "%d", info.frame_count );
390 xmlNewProp( entry, "length", length );
395 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
396 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
397 xmlNewProp( entry, "producer", id );
398 sprintf( temp, "%d", info.frame_in );
399 xmlNewProp( entry, "in", temp );
400 sprintf( temp, "%d", info.frame_out );
401 xmlNewProp( entry, "out", temp );
402 if ( info.repeat > 1 )
404 sprintf( temp, "%d", info.repeat );
405 xmlNewProp( entry, "repeat", temp );
407 if ( mlt_producer_is_cut( info.cut ) )
408 serialise_service_filters( context, mlt_producer_service( info.cut ), entry );
413 serialise_service_filters( context, service, child );
415 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
417 char *id = westley_get_id( context, service, westley_existing );
418 xmlNewProp( node, "producer", id );
422 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
424 xmlNode *child = node;
425 mlt_properties properties = mlt_service_properties( service );
427 if ( context->pass == 0 )
429 // Recurse on connected producer
430 serialise_service( context, mlt_service_producer( service ), node );
434 // Get a new id - if already allocated, do nothing
435 char *id = westley_get_id( context, service, westley_tractor );
439 child = xmlNewChild( node, NULL, "tractor", NULL );
442 xmlNewProp( child, "id", id );
443 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
444 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
446 // Recurse on connected producer
447 serialise_service( context, mlt_service_producer( service ), child );
448 serialise_service_filters( context, service, child );
452 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
454 xmlNode *child = node;
455 mlt_properties properties = mlt_service_properties( service );
457 // Recurse on connected producer
458 serialise_service( context, mlt_service_producer( service ), node );
460 if ( context->pass == 1 )
462 // Get a new id - if already allocated, do nothing
463 char *id = westley_get_id( context, service, westley_filter );
467 child = xmlNewChild( node, NULL, "filter", NULL );
470 xmlNewProp( child, "id", id );
471 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
472 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
474 serialise_properties( context, properties, child );
475 serialise_service_filters( context, service, child );
479 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
481 xmlNode *child = node;
482 mlt_properties properties = mlt_service_properties( service );
484 // Recurse on connected producer
485 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
487 if ( context->pass == 1 )
489 // Get a new id - if already allocated, do nothing
490 char *id = westley_get_id( context, service, westley_transition );
494 child = xmlNewChild( node, NULL, "transition", NULL );
497 xmlNewProp( child, "id", id );
498 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
499 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
501 serialise_properties( context, properties, child );
502 serialise_service_filters( context, service, child );
506 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
508 // Iterate over consumer/producer connections
509 while ( service != NULL )
511 mlt_properties properties = mlt_service_properties( service );
512 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
514 // Tell about the producer
515 if ( strcmp( mlt_type, "producer" ) == 0 )
517 serialise_producer( context, service, node );
518 if ( mlt_properties_get( properties, "westley" ) != NULL )
522 // Tell about the framework container producers
523 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
525 char *resource = mlt_properties_get( properties, "resource" );
527 // Recurse on multitrack's tracks
528 if ( strcmp( resource, "<multitrack>" ) == 0 )
530 serialise_multitrack( context, service, node );
534 // Recurse on playlist's clips
535 else if ( strcmp( resource, "<playlist>" ) == 0 )
537 serialise_playlist( context, service, node );
540 // Recurse on tractor's producer
541 else if ( strcmp( resource, "<tractor>" ) == 0 )
543 serialise_tractor( context, service, node );
547 // Treat it as a normal producer
550 serialise_producer( context, service, node );
554 // Tell about a filter
555 else if ( strcmp( mlt_type, "filter" ) == 0 )
557 serialise_filter( context, service, node );
561 // Tell about a transition
562 else if ( strcmp( mlt_type, "transition" ) == 0 )
564 serialise_transition( context, service, node );
568 // Get the next connected service
569 service = mlt_service_producer( service );
573 xmlDocPtr westley_make_doc( mlt_service service )
575 mlt_properties properties = mlt_service_properties( service );
576 xmlDocPtr doc = xmlNewDoc( "1.0" );
577 xmlNodePtr root = xmlNewNode( NULL, "westley" );
578 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
580 xmlDocSetRootElement( doc, root );
582 // If we have root, then deal with it now
583 if ( mlt_properties_get( properties, "root" ) != NULL )
585 xmlNewProp( root, "root", mlt_properties_get( properties, "root" ) );
586 context->root = strdup( mlt_properties_get( properties, "root" ) );
590 context->root = strdup( "" );
593 // Assign a title property
594 if ( mlt_properties_get( properties, "title" ) != NULL )
595 xmlNewProp( root, "title", mlt_properties_get( properties, "title" ) );
597 // Construct the context maps
598 context->id_map = mlt_properties_new();
599 context->hide_map = mlt_properties_new();
601 // Ensure producer is a framework producer
602 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
604 // In pass one, we serialise the end producers and playlists,
605 // adding them to a map keyed by address.
606 serialise_service( context, service, root );
608 // In pass two, we serialise the tractor and reference the
609 // producers and playlists
611 serialise_service( context, service, root );
614 mlt_properties_close( context->id_map );
615 mlt_properties_close( context->hide_map );
616 free( context->root );
622 static int consumer_start( mlt_consumer this )
624 xmlDocPtr doc = NULL;
626 // Get the producer service
627 mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
628 if ( service != NULL )
630 mlt_properties properties = mlt_consumer_properties( this );
631 char *resource = mlt_properties_get( properties, "resource" );
633 // Set the title if provided
634 if ( mlt_properties_get( properties, "title" ) )
635 mlt_properties_set( mlt_service_properties( service ), "title", mlt_properties_get( properties, "title" ) );
636 else if ( mlt_properties_get( mlt_service_properties( service ), "title" ) == NULL )
637 mlt_properties_set( mlt_service_properties( service ), "title", "Anonymous Submission" );
639 // Check for a root on the consumer properties and pass to service
640 if ( mlt_properties_get( properties, "root" ) )
641 mlt_properties_set( mlt_service_properties( service ), "root", mlt_properties_get( properties, "root" ) );
643 // Specify roots in other cases...
644 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
646 // Get the current working directory
647 char *cwd = getcwd( NULL, 0 );
648 mlt_properties_set( mlt_service_properties( service ), "root", cwd );
653 doc = westley_make_doc( service );
656 if ( resource == NULL || !strcmp( resource, "" ) )
658 xmlDocFormatDump( stdout, doc, 1 );
660 else if ( !strcmp( resource, "buffer" ) )
662 xmlChar *buffer = NULL;
664 xmlDocDumpMemory( doc, &buffer, &length );
665 mlt_properties_set_data( properties, "buffer", buffer, length, xmlFree, NULL );
669 xmlSaveFormatFile( resource, doc, 1 );
672 // Close the document
676 mlt_consumer_stop( this );
678 mlt_consumer_stopped( this );
683 static int consumer_is_stopped( mlt_consumer this )