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