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>
29 /** Forward references to static functions.
32 static int consumer_start( mlt_consumer parent
);
34 /** This is what will be called by the factory - anything can be passed in
35 via the argument, but keep it simple.
38 mlt_consumer
consumer_westley_init( char *arg
)
40 // Create the consumer object
41 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
43 // If no malloc'd and consumer init ok
44 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
46 // We have stuff to clean up, so override the close method
47 //parent->close = consumer_close;
49 // Allow thread to be started/stopped
50 this->start
= consumer_start
;
52 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg
);
54 // Return the consumer produced
58 // malloc or consumer init failed
66 // This maintains counters for adding ids to elements
67 struct serialise_context_s
76 mlt_properties producer_map
;
78 typedef struct serialise_context_s
* serialise_context
;
81 static inline void serialise_properties( mlt_properties properties
, xmlNode
*node
)
86 // Enumerate the properties
87 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
89 if ( mlt_properties_get_value( properties
, i
) != NULL
)
92 p
= xmlNewChild( node
, NULL
, "prop", NULL
);
96 xmlNewProp( p
, mlt_properties_get_name( properties
, i
), mlt_properties_get_value( properties
, i
) );
101 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
104 xmlNode
*child
= node
;
110 // Iterate over consumer/producer connections
111 while ( service
!= NULL
)
113 mlt_properties properties
= mlt_service_properties( service
);
114 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
116 // Tell about the producer
117 if ( strcmp( mlt_type
, "producer" ) == 0 )
119 if ( context
->pass
== 0 )
121 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
124 if ( mlt_properties_get( properties
, "id" ) == NULL
)
126 snprintf( id
, 30, "producer%d", context
->producer_count
++ );
127 xmlNewProp( child
, "id", id
);
129 serialise_properties( properties
, child
);
131 // Add producer to the map
132 snprintf( key
, 10, "%p", service
);
133 mlt_properties_set( context
->producer_map
, key
, id
);
137 snprintf( key
, 10, "%p", service
);
138 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
142 // Tell about the framework container producers
143 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
145 // Recurse on multitrack's tracks
146 if ( strcmp( mlt_properties_get( properties
, "resource" ), "<multitrack>" ) == 0 )
148 if ( context
->pass
== 0 )
150 // Iterate over the tracks
151 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
153 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
158 // Iterate over the tracks to collect the producers
159 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
161 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
164 child
= xmlNewChild( node
, NULL
, "multitrack", NULL
);
167 if ( mlt_properties_get( properties
, "id" ) == NULL
)
169 snprintf( id
, 30, "multitrack%d", context
->multitrack_count
++ );
170 xmlNewProp( child
, "id", id
);
173 // Iterate over the tracks
174 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
176 xmlNode
*track
= xmlNewChild( child
, NULL
, "track", NULL
);
177 snprintf( key
, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ) );
178 xmlNewProp( track
, "producer", mlt_properties_get( context
->producer_map
, key
) );
184 // Recurse on playlist's clips
185 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<playlist>" ) == 0 )
187 mlt_playlist_clip_info info
;
189 if ( context
->pass
== 0 )
191 // Iterate over the playlist entries to collect the producers
192 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
194 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
196 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "blank" ) != 0 )
198 serialise_service( context
, MLT_SERVICE( info
.producer
), node
);
203 child
= xmlNewChild( node
, NULL
, "playlist", NULL
);
206 if ( mlt_properties_get( properties
, "id" ) == NULL
)
208 snprintf( id
, 30, "playlist%d", context
->playlist_count
++ );
209 xmlNewProp( child
, "id", id
);
212 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
213 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
215 // Add producer to the map
216 snprintf( key
, 10, "%p", service
);
217 mlt_properties_set( context
->producer_map
, key
, id
);
219 // Iterate over the playlist entries
220 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
222 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
224 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "blank" ) == 0 )
228 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
229 snprintf( length
, 19, "%lld", info
.frame_count
);
230 xmlNewProp( entry
, "length", length
);
234 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
235 snprintf( key
, 10, "%p", MLT_SERVICE( info
.producer
) );
236 xmlNewProp( entry
, "producer", mlt_properties_get( context
->producer_map
, key
) );
243 snprintf( key
, 10, "%p", service
);
244 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
248 // Recurse on tractor's producer
249 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<tractor>" ) == 0 )
251 if ( context
->pass
== 0 )
253 // Recurse on connected producer
254 serialise_service( context
, mlt_service_get_producer( service
), node
);
258 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
261 if ( mlt_properties_get( properties
, "id" ) == NULL
)
263 snprintf( id
, 30, "tractor%d", context
->tractor_count
++ );
264 xmlNewProp( child
, "id", id
);
267 // Recurse on connected producer
268 serialise_service( context
, mlt_service_get_producer( service
), child
);
274 // Tell about a filter
275 else if ( strcmp( mlt_type
, "filter" ) == 0 )
277 // Recurse on connected producer
278 serialise_service( context
, MLT_SERVICE( MLT_FILTER( service
)->producer
), node
);
280 if ( context
->pass
== 1 )
282 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
285 if ( mlt_properties_get( properties
, "id" ) == NULL
)
287 snprintf( id
, 30, "filter%d", context
->filter_count
++ );
288 xmlNewProp( child
, "id", id
);
291 serialise_properties( properties
, child
);
296 // Tell about a transition
297 else if ( strcmp( mlt_type
, "transition" ) == 0 )
299 // Recurse on connected producer
300 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
302 if ( context
->pass
== 1 )
304 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
307 if ( mlt_properties_get( properties
, "id" ) == NULL
)
309 snprintf( id
, 30, "transition%d", context
->transition_count
++ );
310 xmlNewProp( child
, "id", id
);
313 serialise_properties( properties
, child
);
318 // Get the next connected service
319 service
= mlt_service_get_producer( service
);
323 static int consumer_start( mlt_consumer
this )
325 mlt_service inigo
= NULL
;
326 xmlDoc
*doc
= xmlNewDoc( "1.0" );
327 xmlNode
*root
= xmlNewNode( NULL
, "westley" );
328 xmlDocSetRootElement( doc
, root
);
330 // Get the producer service
331 mlt_service service
= mlt_service_get_producer( mlt_consumer_service( this ) );
332 if ( service
!= NULL
)
334 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
335 context
->producer_map
= mlt_properties_new();
338 if ( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ) != NULL
&&
339 strcmp( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ), "inigo" ) == 0 )
342 // Ensure producer is a framework producer
343 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
345 // In pass one, we serialise the end producers and playlists,
346 // adding them to a map keyed by address.
347 serialise_service( context
, service
, root
);
349 // In pass two, we serialise the tractor and reference the
350 // producers and playlists
352 serialise_service( context
, service
, root
);
354 mlt_properties_close( context
->producer_map
);
357 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL
)
358 xmlDocFormatDump( stdout
, doc
, 1 );
360 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc
, 1 );
364 mlt_consumer_stop( this );
366 // Tell inigo, enough already!
368 mlt_properties_set_int( mlt_service_properties( inigo
), "done", 1 );