]> git.sesse.net Git - mlt/blob - src/framework/mlt_service.c
Revised attached filter handling and clones
[mlt] / 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 mlt_service_type mlt_service_identify( mlt_service this )
103 {
104         mlt_service_type type = invalid_type;
105         if ( this != NULL )
106         {
107                 mlt_properties properties = mlt_service_properties( this );
108                 char *mlt_type = mlt_properties_get( properties, "mlt_type" );
109                 char *resource = mlt_properties_get( properties, "resource" );
110                 if ( mlt_type == NULL )
111                         type = unknown_type;
112                 else if ( !strcmp( mlt_type, "producer" ) )
113                         type = producer_type;
114                 else if ( !strcmp( mlt_type, "mlt_producer" ) )
115                 {
116                         if ( resource == NULL || !strcmp( resource, "<producer>" ) )
117                                 type = producer_type;
118                         else if ( !strcmp( resource, "<playlist>" ) )
119                                 type = playlist_type;
120                         else if ( !strcmp( resource, "<tractor>" ) )
121                                 type = tractor_type;
122                         else if ( !strcmp( resource, "<multitrack>" ) )
123                                 type = multitrack_type;
124                 }
125                 else if ( !strcmp( mlt_type, "filter" ) )
126                         type = filter_type;
127                 else if ( !strcmp( mlt_type, "transition" ) )
128                         type = transition_type;
129                 else if ( !strcmp( mlt_type, "consumer" ) )
130                         type = consumer_type;
131                 else
132                         type = unknown_type;
133         }
134         return type;
135 }
136
137 /** Connect a producer service.
138         Returns: > 0 warning, == 0 success, < 0 serious error
139                          1 = this service does not accept input
140                          2 = the producer is invalid
141                          3 = the producer is already registered with this consumer
142 */
143
144 int mlt_service_connect_producer( mlt_service this, mlt_service producer, int index )
145 {
146         int i = 0;
147
148         // Get the service base
149         mlt_service_base *base = this->local;
150
151         // Check if the producer is already registered with this service
152         for ( i = 0; i < base->count; i ++ )
153                 if ( base->in[ i ] == producer )
154                         return 3;
155
156         // Allocate space
157         if ( index >= base->size )
158         {
159                 int new_size = base->size + index + 10;
160                 base->in = realloc( base->in, new_size * sizeof( mlt_service ) );
161                 if ( base->in != NULL )
162                 {
163                         for ( i = base->size; i < new_size; i ++ )
164                                 base->in[ i ] = NULL;
165                         base->size = new_size;
166                 }
167         }
168
169         // If we have space, assign the input
170         if ( base->in != NULL && index >= 0 && index < base->size )
171         {
172                 // Get the current service
173                 mlt_service current = base->in[ index ];
174
175                 // Increment the reference count on this producer
176                 if ( producer != NULL )
177                         mlt_properties_inc_ref( mlt_service_properties( producer ) );
178
179                 // Now we disconnect the producer service from its consumer
180                 mlt_service_disconnect( producer );
181                 
182                 // Add the service to index specified
183                 base->in[ index ] = producer;
184                 
185                 // Determine the number of active tracks
186                 if ( index >= base->count )
187                         base->count = index + 1;
188
189                 // Now we connect the producer to its connected consumer
190                 mlt_service_connect( producer, this );
191
192                 // Close the current service
193                 mlt_service_close( current );
194
195                 // Inform caller that all went well
196                 return 0;
197         }
198         else
199         {
200                 return -1;
201         }
202 }
203
204 /** Disconnect this service from its consumer.
205 */
206
207 static void mlt_service_disconnect( mlt_service this )
208 {
209         if ( this != NULL )
210         {
211                 // Get the service base
212                 mlt_service_base *base = this->local;
213
214                 // Disconnect
215                 base->out = NULL;
216         }
217 }
218
219 /** Obtain the consumer this service is connected to.
220 */
221
222 mlt_service mlt_service_consumer( mlt_service this )
223 {
224         // Get the service base
225         mlt_service_base *base = this->local;
226
227         // Return the connected consumer
228         return base->out;
229 }
230
231 /** Obtain the producer this service is connected to.
232 */
233
234 mlt_service mlt_service_producer( mlt_service this )
235 {
236         // Get the service base
237         mlt_service_base *base = this->local;
238
239         // Return the connected producer
240         return base->count > 0 ? base->in[ base->count - 1 ] : NULL;
241 }
242
243 /** Associate this service to the consumer.
244 */
245
246 static void mlt_service_connect( mlt_service this, mlt_service that )
247 {
248         if ( this != NULL )
249         {
250                 // Get the service base
251                 mlt_service_base *base = this->local;
252
253                 // There's a bit more required here...
254                 base->out = that;
255         }
256 }
257
258 /** Get the first connected producer service.
259 */
260
261 mlt_service mlt_service_get_producer( mlt_service this )
262 {
263         mlt_service producer = NULL;
264
265         // Get the service base
266         mlt_service_base *base = this->local;
267
268         if ( base->in != NULL )
269                 producer = base->in[ 0 ];
270         
271         return producer;
272 }
273
274 /** Default implementation of get_frame.
275 */
276
277 static int service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
278 {
279         mlt_service_base *base = this->local;
280         if ( index < base->count )
281         {
282                 mlt_service producer = base->in[ index ];
283                 if ( producer != NULL )
284                         return mlt_service_get_frame( producer, frame, index );
285         }
286         *frame = mlt_frame_init( );
287         return 0;
288 }
289
290 /** Return the properties object.
291 */
292
293 mlt_properties mlt_service_properties( mlt_service self )
294 {
295         return self != NULL ? &self->parent : NULL;
296 }
297
298 /** Recursively apply attached filters
299 */
300
301 void mlt_service_apply_filters( mlt_service this, mlt_frame frame, int index )
302 {
303         int i;
304         mlt_properties frame_properties = mlt_frame_properties( frame );
305         mlt_properties service_properties = mlt_service_properties( this );
306         mlt_service_base *base = this->local;
307         mlt_position position = mlt_frame_get_position( frame );
308         mlt_position this_in = mlt_properties_get_position( service_properties, "in" );
309         mlt_position this_out = mlt_properties_get_position( service_properties, "out" );
310
311         if ( index == 0 || mlt_properties_get_int( service_properties, "_filter_private" ) == 0 )
312         {
313                 // Process the frame with the attached filters
314                 for ( i = 0; i < base->filter_count; i ++ )
315                 {
316                         if ( base->filters[ i ] != NULL )
317                         {
318                                 mlt_position in = mlt_filter_get_in( base->filters[ i ] );
319                                 mlt_position out = mlt_filter_get_out( base->filters[ i ] );
320                                 if ( ( in == 0 && out == 0 ) || ( position >= in && ( position <= out || out == 0 ) ) )
321                                 {
322                                         mlt_properties_set_position( frame_properties, "in", 0 );
323                                         mlt_properties_set_position( frame_properties, "out", out == 0 ? this_out - this_in : out - in );
324                                         mlt_filter_process( base->filters[ i ], frame );
325                                         mlt_service_apply_filters( mlt_filter_service( base->filters[ i ] ), frame, index + 1 );
326                                 }
327                         }
328                 }
329         }
330 }
331
332 /** Obtain a frame.
333 */
334
335 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
336 {
337         if ( this != NULL && this->get_frame != NULL )
338         {
339                 int result = 0;
340                 mlt_properties properties = mlt_service_properties( this );
341                 mlt_position in = mlt_properties_get_position( properties, "in" );
342                 mlt_position out = mlt_properties_get_position( properties, "out" );
343                 result = this->get_frame( this, frame, index );
344                 if ( result == 0 )
345                 {
346                         if ( in >=0 && out > 0 )
347                         {
348                                 properties = mlt_frame_properties( *frame );
349                                 mlt_properties_set_position( properties, "in", in );
350                                 mlt_properties_set_position( properties, "out", out );
351                         }
352                         mlt_service_apply_filters( this, *frame, 1 );
353                 }
354                 return result;
355         }
356         *frame = mlt_frame_init( );
357         return 0;
358 }
359
360 static void mlt_service_filter_changed( mlt_service owner, mlt_service this )
361 {
362         mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL );
363 }
364
365 /** Attach a filter.
366 */
367
368 int mlt_service_attach( mlt_service this, mlt_filter filter )
369 {
370         int error = this == NULL || filter == NULL;
371         if ( error == 0 )
372         {
373                 int i = 0;
374                 mlt_properties properties = mlt_service_properties( this );
375                 mlt_service_base *base = this->local;
376
377                 for ( i = 0; error == 0 && i < base->filter_count; i ++ )
378                         if ( base->filters[ i ] == filter )
379                                 error = 1;
380
381                 if ( error == 0 )
382                 {
383                         if ( base->filter_count == base->filter_size )
384                         {
385                                 base->filter_size += 10;
386                                 base->filters = realloc( base->filters, base->filter_size * sizeof( mlt_filter ) );
387                         }
388
389                         if ( base->filters != NULL )
390                         {
391                                 mlt_properties props = mlt_filter_properties( filter );
392                                 mlt_properties_inc_ref( mlt_filter_properties( filter ) );
393                                 base->filters[ base->filter_count ++ ] = filter;
394                                 mlt_events_fire( properties, "service-changed", NULL );
395                                 mlt_events_listen( props, this, "service-changed", ( mlt_listener )mlt_service_filter_changed );
396                                 mlt_events_listen( props, this, "property-changed", ( mlt_listener )mlt_service_filter_changed );
397                         }
398                         else
399                         {
400                                 error = 2;
401                         }
402                 }
403         }
404         return error;
405 }
406
407 /** Detach a filter.
408 */
409
410 int mlt_service_detach( mlt_service this, mlt_filter filter )
411 {
412         int error = this == NULL || filter == NULL;
413         if ( error == 0 )
414         {
415                 int i = 0;
416                 mlt_service_base *base = this->local;
417                 mlt_properties properties = mlt_service_properties( this );
418
419                 for ( i = 0; i < base->filter_count; i ++ )
420                         if ( base->filters[ i ] == filter )
421                                 break;
422
423                 if ( i < base->filter_count )
424                 {
425                         base->filters[ i ] = NULL;
426                         for ( i ++ ; i < base->filter_count; i ++ )
427                                 base->filters[ i - 1 ] = base->filters[ i ];
428                         base->filter_count --;
429                         mlt_events_disconnect( mlt_filter_properties( filter ), this );
430                         mlt_filter_close( filter );
431                         mlt_events_fire( properties, "service-changed", NULL );
432                 }
433         }
434         return error;
435 }
436
437 /** Retrieve a filter.
438 */
439
440 mlt_filter mlt_service_filter( mlt_service this, int index )
441 {
442         mlt_filter filter = NULL;
443         if ( this != NULL )
444         {
445                 mlt_service_base *base = this->local;
446                 if ( index >= 0 && index < base->filter_count )
447                         filter = base->filters[ index ];
448         }
449         return filter;
450 }
451
452 /** Close the service.
453 */
454
455 void mlt_service_close( mlt_service this )
456 {
457         if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
458         {
459                 if ( this->close != NULL )
460                 {
461                         this->close( this->close_object );
462                 }
463                 else
464                 {
465                         mlt_service_base *base = this->local;
466                         int i = 0;
467                         int count = base->filter_count;
468                         mlt_events_block( mlt_service_properties( this ), this );
469                         while( count -- )
470                                 mlt_service_detach( this, base->filters[ 0 ] );
471                         free( base->filters );
472                         for ( i = 0; i < base->count; i ++ )
473                                 if ( base->in[ i ] != NULL )
474                                         mlt_service_close( base->in[ i ] );
475                         this->parent.close = NULL;
476                         free( base->in );
477                         free( base );
478                         mlt_properties_close( &this->parent );
479                 }
480         }
481 }
482