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 &&
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_service( serialise_context context, mlt_service service, xmlNode *node )
108 xmlNode *child = node;
109 char id[ ID_SIZE + 1 ];
111 id[ ID_SIZE ] = '\0';
114 // Iterate over consumer/producer connections
115 while ( service != NULL )
117 mlt_properties properties = mlt_service_properties( service );
118 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
120 // Tell about the producer
121 if ( strcmp( mlt_type, "producer" ) == 0 )
123 if ( context->pass == 0 )
125 child = xmlNewChild( node, NULL, "producer", NULL );
128 if ( mlt_properties_get( properties, "id" ) == NULL )
130 snprintf( id, ID_SIZE, "producer%d", context->producer_count++ );
131 xmlNewProp( child, "id", id );
134 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
135 serialise_properties( properties, child );
137 // Add producer to the map
138 snprintf( key, 10, "%p", service );
139 mlt_properties_set( context->producer_map, key, id );
143 snprintf( key, 10, "%p", service );
144 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
146 if ( mlt_properties_get( properties, "westley" ) != NULL )
150 // Tell about the framework container producers
151 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
153 // Recurse on multitrack's tracks
154 if ( strcmp( mlt_properties_get( properties, "resource" ), "<multitrack>" ) == 0 )
156 if ( context->pass == 0 )
158 // Iterate over the tracks
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 );
166 // Iterate over the tracks to collect the producers
167 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
169 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
172 child = xmlNewChild( node, NULL, "multitrack", NULL );
175 if ( mlt_properties_get( properties, "id" ) == NULL )
177 snprintf( id, ID_SIZE, "multitrack%d", context->multitrack_count++ );
178 xmlNewProp( child, "id", id );
181 // Iterate over the tracks
182 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
184 xmlNode *track = xmlNewChild( child, NULL, "track", NULL );
185 snprintf( key, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ) );
186 xmlNewProp( track, "producer", mlt_properties_get( context->producer_map, key ) );
192 // Recurse on playlist's clips
193 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<playlist>" ) == 0 )
195 mlt_playlist_clip_info info;
197 if ( context->pass == 0 )
199 // Iterate over the playlist entries to collect the producers
200 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
202 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
204 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) != 0 )
206 serialise_service( context, MLT_SERVICE( info.producer ), node );
211 child = xmlNewChild( node, NULL, "playlist", NULL );
214 if ( mlt_properties_get( properties, "id" ) == NULL )
216 snprintf( id, ID_SIZE, "playlist%d", context->playlist_count++ );
217 xmlNewProp( child, "id", id );
220 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
222 // Add producer to the map
223 snprintf( key, 10, "%p", service );
224 mlt_properties_set( context->producer_map, key, id );
226 // Iterate over the playlist entries
227 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
229 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
231 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) == 0 )
235 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
236 snprintf( length, 19, "%lld", info.frame_count );
237 xmlNewProp( entry, "length", length );
241 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
242 snprintf( key, 10, "%p", MLT_SERVICE( info.producer ) );
243 xmlNewProp( entry, "producer", mlt_properties_get( context->producer_map, key ) );
244 xmlNewProp( entry, "in", mlt_properties_get( mlt_producer_properties( info.producer ), "in" ) );
245 xmlNewProp( entry, "out", mlt_properties_get( mlt_producer_properties( info.producer ), "out" ) );
250 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
252 snprintf( key, 10, "%p", service );
253 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
257 // Recurse on tractor's producer
258 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<tractor>" ) == 0 )
260 if ( context->pass == 0 )
262 // Recurse on connected producer
263 serialise_service( context, mlt_service_get_producer( service ), node );
267 child = xmlNewChild( node, NULL, "tractor", NULL );
270 if ( mlt_properties_get( properties, "id" ) == NULL )
272 snprintf( id, ID_SIZE, "tractor%d", context->tractor_count++ );
273 xmlNewProp( child, "id", id );
276 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
277 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
279 // Recurse on connected producer
280 serialise_service( context, mlt_service_get_producer( service ), child );
286 // Tell about a filter
287 else if ( strcmp( mlt_type, "filter" ) == 0 )
289 // Recurse on connected producer
290 serialise_service( context, MLT_SERVICE( MLT_FILTER( service )->producer ), node );
292 if ( context->pass == 1 )
294 child = xmlNewChild( node, NULL, "filter", NULL );
297 if ( mlt_properties_get( properties, "id" ) == NULL )
299 snprintf( id, ID_SIZE, "filter%d", context->filter_count++ );
300 xmlNewProp( child, "id", id );
301 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
302 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
305 serialise_properties( properties, child );
310 // Tell about a transition
311 else if ( strcmp( mlt_type, "transition" ) == 0 )
313 // Recurse on connected producer
314 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
316 if ( context->pass == 1 )
318 child = xmlNewChild( node, NULL, "transition", NULL );
321 if ( mlt_properties_get( properties, "id" ) == NULL )
323 snprintf( id, ID_SIZE, "transition%d", context->transition_count++ );
324 xmlNewProp( child, "id", id );
325 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
326 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
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 )