437d9f4c159f3adb8d298f74bd26b1e8c0009721
[melted] / src / modules / westley / consumer_westley.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "consumer_westley.h"
22 #include <framework/mlt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <libxml/tree.h>
28
29 #define ID_SIZE 128
30
31 /** Forward references to static functions.
32 */
33
34 static int consumer_start( mlt_consumer parent );
35 static int consumer_is_stopped( mlt_consumer this );
36
37 /** This is what will be called by the factory - anything can be passed in
38 via the argument, but keep it simple.
39 */
40
41 mlt_consumer consumer_westley_init( char *arg )
42 {
43 // Create the consumer object
44 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
45
46 // If no malloc'd and consumer init ok
47 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
48 {
49 // Allow thread to be started/stopped
50 this->start = consumer_start;
51 this->is_stopped = consumer_is_stopped;
52
53 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg );
54
55 // Return the consumer produced
56 return this;
57 }
58
59 // malloc or consumer init failed
60 free( this );
61
62 // Indicate failure
63 return NULL;
64 }
65
66
67 // This maintains counters for adding ids to elements
68 struct serialise_context_s
69 {
70 int producer_count;
71 int multitrack_count;
72 int playlist_count;
73 int tractor_count;
74 int filter_count;
75 int transition_count;
76 int pass;
77 mlt_properties producer_map;
78 };
79 typedef struct serialise_context_s* serialise_context;
80
81
82 static inline void serialise_properties( mlt_properties properties, xmlNode *node )
83 {
84 int i;
85 xmlNode *p;
86
87 // Enumerate the properties
88 for ( i = 0; i < mlt_properties_count( properties ); i++ )
89 {
90 char *name = mlt_properties_get_name( properties, i );
91 if ( name != NULL &&
92 name[ 0 ] != '_' &&
93 mlt_properties_get_value( properties, i ) != NULL &&
94 strcmp( name, "westley" ) != 0 &&
95 strcmp( name, "in" ) != 0 &&
96 strcmp( name, "out" ) != 0 )
97 {
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 ) );
101 }
102 }
103 }
104
105 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
106 {
107 int i;
108 xmlNode *child = node;
109 char id[ ID_SIZE + 1 ];
110 char key[ 11 ];
111 id[ ID_SIZE ] = '\0';
112 key[ 10 ] = '\0';
113
114 // Iterate over consumer/producer connections
115 while ( service != NULL )
116 {
117 mlt_properties properties = mlt_service_properties( service );
118 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
119
120 // Tell about the producer
121 if ( strcmp( mlt_type, "producer" ) == 0 )
122 {
123 if ( context->pass == 0 )
124 {
125 child = xmlNewChild( node, NULL, "producer", NULL );
126
127 // Set the id
128 if ( mlt_properties_get( properties, "id" ) == NULL )
129 {
130 snprintf( id, ID_SIZE, "producer%d", context->producer_count++ );
131 xmlNewProp( child, "id", id );
132 }
133 else
134 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
135 serialise_properties( properties, child );
136
137 // Add producer to the map
138 snprintf( key, 10, "%p", service );
139 mlt_properties_set( context->producer_map, key, id );
140 }
141 else
142 {
143 snprintf( key, 10, "%p", service );
144 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
145 }
146 if ( mlt_properties_get( properties, "westley" ) != NULL )
147 break;
148 }
149
150 // Tell about the framework container producers
151 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
152 {
153 // Recurse on multitrack's tracks
154 if ( strcmp( mlt_properties_get( properties, "resource" ), "<multitrack>" ) == 0 )
155 {
156 if ( context->pass == 0 )
157 {
158 // Iterate over the tracks
159 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
160 {
161 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
162 }
163 }
164 else
165 {
166 // Iterate over the tracks to collect the producers
167 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
168 {
169 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
170 }
171
172 child = xmlNewChild( node, NULL, "multitrack", NULL );
173
174 // Set the id
175 if ( mlt_properties_get( properties, "id" ) == NULL )
176 {
177 snprintf( id, ID_SIZE, "multitrack%d", context->multitrack_count++ );
178 xmlNewProp( child, "id", id );
179 }
180
181 // Iterate over the tracks
182 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
183 {
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 ) );
187 }
188 }
189 break;
190 }
191
192 // Recurse on playlist's clips
193 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<playlist>" ) == 0 )
194 {
195 mlt_playlist_clip_info info;
196
197 if ( context->pass == 0 )
198 {
199 // Iterate over the playlist entries to collect the producers
200 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
201 {
202 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
203 {
204 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) != 0 )
205 {
206 serialise_service( context, MLT_SERVICE( info.producer ), node );
207 }
208 }
209 }
210
211 child = xmlNewChild( node, NULL, "playlist", NULL );
212
213 // Set the id
214 if ( mlt_properties_get( properties, "id" ) == NULL )
215 {
216 snprintf( id, ID_SIZE, "playlist%d", context->playlist_count++ );
217 xmlNewProp( child, "id", id );
218 }
219 else
220 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
221
222 // Add producer to the map
223 snprintf( key, 10, "%p", service );
224 mlt_properties_set( context->producer_map, key, id );
225
226 // Iterate over the playlist entries
227 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
228 {
229 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
230 {
231 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) == 0 )
232 {
233 char length[ 20 ];
234 length[ 19 ] = '\0';
235 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
236 snprintf( length, 19, "%lld", info.frame_count );
237 xmlNewProp( entry, "length", length );
238 }
239 else
240 {
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" ) );
246 }
247 }
248 }
249 }
250 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
251 {
252 snprintf( key, 10, "%p", service );
253 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
254 }
255 }
256
257 // Recurse on tractor's producer
258 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<tractor>" ) == 0 )
259 {
260 if ( context->pass == 0 )
261 {
262 // Recurse on connected producer
263 serialise_service( context, mlt_service_get_producer( service ), node );
264 }
265 else
266 {
267 child = xmlNewChild( node, NULL, "tractor", NULL );
268
269 // Set the id
270 if ( mlt_properties_get( properties, "id" ) == NULL )
271 {
272 snprintf( id, ID_SIZE, "tractor%d", context->tractor_count++ );
273 xmlNewProp( child, "id", id );
274 }
275
276 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
277 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
278
279 // Recurse on connected producer
280 serialise_service( context, mlt_service_get_producer( service ), child );
281 }
282 break;
283 }
284 }
285
286 // Tell about a filter
287 else if ( strcmp( mlt_type, "filter" ) == 0 )
288 {
289 // Recurse on connected producer
290 serialise_service( context, MLT_SERVICE( MLT_FILTER( service )->producer ), node );
291
292 if ( context->pass == 1 )
293 {
294 child = xmlNewChild( node, NULL, "filter", NULL );
295
296 // Set the id
297 if ( mlt_properties_get( properties, "id" ) == NULL )
298 {
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" ) );
303 }
304
305 serialise_properties( properties, child );
306 }
307 break;
308 }
309
310 // Tell about a transition
311 else if ( strcmp( mlt_type, "transition" ) == 0 )
312 {
313 // Recurse on connected producer
314 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
315
316 if ( context->pass == 1 )
317 {
318 child = xmlNewChild( node, NULL, "transition", NULL );
319
320 // Set the id
321 if ( mlt_properties_get( properties, "id" ) == NULL )
322 {
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" ) );
327 }
328
329 serialise_properties( properties, child );
330 }
331 break;
332 }
333
334 // Get the next connected service
335 service = mlt_service_get_producer( service );
336 }
337 }
338
339 static int consumer_start( mlt_consumer this )
340 {
341 mlt_service inigo = NULL;
342 xmlDoc *doc = xmlNewDoc( "1.0" );
343 xmlNode *root = xmlNewNode( NULL, "westley" );
344 xmlDocSetRootElement( doc, root );
345
346 // Get the producer service
347 mlt_service service = mlt_service_get_producer( mlt_consumer_service( this ) );
348 if ( service != NULL )
349 {
350 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
351 context->producer_map = mlt_properties_new();
352
353 // Remember inigo
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 )
356 inigo = service;
357
358 // Ensure producer is a framework producer
359 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
360
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 );
364
365 // In pass two, we serialise the tractor and reference the
366 // producers and playlists
367 context->pass++;
368 serialise_service( context, service, root );
369
370 mlt_properties_close( context->producer_map );
371 free( context );
372
373 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL )
374 xmlDocFormatDump( stdout, doc, 1 );
375 else
376 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc, 1 );
377 }
378
379 xmlFreeDoc( doc );
380 mlt_consumer_stop( this );
381
382 return 0;
383 }
384
385 static int consumer_is_stopped( mlt_consumer this )
386 {
387 return 1;
388 }
389