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 /** Forward references to static functions.
34 static int consumer_start( mlt_consumer parent
);
35 static int consumer_is_stopped( mlt_consumer
this );
37 /** This is what will be called by the factory - anything can be passed in
38 via the argument, but keep it simple.
41 mlt_consumer
consumer_westley_init( char *arg
)
43 // Create the consumer object
44 mlt_consumer
this = calloc( sizeof( struct mlt_consumer_s
), 1 );
46 // If no malloc'd and consumer init ok
47 if ( this != NULL
&& mlt_consumer_init( this, NULL
) == 0 )
49 // Allow thread to be started/stopped
50 this->start
= consumer_start
;
51 this->is_stopped
= consumer_is_stopped
;
53 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg
);
55 // Return the consumer produced
59 // malloc or consumer init failed
67 // This maintains counters for adding ids to elements
68 struct serialise_context_s
77 mlt_properties producer_map
;
79 typedef struct serialise_context_s
* serialise_context
;
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 )
97 p
= xmlNewChild( node
, NULL
, "property", NULL
);
98 xmlNewProp( p
, "name", mlt_properties_get_name( properties
, i
) );
99 xmlNodeSetContent( p
, mlt_properties_get_value( properties
, i
) );
102 xmlNewProp( p
, mlt_properties_get_name( properties
, i
), mlt_properties_get_value( properties
, i
) );
108 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
111 xmlNode
*child
= node
;
112 char id
[ ID_SIZE
+ 1 ];
114 id
[ ID_SIZE
] = '\0';
117 // Iterate over consumer/producer connections
118 while ( service
!= NULL
)
120 mlt_properties properties
= mlt_service_properties( service
);
121 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
123 // Tell about the producer
124 if ( strcmp( mlt_type
, "producer" ) == 0 )
126 if ( context
->pass
== 0 )
128 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
131 if ( mlt_properties_get( properties
, "id" ) == NULL
)
133 snprintf( id
, ID_SIZE
, "producer%d", context
->producer_count
++ );
134 xmlNewProp( child
, "id", id
);
137 strncpy( id
, mlt_properties_get( properties
, "id" ), ID_SIZE
);
138 serialise_properties( properties
, child
);
140 // Add producer to the map
141 snprintf( key
, 10, "%p", service
);
142 mlt_properties_set( context
->producer_map
, key
, id
);
146 snprintf( key
, 10, "%p", service
);
147 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
149 if ( mlt_properties_get( properties
, "westley" ) != NULL
)
153 // Tell about the framework container producers
154 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
156 // Recurse on multitrack's tracks
157 if ( strcmp( mlt_properties_get( properties
, "resource" ), "<multitrack>" ) == 0 )
159 if ( context
->pass
== 0 )
161 // Iterate over the tracks
162 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
164 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
169 // Iterate over the tracks to collect the producers
170 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
172 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
175 child
= xmlNewChild( node
, NULL
, "multitrack", NULL
);
178 if ( mlt_properties_get( properties
, "id" ) == NULL
)
180 snprintf( id
, ID_SIZE
, "multitrack%d", context
->multitrack_count
++ );
181 xmlNewProp( child
, "id", id
);
184 // Iterate over the tracks
185 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
187 xmlNode
*track
= xmlNewChild( child
, NULL
, "track", NULL
);
188 snprintf( key
, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ) );
189 xmlNewProp( track
, "producer", mlt_properties_get( context
->producer_map
, key
) );
195 // Recurse on playlist's clips
196 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<playlist>" ) == 0 )
198 mlt_playlist_clip_info info
;
200 if ( context
->pass
== 0 )
202 // Iterate over the playlist entries to collect the producers
203 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
205 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
207 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "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 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
226 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
228 // Add producer to the map
229 snprintf( key
, 10, "%p", service
);
230 mlt_properties_set( context
->producer_map
, key
, id
);
232 // Iterate over the playlist entries
233 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
235 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
237 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "blank" ) == 0 )
241 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
242 snprintf( length
, 19, "%lld", info
.frame_count
);
243 xmlNewProp( entry
, "length", length
);
247 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
248 snprintf( key
, 10, "%p", MLT_SERVICE( info
.producer
) );
249 xmlNewProp( entry
, "producer", mlt_properties_get( context
->producer_map
, key
) );
254 else if ( strcmp( (const char*) node
->name
, "tractor" ) != 0 )
256 snprintf( key
, 10, "%p", service
);
257 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
261 // Recurse on tractor's producer
262 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<tractor>" ) == 0 )
264 if ( context
->pass
== 0 )
266 // Recurse on connected producer
267 serialise_service( context
, mlt_service_get_producer( service
), node
);
271 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
274 if ( mlt_properties_get( properties
, "id" ) == NULL
)
276 snprintf( id
, ID_SIZE
, "tractor%d", context
->tractor_count
++ );
277 xmlNewProp( child
, "id", id
);
280 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
281 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
283 // Recurse on connected producer
284 serialise_service( context
, mlt_service_get_producer( service
), child
);
290 // Tell about a filter
291 else if ( strcmp( mlt_type
, "filter" ) == 0 )
293 // Recurse on connected producer
294 serialise_service( context
, MLT_SERVICE( MLT_FILTER( service
)->producer
), node
);
296 if ( context
->pass
== 1 )
298 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
301 if ( mlt_properties_get( properties
, "id" ) == NULL
)
303 snprintf( id
, ID_SIZE
, "filter%d", context
->filter_count
++ );
304 xmlNewProp( child
, "id", id
);
307 serialise_properties( properties
, child
);
312 // Tell about a transition
313 else if ( strcmp( mlt_type
, "transition" ) == 0 )
315 // Recurse on connected producer
316 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
318 if ( context
->pass
== 1 )
320 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
323 if ( mlt_properties_get( properties
, "id" ) == NULL
)
325 snprintf( id
, ID_SIZE
, "transition%d", context
->transition_count
++ );
326 xmlNewProp( child
, "id", id
);
329 serialise_properties( properties
, child
);
334 // Get the next connected service
335 service
= mlt_service_get_producer( service
);
339 static int consumer_start( mlt_consumer
this )
341 mlt_service inigo
= NULL
;
342 xmlDoc
*doc
= xmlNewDoc( "1.0" );
343 xmlNode
*root
= xmlNewNode( NULL
, "westley" );
344 xmlDocSetRootElement( doc
, root
);
346 // Get the producer service
347 mlt_service service
= mlt_service_get_producer( mlt_consumer_service( this ) );
348 if ( service
!= NULL
)
350 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
351 context
->producer_map
= mlt_properties_new();
354 if ( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ) != NULL
&&
355 strcmp( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ), "inigo" ) == 0 )
358 // Ensure producer is a framework producer
359 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
361 // In pass one, we serialise the end producers and playlists,
362 // adding them to a map keyed by address.
363 serialise_service( context
, service
, root
);
365 // In pass two, we serialise the tractor and reference the
366 // producers and playlists
368 serialise_service( context
, service
, root
);
370 mlt_properties_close( context
->producer_map
);
373 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL
)
374 xmlDocFormatDump( stdout
, doc
, 1 );
376 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc
, 1 );
380 mlt_consumer_stop( this );
385 static int consumer_is_stopped( mlt_consumer
this )