cc02e9933e77290b2a2d44e0df98614f8cc0fcff
[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 // This maintains counters for adding ids to elements
32 struct serialise_context_s
33 {
34 mlt_properties id_map;
35 int producer_count;
36 int multitrack_count;
37 int playlist_count;
38 int tractor_count;
39 int filter_count;
40 int transition_count;
41 int pass;
42 mlt_properties hide_map;
43 };
44 typedef struct serialise_context_s* serialise_context;
45
46 /** Forward references to static functions.
47 */
48
49 static int consumer_start( mlt_consumer parent );
50 static int consumer_is_stopped( mlt_consumer this );
51 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
52
53 typedef enum
54 {
55 westley_existing,
56 westley_producer,
57 westley_multitrack,
58 westley_playlist,
59 westley_tractor,
60 westley_filter,
61 westley_transition
62 }
63 westley_type;
64
65 /** Create or retrieve an id associated to this service.
66 */
67
68 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
69 {
70 char *id = NULL;
71 int i = 0;
72 mlt_properties map = context->id_map;
73
74 // Search the map for the service
75 for ( i = 0; i < mlt_properties_count( map ); i ++ )
76 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
77 break;
78
79 // If the service is not in the map, and the type indicates a new id is needed...
80 if ( i >= mlt_properties_count( map ) && type != westley_existing )
81 {
82 // Attempt to reuse existing id
83 id = mlt_properties_get( mlt_service_properties( service ), "id" );
84
85 // If no id, or the id is used in the map (for another service), then
86 // create a new one.
87 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
88 {
89 char temp[ ID_SIZE ];
90 do
91 {
92 switch( type )
93 {
94 case westley_producer:
95 sprintf( temp, "producer%d", context->producer_count ++ );
96 break;
97 case westley_multitrack:
98 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
99 break;
100 case westley_playlist:
101 sprintf( temp, "playlist%d", context->playlist_count ++ );
102 break;
103 case westley_tractor:
104 sprintf( temp, "tractor%d", context->tractor_count ++ );
105 break;
106 case westley_filter:
107 sprintf( temp, "filter%d", context->filter_count ++ );
108 break;
109 case westley_transition:
110 sprintf( temp, "transition%d", context->transition_count ++ );
111 break;
112 case westley_existing:
113 // Never gets here
114 break;
115 }
116 }
117 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
118
119 // Set the data at the generated name
120 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
121
122 // Get the pointer to the name (i is the end of the list)
123 id = mlt_properties_get_name( map, i );
124 }
125 else
126 {
127 // Store the existing id in the map
128 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
129 }
130 }
131 else if ( type == westley_existing )
132 {
133 id = mlt_properties_get_name( map, i );
134 }
135
136 return id;
137 }
138
139 /** This is what will be called by the factory - anything can be passed in
140 via the argument, but keep it simple.
141 */
142
143 mlt_consumer consumer_westley_init( char *arg )
144 {
145 // Create the consumer object
146 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
147
148 // If no malloc'd and consumer init ok
149 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
150 {
151 // Allow thread to be started/stopped
152 this->start = consumer_start;
153 this->is_stopped = consumer_is_stopped;
154
155 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg );
156
157 // Return the consumer produced
158 return this;
159 }
160
161 // malloc or consumer init failed
162 free( this );
163
164 // Indicate failure
165 return NULL;
166 }
167
168 static inline void serialise_properties( mlt_properties properties, xmlNode *node )
169 {
170 int i;
171 xmlNode *p;
172
173 // Enumerate the properties
174 for ( i = 0; i < mlt_properties_count( properties ); i++ )
175 {
176 char *name = mlt_properties_get_name( properties, i );
177 if ( name != NULL &&
178 name[ 0 ] != '_' &&
179 mlt_properties_get_value( properties, i ) != NULL &&
180 strcmp( name, "westley" ) != 0 &&
181 strcmp( name, "in" ) != 0 &&
182 strcmp( name, "out" ) != 0 )
183 {
184 p = xmlNewChild( node, NULL, "property", NULL );
185 xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
186 xmlNodeSetContent( p, mlt_properties_get_value( properties, i ) );
187 }
188 }
189 }
190
191 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
192 {
193 int i;
194 xmlNode *p;
195 mlt_filter filter = NULL;
196
197 // Enumerate the filters
198 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
199 {
200 mlt_properties properties = mlt_filter_properties( filter );
201 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
202 {
203 // Get a new id - if already allocated, do nothing
204 char *id = westley_get_id( context, mlt_filter_service( filter ), westley_filter );
205 if ( id != NULL )
206 {
207 p = xmlNewChild( node, NULL, "filter", NULL );
208 xmlNewProp( p, "id", id );
209 serialise_properties( properties, p );
210 serialise_service_filters( context, mlt_filter_service( filter ), p );
211 }
212 }
213 }
214 }
215
216 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
217 {
218 xmlNode *child = node;
219 mlt_properties properties = mlt_service_properties( service );
220
221 if ( context->pass == 0 )
222 {
223 // Get a new id - if already allocated, do nothing
224 char *id = westley_get_id( context, service, westley_producer );
225 if ( id == NULL )
226 return;
227
228 child = xmlNewChild( node, NULL, "producer", NULL );
229
230 // Set the id
231 xmlNewProp( child, "id", id );
232 serialise_properties( properties, child );
233 serialise_service_filters( context, service, child );
234
235 // Add producer to the map
236 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
237 }
238 else
239 {
240 // Get a new id - if already allocated, do nothing
241 char *id = westley_get_id( context, service, westley_existing );
242 xmlNewProp( node, "producer", id );
243 }
244 }
245
246 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
247 {
248 int i;
249 xmlNode *child = node;
250
251 if ( context->pass == 0 )
252 {
253 // Iterate over the tracks to collect the producers
254 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
255 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
256 }
257 else
258 {
259 // Get a new id - if already allocated, do nothing
260 char *id = westley_get_id( context, service, westley_multitrack );
261 if ( id == NULL )
262 return;
263
264 // Create the multitrack node
265 child = xmlNewChild( node, NULL, "multitrack", NULL );
266
267 // Set the id
268 xmlNewProp( child, "id", id );
269
270 // Serialise the tracks
271 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
272 {
273 xmlNode *track = xmlNewChild( child, NULL, "track", NULL );
274 int hide = 0;
275 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
276
277 char *id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
278 xmlNewProp( track, "producer", id );
279
280 hide = mlt_properties_get_int( context->hide_map, id );
281 if ( hide )
282 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
283 }
284 serialise_service_filters( context, service, child );
285 }
286 }
287
288 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
289 {
290 int i;
291 xmlNode *child = node;
292 mlt_playlist_clip_info info;
293 mlt_properties properties = mlt_service_properties( service );
294
295 if ( context->pass == 0 )
296 {
297 // Get a new id - if already allocated, do nothing
298 char *id = westley_get_id( context, service, westley_playlist );
299 if ( id == NULL )
300 return;
301
302 // Iterate over the playlist entries to collect the producers
303 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
304 {
305 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
306 {
307 if ( info.producer != NULL )
308 {
309 char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
310 char *resource_s = mlt_properties_get( mlt_producer_properties( info.producer ), "resource" );
311 if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
312 serialise_service( context, MLT_SERVICE( info.producer ), node );
313 else if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
314 serialise_playlist( context, MLT_SERVICE( info.producer ), node );
315 }
316 }
317 }
318
319 child = xmlNewChild( node, NULL, "playlist", NULL );
320
321 // Set the id
322 xmlNewProp( child, "id", id );
323
324 // Add producer to the map
325 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
326
327 // Iterate over the playlist entries
328 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
329 {
330 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
331 {
332 char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
333 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
334 {
335 char length[ 20 ];
336 length[ 19 ] = '\0';
337 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
338 snprintf( length, 19, "%d", info.frame_count );
339 xmlNewProp( entry, "length", length );
340 }
341 else
342 {
343 char temp[ 20 ];
344 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
345 id = westley_get_id( context, MLT_SERVICE( info.producer ), westley_existing );
346 xmlNewProp( entry, "producer", id );
347 sprintf( temp, "%d", info.frame_in );
348 xmlNewProp( entry, "in", temp );
349 sprintf( temp, "%d", info.frame_out );
350 xmlNewProp( entry, "out", temp );
351 }
352 }
353 }
354
355 serialise_service_filters( context, service, child );
356 }
357 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
358 {
359 char *id = westley_get_id( context, service, westley_existing );
360 xmlNewProp( node, "producer", id );
361 }
362 }
363
364 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
365 {
366 xmlNode *child = node;
367 mlt_properties properties = mlt_service_properties( service );
368
369 if ( context->pass == 0 )
370 {
371 // Recurse on connected producer
372 serialise_service( context, mlt_service_producer( service ), node );
373 }
374 else
375 {
376 // Get a new id - if already allocated, do nothing
377 char *id = westley_get_id( context, service, westley_tractor );
378 if ( id == NULL )
379 return;
380
381 child = xmlNewChild( node, NULL, "tractor", NULL );
382
383 // Set the id
384 xmlNewProp( child, "id", id );
385 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
386 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
387
388 // Recurse on connected producer
389 serialise_service( context, mlt_service_producer( service ), child );
390 serialise_service_filters( context, service, child );
391 }
392 }
393
394 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
395 {
396 xmlNode *child = node;
397 mlt_properties properties = mlt_service_properties( service );
398
399 // Recurse on connected producer
400 serialise_service( context, mlt_service_producer( service ), node );
401
402 if ( context->pass == 1 )
403 {
404 // Get a new id - if already allocated, do nothing
405 char *id = westley_get_id( context, service, westley_filter );
406 if ( id == NULL )
407 return;
408
409 child = xmlNewChild( node, NULL, "filter", NULL );
410
411 // Set the id
412 xmlNewProp( child, "id", id );
413 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
414 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
415
416 serialise_properties( properties, child );
417 serialise_service_filters( context, service, child );
418 }
419 }
420
421 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
422 {
423 xmlNode *child = node;
424 mlt_properties properties = mlt_service_properties( service );
425
426 // Recurse on connected producer
427 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
428
429 if ( context->pass == 1 )
430 {
431 // Get a new id - if already allocated, do nothing
432 char *id = westley_get_id( context, service, westley_transition );
433 if ( id == NULL )
434 return;
435
436 child = xmlNewChild( node, NULL, "transition", NULL );
437
438 // Set the id
439 xmlNewProp( child, "id", id );
440 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
441 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
442
443 serialise_properties( properties, child );
444 serialise_service_filters( context, service, child );
445 }
446 }
447
448 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
449 {
450 // Iterate over consumer/producer connections
451 while ( service != NULL )
452 {
453 mlt_properties properties = mlt_service_properties( service );
454 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
455
456 // Tell about the producer
457 if ( strcmp( mlt_type, "producer" ) == 0 )
458 {
459 serialise_producer( context, service, node );
460 if ( mlt_properties_get( properties, "westley" ) != NULL )
461 break;
462 }
463
464 // Tell about the framework container producers
465 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
466 {
467 char *resource = mlt_properties_get( properties, "resource" );
468
469 // Recurse on multitrack's tracks
470 if ( strcmp( resource, "<multitrack>" ) == 0 )
471 {
472 serialise_multitrack( context, service, node );
473 break;
474 }
475
476 // Recurse on playlist's clips
477 else if ( strcmp( resource, "<playlist>" ) == 0 )
478 {
479 serialise_playlist( context, service, node );
480 }
481
482 // Recurse on tractor's producer
483 else if ( strcmp( resource, "<tractor>" ) == 0 )
484 {
485 serialise_tractor( context, service, node );
486 break;
487 }
488
489 // Treat it as a normal producer
490 else
491 {
492 serialise_producer( context, service, node );
493 }
494 }
495
496 // Tell about a filter
497 else if ( strcmp( mlt_type, "filter" ) == 0 )
498 {
499 serialise_filter( context, service, node );
500 break;
501 }
502
503 // Tell about a transition
504 else if ( strcmp( mlt_type, "transition" ) == 0 )
505 {
506 serialise_transition( context, service, node );
507 break;
508 }
509
510 // Get the next connected service
511 service = mlt_service_producer( service );
512 }
513 }
514
515 xmlDocPtr westley_make_doc( mlt_service service )
516 {
517 xmlDocPtr doc = xmlNewDoc( "1.0" );
518 xmlNodePtr root = xmlNewNode( NULL, "westley" );
519 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
520
521 xmlDocSetRootElement( doc, root );
522
523 // Construct the context maps
524 context->id_map = mlt_properties_new();
525 context->hide_map = mlt_properties_new();
526
527 // Ensure producer is a framework producer
528 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
529
530 // In pass one, we serialise the end producers and playlists,
531 // adding them to a map keyed by address.
532 serialise_service( context, service, root );
533
534 // In pass two, we serialise the tractor and reference the
535 // producers and playlists
536 context->pass++;
537 serialise_service( context, service, root );
538
539 // Cleanup resource
540 mlt_properties_close( context->id_map );
541 mlt_properties_close( context->hide_map );
542 free( context );
543
544 return doc;
545 }
546
547
548 static int consumer_start( mlt_consumer this )
549 {
550 xmlDocPtr doc = NULL;
551
552 // Get the producer service
553 mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
554 if ( service != NULL )
555 {
556 char *resource = mlt_properties_get( mlt_consumer_properties( this ), "resource" );
557
558 doc = westley_make_doc( service );
559
560 if ( resource == NULL || !strcmp( resource, "" ) )
561 xmlDocFormatDump( stdout, doc, 1 );
562 else
563 xmlSaveFormatFile( resource, doc, 1 );
564
565 xmlFreeDoc( doc );
566 }
567
568 mlt_consumer_stop( this );
569
570 mlt_consumer_stopped( this );
571
572 return 0;
573 }
574
575 static int consumer_is_stopped( mlt_consumer this )
576 {
577 return 1;
578 }