]> git.sesse.net Git - mlt/blob - src/modules/westley/consumer_westley.c
Mlt Ref Counts and Playlist split/join
[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 <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         int producer_count;
35         int multitrack_count;
36         int playlist_count;
37         int tractor_count;
38         int filter_count;
39         int transition_count;
40         int pass;
41         mlt_properties producer_map;
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 /** This is what will be called by the factory - anything can be passed in
54         via the argument, but keep it simple.
55 */
56
57 mlt_consumer consumer_westley_init( char *arg )
58 {
59         // Create the consumer object
60         mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
61
62         // If no malloc'd and consumer init ok
63         if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
64         {
65                 // Allow thread to be started/stopped
66                 this->start = consumer_start;
67                 this->is_stopped = consumer_is_stopped;
68
69                 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg );
70
71                 // Return the consumer produced
72                 return this;
73         }
74
75         // malloc or consumer init failed
76         free( this );
77
78         // Indicate failure
79         return NULL;
80 }
81
82 static inline void serialise_properties( mlt_properties properties, xmlNode *node )
83 {
84         int i;
85         xmlNode *p;
86         
87         // Enumerate the properties
88         for ( i = 0; i < mlt_properties_count( properties ); i++ )
89         {
90                 char *name = mlt_properties_get_name( properties, i );
91                 if ( name != NULL &&
92                          name[ 0 ] != '_' &&
93                          mlt_properties_get_value( properties, i ) != NULL &&
94                          strcmp( name, "westley" ) != 0 &&
95                          strcmp( name, "in" ) != 0 &&
96                          strcmp( name, "out" ) != 0 )
97                 {
98                         p = xmlNewChild( node, NULL, "property", NULL );
99                         xmlNewProp( p, "name", mlt_properties_get_name( properties, i ) );
100                         xmlNodeSetContent( p, mlt_properties_get_value( properties, i ) );
101                 }
102         }
103 }
104
105 static void serialise_producer( serialise_context context, mlt_service service, xmlNode *node )
106 {
107         xmlNode *child = node;
108         char id[ ID_SIZE + 1 ];
109         char key[ 11 ];
110         mlt_properties properties = mlt_service_properties( service );
111         
112         id[ ID_SIZE ] = '\0';
113         key[ 10 ] = '\0';
114         
115         if ( context->pass == 0 )
116         {
117                 child = xmlNewChild( node, NULL, "producer", NULL );
118
119                 // Set the id
120                 if ( mlt_properties_get( properties, "id" ) == NULL )
121                 {
122                         snprintf( id, ID_SIZE, "producer%d", context->producer_count++ );
123                         xmlNewProp( child, "id", id );
124                 }
125                 else
126                         strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
127                 serialise_properties( properties, child );
128
129                 // Add producer to the map
130                 snprintf( key, 10, "%p", service );
131                 mlt_properties_set( context->producer_map, key, id );
132                 mlt_properties_set_int( context->hide_map, key, mlt_properties_get_int( properties, "hide" ) );
133         }
134         else
135         {
136                 snprintf( key, 10, "%p", service );
137                 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
138         }
139 }
140
141 static void serialise_multitrack( serialise_context context, mlt_service service, xmlNode *node )
142 {
143         int i;
144         xmlNode *child = node;
145         char id[ ID_SIZE + 1 ];
146         char key[ 11 ];
147         mlt_properties properties = mlt_service_properties( service );
148         
149         id[ ID_SIZE ] = '\0';
150         key[ 10 ] = '\0';
151
152         if ( context->pass == 0 )
153         {
154                 // Iterate over the tracks to collect the producers
155                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
156                         serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), node );
157         }
158         else
159         {
160                 // Create the multitrack node
161                 child = xmlNewChild( node, NULL, "multitrack", NULL );
162                 
163                 // Set the id
164                 if ( mlt_properties_get( properties, "id" ) == NULL )
165                 {
166                         snprintf( id, ID_SIZE, "multitrack%d", context->multitrack_count++ );
167                         xmlNewProp( child, "id", id );
168                 }
169
170                 // Serialise the tracks
171                 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
172                 {
173                         xmlNode *track = xmlNewChild( child, NULL, "track", NULL );
174                         int hide = 0;
175                                                 
176                         snprintf( key, 10, "%p", MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ) );
177                         xmlNewProp( track, "producer", mlt_properties_get( context->producer_map, key ) );
178                         
179                         hide = mlt_properties_get_int( context->hide_map, key );
180                         if ( hide )
181                                 xmlNewProp( track, "hide", hide == 1 ? "video" : ( hide == 2 ? "audio" : "both" ) );
182                 }
183         }
184 }
185
186 static void serialise_playlist( serialise_context context, mlt_service service, xmlNode *node )
187 {
188         int i;
189         xmlNode *child = node;
190         char id[ ID_SIZE + 1 ];
191         char key[ 11 ];
192         mlt_playlist_clip_info info;
193         mlt_properties properties = mlt_service_properties( service );
194         
195         id[ ID_SIZE ] = '\0';
196         key[ 10 ] = '\0';
197
198         if ( context->pass == 0 )
199         {
200                 // Iterate over the playlist entries to collect the producers
201                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
202                 {
203                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
204                         {
205                                 if ( info.producer != NULL )
206                                 {
207                                         char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
208                                         char *resource_s = mlt_properties_get( mlt_producer_properties( info.producer ), "resource" );
209                                         if ( service_s != NULL && strcmp( service_s, "blank" ) != 0 )
210                                                 serialise_service( context, MLT_SERVICE( info.producer ), node );
211                                         else if ( resource_s != NULL && !strcmp( resource_s, "<playlist>" ) )
212                                                 serialise_playlist( context, MLT_SERVICE( info.producer ), node );
213                                 }
214                         }
215                 }
216                 
217                 child = xmlNewChild( node, NULL, "playlist", NULL );
218
219                 // Set the id
220                 if ( mlt_properties_get( properties, "id" ) == NULL )
221                 {
222                         snprintf( id, ID_SIZE, "playlist%d", context->playlist_count++ );
223                         xmlNewProp( child, "id", id );
224                 }
225                 else
226                         strncpy( id, mlt_properties_get( properties, "id" ), ID_SIZE );
227
228                 // Add producer to the map
229                 snprintf( key, 10, "%p", service );
230                 mlt_properties_set( context->producer_map, key, id );
231                 mlt_properties_set_int( context->hide_map, key, mlt_properties_get_int( properties, "hide" ) );
232         
233                 // Iterate over the playlist entries
234                 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
235                 {
236                         if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
237                         {
238                                 char *service_s = mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" );
239                                 if ( service_s != NULL && strcmp( service_s, "blank" ) == 0 )
240                                 {
241                                         char length[ 20 ];
242                                         length[ 19 ] = '\0';
243                                         xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
244                                         snprintf( length, 19, "%d", info.frame_count );
245                                         xmlNewProp( entry, "length", length );
246                                 }
247                                 else
248                                 {
249                                         char temp[ 20 ];
250                                         xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
251                                         snprintf( key, 10, "%p", MLT_SERVICE( info.producer ) );
252                                         xmlNewProp( entry, "producer", mlt_properties_get( context->producer_map, key ) );
253                                         sprintf( temp, "%d", info.frame_in );
254                                         xmlNewProp( entry, "in", temp );
255                                         sprintf( temp, "%d", info.frame_out );
256                                         xmlNewProp( entry, "out", temp );
257                                 }
258                         }
259                 }
260         }
261         else if ( strcmp( (const char*) node->name, "tractor" ) != 0 )
262         {
263                 snprintf( key, 10, "%p", service );
264                 xmlNewProp( node, "producer", mlt_properties_get( context->producer_map, key ) );
265         }
266 }
267
268 static void serialise_tractor( serialise_context context, mlt_service service, xmlNode *node )
269 {
270         xmlNode *child = node;
271         char id[ ID_SIZE + 1 ];
272         mlt_properties properties = mlt_service_properties( service );
273         
274         id[ ID_SIZE ] = '\0';
275         
276         if ( context->pass == 0 )
277         {
278                 // Recurse on connected producer
279                 serialise_service( context, mlt_service_producer( service ), node );
280         }
281         else
282         {
283                 child = xmlNewChild( node, NULL, "tractor", NULL );
284
285                 // Set the id
286                 if ( mlt_properties_get( properties, "id" ) == NULL )
287                 {
288                         snprintf( id, ID_SIZE, "tractor%d", context->tractor_count++ );
289                         xmlNewProp( child, "id", id );
290                 }
291                 
292                 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
293                 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
294
295                 // Recurse on connected producer
296                 serialise_service( context, mlt_service_producer( service ), child );
297         }
298 }
299
300 static void serialise_filter( serialise_context context, mlt_service service, xmlNode *node )
301 {
302         xmlNode *child = node;
303         char id[ ID_SIZE + 1 ];
304         mlt_properties properties = mlt_service_properties( service );
305         
306         id[ ID_SIZE ] = '\0';
307         
308         // Recurse on connected producer
309         serialise_service( context, mlt_service_producer( service ), node );
310
311         if ( context->pass == 1 )
312         {
313                 child = xmlNewChild( node, NULL, "filter", NULL );
314
315                 // Set the id
316                 if ( mlt_properties_get( properties, "id" ) == NULL )
317                 {
318                         snprintf( id, ID_SIZE, "filter%d", context->filter_count++ );
319                         xmlNewProp( child, "id", id );
320                         xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
321                         xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
322                 }
323
324                 serialise_properties( properties, child );
325         }
326 }
327
328 static void serialise_transition( serialise_context context, mlt_service service, xmlNode *node )
329 {
330         xmlNode *child = node;
331         char id[ ID_SIZE + 1 ];
332         mlt_properties properties = mlt_service_properties( service );
333         
334         id[ ID_SIZE ] = '\0';
335         
336         // Recurse on connected producer
337         serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
338
339         if ( context->pass == 1 )
340         {
341                 child = xmlNewChild( node, NULL, "transition", NULL );
342         
343                 // Set the id
344                 if ( mlt_properties_get( properties, "id" ) == NULL )
345                 {
346                         snprintf( id, ID_SIZE, "transition%d", context->transition_count++ );
347                         xmlNewProp( child, "id", id );
348                         xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
349                         xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
350                 }
351
352                 serialise_properties( properties, child );
353         }
354 }
355
356 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
357 {
358         // Iterate over consumer/producer connections
359         while ( service != NULL )
360         {
361                 mlt_properties properties = mlt_service_properties( service );
362                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
363                 
364                 // Tell about the producer
365                 if ( strcmp( mlt_type, "producer" ) == 0 )
366                 {
367                         serialise_producer( context, service, node );
368                         if ( mlt_properties_get( properties, "westley" ) != NULL )
369                                 break;
370                 }
371
372                 // Tell about the framework container producers
373                 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
374                 {
375                         char *resource = mlt_properties_get( properties, "resource" );
376                         
377                         // Recurse on multitrack's tracks
378                         if ( strcmp( resource, "<multitrack>" ) == 0 )
379                         {
380                                 serialise_multitrack( context, service, node );
381                                 break;
382                         }
383                         
384                         // Recurse on playlist's clips
385                         else if ( strcmp( resource, "<playlist>" ) == 0 )
386                         {
387                                 serialise_playlist( context, service, node );
388                         }
389                         
390                         // Recurse on tractor's producer
391                         else if ( strcmp( resource, "<tractor>" ) == 0 )
392                         {
393                                 serialise_tractor( context, service, node );
394                                 break;
395                         }
396                 }
397                 
398                 // Tell about a filter
399                 else if ( strcmp( mlt_type, "filter" ) == 0 )
400                 {
401                         serialise_filter( context, service, node );
402                         break;
403                 }
404                 
405                 // Tell about a transition
406                 else if ( strcmp( mlt_type, "transition" ) == 0 )
407                 {
408                         serialise_transition( context, service, node );
409                         break;
410                 }
411                 
412                 // Get the next connected service
413                 service = mlt_service_producer( service );
414         }
415 }
416
417 xmlDocPtr westley_make_doc( mlt_service service )
418 {
419         xmlDocPtr doc = xmlNewDoc( "1.0" );
420         xmlNodePtr root = xmlNewNode( NULL, "westley" );
421         struct serialise_context_s *context = calloc( 1, sizeof( struct serialise_context_s ) );
422         
423         xmlDocSetRootElement( doc, root );
424                 
425         // Construct the context maps
426         context->producer_map = mlt_properties_new();
427         context->hide_map = mlt_properties_new();
428         
429         // Ensure producer is a framework producer
430         mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
431
432         // In pass one, we serialise the end producers and playlists,
433         // adding them to a map keyed by address.
434         serialise_service( context, service, root );
435
436         // In pass two, we serialise the tractor and reference the
437         // producers and playlists
438         context->pass++;
439         serialise_service( context, service, root );
440
441         // Cleanup resource
442         mlt_properties_close( context->producer_map );
443         mlt_properties_close( context->hide_map );
444         free( context );
445         
446         return doc;
447 }
448
449
450 static int consumer_start( mlt_consumer this )
451 {
452         xmlDocPtr doc = NULL;
453         
454         // Get the producer service
455         mlt_service service = mlt_service_producer( mlt_consumer_service( this ) );
456         if ( service != NULL )
457         {
458                 doc = westley_make_doc( service );
459                 
460                 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL )
461                         xmlDocFormatDump( stdout, doc, 1 );
462                 else
463                         xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc, 1 );
464                 
465                 xmlFreeDoc( doc );
466         }
467         
468         mlt_consumer_stop( this );
469
470         return 0;
471 }
472
473 static int consumer_is_stopped( mlt_consumer this )
474 {
475         return 1;
476 }