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 // We have stuff to clean up, so override the close method
50 //parent->close = consumer_close;
52 // Allow thread to be started/stopped
53 this->start
= consumer_start
;
54 this->is_stopped
= consumer_is_stopped
;
56 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg
);
58 // Return the consumer produced
62 // malloc or consumer init failed
70 // This maintains counters for adding ids to elements
71 struct serialise_context_s
80 mlt_properties producer_map
;
82 typedef struct serialise_context_s
* serialise_context
;
85 static inline void serialise_properties( mlt_properties properties
, xmlNode
*node
)
90 // Enumerate the properties
91 for ( i
= 0; i
< mlt_properties_count( properties
); i
++ )
93 char *name
= mlt_properties_get_name( properties
, i
);
96 mlt_properties_get_value( properties
, i
) != NULL
&&
97 strcmp( name
, "westley" ) != 0 )
100 p
= xmlNewChild( node
, NULL
, "prop", NULL
);
104 xmlNewProp( p
, mlt_properties_get_name( properties
, i
), mlt_properties_get_value( properties
, i
) );
109 static void serialise_service( serialise_context context
, mlt_service service
, xmlNode
*node
)
112 xmlNode
*child
= node
;
113 char id
[ ID_SIZE
+ 1 ];
115 id
[ ID_SIZE
] = '\0';
118 // Iterate over consumer/producer connections
119 while ( service
!= NULL
)
121 mlt_properties properties
= mlt_service_properties( service
);
122 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
124 // Tell about the producer
125 if ( strcmp( mlt_type
, "producer" ) == 0 )
127 if ( context
->pass
== 0 )
129 child
= xmlNewChild( node
, NULL
, "producer", NULL
);
132 if ( mlt_properties_get( properties
, "id" ) == NULL
)
134 snprintf( id
, ID_SIZE
, "producer%d", context
->producer_count
++ );
135 xmlNewProp( child
, "id", id
);
138 strncpy( id
, mlt_properties_get( properties
, "id" ), ID_SIZE
);
139 serialise_properties( properties
, child
);
141 // Add producer to the map
142 snprintf( key
, 10, "%p", service
);
143 mlt_properties_set( context
->producer_map
, key
, id
);
147 snprintf( key
, 10, "%p", service
);
148 xmlNewProp( node
, "producer", mlt_properties_get( context
->producer_map
, key
) );
150 if ( mlt_properties_get( properties
, "westley" ) != NULL
)
154 // Tell about the framework container producers
155 else if ( strcmp( mlt_type
, "mlt_producer" ) == 0 )
157 // Recurse on multitrack's tracks
158 if ( strcmp( mlt_properties_get( properties
, "resource" ), "<multitrack>" ) == 0 )
160 if ( context
->pass
== 0 )
162 // Iterate over the tracks
163 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
165 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
170 // Iterate over the tracks to collect the producers
171 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
173 serialise_service( context
, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ), node
);
176 child
= xmlNewChild( node
, NULL
, "multitrack", NULL
);
179 if ( mlt_properties_get( properties
, "id" ) == NULL
)
181 snprintf( id
, ID_SIZE
, "multitrack%d", context
->multitrack_count
++ );
182 xmlNewProp( child
, "id", id
);
185 // Iterate over the tracks
186 for ( i
= 0; i
< mlt_multitrack_count( MLT_MULTITRACK( service
) ); i
++ )
188 xmlNode
*track
= xmlNewChild( child
, NULL
, "track", NULL
);
189 snprintf( key
, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service
), i
) ) );
190 xmlNewProp( track
, "producer", mlt_properties_get( context
->producer_map
, key
) );
196 // Recurse on playlist's clips
197 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<playlist>" ) == 0 )
199 mlt_playlist_clip_info info
;
201 if ( context
->pass
== 0 )
203 // Iterate over the playlist entries to collect the producers
204 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
206 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
208 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "blank" ) != 0 )
210 serialise_service( context
, MLT_SERVICE( info
.producer
), node
);
215 child
= xmlNewChild( node
, NULL
, "playlist", NULL
);
218 if ( mlt_properties_get( properties
, "id" ) == NULL
)
220 snprintf( id
, ID_SIZE
, "playlist%d", context
->playlist_count
++ );
221 xmlNewProp( child
, "id", id
);
224 strncpy( id
, mlt_properties_get( properties
, "id" ), ID_SIZE
);
226 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
227 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
229 // Add producer to the map
230 snprintf( key
, 10, "%p", service
);
231 mlt_properties_set( context
->producer_map
, key
, id
);
233 // Iterate over the playlist entries
234 for ( i
= 0; i
< mlt_playlist_count( MLT_PLAYLIST( service
) ); i
++ )
236 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service
), &info
, i
) )
238 if ( strcmp( mlt_properties_get( mlt_producer_properties( info
.producer
), "mlt_service" ), "blank" ) == 0 )
242 xmlNode
*entry
= xmlNewChild( child
, NULL
, "blank", NULL
);
243 snprintf( length
, 19, "%lld", info
.frame_count
);
244 xmlNewProp( entry
, "length", length
);
248 xmlNode
*entry
= xmlNewChild( child
, NULL
, "entry", NULL
);
249 snprintf( key
, 10, "%p", MLT_SERVICE( info
.producer
) );
250 xmlNewProp( entry
, "producer", mlt_properties_get( context
->producer_map
, key
) );
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 // Recurse on tractor's producer
263 else if ( strcmp( mlt_properties_get( properties
, "resource" ), "<tractor>" ) == 0 )
265 if ( context
->pass
== 0 )
267 // Recurse on connected producer
268 serialise_service( context
, mlt_service_get_producer( service
), node
);
272 child
= xmlNewChild( node
, NULL
, "tractor", NULL
);
275 if ( mlt_properties_get( properties
, "id" ) == NULL
)
277 snprintf( id
, ID_SIZE
, "tractor%d", context
->tractor_count
++ );
278 xmlNewProp( child
, "id", id
);
281 xmlNewProp( child
, "in", mlt_properties_get( properties
, "in" ) );
282 xmlNewProp( child
, "out", mlt_properties_get( properties
, "out" ) );
284 // Recurse on connected producer
285 serialise_service( context
, mlt_service_get_producer( service
), child
);
291 // Tell about a filter
292 else if ( strcmp( mlt_type
, "filter" ) == 0 )
294 // Recurse on connected producer
295 serialise_service( context
, MLT_SERVICE( MLT_FILTER( service
)->producer
), node
);
297 if ( context
->pass
== 1 )
299 child
= xmlNewChild( node
, NULL
, "filter", NULL
);
302 if ( mlt_properties_get( properties
, "id" ) == NULL
)
304 snprintf( id
, ID_SIZE
, "filter%d", context
->filter_count
++ );
305 xmlNewProp( child
, "id", id
);
308 serialise_properties( properties
, child
);
313 // Tell about a transition
314 else if ( strcmp( mlt_type
, "transition" ) == 0 )
316 // Recurse on connected producer
317 serialise_service( context
, MLT_SERVICE( MLT_TRANSITION( service
)->producer
), node
);
319 if ( context
->pass
== 1 )
321 child
= xmlNewChild( node
, NULL
, "transition", NULL
);
324 if ( mlt_properties_get( properties
, "id" ) == NULL
)
326 snprintf( id
, ID_SIZE
, "transition%d", context
->transition_count
++ );
327 xmlNewProp( child
, "id", id
);
330 serialise_properties( properties
, child
);
335 // Get the next connected service
336 service
= mlt_service_get_producer( service
);
340 static int consumer_start( mlt_consumer
this )
342 mlt_service inigo
= NULL
;
343 xmlDoc
*doc
= xmlNewDoc( "1.0" );
344 xmlNode
*root
= xmlNewNode( NULL
, "westley" );
345 xmlDocSetRootElement( doc
, root
);
347 // Get the producer service
348 mlt_service service
= mlt_service_get_producer( mlt_consumer_service( this ) );
349 if ( service
!= NULL
)
351 struct serialise_context_s
*context
= calloc( 1, sizeof( struct serialise_context_s
) );
352 context
->producer_map
= mlt_properties_new();
355 if ( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ) != NULL
&&
356 strcmp( mlt_properties_get( mlt_service_properties( service
), "mlt_service" ), "inigo" ) == 0 )
359 // Ensure producer is a framework producer
360 mlt_properties_set( mlt_service_properties( service
), "mlt_type", "mlt_producer" );
362 // In pass one, we serialise the end producers and playlists,
363 // adding them to a map keyed by address.
364 serialise_service( context
, service
, root
);
366 // In pass two, we serialise the tractor and reference the
367 // producers and playlists
369 serialise_service( context
, service
, root
);
371 mlt_properties_close( context
->producer_map
);
374 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL
)
375 xmlDocFormatDump( stdout
, doc
, 1 );
377 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc
, 1 );
381 mlt_consumer_stop( this );
386 static int consumer_is_stopped( mlt_consumer
this )