More work with events
[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, "service-changed", NULL );
90 mlt_events_register( &this->parent, "property-changed", ( mlt_transmitter )mlt_service_property_changed );
91 }
92
93 return error;
94 }
95
96 static void mlt_service_property_changed( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
97 {
98 if ( listener != NULL )
99 listener( owner, this, ( char * )args[ 0 ] );
100 }
101
102 /** Connect a producer service.
103 Returns: > 0 warning, == 0 success, < 0 serious error
104 1 = this service does not accept input
105 2 = the producer is invalid
106 3 = the producer is already registered with this consumer
107 */
108
109 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
110 {
111 int i = 0;
112
113 // Get the service base
114 mlt_service_base *base = this->local;
115
116 // Check if the producer is already registered with this service
117 for ( i = 0; i < base->count; i ++ )
118 if ( base->in[ i ] == producer )
119 return 3;
120
121 // Allocate space
122 if ( index >= base->size )
123 {
124 int new_size = base->size + index + 10;
125 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
126 if ( base->in != NULL )
127 {
128 for ( i = base->size; i < new_size; i ++ )
129 base->in[ i ] = NULL;
130 base->size = new_size;
131 }
132 }
133
134 // If we have space, assign the input
135 if ( base->in != NULL && index >= 0 && index < base->size )
136 {
137 // Get the current service
138 mlt_service current = base->in[ index ];
139
140 // Increment the reference count on this producer
141 if ( producer != NULL )
142 mlt_properties_inc_ref( mlt_service_properties( producer ) );
143
144 // Now we disconnect the producer service from its consumer
145 mlt_service_disconnect( producer );
146
147 // Add the service to index specified
148 base->in[ index ] = producer;
149
150 // Determine the number of active tracks
151 if ( index >= base->count )
152 base->count = index + 1;
153
154 // Now we connect the producer to its connected consumer
155 mlt_service_connect( producer, this );
156
157 // Close the current service
158 mlt_service_close( current );
159
160 // Inform caller that all went well
161 return 0;
162 }
163 else
164 {
165 return -1;
166 }
167 }
168
169 /** Disconnect this service from its consumer.
170 */
171
172 static void mlt_service_disconnect( mlt_service this )
173 {
174 if ( this != NULL )
175 {
176 // Get the service base
177 mlt_service_base *base = this->local;
178
179 // Disconnect
180 base->out = NULL;
181 }
182 }
183
184 /** Obtain the consumer this service is connected to.
185 */
186
187 mlt_service mlt_service_consumer( mlt_service this )
188 {
189 // Get the service base
190 mlt_service_base *base = this->local;
191
192 // Return the connected consumer
193 return base->out;
194 }
195
196 /** Obtain the producer this service is connected to.
197 */
198
199 mlt_service mlt_service_producer( mlt_service this )
200 {
201 // Get the service base
202 mlt_service_base *base = this->local;
203
204 // Return the connected producer
205 return base->count > 0 ? base->in[ base->count - 1 ] : NULL;
206 }
207
208 /** Associate this service to the consumer.
209 */
210
211 static void mlt_service_connect( mlt_service this, mlt_service that )
212 {
213 if ( this != NULL )
214 {
215 // Get the service base
216 mlt_service_base *base = this->local;
217
218 // There's a bit more required here...
219 base->out = that;
220 }
221 }
222
223 /** Get the first connected producer service.
224 */
225
226 mlt_service mlt_service_get_producer( mlt_service this )
227 {
228 mlt_service producer = NULL;
229
230 // Get the service base
231 mlt_service_base *base = this->local;
232
233 if ( base->in != NULL )
234 producer = base->in[ 0 ];
235
236 return producer;
237 }
238
239 /** Default implementation of get_frame.
240 */
241
242 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
243 {
244 mlt_service_base *base = this->local;
245 if ( index < base->count )
246 {
247 mlt_service producer = base->in[ index ];
248 if ( producer != NULL )
249 return mlt_service_get_frame( producer, frame, index );
250 }
251 *frame = mlt_frame_init( );
252 return 0;
253 }
254
255 /** Return the properties object.
256 */
257
258 mlt_properties mlt_service_properties( mlt_service self )
259 {
260 return self != NULL ? &self->parent : NULL;
261 }
262
263 /** Recursively apply attached filters
264 */
265
266 static void apply_filters( mlt_service this, mlt_frame frame, int index )
267 {
268 mlt_properties properties = mlt_service_properties( this );
269 mlt_service_base *base = this->local;
270 int i;
271
272 if ( mlt_properties_get_int( properties, "_filter_private" ) == 0 )
273 {
274 // Process the frame with the attached filters
275 for ( i = 0; i < base->filter_count; i ++ )
276 {
277 if ( base->filters[ i ] != NULL )
278 {
279 mlt_filter_process( base->filters[ i ], frame );
280 apply_filters( mlt_filter_service( base->filters[ i ] ), frame, index );
281 }
282 }
283 }
284 }
285
286 /** Obtain a frame.
287 */
288
289 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
290 {
291 if ( this != NULL )
292 {
293 int result = this->get_frame( this, frame, index );
294 if ( result == 0 )
295 apply_filters( this, *frame, index );
296 return result;
297 }
298 *frame = mlt_frame_init( );
299 return 0;
300 }
301
302 static void mlt_service_filter_changed( mlt_service owner, mlt_service this )
303 {
304 mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL );
305 }
306
307 /** Attach a filter.
308 */
309
310 int mlt_service_attach( mlt_service this, mlt_filter filter )
311 {
312 int error = this == NULL || filter == NULL;
313 if ( error == 0 )
314 {
315 int i = 0;
316 mlt_properties properties = mlt_service_properties( this );
317 mlt_service_base *base = this->local;
318
319 for ( i = 0; error == 0 && i < base->filter_count; i ++ )
320 if ( base->filters[ i ] == filter )
321 error = 1;
322
323 if ( error == 0 )
324 {
325 if ( base->filter_count == base->filter_size )
326 {
327 base->filter_size += 10;
328 base->filters = realloc( base->filters, base->filter_size * sizeof( mlt_filter ) );
329 }
330
331 if ( base->filters != NULL )
332 {
333 mlt_properties props = mlt_filter_properties( filter );
334 mlt_properties_inc_ref( mlt_filter_properties( filter ) );
335 base->filters[ base->filter_count ++ ] = filter;
336 mlt_events_fire( properties, "service-changed", NULL );
337 mlt_events_listen( props, this, "service-changed", ( mlt_listener )mlt_service_filter_changed );
338 mlt_events_listen( props, this, "property-changed", ( mlt_listener )mlt_service_filter_changed );
339 }
340 else
341 {
342 error = 2;
343 }
344 }
345 }
346 return error;
347 }
348
349 /** Detach a filter.
350 */
351
352 int mlt_service_detach( mlt_service this, mlt_filter filter )
353 {
354 int error = this == NULL || filter == NULL;
355 if ( error == 0 )
356 {
357 int i = 0;
358 mlt_service_base *base = this->local;
359 mlt_properties properties = mlt_service_properties( this );
360
361 for ( i = 0; i < base->filter_count; i ++ )
362 if ( base->filters[ i ] == filter )
363 break;
364
365 if ( i < base->filter_count )
366 {
367 base->filters[ i ] = NULL;
368 for ( i ++ ; i < base->filter_count; i ++ )
369 base->filters[ i - 1 ] = base->filters[ i ];
370 base->filter_count --;
371 mlt_events_disconnect( mlt_filter_properties( filter ), this );
372 mlt_filter_close( filter );
373 mlt_events_fire( properties, "service-changed", NULL );
374 }
375 }
376 return error;
377 }
378
379 /** Retrieve a filter.
380 */
381
382 mlt_filter mlt_service_filter( mlt_service this, int index )
383 {
384 mlt_filter filter = NULL;
385 if ( this != NULL )
386 {
387 mlt_service_base *base = this->local;
388 if ( index >= 0 && index < base->filter_count )
389 filter = base->filters[ index ];
390 }
391 return filter;
392 }
393
394 /** Close the service.
395 */
396
397 void mlt_service_close( mlt_service this )
398 {
399 if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
400 {
401 if ( this->close != NULL )
402 {
403 this->close( this->close_object );
404 }
405 else
406 {
407 mlt_service_base *base = this->local;
408 int i = 0;
409 int count = base->filter_count;
410 mlt_events_block( mlt_service_properties( this ), this );
411 while( count -- )
412 mlt_service_detach( this, base->filters[ 0 ] );
413 free( base->filters );
414 for ( i = 0; i < base->count; i ++ )
415 if ( base->in[ i ] != NULL )
416 mlt_service_close( base->in[ i ] );
417 this->parent.close = NULL;
418 free( base->in );
419 free( base );
420 mlt_properties_close( &this->parent );
421 }
422 }
423 }
424