Filter attachments to services
[melted] / src / framework / mlt_service.c
1 /*
2 * mlt_service.c -- interface for all service classes
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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 "config.h"
22 #include "mlt_service.h"
23 #include "mlt_filter.h"
24 #include "mlt_frame.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** IMPORTANT NOTES
30
31 The base service implements a null frame producing service - as such,
32 it is functional without extension and will produce test cards frames
33 and PAL sized audio frames.
34
35 PLEASE DO NOT CHANGE THIS BEHAVIOUR!!! OVERRIDE THE METHODS THAT
36 CONTROL THIS IN EXTENDING CLASSES.
37 */
38
39 /** Private service definition.
40 */
41
42 typedef struct
43 {
44 int size;
45 int count;
46 mlt_service *in;
47 mlt_service out;
48 int filter_count;
49 int filter_size;
50 mlt_filter *filters;
51 }
52 mlt_service_base;
53
54 /** Private methods
55 */
56
57 static void mlt_service_disconnect( mlt_service this );
58 static void mlt_service_connect( mlt_service this, mlt_service that );
59 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
60 static void mlt_service_property_changed( mlt_listener, mlt_properties owner, mlt_service this, void **args );
61
62 /** Constructor
63 */
64
65 int mlt_service_init( mlt_service this, void *child )
66 {
67 int error = 0;
68
69 // Initialise everything to NULL
70 memset( this, 0, sizeof( struct mlt_service_s ) );
71
72 // Assign the child
73 this->child = child;
74
75 // Generate local space
76 this->local = calloc( sizeof( mlt_service_base ), 1 );
77
78 // Associate the methods
79 this->get_frame = service_get_frame;
80
81 // Initialise the properties
82 error = mlt_properties_init( &this->parent, this );
83 if ( error == 0 )
84 {
85 this->parent.close = ( mlt_destructor )mlt_service_close;
86 this->parent.close_object = this;
87
88 mlt_events_init( &this->parent );
89 mlt_events_register( &this->parent, "property-changed", ( mlt_transmitter )mlt_service_property_changed );
90 }
91
92 return error;
93 }
94
95 static void mlt_service_property_changed( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
96 {
97 if ( listener != NULL )
98 listener( owner, this, ( char * )args[ 0 ] );
99 }
100
101 /** Connect a producer service.
102 Returns: > 0 warning, == 0 success, < 0 serious error
103 1 = this service does not accept input
104 2 = the producer is invalid
105 3 = the producer is already registered with this consumer
106 */
107
108 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
109 {
110 int i = 0;
111
112 // Get the service base
113 mlt_service_base *base = this->local;
114
115 // Check if the producer is already registered with this service
116 for ( i = 0; i < base->count; i ++ )
117 if ( base->in[ i ] == producer )
118 return 3;
119
120 // Allocate space
121 if ( index >= base->size )
122 {
123 int new_size = base->size + index + 10;
124 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
125 if ( base->in != NULL )
126 {
127 for ( i = base->size; i < new_size; i ++ )
128 base->in[ i ] = NULL;
129 base->size = new_size;
130 }
131 }
132
133 // If we have space, assign the input
134 if ( base->in != NULL && index >= 0 && index < base->size )
135 {
136 // Get the current service
137 mlt_service current = base->in[ index ];
138
139 // Increment the reference count on this producer
140 if ( producer != NULL )
141 mlt_properties_inc_ref( mlt_service_properties( producer ) );
142
143 // Now we disconnect the producer service from its consumer
144 mlt_service_disconnect( producer );
145
146 // Add the service to index specified
147 base->in[ index ] = producer;
148
149 // Determine the number of active tracks
150 if ( index >= base->count )
151 base->count = index + 1;
152
153 // Now we connect the producer to its connected consumer
154 mlt_service_connect( producer, this );
155
156 // Close the current service
157 mlt_service_close( current );
158
159 // Inform caller that all went well
160 return 0;
161 }
162 else
163 {
164 return -1;
165 }
166 }
167
168 /** Disconnect this service from its consumer.
169 */
170
171 static void mlt_service_disconnect( mlt_service this )
172 {
173 if ( this != NULL )
174 {
175 // Get the service base
176 mlt_service_base *base = this->local;
177
178 // Disconnect
179 base->out = NULL;
180 }
181 }
182
183 /** Obtain the consumer this service is connected to.
184 */
185
186 mlt_service mlt_service_consumer( mlt_service this )
187 {
188 // Get the service base
189 mlt_service_base *base = this->local;
190
191 // Return the connected consumer
192 return base->out;
193 }
194
195 /** Obtain the producer this service is connected to.
196 */
197
198 mlt_service mlt_service_producer( mlt_service this )
199 {
200 // Get the service base
201 mlt_service_base *base = this->local;
202
203 // Return the connected producer
204 return base->count > 0 ? base->in[ base->count - 1 ] : NULL;
205 }
206
207 /** Associate this service to the consumer.
208 */
209
210 static void mlt_service_connect( mlt_service this, mlt_service that )
211 {
212 if ( this != NULL )
213 {
214 // Get the service base
215 mlt_service_base *base = this->local;
216
217 // There's a bit more required here...
218 base->out = that;
219 }
220 }
221
222 /** Get the first connected producer service.
223 */
224
225 mlt_service mlt_service_get_producer( mlt_service this )
226 {
227 mlt_service producer = NULL;
228
229 // Get the service base
230 mlt_service_base *base = this->local;
231
232 if ( base->in != NULL )
233 producer = base->in[ 0 ];
234
235 return producer;
236 }
237
238 /** Default implementation of get_frame.
239 */
240
241 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
242 {
243 mlt_service_base *base = this->local;
244 if ( index < base->count )
245 {
246 mlt_service producer = base->in[ index ];
247 if ( producer != NULL )
248 return mlt_service_get_frame( producer, frame, index );
249 }
250 *frame = mlt_frame_init( );
251 return 0;
252 }
253
254 /** Return the properties object.
255 */
256
257 mlt_properties mlt_service_properties( mlt_service self )
258 {
259 return self != NULL ? &self->parent : NULL;
260 }
261
262 /** Recursively apply attached filters
263 */
264
265 static void apply_filters( mlt_service this, mlt_frame frame, int index )
266 {
267 mlt_properties properties = mlt_service_properties( this );
268 mlt_service_base *base = this->local;
269 int i;
270
271 if ( mlt_properties_get_int( properties, "_filter_private" ) == 0 )
272 {
273 // Process the frame with the attached filters
274 for ( i = 0; i < base->filter_count; i ++ )
275 {
276 if ( base->filters[ i ] != NULL )
277 {
278 mlt_filter_process( base->filters[ i ], frame );
279 apply_filters( mlt_filter_service( base->filters[ i ] ), frame, index );
280 }
281 }
282 }
283 }
284
285 /** Obtain a frame.
286 */
287
288 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
289 {
290 if ( this != NULL )
291 {
292 int result = this->get_frame( this, frame, index );
293 if ( result == 0 )
294 apply_filters( this, *frame, index );
295 return result;
296 }
297 *frame = mlt_frame_init( );
298 return 0;
299 }
300
301 /** Attach a filter.
302 */
303
304 int mlt_service_attach( mlt_service this, mlt_filter filter )
305 {
306 int error = this == NULL || filter == NULL;
307 if ( error == 0 )
308 {
309 int i = 0;
310 mlt_properties properties = mlt_service_properties( this );
311 mlt_service_base *base = this->local;
312
313 for ( i = 0; error == 0 && i < base->filter_count; i ++ )
314 if ( base->filters[ i ] == filter )
315 error = 1;
316
317 if ( error == 0 )
318 {
319 if ( base->filter_count == base->filter_size )
320 {
321 base->filter_size += 10;
322 base->filters = realloc( base->filters, base->filter_size * sizeof( mlt_filter ) );
323 }
324
325 if ( base->filters != NULL )
326 {
327 mlt_properties_inc_ref( mlt_filter_properties( filter ) );
328 base->filters[ base->filter_count ++ ] = filter;
329 mlt_events_fire( properties, "service-changed", NULL );
330 }
331 else
332 {
333 error = 2;
334 }
335 }
336 }
337 return error;
338 }
339
340 /** Detach a filter.
341 */
342
343 int mlt_service_detach( mlt_service this, mlt_filter filter )
344 {
345 int error = this == NULL || filter == NULL;
346 if ( error == 0 )
347 {
348 int i = 0;
349 mlt_service_base *base = this->local;
350 mlt_properties properties = mlt_service_properties( this );
351
352 for ( i = 0; i < base->filter_count; i ++ )
353 if ( base->filters[ i ] == filter )
354 break;
355
356 if ( i < base->filter_count )
357 {
358 base->filters[ i ] = NULL;
359 for ( i ++ ; i < base->filter_count; i ++ )
360 base->filters[ i - 1 ] = base->filters[ i ];
361 base->filter_count --;
362 mlt_filter_close( filter );
363 mlt_events_fire( properties, "service-changed", NULL );
364 }
365 }
366 return error;
367 }
368
369 /** Retrieve a filter.
370 */
371
372 mlt_filter mlt_service_filter( mlt_service this, int index )
373 {
374 mlt_filter filter = NULL;
375 if ( this != NULL )
376 {
377 mlt_service_base *base = this->local;
378 if ( index >= 0 && index < base->filter_count )
379 filter = base->filters[ index ];
380 }
381 return filter;
382 }
383
384 /** Close the service.
385 */
386
387 void mlt_service_close( mlt_service this )
388 {
389 if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
390 {
391 if ( this->close != NULL )
392 {
393 this->close( this->close_object );
394 }
395 else
396 {
397 mlt_service_base *base = this->local;
398 int i = 0;
399 int count = base->filter_count;
400 while( count -- )
401 mlt_service_detach( this, base->filters[ 0 ] );
402 free( base->filters );
403 for ( i = 0; i < base->count; i ++ )
404 if ( base->in[ i ] != NULL )
405 mlt_service_close( base->in[ i ] );
406 this->parent.close = NULL;
407 free( base->in );
408 free( base );
409 mlt_properties_close( &this->parent );
410 }
411 }
412 }
413