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
41 mlt_properties producer_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
);
53 /** This is what will be called by the factory - anything can be passed in
54 via the argument, but keep it simple.
57 mlt_consumer
consumer_westley_init( char *arg
)
59 // Create the consumer object
60 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
62 // If no malloc'd and consumer init ok
63 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
65 // Allow thread to be started/stopped
66 this->start
= consumer_start
;
67 this->is_stopped
= consumer_is_stopped
;
69 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg
);
71 // Return the consumer produced
75 // malloc or consumer init failed
82 static inline void serialise_properties( mlt_properties properties
, xmlNode
*node
)
87 // Enumerate the properties
88 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
90 char *name
= mlt_properties_get_name( properties
, i
);
93 mlt_properties_get_value( properties
, i
) != NULL
&&
94 strcmp( name
, "westley" ) != 0 &&
95 strcmp( name
, "in" ) != 0 &&
96 strcmp( name
, "out" ) != 0 )
98 p
= xmlNewChild( node
, NULL
, "property", NULL
);
99 xmlNewProp( p
, "name", mlt_properties_get_name( properties
, i
) );
100 xmlNodeSetContent( p
, mlt_properties_get_value( properties
, i
) );
105 static void serialise_producer( serialise_context context
, mlt_service service
, xmlNode
*node
)
107 xmlNode
*child
= node
;
108 char id
[ ID_SIZE
+ 1 ];
110 mlt_properties properties
= mlt_service_properties( service
);
112 id
[ ID_SIZE
] = '\0';
115 if ( context
->pass
== 0 )
117 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
120 if ( mlt_properties_get( properties
, "id" ) == NULL
)
122 snprintf( id
, ID_SIZE
, "producer%d", context
->producer_count
++ );
123 xmlNewProp( child
, "id", id
);
126 strncpy( id
, mlt_properties_get( properties
, "id" ), ID_SIZE
);
127 serialise_properties( properties
, child
);
129 // Add producer to the map
130 snprintf( key
, 10, "%p", service
);
131 mlt_properties_set( context
->producer_map
, key
, id
);
132 mlt_properties_set_int( context
->hide_map
, key
, mlt_properties_get_int( properties
, "hide" ) );
136 snprintf( key
, 10, "%p", service
);
137 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
141 static void serialise_multitrack( serialise_context context
, mlt_service service
, xmlNode
*node
)
144 xmlNode
*child
= node
;
145 char id
[ ID_SIZE
+ 1 ];
147 mlt_properties properties
= mlt_service_properties( service
);
149 id
[ ID_SIZE
] = '\0';
152 if ( context
->pass
== 0 )
154 // Iterate over the tracks to collect the producers
155 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
156 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
160 // Create the multitrack node
161 child
= xmlNewChild( node
, NULL
, "multitrack", NULL
);
164 if ( mlt_properties_get( properties
, "id" ) == NULL
)
166 snprintf( id
, ID_SIZE
, "multitrack%d", context
->multitrack_count
++ );
167 xmlNewProp( child
, "id", id
);
170 // Serialise the tracks
171 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
173 xmlNode
*track
= xmlNewChild( child
, NULL
, "track", NULL
);
176 snprintf( key
, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ) );
177 xmlNewProp( track
, "producer", mlt_properties_get( context
->producer_map
, key
) );
179 hide
= mlt_properties_get_int( context
->hide_map
, key
);
181 xmlNewProp( track
, "hide", hide
== 1 ?
"video" : ( hide
== 2 ?
"audio" : "both" ) );
186 static void serialise_playlist( serialise_context context
, mlt_service service
, xmlNode
*node
)
189 xmlNode
*child
= node
;
190 char id
[ ID_SIZE
+ 1 ];
192 mlt_playlist_clip_info info
;
193 mlt_properties properties
= mlt_service_properties( service
);
195 id
[ ID_SIZE
] = '\0';
198 if ( context
->pass
== 0 )
200 // Iterate over the playlist entries to collect the producers
201 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
203 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
205 if ( info
.producer
!= NULL
)
207 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
208 if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) != 0 )
209 serialise_service( context
, MLT_SERVICE( info
.producer
), node
);
214 child
= xmlNewChild( node
, NULL
, "playlist", NULL
);
217 if ( mlt_properties_get( properties
, "id" ) == NULL
)
219 snprintf( id
, ID_SIZE
, "playlist%d", context
->playlist_count
++ );
220 xmlNewProp( child
, "id", id
);
223 strncpy( id
, mlt_properties_get( properties
, "id" ), ID_SIZE
);
225 // Add producer to the map
226 snprintf( key
, 10, "%p", service
);
227 mlt_properties_set( context
->producer_map
, key
, id
);
228 mlt_properties_set_int( context
->hide_map
, key
, mlt_properties_get_int( properties
, "hide" ) );
230 // Iterate over the playlist entries
231 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
233 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
235 char *service_s
= mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" );
236 if ( service_s
!= NULL
&& strcmp( service_s
, "blank" ) == 0 )
240 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
241 snprintf( length
, 19, "%d", info
.frame_count
);
242 xmlNewProp( entry
, "length", length
);
246 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
247 snprintf( key
, 10, "%p", MLT_SERVICE( info
.producer
) );
248 xmlNewProp( entry
, "producer", mlt_properties_get( context
->producer_map
, key
) );
249 xmlNewProp( entry
, "in", mlt_properties_get( mlt_producer_properties( info
.producer
), "in" ) );
250 xmlNewProp( entry
, "out", mlt_properties_get( mlt_producer_properties( info
.producer
), "out" ) );
255 else if ( strcmp( (const char*) node
->name
, "tractor" ) != 0 )
257 snprintf( key
, 10, "%p", service
);
258 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
262 static void serialise_tractor( serialise_context context
, mlt_service service
, xmlNode
*node
)
264 xmlNode
*child
= node
;
265 char id
[ ID_SIZE
+ 1 ];
266 mlt_properties properties
= mlt_service_properties( service
);
268 id
[ ID_SIZE
] = '\0';
270 if ( context
->pass
== 0 )
272 // Recurse on connected producer
273 serialise_service( context
, mlt_service_get_producer( service
), node
);
277 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
280 if ( mlt_properties_get( properties
, "id" ) == NULL
)
282 snprintf( id
, ID_SIZE
, "tractor%d", context
->tractor_count
++ );
283 xmlNewProp( child
, "id", id
);
286 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
287 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
289 // Recurse on connected producer
290 serialise_service( context
, mlt_service_get_producer( service
), child
);
294 static void serialise_filter( serialise_context context
, mlt_service service
, xmlNode
*node
)
296 xmlNode
*child
= node
;
297 char id
[ ID_SIZE
+ 1 ];
298 mlt_properties properties
= mlt_service_properties( service
);
300 id
[ ID_SIZE
] = '\0';
302 // Recurse on connected producer
303 serialise_service( context
, MLT_SERVICE( MLT_FILTER( service
)->producer
), node
);
305 if ( context
->pass
== 1 )
307 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
310 if ( mlt_properties_get( properties
, "id" ) == NULL
)
312 snprintf( id
, ID_SIZE
, "filter%d", context
->filter_count
++ );
313 xmlNewProp( child
, "id", id
);
314 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
315 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
318 serialise_properties( properties
, child
);
322 static void serialise_transition( serialise_context context
, mlt_service service
, xmlNode
*node
)
324 xmlNode
*child
= node
;
325 char id
[ ID_SIZE
+ 1 ];
326 mlt_properties properties
= mlt_service_properties( service
);
328 id
[ ID_SIZE
] = '\0';
330 // Recurse on connected producer
331 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
333 if ( context
->pass
== 1 )
335 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
338 if ( mlt_properties_get( properties
, "id" ) == NULL
)
340 snprintf( id
, ID_SIZE
, "transition%d", context
->transition_count
++ );
341 xmlNewProp( child
, "id", id
);
342 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
343 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
346 serialise_properties( properties
, child
);
350 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
352 // Iterate over consumer/producer connections
353 while ( service
!= NULL
)
355 mlt_properties properties
= mlt_service_properties( service
);
356 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
358 // Tell about the producer
359 if ( strcmp( mlt_type
, "producer" ) == 0 )
361 serialise_producer( context
, service
, node
);
362 if ( mlt_properties_get( properties
, "westley" ) != NULL
)
366 // Tell about the framework container producers
367 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
369 char *resource
= mlt_properties_get( properties
, "resource" );
371 // Recurse on multitrack's tracks
372 if ( strcmp( resource
, "<multitrack>" ) == 0 )
374 serialise_multitrack( context
, service
, node
);
378 // Recurse on playlist's clips
379 else if ( strcmp( resource
, "<playlist>" ) == 0 )
381 serialise_playlist( context
, service
, node
);
384 // Recurse on tractor's producer
385 else if ( strcmp( resource
, "<tractor>" ) == 0 )
387 serialise_tractor( context
, service
, node
);
392 // Tell about a filter
393 else if ( strcmp( mlt_type
, "filter" ) == 0 )
395 serialise_filter( context
, service
, node
);
399 // Tell about a transition
400 else if ( strcmp( mlt_type
, "transition" ) == 0 )
402 serialise_transition( context
, service
, node
);
406 // Get the next connected service
407 service
= mlt_service_get_producer( service
);
411 xmlDocPtr
westley_make_doc( mlt_service service
)
413 xmlDocPtr doc
= xmlNewDoc( "1.0" );
414 xmlNodePtr root
= xmlNewNode( NULL
, "westley" );
415 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
417 xmlDocSetRootElement( doc
, root
);
419 // Construct the context maps
420 context
->producer_map
= mlt_properties_new();
421 context
->hide_map
= mlt_properties_new();
423 // Ensure producer is a framework producer
424 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
426 // In pass one, we serialise the end producers and playlists,
427 // adding them to a map keyed by address.
428 serialise_service( context
, service
, root
);
430 // In pass two, we serialise the tractor and reference the
431 // producers and playlists
433 serialise_service( context
, service
, root
);
436 mlt_properties_close( context
->producer_map
);
437 mlt_properties_close( context
->hide_map
);
444 static int consumer_start( mlt_consumer
this )
446 mlt_service inigo
= NULL
;
447 xmlDocPtr doc
= NULL
;
449 // Get the producer service
450 mlt_service service
= mlt_service_get_producer( mlt_consumer_service( this ) );
451 if ( service
!= NULL
)
454 if ( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ) != NULL
&&
455 strcmp( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ), "inigo" ) == 0 )
458 doc
= westley_make_doc( service
);
460 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL
)
461 xmlDocFormatDump( stdout
, doc
, 1 );
463 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc
, 1 );
468 mlt_consumer_stop( this );
473 static int consumer_is_stopped( mlt_consumer
this )