westley serialises with entry in/out; full field, aspect, and colour space normalisat...
[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 {
96 #if 1
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 ) );
100 #else
101 p = node;
102 xmlNewProp( p, mlt_properties_get_name( properties, i ), mlt_properties_get_value( properties, i ) );
103 #endif
104 }
105 }
106 }
107
108 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
109 {
110 int i;
111 xmlNode *child = node;
112 char id[ ID_SIZE + 1 ];
113 char key[ 11 ];
114 id[ ID_SIZE ] = '\0';
115 key[ 10 ] = '\0';
116
117 // Iterate over consumer/producer connections
118 while ( service != NULL )
119 {
120 mlt_properties properties = mlt_service_properties( service );
121 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
122
123 // Tell about the producer
124 if ( strcmp( mlt_type, "producer" ) == 0 )
125 {
126 if ( context->pass == 0 )
127 {
128 child = xmlNewChild( node, NULL, "producer", NULL );
129
130 // Set the id
131 if ( mlt_properties_get( properties, "id" ) == NULL )
132 {
133 snprintf( id, ID_SIZE, "producer%d", context->producer_count++ );
134 xmlNewProp( child, "id", id );
135 }
136 else
137 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
138 serialise_properties( properties, child );
139
140 // Add producer to the map
141 snprintf( key, 10, "%p", service );
142 mlt_properties_set( context->producer_map, key, id );
143 }
144 else
145 {
146 snprintf( key, 10, "%p", service );
147 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
148 }
149 if ( mlt_properties_get( properties, "westley" ) != NULL )
150 break;
151 }
152
153 // Tell about the framework container producers
154 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
155 {
156 // Recurse on multitrack's tracks
157 if ( strcmp( mlt_properties_get( properties, "resource" ), "<multitrack>" ) == 0 )
158 {
159 if ( context->pass == 0 )
160 {
161 // Iterate over the tracks
162 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
163 {
164 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
165 }
166 }
167 else
168 {
169 // Iterate over the tracks to collect the producers
170 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
171 {
172 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
173 }
174
175 child = xmlNewChild( node, NULL, "multitrack", NULL );
176
177 // Set the id
178 if ( mlt_properties_get( properties, "id" ) == NULL )
179 {
180 snprintf( id, ID_SIZE, "multitrack%d", context->multitrack_count++ );
181 xmlNewProp( child, "id", id );
182 }
183
184 // Iterate over the tracks
185 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
186 {
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 ) );
190 }
191 }
192 break;
193 }
194
195 // Recurse on playlist's clips
196 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<playlist>" ) == 0 )
197 {
198 mlt_playlist_clip_info info;
199
200 if ( context->pass == 0 )
201 {
202 // Iterate over the playlist entries to collect the producers
203 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
204 {
205 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
206 {
207 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) != 0 )
208 {
209 serialise_service( context, MLT_SERVICE( info.producer ), node );
210 }
211 }
212 }
213
214 child = xmlNewChild( node, NULL, "playlist", NULL );
215
216 // Set the id
217 if ( mlt_properties_get( properties, "id" ) == NULL )
218 {
219 snprintf( id, ID_SIZE, "playlist%d", context->playlist_count++ );
220 xmlNewProp( child, "id", id );
221 }
222 else
223 strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
224
225 // Add producer to the map
226 snprintf( key, 10, "%p", service );
227 mlt_properties_set( context->producer_map, key, id );
228
229 // Iterate over the playlist entries
230 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
231 {
232 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
233 {
234 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) == 0 )
235 {
236 char length[ 20 ];
237 length[ 19 ] = '\0';
238 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
239 snprintf( length, 19, "%lld", info.frame_count );
240 xmlNewProp( entry, "length", length );
241 }
242 else
243 {
244 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
245 snprintf( key, 10, "%p", MLT_SERVICE( info.producer ) );
246 xmlNewProp( entry, "producer", mlt_properties_get( context->producer_map, key ) );
247 xmlNewProp( entry, "in", mlt_properties_get( mlt_producer_properties( info.producer ), "in" ) );
248 xmlNewProp( entry, "out", mlt_properties_get( mlt_producer_properties( info.producer ), "out" ) );
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 return 0;
382 }
383
384 static int consumer_is_stopped( mlt_consumer this )
385 {
386 return 1;
387 }
388