29e787cff78cfc393ee942804cceea32ac6c2e89
[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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, 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 <unistd.h>
28 #include <libxml/tree.h>
29
30 #define ID_SIZE 128
31
32 #define _x (xmlChar*)
33 #define _s (char*)
34
35 // This maintains counters for adding ids to elements
36 struct serialise_context_s
37 {
38 mlt_properties id_map;
39 int producer_count;
40 int multitrack_count;
41 int playlist_count;
42 int tractor_count;
43 int filter_count;
44 int transition_count;
45 int pass;
46 mlt_properties hide_map;
47 char *root;
48 char *store;
49 };
50 typedef struct serialise_context_s* serialise_context;
51
52 /** Forward references to static functions.
53 */
54
55 static int consumer_start( mlt_consumer parent );
56 static int consumer_is_stopped( mlt_consumer this );
57 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node );
58
59 typedef enum
60 {
61 westley_existing,
62 westley_producer,
63 westley_multitrack,
64 westley_playlist,
65 westley_tractor,
66 westley_filter,
67 westley_transition
68 }
69 westley_type;
70
71 /** Create or retrieve an id associated to this service.
72 */
73
74 static char *westley_get_id( serialise_context context, mlt_service service, westley_type type )
75 {
76 char *id = NULL;
77 int i = 0;
78 mlt_properties map = context->id_map;
79
80 // Search the map for the service
81 for ( i = 0; i < mlt_properties_count( map ); i ++ )
82 if ( mlt_properties_get_data_at( map, i, NULL ) == service )
83 break;
84
85 // If the service is not in the map, and the type indicates a new id is needed...
86 if ( i >= mlt_properties_count( map ) && type != westley_existing )
87 {
88 // Attempt to reuse existing id
89 id = mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "id" );
90
91 // If no id, or the id is used in the map (for another service), then
92 // create a new one.
93 if ( id == NULL || mlt_properties_get_data( map, id, NULL ) != NULL )
94 {
95 char temp[ ID_SIZE ];
96 do
97 {
98 switch( type )
99 {
100 case westley_producer:
101 sprintf( temp, "producer%d", context->producer_count ++ );
102 break;
103 case westley_multitrack:
104 sprintf( temp, "multitrack%d", context->multitrack_count ++ );
105 break;
106 case westley_playlist:
107 sprintf( temp, "playlist%d", context->playlist_count ++ );
108 break;
109 case westley_tractor:
110 sprintf( temp, "tractor%d", context->tractor_count ++ );
111 break;
112 case westley_filter:
113 sprintf( temp, "filter%d", context->filter_count ++ );
114 break;
115 case westley_transition:
116 sprintf( temp, "transition%d", context->transition_count ++ );
117 break;
118 case westley_existing:
119 // Never gets here
120 break;
121 }
122 }
123 while( mlt_properties_get_data( map, temp, NULL ) != NULL );
124
125 // Set the data at the generated name
126 mlt_properties_set_data( map, temp, service, 0, NULL, NULL );
127
128 // Get the pointer to the name (i is the end of the list)
129 id = mlt_properties_get_name( map, i );
130 }
131 else
132 {
133 // Store the existing id in the map
134 mlt_properties_set_data( map, id, service, 0, NULL, NULL );
135 }
136 }
137 else if ( type == westley_existing )
138 {
139 id = mlt_properties_get_name( map, i );
140 }
141
142 return id;
143 }
144
145 /** This is what will be called by the factory - anything can be passed in
146 via the argument, but keep it simple.
147 */
148
149 mlt_consumer consumer_westley_init( char *arg )
150 {
151 // Create the consumer object
152 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
153
154 // If no malloc'd and consumer init ok
155 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
156 {
157 // Allow thread to be started/stopped
158 this->start = consumer_start;
159 this->is_stopped = consumer_is_stopped;
160
161 mlt_properties_set( MLT_CONSUMER_PROPERTIES( this ), "resource", arg );
162
163 // Return the consumer produced
164 return this;
165 }
166
167 // malloc or consumer init failed
168 free( this );
169
170 // Indicate failure
171 return NULL;
172 }
173
174 static void serialise_properties( serialise_context context, mlt_properties properties, xmlNode *node )
175 {
176 int i;
177 xmlNode *p;
178
179 // Enumerate the properties
180 for ( i = 0; i < mlt_properties_count( properties ); i++ )
181 {
182 char *name = mlt_properties_get_name( properties, i );
183 if ( name != NULL &&
184 name[ 0 ] != '_' &&
185 mlt_properties_get_value( properties, i ) != NULL &&
186 strcmp( name, "westley" ) != 0 &&
187 strcmp( name, "in" ) != 0 &&
188 strcmp( name, "out" ) != 0 &&
189 strcmp( name, "id" ) != 0 &&
190 strcmp( name, "title" ) != 0 &&
191 strcmp( name, "root" ) != 0 &&
192 strcmp( name, "width" ) != 0 &&
193 strcmp( name, "height" ) != 0 )
194 {
195 char *value = mlt_properties_get_value( properties, i );
196 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
197 value += strlen( context->root ) + 1;
198 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
199 xmlNewProp( p, _x("name"), _x(name) );
200 }
201 }
202 }
203
204 static void serialise_store_properties( serialise_context context, mlt_properties properties, xmlNode *node, char *store )
205 {
206 int i;
207 xmlNode *p;
208
209 // Enumerate the properties
210 for ( i = 0; store != NULL && i < mlt_properties_count( properties ); i++ )
211 {
212 char *name = mlt_properties_get_name( properties, i );
213 if ( !strncmp( name, store, strlen( store ) ) )
214 {
215 char *value = mlt_properties_get_value( properties, i );
216 if ( value != NULL )
217 {
218 if ( strcmp( context->root, "" ) && !strncmp( value, context->root, strlen( context->root ) ) )
219 value += strlen( context->root ) + 1;
220 p = xmlNewTextChild( node, NULL, _x("property"), _x(value) );
221 xmlNewProp( p, _x("name"), _x(name) );
222 }
223 }
224 }
225 }
226
227 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
228 {
229 int i;
230 xmlNode *p;
231 mlt_filter filter = NULL;
232
233 // Enumerate the filters
234 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
235 {
236 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
237 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
238 {
239 // Get a new id - if already allocated, do nothing
240 char *id = westley_get_id( context, MLT_FILTER_SERVICE( filter ), westley_filter );
241 if ( id != NULL )
242 {
243 int in = mlt_properties_get_position( properties, "in" );
244 int out = mlt_properties_get_position( properties, "out" );
245 p = xmlNewChild( node, NULL, _x("filter"), NULL );
246 xmlNewProp( p, _x("id"), _x(id) );
247 if ( mlt_properties_get( properties, "title" ) )
248 xmlNewProp( p, _x("title"), _x(mlt_properties_get( properties, "title" )) );
249 if ( in != 0 || out != 0 )
250 {
251 char temp[ 20 ];
252 sprintf( temp, "%d", in );
253 xmlNewProp( p, _x("in"), _x(temp) );
254 sprintf( temp, "%d", out );
255 xmlNewProp( p, _x("out"), _x(temp) );
256 }
257 serialise_properties( context, properties, p );
258 serialise_service_filters( context, MLT_FILTER_SERVICE( filter ), p );
259 }
260 }
261 }
262 }
263
264 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
265 {
266 xmlNode *child = node;
267 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( MLT_PRODUCER( service ) ) );
268
269 if ( context->pass == 0 )
270 {
271 mlt_properties properties = MLT_SERVICE_PROPERTIES( parent );
272 // Get a new id - if already allocated, do nothing
273 char *id = westley_get_id( context, parent, westley_producer );
274 if ( id == NULL )
275 return;
276
277 child = xmlNewChild( node, NULL, _x("producer"), NULL );
278
279 // Set the id
280 xmlNewProp( child, _x("id"), _x(id) );
281 if ( mlt_properties_get( properties, "title" ) )
282 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
283 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
284 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
285 serialise_properties( context, properties, child );
286 serialise_service_filters( context, service, child );
287
288 // Add producer to the map
289 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
290 }
291 else
292 {
293 char *id = westley_get_id( context, parent, westley_existing );
294 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
295 xmlNewProp( node, _x("parent"), _x(id) );
296 xmlNewProp( node, _x("in"), _x(mlt_properties_get( properties, "in" )) );
297 xmlNewProp( node, _x("out"), _x(mlt_properties_get( properties, "out" )) );
298 }
299 }
300
301 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
302
303 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
304 {
305 int i;
306
307 if ( context->pass == 0 )
308 {
309 // Iterate over the tracks to collect the producers
310 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
311 {
312 mlt_producer producer = mlt_producer_cut_parent( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) );
313 serialise_service( context, MLT_SERVICE( producer ), node );
314 }
315 }
316 else
317 {
318 // Get a new id - if already allocated, do nothing
319 char *id = westley_get_id( context, service, westley_multitrack );
320 if ( id == NULL )
321 return;
322
323 // Serialise the tracks
324 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
325 {
326 xmlNode *track = xmlNewChild( node, NULL, _x("track"), NULL );
327 int hide = 0;
328 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
329 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
330
331 mlt_service parent = MLT_SERVICE( mlt_producer_cut_parent( producer ) );
332
333 char *id = westley_get_id( context, MLT_SERVICE( parent ), westley_existing );
334 xmlNewProp( track, _x("producer"), _x(id) );
335 if ( mlt_producer_is_cut( producer ) )
336 {
337 xmlNewProp( track, _x("in"), _x(mlt_properties_get( properties, "in" )) );
338 xmlNewProp( track, _x("out"), _x(mlt_properties_get( properties, "out" )) );
339 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, context->store );
340 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( producer ), track, "meta." );
341 serialise_service_filters( context, MLT_PRODUCER_SERVICE( producer ), track );
342 }
343
344 hide = mlt_properties_get_int( context->hide_map, id );
345 if ( hide )
346 xmlNewProp( track, _x("hide"), _x( hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) ) );
347 }
348 serialise_service_filters( context, service, node );
349 }
350 }
351
352 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
353 {
354 int i;
355 xmlNode *child = node;
356 mlt_playlist_clip_info info;
357 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
358
359 if ( context->pass == 0 )
360 {
361 // Get a new id - if already allocated, do nothing
362 char *id = westley_get_id( context, service, westley_playlist );
363 if ( id == NULL )
364 return;
365
366 // Iterate over the playlist entries to collect the producers
367 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
368 {
369 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
370 {
371 if ( info.producer != NULL )
372 {
373 mlt_producer producer = mlt_producer_cut_parent( info.producer );
374 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
375 char *resource_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "resource" );
376 if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
377 serialise_playlist( context, MLT_SERVICE( producer ), node );
378 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
379 serialise_service( context, MLT_SERVICE( producer ), node );
380 }
381 }
382 }
383
384 child = xmlNewChild( node, NULL, _x("playlist"), NULL );
385
386 // Set the id
387 xmlNewProp( child, _x("id"), _x(id) );
388 if ( mlt_properties_get( properties, "title" ) )
389 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
390
391 // Store application specific properties
392 serialise_store_properties( context, properties, child, context->store );
393 serialise_store_properties( context, properties, child, "meta." );
394
395 // Add producer to the map
396 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
397
398 // Iterate over the playlist entries
399 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
400 {
401 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
402 {
403 mlt_producer producer = mlt_producer_cut_parent( info.producer );
404 char *service_s = mlt_properties_get( MLT_PRODUCER_PROPERTIES( producer ), "mlt_service" );
405 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
406 {
407 char length[ 20 ];
408 length[ 19 ] = '\0';
409 xmlNode *entry = xmlNewChild( child, NULL, _x("blank"), NULL );
410 snprintf( length, 19, "%d", (int)info.frame_count );
411 xmlNewProp( entry, _x("length"), _x(length) );
412 }
413 else
414 {
415 char temp[ 20 ];
416 xmlNode *entry = xmlNewChild( child, NULL, _x("entry"), NULL );
417 id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
418 xmlNewProp( entry, _x("producer"), _x(id) );
419 sprintf( temp, "%d", (int)info.frame_in );
420 xmlNewProp( entry, _x("in"), _x(temp) );
421 sprintf( temp, "%d", (int)info.frame_out );
422 xmlNewProp( entry, _x("out"), _x(temp) );
423 if ( info.repeat > 1 )
424 {
425 sprintf( temp, "%d", info.repeat );
426 xmlNewProp( entry, _x("repeat"), _x(temp) );
427 }
428 if ( mlt_producer_is_cut( info.cut ) )
429 {
430 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, context->store );
431 serialise_store_properties( context, MLT_PRODUCER_PROPERTIES( info.cut ), entry, "meta." );
432 serialise_service_filters( context, MLT_PRODUCER_SERVICE( info.cut ), entry );
433 }
434 }
435 }
436 }
437
438 serialise_service_filters( context, service, child );
439 }
440 else if ( xmlStrcmp( node->name, _x("tractor") ) != 0 )
441 {
442 char *id = westley_get_id( context, service, westley_existing );
443 xmlNewProp( node, _x("producer"), _x(id) );
444 }
445 }
446
447 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
448 {
449 xmlNode *child = node;
450 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
451
452 if ( context->pass == 0 )
453 {
454 // Recurse on connected producer
455 serialise_service( context, mlt_service_producer( service ), node );
456 }
457 else
458 {
459 // Get a new id - if already allocated, do nothing
460 char *id = westley_get_id( context, service, westley_tractor );
461 if ( id == NULL )
462 return;
463
464 child = xmlNewChild( node, NULL, _x("tractor"), NULL );
465
466 // Set the id
467 xmlNewProp( child, _x("id"), _x(id) );
468 if ( mlt_properties_get( properties, "title" ) )
469 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
470 if ( mlt_properties_get( properties, "global_feed" ) )
471 xmlNewProp( child, _x("global_feed"), _x(mlt_properties_get( properties, "global_feed" )) );
472 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
473 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
474
475 // Store application specific properties
476 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, context->store );
477 serialise_store_properties( context, MLT_SERVICE_PROPERTIES( service ), child, "meta." );
478
479 // Recurse on connected producer
480 serialise_service( context, mlt_service_producer( service ), child );
481 serialise_service_filters( context, service, child );
482 }
483 }
484
485 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
486 {
487 xmlNode *child = node;
488 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
489
490 // Recurse on connected producer
491 serialise_service( context, mlt_service_producer( service ), node );
492
493 if ( context->pass == 1 )
494 {
495 // Get a new id - if already allocated, do nothing
496 char *id = westley_get_id( context, service, westley_filter );
497 if ( id == NULL )
498 return;
499
500 child = xmlNewChild( node, NULL, _x("filter"), NULL );
501
502 // Set the id
503 xmlNewProp( child, _x("id"), _x(id) );
504 if ( mlt_properties_get( properties, "title" ) )
505 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
506 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
507 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
508
509 serialise_properties( context, properties, child );
510 serialise_service_filters( context, service, child );
511 }
512 }
513
514 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
515 {
516 xmlNode *child = node;
517 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
518
519 // Recurse on connected producer
520 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
521
522 if ( context->pass == 1 )
523 {
524 // Get a new id - if already allocated, do nothing
525 char *id = westley_get_id( context, service, westley_transition );
526 if ( id == NULL )
527 return;
528
529 child = xmlNewChild( node, NULL, _x("transition"), NULL );
530
531 // Set the id
532 xmlNewProp( child, _x("id"), _x(id) );
533 if ( mlt_properties_get( properties, "title" ) )
534 xmlNewProp( child, _x("title"), _x(mlt_properties_get( properties, "title" )) );
535 xmlNewProp( child, _x("in"), _x(mlt_properties_get( properties, "in" )) );
536 xmlNewProp( child, _x("out"), _x(mlt_properties_get( properties, "out" )) );
537
538 serialise_properties( context, properties, child );
539 serialise_service_filters( context, service, child );
540 }
541 }
542
543 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
544 {
545 // Iterate over consumer/producer connections
546 while ( service != NULL )
547 {
548 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
549 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
550
551 // Tell about the producer
552 if ( strcmp( mlt_type, "producer" ) == 0 )
553 {
554 char *mlt_service = mlt_properties_get( properties, "mlt_service" );
555 if ( mlt_properties_get( properties, "westley" ) == NULL && ( mlt_service != NULL && !strcmp( mlt_service, "tractor" ) ) )
556 {
557 context->pass = 0;
558 serialise_tractor( context, service, node );
559 context->pass = 1;
560 serialise_tractor( context, service, node );
561 context->pass = 0;
562 break;
563 }
564 else
565 {
566 serialise_producer( context, service, node );
567 }
568 if ( mlt_properties_get( properties, "westley" ) != NULL )
569 break;
570 }
571
572 // Tell about the framework container producers
573 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
574 {
575 char *resource = mlt_properties_get( properties, "resource" );
576
577 // Recurse on multitrack's tracks
578 if ( strcmp( resource, "<multitrack>" ) == 0 )
579 {
580 serialise_multitrack( context, service, node );
581 break;
582 }
583
584 // Recurse on playlist's clips
585 else if ( strcmp( resource, "<playlist>" ) == 0 )
586 {
587 serialise_playlist( context, service, node );
588 }
589
590 // Recurse on tractor's producer
591 else if ( strcmp( resource, "<tractor>" ) == 0 )
592 {
593 context->pass = 0;
594 serialise_tractor( context, service, node );
595 context->pass = 1;
596 serialise_tractor( context, service, node );
597 context->pass = 0;
598 break;
599 }
600
601 // Treat it as a normal producer
602 else
603 {
604 serialise_producer( context, service, node );
605 }
606 }
607
608 // Tell about a filter
609 else if ( strcmp( mlt_type, "filter" ) == 0 )
610 {
611 serialise_filter( context, service, node );
612 break;
613 }
614
615 // Tell about a transition
616 else if ( strcmp( mlt_type, "transition" ) == 0 )
617 {
618 serialise_transition( context, service, node );
619 break;
620 }
621
622 // Get the next connected service
623 service = mlt_service_producer( service );
624 }
625 }
626
627 xmlDocPtr westley_make_doc( mlt_consumer consumer, mlt_service service )
628 {
629 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
630 xmlDocPtr doc = xmlNewDoc( _x("1.0") );
631 xmlNodePtr root = xmlNewNode( NULL, _x("westley") );
632 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
633
634 xmlDocSetRootElement( doc, root );
635
636 // If we have root, then deal with it now
637 if ( mlt_properties_get( properties, "root" ) != NULL )
638 {
639 xmlNewProp( root, _x("root"), _x(mlt_properties_get( properties, "root" )) );
640 context->root = strdup( mlt_properties_get( properties, "root" ) );
641 }
642 else
643 {
644 context->root = strdup( "" );
645 }
646
647 // Assign the additional 'storage' pattern for properties
648 context->store = mlt_properties_get( MLT_CONSUMER_PROPERTIES( consumer ), "store" );
649
650 // Assign a title property
651 if ( mlt_properties_get( properties, "title" ) != NULL )
652 xmlNewProp( root, _x("title"), _x(mlt_properties_get( properties, "title" )) );
653 mlt_properties_set_int( properties, "global_feed", 1 );
654
655 // Construct the context maps
656 context->id_map = mlt_properties_new();
657 context->hide_map = mlt_properties_new();
658
659 // Ensure producer is a framework producer
660 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "mlt_type", "mlt_producer" );
661
662 // In pass one, we serialise the end producers and playlists,
663 // adding them to a map keyed by address.
664 serialise_service( context, service, root );
665
666 // In pass two, we serialise the tractor and reference the
667 // producers and playlists
668 context->pass++;
669 serialise_service( context, service, root );
670
671 // Cleanup resource
672 mlt_properties_close( context->id_map );
673 mlt_properties_close( context->hide_map );
674 free( context->root );
675 free( context );
676
677 return doc;
678 }
679
680 static int consumer_start( mlt_consumer this )
681 {
682 xmlDocPtr doc = NULL;
683
684 // Get the producer service
685 mlt_service service = mlt_service_producer( MLT_CONSUMER_SERVICE( this ) );
686 if ( service != NULL )
687 {
688 mlt_properties properties = MLT_CONSUMER_PROPERTIES( this );
689 char *resource = mlt_properties_get( properties, "resource" );
690
691 // Set the title if provided
692 if ( mlt_properties_get( properties, "title" ) )
693 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", mlt_properties_get( properties, "title" ) );
694 else if ( mlt_properties_get( MLT_SERVICE_PROPERTIES( service ), "title" ) == NULL )
695 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "title", "Anonymous Submission" );
696
697 // Check for a root on the consumer properties and pass to service
698 if ( mlt_properties_get( properties, "root" ) )
699 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", mlt_properties_get( properties, "root" ) );
700
701 // Specify roots in other cases...
702 if ( resource != NULL && mlt_properties_get( properties, "root" ) == NULL )
703 {
704 // Get the current working directory
705 char *cwd = getcwd( NULL, 0 );
706 mlt_properties_set( MLT_SERVICE_PROPERTIES( service ), "root", cwd );
707 free( cwd );
708 }
709
710 // Make the document
711 doc = westley_make_doc( this, service );
712
713 // Handle the output
714 if ( resource == NULL || !strcmp( resource, "" ) )
715 {
716 xmlDocFormatDump( stdout, doc, 1 );
717 }
718 else if ( strchr( resource, '.' ) == NULL )
719 {
720 xmlChar *buffer = NULL;
721 int length = 0;
722 xmlDocDumpMemoryEnc( doc, &buffer, &length, "utf-8" );
723 mlt_properties_set( properties, resource, _s(buffer) );
724 xmlFree( buffer );
725 }
726 else
727 {
728 xmlSaveFormatFileEnc( resource, doc, "utf-8", 1 );
729 }
730
731 // Close the document
732 xmlFreeDoc( doc );
733 }
734
735 mlt_consumer_stop( this );
736
737 mlt_consumer_stopped( this );
738
739 return 0;
740 }
741
742 static int consumer_is_stopped( mlt_consumer this )
743 {
744 return 1;
745 }