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_properties properties
= mlt_service_properties( service
);
241 if ( context
->pass
== 0 )
243 // Get a new id - if already allocated, do nothing
244 char *id
= westley_get_id( context
, service
, westley_producer
);
248 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
251 xmlNewProp( child
, "id", id
);
252 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
253 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
254 serialise_properties( context
, properties
, child
);
255 serialise_service_filters( context
, service
, child
);
257 // Add producer to the map
258 mlt_properties_set_int( context
->hide_map
, id
, mlt_properties_get_int( properties
, "hide" ) );
262 // Get a new id - if already allocated, do nothing
263 char *id
= westley_get_id( context
, service
, westley_existing
);
264 xmlNewProp( node
, "producer", id
);
268 static void serialise_multitrack( serialise_context context
, mlt_service service
, xmlNode
*node
)
272 if ( context
->pass
== 0 )
274 // Iterate over the tracks to collect the producers
275 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
276 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
280 // Get a new id - if already allocated, do nothing
281 char *id
= westley_get_id( context
, service
, westley_multitrack
);
285 // Serialise the tracks
286 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
288 xmlNode
*track
= xmlNewChild( node
, NULL
, "track", NULL
);
290 mlt_producer producer
= mlt_multitrack_track( MLT_MULTITRACK( service
), i
);
292 char *id
= westley_get_id( context
, MLT_SERVICE( producer
), westley_existing
);
293 xmlNewProp( track
, "producer", id
);
295 hide
= mlt_properties_get_int( context
->hide_map
, id
);
297 xmlNewProp( track
, "hide", hide
== 1 ?
"video" : ( hide
== 2 ?
"audio" : "both" ) );
299 serialise_service_filters( context
, service
, node
);
303 static void serialise_tractor( serialise_context context
, mlt_service service
, xmlNode
*node
);
305 static void serialise_playlist( serialise_context context
, mlt_service service
, xmlNode
*node
)
308 xmlNode
*child
= node
;
309 mlt_playlist_clip_info info
;
310 mlt_properties properties
= mlt_service_properties( service
);
312 if ( context
->pass
== 0 )
314 // Get a new id - if already allocated, do nothing
315 char *id
= westley_get_id( context
, service
, westley_playlist
);
319 // Iterate over the playlist entries to collect the producers
320 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
322 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
324 if ( info
.producer
!= NULL
)
326 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
327 char *resource_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "resource" );
328 if ( resource_s
!= NULL
&& !strcmp( resource_s
, "<tractor>" ) )
330 serialise_tractor( context
, MLT_SERVICE( info
.producer
), node
);
332 serialise_tractor( context
, MLT_SERVICE( info
.producer
), node
);
335 else if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) != 0 )
336 serialise_service( context
, MLT_SERVICE( info
.producer
), node
);
337 else if ( resource_s
!= NULL
&& !strcmp( resource_s
, "<playlist>" ) )
338 serialise_playlist( context
, MLT_SERVICE( info
.producer
), node
);
343 child
= xmlNewChild( node
, NULL
, "playlist", NULL
);
346 xmlNewProp( child
, "id", id
);
348 // Add producer to the map
349 mlt_properties_set_int( context
->hide_map
, id
, mlt_properties_get_int( properties
, "hide" ) );
351 // Iterate over the playlist entries
352 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
354 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
356 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
357 if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) == 0 )
361 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
362 snprintf( length
, 19, "%d", info
.frame_count
);
363 xmlNewProp( entry
, "length", length
);
368 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
369 id
= westley_get_id( context
, MLT_SERVICE( info
.producer
), westley_existing
);
370 xmlNewProp( entry
, "producer", id
);
371 sprintf( temp
, "%d", info
.frame_in
);
372 xmlNewProp( entry
, "in", temp
);
373 sprintf( temp
, "%d", info
.frame_out
);
374 xmlNewProp( entry
, "out", temp
);
379 serialise_service_filters( context
, service
, child
);
381 else if ( strcmp( (const char*) node
->name
, "tractor" ) != 0 )
383 char *id
= westley_get_id( context
, service
, westley_existing
);
384 xmlNewProp( node
, "producer", id
);
388 static void serialise_tractor( serialise_context context
, mlt_service service
, xmlNode
*node
)
390 xmlNode
*child
= node
;
391 mlt_properties properties
= mlt_service_properties( service
);
393 if ( context
->pass
== 0 )
395 // Recurse on connected producer
396 serialise_service( context
, mlt_service_producer( service
), node
);
400 // Get a new id - if already allocated, do nothing
401 char *id
= westley_get_id( context
, service
, westley_tractor
);
405 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
408 xmlNewProp( child
, "id", id
);
409 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
410 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
412 // Recurse on connected producer
413 serialise_service( context
, mlt_service_producer( service
), child
);
414 serialise_service_filters( context
, service
, child
);
418 static void serialise_filter( serialise_context context
, mlt_service service
, xmlNode
*node
)
420 xmlNode
*child
= node
;
421 mlt_properties properties
= mlt_service_properties( service
);
423 // Recurse on connected producer
424 serialise_service( context
, mlt_service_producer( service
), node
);
426 if ( context
->pass
== 1 )
428 // Get a new id - if already allocated, do nothing
429 char *id
= westley_get_id( context
, service
, westley_filter
);
433 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
436 xmlNewProp( child
, "id", id
);
437 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
438 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
440 serialise_properties( context
, properties
, child
);
441 serialise_service_filters( context
, service
, child
);
445 static void serialise_transition( serialise_context context
, mlt_service service
, xmlNode
*node
)
447 xmlNode
*child
= node
;
448 mlt_properties properties
= mlt_service_properties( service
);
450 // Recurse on connected producer
451 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
453 if ( context
->pass
== 1 )
455 // Get a new id - if already allocated, do nothing
456 char *id
= westley_get_id( context
, service
, westley_transition
);
460 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
463 xmlNewProp( child
, "id", id
);
464 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
465 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
467 serialise_properties( context
, properties
, child
);
468 serialise_service_filters( context
, service
, child
);
472 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
474 // Iterate over consumer/producer connections
475 while ( service
!= NULL
)
477 mlt_properties properties
= mlt_service_properties( service
);
478 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
480 // Tell about the producer
481 if ( strcmp( mlt_type
, "producer" ) == 0 )
483 serialise_producer( context
, service
, node
);
484 if ( mlt_properties_get( properties
, "westley" ) != NULL
)
488 // Tell about the framework container producers
489 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
491 char *resource
= mlt_properties_get( properties
, "resource" );
493 // Recurse on multitrack's tracks
494 if ( strcmp( resource
, "<multitrack>" ) == 0 )
496 serialise_multitrack( context
, service
, node
);
500 // Recurse on playlist's clips
501 else if ( strcmp( resource
, "<playlist>" ) == 0 )
503 serialise_playlist( context
, service
, node
);
506 // Recurse on tractor's producer
507 else if ( strcmp( resource
, "<tractor>" ) == 0 )
509 serialise_tractor( context
, service
, node
);
513 // Treat it as a normal producer
516 serialise_producer( context
, service
, node
);
520 // Tell about a filter
521 else if ( strcmp( mlt_type
, "filter" ) == 0 )
523 serialise_filter( context
, service
, node
);
527 // Tell about a transition
528 else if ( strcmp( mlt_type
, "transition" ) == 0 )
530 serialise_transition( context
, service
, node
);
534 // Get the next connected service
535 service
= mlt_service_producer( service
);
539 xmlDocPtr
westley_make_doc( mlt_service service
)
541 mlt_properties properties
= mlt_service_properties( service
);
542 xmlDocPtr doc
= xmlNewDoc( "1.0" );
543 xmlNodePtr root
= xmlNewNode( NULL
, "westley" );
544 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
546 xmlDocSetRootElement( doc
, root
);
548 // If we have root, then deal with it now
549 if ( mlt_properties_get( properties
, "root" ) != NULL
)
551 xmlNewProp( root
, "root", mlt_properties_get( properties
, "root" ) );
552 context
->root
= strdup( mlt_properties_get( properties
, "root" ) );
556 context
->root
= strdup( "" );
559 // Assign a title property
560 if ( mlt_properties_get( properties
, "title" ) != NULL
)
561 xmlNewProp( root
, "title", mlt_properties_get( properties
, "title" ) );
563 // Construct the context maps
564 context
->id_map
= mlt_properties_new();
565 context
->hide_map
= mlt_properties_new();
567 // Ensure producer is a framework producer
568 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
570 // In pass one, we serialise the end producers and playlists,
571 // adding them to a map keyed by address.
572 serialise_service( context
, service
, root
);
574 // In pass two, we serialise the tractor and reference the
575 // producers and playlists
577 serialise_service( context
, service
, root
);
580 mlt_properties_close( context
->id_map
);
581 mlt_properties_close( context
->hide_map
);
582 free( context
->root
);
588 static int consumer_start( mlt_consumer
this )
590 xmlDocPtr doc
= NULL
;
592 // Get the producer service
593 mlt_service service
= mlt_service_producer( mlt_consumer_service( this ) );
594 if ( service
!= NULL
)
596 mlt_properties properties
= mlt_consumer_properties( this );
597 char *resource
= mlt_properties_get( properties
, "resource" );
599 // Set the title if provided
600 if ( mlt_properties_get( properties
, "title" ) )
601 mlt_properties_set( mlt_service_properties( service
), "title", mlt_properties_get( properties
, "title" ) );
602 else if ( mlt_properties_get( mlt_service_properties( service
), "title" ) == NULL
)
603 mlt_properties_set( mlt_service_properties( service
), "title", "Anonymous Submission" );
605 // Check for a root on the consumer properties and pass to service
606 if ( mlt_properties_get( properties
, "root" ) )
607 mlt_properties_set( mlt_service_properties( service
), "root", mlt_properties_get( properties
, "root" ) );
609 // Specify roots in other cases...
610 if ( resource
!= NULL
&& mlt_properties_get( properties
, "root" ) == NULL
)
612 // Get the current working directory
613 char *cwd
= getcwd( NULL
, 0 );
614 mlt_properties_set( mlt_service_properties( service
), "root", cwd
);
619 doc
= westley_make_doc( service
);
622 if ( resource
== NULL
|| !strcmp( resource
, "" ) )
624 xmlDocFormatDump( stdout
, doc
, 1 );
626 else if ( !strcmp( resource
, "buffer" ) )
628 xmlChar
*buffer
= NULL
;
630 xmlDocDumpMemory( doc
, &buffer
, &length
);
631 mlt_properties_set_data( properties
, "buffer", buffer
, length
, xmlFree
, NULL
);
635 xmlSaveFormatFile( resource
, doc
, 1 );
638 // Close the document
642 mlt_consumer_stop( this );
644 mlt_consumer_stopped( this );
649 static int consumer_is_stopped( mlt_consumer
this )