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