Adding the mix part 1
[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 strcmp( name, "id" ) != 0 &&
184 strcmp( name, "width" ) != 0 &&
185 strcmp( name, "height" ) != 0 )
186 {
187 p = xmlNewChild( node, NULL, "property", NULL );
188 xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
189 xmlNodeSetContent( p, mlt_properties_get_value( properties, i ) );
190 }
191 }
192 }
193
194 static inline void serialise_service_filters( serialise_context context, mlt_service service, xmlNode *node )
195 {
196 int i;
197 xmlNode *p;
198 mlt_filter filter = NULL;
199
200 // Enumerate the filters
201 for ( i = 0; ( filter = mlt_producer_filter( MLT_PRODUCER( service ), i ) ) != NULL; i ++ )
202 {
203 mlt_properties properties = mlt_filter_properties( filter );
204 if ( mlt_properties_get_int( properties, "_fezzik" ) == 0 )
205 {
206 // Get a new id - if already allocated, do nothing
207 char *id = westley_get_id( context, mlt_filter_service( filter ), westley_filter );
208 if ( id != NULL )
209 {
210 p = xmlNewChild( node, NULL, "filter", NULL );
211 xmlNewProp( p, "id", id );
212 serialise_properties( properties, p );
213 serialise_service_filters( context, mlt_filter_service( filter ), p );
214 }
215 }
216 }
217 }
218
219 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
220 {
221 xmlNode *child = node;
222 mlt_properties properties = mlt_service_properties( service );
223
224 if ( context->pass == 0 )
225 {
226 // Get a new id - if already allocated, do nothing
227 char *id = westley_get_id( context, service, westley_producer );
228 if ( id == NULL )
229 return;
230
231 child = xmlNewChild( node, NULL, "producer", NULL );
232
233 // Set the id
234 xmlNewProp( child, "id", id );
235 serialise_properties( properties, child );
236 serialise_service_filters( context, service, child );
237
238 // Add producer to the map
239 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
240 }
241 else
242 {
243 // Get a new id - if already allocated, do nothing
244 char *id = westley_get_id( context, service, westley_existing );
245 xmlNewProp( node, "producer", id );
246 }
247 }
248
249 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
250 {
251 int i;
252
253 if ( context->pass == 0 )
254 {
255 // Iterate over the tracks to collect the producers
256 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
257 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
258 }
259 else
260 {
261 // Get a new id - if already allocated, do nothing
262 char *id = westley_get_id( context, service, westley_multitrack );
263 if ( id == NULL )
264 return;
265
266 // Serialise the tracks
267 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
268 {
269 xmlNode *track = xmlNewChild( node, NULL, "track", NULL );
270 int hide = 0;
271 mlt_producer producer = mlt_multitrack_track( MLT_MULTITRACK( service ), i );
272
273 char *id = westley_get_id( context, MLT_SERVICE( producer ), westley_existing );
274 xmlNewProp( track, "producer", id );
275
276 hide = mlt_properties_get_int( context->hide_map, id );
277 if ( hide )
278 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
279 }
280 serialise_service_filters( context, service, node );
281 }
282 }
283
284 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node );
285
286 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
287 {
288 int i;
289 xmlNode *child = node;
290 mlt_playlist_clip_info info;
291 mlt_properties properties = mlt_service_properties( service );
292
293 if ( context->pass == 0 )
294 {
295 // Get a new id - if already allocated, do nothing
296 char *id = westley_get_id( context, service, westley_playlist );
297 if ( id == NULL )
298 return;
299
300 // Iterate over the playlist entries to collect the producers
301 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
302 {
303 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
304 {
305 if ( info.producer != NULL )
306 {
307 char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
308 char *resource_s = mlt_properties_get( mlt_producer_properties( info.producer ), "resource" );
309 if ( resource_s != NULL && !strcmp( resource_s, "<tractor>" ) )
310 {
311 serialise_tractor( context, MLT_SERVICE( info.producer ), node );
312 context->pass ++;
313 serialise_tractor( context, MLT_SERVICE( info.producer ), node );
314 context->pass --;
315 }
316 else if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
317 serialise_service( context, MLT_SERVICE( info.producer ), node );
318 else if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
319 serialise_playlist( context, MLT_SERVICE( info.producer ), node );
320 }
321 }
322 }
323
324 child = xmlNewChild( node, NULL, "playlist", NULL );
325
326 // Set the id
327 xmlNewProp( child, "id", id );
328
329 // Add producer to the map
330 mlt_properties_set_int( context->hide_map, id, mlt_properties_get_int( properties, "hide" ) );
331
332 // Iterate over the playlist entries
333 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
334 {
335 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
336 {
337 char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
338 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
339 {
340 char length[ 20 ];
341 length[ 19 ] = '\0';
342 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
343 snprintf( length, 19, "%d", info.frame_count );
344 xmlNewProp( entry, "length", length );
345 }
346 else
347 {
348 char temp[ 20 ];
349 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
350 id = westley_get_id( context, MLT_SERVICE( info.producer ), westley_existing );
351 xmlNewProp( entry, "producer", id );
352 sprintf( temp, "%d", info.frame_in );
353 xmlNewProp( entry, "in", temp );
354 sprintf( temp, "%d", info.frame_out );
355 xmlNewProp( entry, "out", temp );
356 }
357 }
358 }
359
360 serialise_service_filters( context, service, child );
361 }
362 else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
363 {
364 char *id = westley_get_id( context, service, westley_existing );
365 xmlNewProp( node, "producer", id );
366 }
367 }
368
369 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
370 {
371 xmlNode *child = node;
372 mlt_properties properties = mlt_service_properties( service );
373
374 if ( context->pass == 0 )
375 {
376 // Recurse on connected producer
377 serialise_service( context, mlt_service_producer( service ), node );
378 }
379 else
380 {
381 // Get a new id - if already allocated, do nothing
382 char *id = westley_get_id( context, service, westley_tractor );
383 if ( id == NULL )
384 return;
385
386 child = xmlNewChild( node, NULL, "tractor", NULL );
387
388 // Set the id
389 xmlNewProp( child, "id", id );
390 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
391 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
392
393 // Recurse on connected producer
394 serialise_service( context, mlt_service_producer( service ), child );
395 serialise_service_filters( context, service, child );
396 }
397 }
398
399 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
400 {
401 xmlNode *child = node;
402 mlt_properties properties = mlt_service_properties( service );
403
404 // Recurse on connected producer
405 serialise_service( context, mlt_service_producer( service ), node );
406
407 if ( context->pass == 1 )
408 {
409 // Get a new id - if already allocated, do nothing
410 char *id = westley_get_id( context, service, westley_filter );
411 if ( id == NULL )
412 return;
413
414 child = xmlNewChild( node, NULL, "filter", NULL );
415
416 // Set the id
417 xmlNewProp( child, "id", id );
418 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
419 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
420
421 serialise_properties( properties, child );
422 serialise_service_filters( context, service, child );
423 }
424 }
425
426 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
427 {
428 xmlNode *child = node;
429 mlt_properties properties = mlt_service_properties( service );
430
431 // Recurse on connected producer
432 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
433
434 if ( context->pass == 1 )
435 {
436 // Get a new id - if already allocated, do nothing
437 char *id = westley_get_id( context, service, westley_transition );
438 if ( id == NULL )
439 return;
440
441 child = xmlNewChild( node, NULL, "transition", NULL );
442
443 // Set the id
444 xmlNewProp( child, "id", id );
445 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
446 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
447
448 serialise_properties( properties, child );
449 serialise_service_filters( context, service, child );
450 }
451 }
452
453 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
454 {
455 // Iterate over consumer/producer connections
456 while ( service != NULL )
457 {
458 mlt_properties properties = mlt_service_properties( service );
459 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
460
461 // Tell about the producer
462 if ( strcmp( mlt_type, "producer" ) == 0 )
463 {
464 serialise_producer( context, service, node );
465 if ( mlt_properties_get( properties, "westley" ) != NULL )
466 break;
467 }
468
469 // Tell about the framework container producers
470 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
471 {
472 char *resource = mlt_properties_get( properties, "resource" );
473
474 // Recurse on multitrack's tracks
475 if ( strcmp( resource, "<multitrack>" ) == 0 )
476 {
477 serialise_multitrack( context, service, node );
478 break;
479 }
480
481 // Recurse on playlist's clips
482 else if ( strcmp( resource, "<playlist>" ) == 0 )
483 {
484 serialise_playlist( context, service, node );
485 }
486
487 // Recurse on tractor's producer
488 else if ( strcmp( resource, "<tractor>" ) == 0 )
489 {
490 serialise_tractor( context, service, node );
491 break;
492 }
493
494 // Treat it as a normal producer
495 else
496 {
497 serialise_producer( context, service, node );
498 }
499 }
500
501 // Tell about a filter
502 else if ( strcmp( mlt_type, "filter" ) == 0 )
503 {
504 serialise_filter( context, service, node );
505 break;
506 }
507
508 // Tell about a transition
509 else if ( strcmp( mlt_type, "transition" ) == 0 )
510 {
511 serialise_transition( context, service, node );
512 break;
513 }
514
515 // Get the next connected service
516 service = mlt_service_producer( service );
517 }
518 }
519
520 xmlDocPtr westley_make_doc( mlt_service service )
521 {
522 xmlDocPtr doc = xmlNewDoc( "1.0" );
523 xmlNodePtr root = xmlNewNode( NULL, "westley" );
524 struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
525
526 xmlDocSetRootElement( doc, root );
527
528 // Construct the context maps
529 context->id_map = mlt_properties_new();
530 context->hide_map = mlt_properties_new();
531
532 // Ensure producer is a framework producer
533 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
534
535 // In pass one, we serialise the end producers and playlists,
536 // adding them to a map keyed by address.
537 serialise_service( context, service, root );
538
539 // In pass two, we serialise the tractor and reference the
540 // producers and playlists
541 context->pass++;
542 serialise_service( context, service, root );
543
544 // Cleanup resource
545 mlt_properties_close( context->id_map );
546 mlt_properties_close( context->hide_map );
547 free( context );
548
549 return doc;
550 }
551
552
553 static int consumer_start( mlt_consumer this )
554 {
555 xmlDocPtr doc = NULL;
556
557 // Get the producer service
558 mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
559 if ( service != NULL )
560 {
561 char *resource = mlt_properties_get( mlt_consumer_properties( this ), "resource" );
562
563 doc = westley_make_doc( service );
564
565 if ( resource == NULL || !strcmp( resource, "" ) )
566 xmlDocFormatDump( stdout, doc, 1 );
567 else
568 xmlSaveFormatFile( resource, doc, 1 );
569
570 xmlFreeDoc( doc );
571 }
572
573 mlt_consumer_stop( this );
574
575 mlt_consumer_stopped( this );
576
577 return 0;
578 }
579
580 static int consumer_is_stopped( mlt_consumer this )
581 {
582 return 1;
583 }