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