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>
27 #include <libxml/tree.h>
31 // This maintains counters for adding ids to elements
32 struct serialise_context_s
34 mlt_properties id_map
;
42 mlt_properties hide_map
;
44 typedef struct serialise_context_s
* serialise_context
;
46 /** Forward references to static functions.
49 static int consumer_start( mlt_consumer parent
);
50 static int consumer_is_stopped( mlt_consumer
this );
51 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
);
65 /** Create or retrieve an id associated to this service.
68 static char *westley_get_id( serialise_context context
, mlt_service service
, westley_type type
)
72 mlt_properties map
= context
->id_map
;
74 // Search the map for the service
75 for ( i
= 0; i
< mlt_properties_count( map
); i
++ )
76 if ( mlt_properties_get_data_at( map
, i
, NULL
) == service
)
79 // If the service is not in the map, and the type indicates a new id is needed...
80 if ( i
>= mlt_properties_count( map
) && type
!= westley_existing
)
82 // Attempt to reuse existing id
83 id
= mlt_properties_get( mlt_service_properties( service
), "id" );
85 // If no id, or the id is used in the map (for another service), then
87 if ( id
== NULL
|| mlt_properties_get_data( map
, id
, NULL
) != NULL
)
94 case westley_producer
:
95 sprintf( temp
, "producer%d", context
->producer_count
++ );
97 case westley_multitrack
:
98 sprintf( temp
, "multitrack%d", context
->multitrack_count
++ );
100 case westley_playlist
:
101 sprintf( temp
, "playlist%d", context
->playlist_count
++ );
103 case westley_tractor
:
104 sprintf( temp
, "tractor%d", context
->tractor_count
++ );
107 sprintf( temp
, "filter%d", context
->filter_count
++ );
109 case westley_transition
:
110 sprintf( temp
, "transition%d", context
->transition_count
++ );
112 case westley_existing
:
117 while( mlt_properties_get_data( map
, temp
, NULL
) != NULL
);
119 // Set the data at the generated name
120 mlt_properties_set_data( map
, temp
, service
, 0, NULL
, NULL
);
122 // Get the pointer to the name (i is the end of the list)
123 id
= mlt_properties_get_name( map
, i
);
127 // Store the existing id in the map
128 mlt_properties_set_data( map
, id
, service
, 0, NULL
, NULL
);
131 else if ( type
== westley_existing
)
133 id
= mlt_properties_get_name( map
, i
);
139 /** This is what will be called by the factory - anything can be passed in
140 via the argument, but keep it simple.
143 mlt_consumer
consumer_westley_init( char *arg
)
145 // Create the consumer object
146 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
148 // If no malloc'd and consumer init ok
149 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
151 // Allow thread to be started/stopped
152 this->start
= consumer_start
;
153 this->is_stopped
= consumer_is_stopped
;
155 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg
);
157 // Return the consumer produced
161 // malloc or consumer init failed
168 static inline void serialise_properties( mlt_properties properties
, xmlNode
*node
)
173 // Enumerate the properties
174 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
176 char *name
= mlt_properties_get_name( properties
, i
);
179 mlt_properties_get_value( properties
, i
) != NULL
&&
180 strcmp( name
, "westley" ) != 0 &&
181 strcmp( name
, "in" ) != 0 &&
182 strcmp( name
, "out" ) != 0 )
184 p
= xmlNewChild( node
, NULL
, "property", NULL
);
185 xmlNewProp( p
, "name", mlt_properties_get_name( properties
, i
) );
186 xmlNodeSetContent( p
, mlt_properties_get_value( properties
, i
) );
191 static inline void serialise_producer_filters( serialise_context context
, mlt_service service
, xmlNode
*node
)
195 mlt_filter filter
= NULL
;
197 // Enumerate the filters
198 for ( i
= 0; ( filter
= mlt_producer_filter( MLT_PRODUCER( service
), i
) ) != NULL
; i
++ )
200 mlt_properties properties
= mlt_filter_properties( filter
);
201 if ( mlt_properties_get_int( properties
, "_fezzik" ) == 0 )
203 // Get a new id - if already allocated, do nothing
204 char *id
= westley_get_id( context
, mlt_filter_service( filter
), westley_filter
);
208 p
= xmlNewChild( node
, NULL
, "filter", NULL
);
209 xmlNewProp( p
, "id", id
);
210 serialise_properties( properties
, p
);
215 static void serialise_producer( serialise_context context
, mlt_service service
, xmlNode
*node
)
217 xmlNode
*child
= node
;
218 mlt_properties properties
= mlt_service_properties( service
);
220 if ( context
->pass
== 0 )
222 // Get a new id - if already allocated, do nothing
223 char *id
= westley_get_id( context
, service
, westley_producer
);
227 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
230 xmlNewProp( child
, "id", id
);
231 serialise_properties( properties
, child
);
232 serialise_producer_filters( context
, service
, child
);
234 // Add producer to the map
235 mlt_properties_set_int( context
->hide_map
, id
, mlt_properties_get_int( properties
, "hide" ) );
239 // Get a new id - if already allocated, do nothing
240 char *id
= westley_get_id( context
, service
, westley_existing
);
241 xmlNewProp( node
, "producer", id
);
245 static void serialise_multitrack( serialise_context context
, mlt_service service
, xmlNode
*node
)
248 xmlNode
*child
= node
;
250 if ( context
->pass
== 0 )
252 // Iterate over the tracks to collect the producers
253 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
254 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
258 // Get a new id - if already allocated, do nothing
259 char *id
= westley_get_id( context
, service
, westley_multitrack
);
263 // Create the multitrack node
264 child
= xmlNewChild( node
, NULL
, "multitrack", NULL
);
267 xmlNewProp( child
, "id", id
);
269 // Serialise the tracks
270 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
272 xmlNode
*track
= xmlNewChild( child
, NULL
, "track", NULL
);
274 mlt_producer producer
= mlt_multitrack_track( MLT_MULTITRACK( service
), i
);
276 char *id
= westley_get_id( context
, MLT_SERVICE( producer
), westley_existing
);
277 xmlNewProp( track
, "producer", id
);
279 hide
= mlt_properties_get_int( context
->hide_map
, id
);
281 xmlNewProp( track
, "hide", hide
== 1 ?
"video" : ( hide
== 2 ?
"audio" : "both" ) );
283 serialise_producer_filters( context
, service
, child
);
287 static void serialise_playlist( serialise_context context
, mlt_service service
, xmlNode
*node
)
290 xmlNode
*child
= node
;
291 mlt_playlist_clip_info info
;
292 mlt_properties properties
= mlt_service_properties( service
);
294 if ( context
->pass
== 0 )
296 // Get a new id - if already allocated, do nothing
297 char *id
= westley_get_id( context
, service
, westley_playlist
);
301 // Iterate over the playlist entries to collect the producers
302 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
304 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
306 if ( info
.producer
!= NULL
)
308 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
309 char *resource_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "resource" );
310 if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) != 0 )
311 serialise_service( context
, MLT_SERVICE( info
.producer
), node
);
312 else if ( resource_s
!= NULL
&& !strcmp( resource_s
, "<playlist>" ) )
313 serialise_playlist( context
, MLT_SERVICE( info
.producer
), node
);
318 child
= xmlNewChild( node
, NULL
, "playlist", NULL
);
321 xmlNewProp( child
, "id", id
);
323 // Add producer to the map
324 mlt_properties_set_int( context
->hide_map
, id
, mlt_properties_get_int( properties
, "hide" ) );
326 // Iterate over the playlist entries
327 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
329 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
331 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
332 if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) == 0 )
336 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
337 snprintf( length
, 19, "%d", info
.frame_count
);
338 xmlNewProp( entry
, "length", length
);
343 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
344 id
= westley_get_id( context
, MLT_SERVICE( info
.producer
), westley_existing
);
345 xmlNewProp( entry
, "producer", id
);
346 sprintf( temp
, "%d", info
.frame_in
);
347 xmlNewProp( entry
, "in", temp
);
348 sprintf( temp
, "%d", info
.frame_out
);
349 xmlNewProp( entry
, "out", temp
);
354 serialise_producer_filters( context
, service
, child
);
356 else if ( strcmp( (const char*) node
->name
, "tractor" ) != 0 )
358 char *id
= westley_get_id( context
, service
, westley_existing
);
359 xmlNewProp( node
, "producer", id
);
363 static void serialise_tractor( serialise_context context
, mlt_service service
, xmlNode
*node
)
365 xmlNode
*child
= node
;
366 mlt_properties properties
= mlt_service_properties( service
);
368 if ( context
->pass
== 0 )
370 // Recurse on connected producer
371 serialise_service( context
, mlt_service_producer( service
), node
);
375 // Get a new id - if already allocated, do nothing
376 char *id
= westley_get_id( context
, service
, westley_tractor
);
380 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
383 xmlNewProp( child
, "id", id
);
384 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
385 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
387 // Recurse on connected producer
388 serialise_service( context
, mlt_service_producer( service
), child
);
389 serialise_producer_filters( context
, service
, child
);
393 static void serialise_filter( serialise_context context
, mlt_service service
, xmlNode
*node
)
395 xmlNode
*child
= node
;
396 mlt_properties properties
= mlt_service_properties( service
);
398 // Recurse on connected producer
399 serialise_service( context
, mlt_service_producer( service
), node
);
401 if ( context
->pass
== 1 )
403 // Get a new id - if already allocated, do nothing
404 char *id
= westley_get_id( context
, service
, westley_filter
);
408 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
411 xmlNewProp( child
, "id", id
);
412 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
413 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
415 serialise_properties( properties
, child
);
419 static void serialise_transition( serialise_context context
, mlt_service service
, xmlNode
*node
)
421 xmlNode
*child
= node
;
422 mlt_properties properties
= mlt_service_properties( service
);
424 // Recurse on connected producer
425 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
427 if ( context
->pass
== 1 )
429 // Get a new id - if already allocated, do nothing
430 char *id
= westley_get_id( context
, service
, westley_transition
);
434 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
437 xmlNewProp( child
, "id", id
);
438 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
439 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
441 serialise_properties( properties
, child
);
445 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
447 // Iterate over consumer/producer connections
448 while ( service
!= NULL
)
450 mlt_properties properties
= mlt_service_properties( service
);
451 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
453 // Tell about the producer
454 if ( strcmp( mlt_type
, "producer" ) == 0 )
456 serialise_producer( context
, service
, node
);
457 if ( mlt_properties_get( properties
, "westley" ) != NULL
)
461 // Tell about the framework container producers
462 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
464 char *resource
= mlt_properties_get( properties
, "resource" );
466 // Recurse on multitrack's tracks
467 if ( strcmp( resource
, "<multitrack>" ) == 0 )
469 serialise_multitrack( context
, service
, node
);
473 // Recurse on playlist's clips
474 else if ( strcmp( resource
, "<playlist>" ) == 0 )
476 serialise_playlist( context
, service
, node
);
479 // Recurse on tractor's producer
480 else if ( strcmp( resource
, "<tractor>" ) == 0 )
482 serialise_tractor( context
, service
, node
);
486 // Treat it as a normal producer
489 serialise_producer( context
, service
, node
);
493 // Tell about a filter
494 else if ( strcmp( mlt_type
, "filter" ) == 0 )
496 serialise_filter( context
, service
, node
);
500 // Tell about a transition
501 else if ( strcmp( mlt_type
, "transition" ) == 0 )
503 serialise_transition( context
, service
, node
);
507 // Get the next connected service
508 service
= mlt_service_producer( service
);
512 xmlDocPtr
westley_make_doc( mlt_service service
)
514 xmlDocPtr doc
= xmlNewDoc( "1.0" );
515 xmlNodePtr root
= xmlNewNode( NULL
, "westley" );
516 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
518 xmlDocSetRootElement( doc
, root
);
520 // Construct the context maps
521 context
->id_map
= mlt_properties_new();
522 context
->hide_map
= mlt_properties_new();
524 // Ensure producer is a framework producer
525 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
527 // In pass one, we serialise the end producers and playlists,
528 // adding them to a map keyed by address.
529 serialise_service( context
, service
, root
);
531 // In pass two, we serialise the tractor and reference the
532 // producers and playlists
534 serialise_service( context
, service
, root
);
537 mlt_properties_close( context
->id_map
);
538 mlt_properties_close( context
->hide_map
);
545 static int consumer_start( mlt_consumer
this )
547 xmlDocPtr doc
= NULL
;
549 // Get the producer service
550 mlt_service service
= mlt_service_producer( mlt_consumer_service( this ) );
551 if ( service
!= NULL
)
553 char *resource
= mlt_properties_get( mlt_consumer_properties( this ), "resource" );
555 doc
= westley_make_doc( service
);
557 if ( resource
== NULL
|| !strcmp( resource
, "" ) )
558 xmlDocFormatDump( stdout
, doc
, 1 );
560 xmlSaveFormatFile( resource
, doc
, 1 );
565 mlt_consumer_stop( this );
567 mlt_consumer_stopped( this );
572 static int consumer_is_stopped( mlt_consumer
this )