ceba61b0853e6f0da35e51ad947698342965b093
[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 /** Forward references to static functions.
30 */
31
32 static int consumer_start( mlt_consumer parent );
33
34 /** This is what will be called by the factory - anything can be passed in
35 via the argument, but keep it simple.
36 */
37
38 mlt_consumer consumer_westley_init( char *arg )
39 {
40 // Create the consumer object
41 mlt_consumer this = calloc( sizeof( struct mlt_consumer_s ), 1 );
42
43 // If no malloc'd and consumer init ok
44 if ( this != NULL && mlt_consumer_init( this, NULL ) == 0 )
45 {
46 // We have stuff to clean up, so override the close method
47 //parent->close = consumer_close;
48
49 // Allow thread to be started/stopped
50 this->start = consumer_start;
51
52 mlt_properties_set( mlt_consumer_properties( this ), "resource", arg );
53
54 // Return the consumer produced
55 return this;
56 }
57
58 // malloc or consumer init failed
59 free( this );
60
61 // Indicate failure
62 return NULL;
63 }
64
65
66 // This maintains counters for adding ids to elements
67 struct serialise_context_s
68 {
69 int producer_count;
70 int multitrack_count;
71 int playlist_count;
72 int tractor_count;
73 int filter_count;
74 int transition_count;
75 };
76 typedef struct serialise_context_s* serialise_context;
77
78
79 static inline void serialise_properties( mlt_properties properties, xmlNode *node )
80 {
81 int i;
82 xmlNode *p;
83
84 // Enumerate the properties
85 for ( i = 0; i < mlt_properties_count( properties ); i++ )
86 {
87 if ( mlt_properties_get_value( properties, i ) != NULL )
88 {
89 #if 0
90 p = xmlNewChild( node, NULL, "prop", NULL );
91 #else
92 p = node;
93 #endif
94 xmlNewProp( p, mlt_properties_get_name( properties, i ), mlt_properties_get_value( properties, i ) );
95 }
96 }
97 }
98
99 static void serialise_service( serialise_context context, mlt_service service, xmlNode *node )
100 {
101 int i;
102 xmlNode *child = node;
103 char id[ 31 ];
104 id[ 30 ] = '\0';
105
106 // Iterate over consumer/producer connections
107 while ( service != NULL )
108 {
109 mlt_properties properties = mlt_service_properties( service );
110 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
111
112 // Tell about the producer
113 if ( strcmp( mlt_type, "producer" ) == 0 )
114 {
115 child = xmlNewChild( node, NULL, "producer", NULL );
116
117 // Set the id
118 if ( mlt_properties_get( properties, "id" ) == NULL )
119 {
120 snprintf( id, 30, "producer%d", context->producer_count++ );
121 xmlNewProp( child, "id", id );
122 }
123
124 serialise_properties( properties, child );
125 }
126
127 // Tell about the framework container producers
128 else if ( strcmp( mlt_type, "mlt_producer" ) == 0 )
129 {
130 // Recurse on multitrack's tracks
131 if ( strcmp( mlt_properties_get( properties, "resource" ), "<multitrack>" ) == 0 )
132 {
133 child = xmlNewChild( node, NULL, "multitrack", NULL );
134
135 // Set the id
136 if ( mlt_properties_get( properties, "id" ) == NULL )
137 {
138 snprintf( id, 30, "multitrack%d", context->multitrack_count++ );
139 xmlNewProp( child, "id", id );
140 }
141
142 // Iterate over the tracks
143 for ( i = 0; i < mlt_multitrack_count( MLT_MULTITRACK( service ) ); i++ )
144 {
145 xmlNode *track = xmlNewChild( child, NULL, "track", NULL );
146 serialise_service( context, MLT_SERVICE( mlt_multitrack_track( MLT_MULTITRACK( service ), i ) ), track );
147 }
148 break;
149 }
150
151 // Recurse on playlist's clips
152 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<playlist>" ) == 0 )
153 {
154 mlt_playlist_clip_info info;
155 child = xmlNewChild( node, NULL, "playlist", NULL );
156
157 // Set the id
158 if ( mlt_properties_get( properties, "id" ) == NULL )
159 {
160 snprintf( id, 30, "playlist%d", context->playlist_count++ );
161 xmlNewProp( child, "id", id );
162 }
163
164 xmlNewProp( child, "in", mlt_properties_get( properties, "in" ) );
165 xmlNewProp( child, "out", mlt_properties_get( properties, "out" ) );
166
167 // Iterate over the playlist entries
168 for ( i = 0; i < mlt_playlist_count( MLT_PLAYLIST( service ) ); i++ )
169 {
170 if ( ! mlt_playlist_get_clip_info( MLT_PLAYLIST( service ), &info, i ) )
171 {
172 if ( strcmp( mlt_properties_get( mlt_producer_properties( info.producer ), "mlt_service" ), "blank" ) == 0 )
173 {
174 char length[ 20 ];
175 length[ 19 ] = '\0';
176 xmlNode *entry = xmlNewChild( child, NULL, "blank", NULL );
177 snprintf( length, 19, "%lld", info.frame_count );
178 xmlNewProp( entry, "length", length );
179 }
180 else
181 {
182 xmlNode *entry = xmlNewChild( child, NULL, "entry", NULL );
183 serialise_service( context, MLT_SERVICE( info.producer ), entry );
184 }
185 }
186 }
187 }
188
189 // Recurse on tractor's producer
190 else if ( strcmp( mlt_properties_get( properties, "resource" ), "<tractor>" ) == 0 )
191 {
192 child = xmlNewChild( node, NULL, "tractor", NULL );
193
194 // Set the id
195 if ( mlt_properties_get( properties, "id" ) == NULL )
196 {
197 snprintf( id, 30, "tractor%d", context->tractor_count++ );
198 xmlNewProp( child, "id", id );
199 }
200
201 // Recurse on connected producer
202 serialise_service( context, mlt_service_get_producer( service ), child );
203
204 break;
205 }
206 }
207
208 // Tell about a filter
209 else if ( strcmp( mlt_type, "filter" ) == 0 )
210 {
211 // Recurse on connected producer
212 serialise_service( context, MLT_SERVICE( MLT_FILTER( service )->producer ), node );
213
214 child = xmlNewChild( node, NULL, "filter", NULL );
215
216 // Set the id
217 if ( mlt_properties_get( properties, "id" ) == NULL )
218 {
219 snprintf( id, 30, "filter%d", context->filter_count++ );
220 xmlNewProp( child, "id", id );
221 }
222
223 serialise_properties( properties, child );
224
225 break;
226 }
227
228 // Tell about a transition
229 else if ( strcmp( mlt_type, "transition" ) == 0 )
230 {
231 // Recurse on connected producer
232 serialise_service( context, MLT_SERVICE( MLT_TRANSITION( service )->producer ), node );
233
234 child = xmlNewChild( node, NULL, "transition", NULL );
235
236 // Set the id
237 if ( mlt_properties_get( properties, "id" ) == NULL )
238 {
239 snprintf( id, 30, "transition%d", context->transition_count++ );
240 xmlNewProp( child, "id", id );
241 }
242
243 serialise_properties( properties, child );
244
245 break;
246 }
247
248 // Get the next connected service
249 service = mlt_service_get_producer( service );
250 }
251 }
252
253 static int consumer_start( mlt_consumer this )
254 {
255 mlt_service inigo = NULL;
256 xmlDoc *doc = xmlNewDoc( "1.0" );
257 xmlNode *root = xmlNewNode( NULL, "westley" );
258 xmlDocSetRootElement( doc, root );
259
260 // Get the producer service
261 mlt_service service = mlt_service_get_producer( mlt_consumer_service( this ) );
262 if ( service != NULL )
263 {
264 struct serialise_context_s context;
265 memset( &context, 0, sizeof( struct serialise_context_s ) );
266
267 // Remember inigo
268 if ( mlt_properties_get( mlt_service_properties( service ), "mlt_service" ) != NULL &&
269 strcmp( mlt_properties_get( mlt_service_properties( service ), "mlt_service" ), "inigo" ) == 0 )
270 inigo = service;
271
272 // Ensure producer is a framework producer
273 mlt_properties_set( mlt_service_properties( service ), "mlt_type", "mlt_producer" );
274
275 serialise_service( &context, service, root );
276
277 if ( mlt_properties_get( mlt_consumer_properties( this ), "resource" ) == NULL )
278 xmlDocFormatDump( stderr, doc, 1 );
279 else
280 xmlSaveFormatFile( mlt_properties_get( mlt_consumer_properties( this ), "resource" ), doc, 1 );
281 }
282
283 xmlFreeDoc( doc );
284 mlt_consumer_stop( this );
285
286 // Tell inigo, enough already!
287 if ( inigo != NULL )
288 mlt_properties_set_int( mlt_service_properties( inigo ), "done", 1 );
289
290 return 0;
291 }
292