]> git.sesse.net Git - mlt/blob - src/framework/mlt_service.c
9a1cc376e3dbe36209fdc910ec589f06dbcbf948
[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         // Hmm - special case for cuts - apply filters from the parent first
312         if ( mlt_properties_get_int( service_properties, "_cut" ) )
313         {
314                 mlt_service_apply_filters( ( mlt_service )mlt_properties_get_data( service_properties, "_cut_parent", NULL ), frame, 0 );
315                 position -= this_in;
316                 mlt_frame_set_position( frame, position );
317         }
318
319         if ( index == 0 || mlt_properties_get_int( service_properties, "_filter_private" ) == 0 )
320         {
321                 // Process the frame with the attached filters
322                 for ( i = 0; i < base->filter_count; i ++ )
323                 {
324                         if ( base->filters[ i ] != NULL )
325                         {
326                                 mlt_position in = mlt_filter_get_in( base->filters[ i ] );
327                                 mlt_position out = mlt_filter_get_out( base->filters[ i ] );
328                                 if ( ( in == 0 && out == 0 ) || ( position >= in && ( position <= out || out == 0 ) ) )
329                                 {
330                                         mlt_properties_set_position( frame_properties, "in", 0 );
331                                         mlt_properties_set_position( frame_properties, "out", out == 0 ? this_out - this_in : out - in );
332                                         mlt_frame_set_position( frame, position - in );
333                                         mlt_filter_process( base->filters[ i ], frame );
334                                         mlt_service_apply_filters( mlt_filter_service( base->filters[ i ] ), frame, index + 1 );
335                                         mlt_frame_set_position( frame, position + in );
336                                 }
337                         }
338                 }
339         }
340
341         if ( mlt_properties_get_int( service_properties, "_cut" ) )
342         {
343                 mlt_properties_set_position( frame_properties, "in", this_in );
344                 mlt_properties_set_position( frame_properties, "out", this_out );
345                 mlt_frame_set_position( frame, position + this_in );
346         }
347 }
348
349 /** Obtain a frame.
350 */
351
352 int mlt_service_get_frame( mlt_service this, mlt_frame_ptr frame, int index )
353 {
354         if ( this != NULL && this->get_frame != NULL )
355         {
356                 int result = 0;
357                 mlt_properties properties = mlt_service_properties( this );
358                 mlt_position in = mlt_properties_get_position( properties, "in" );
359                 mlt_position out = mlt_properties_get_position( properties, "out" );
360                 result = this->get_frame( this, frame, index );
361                 if ( result == 0 )
362                 {
363                         if ( in >=0 && out > 0 )
364                         {
365                                 properties = mlt_frame_properties( *frame );
366                                 mlt_properties_set_position( properties, "in", in );
367                                 mlt_properties_set_position( properties, "out", out );
368                         }
369                         mlt_service_apply_filters( this, *frame, 1 );
370                 }
371                 return result;
372         }
373         *frame = mlt_frame_init( );
374         return 0;
375 }
376
377 static void mlt_service_filter_changed( mlt_service owner, mlt_service this )
378 {
379         mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL );
380 }
381
382 /** Attach a filter.
383 */
384
385 int mlt_service_attach( mlt_service this, mlt_filter filter )
386 {
387         int error = this == NULL || filter == NULL;
388         if ( error == 0 )
389         {
390                 int i = 0;
391                 mlt_properties properties = mlt_service_properties( this );
392                 mlt_service_base *base = this->local;
393
394                 for ( i = 0; error == 0 && i < base->filter_count; i ++ )
395                         if ( base->filters[ i ] == filter )
396                                 error = 1;
397
398                 if ( error == 0 )
399                 {
400                         if ( base->filter_count == base->filter_size )
401                         {
402                                 base->filter_size += 10;
403                                 base->filters = realloc( base->filters, base->filter_size * sizeof( mlt_filter ) );
404                         }
405
406                         if ( base->filters != NULL )
407                         {
408                                 mlt_properties props = mlt_filter_properties( filter );
409                                 mlt_properties_inc_ref( mlt_filter_properties( filter ) );
410                                 base->filters[ base->filter_count ++ ] = filter;
411                                 mlt_events_fire( properties, "service-changed", NULL );
412                                 mlt_events_listen( props, this, "service-changed", ( mlt_listener )mlt_service_filter_changed );
413                                 mlt_events_listen( props, this, "property-changed", ( mlt_listener )mlt_service_filter_changed );
414                         }
415                         else
416                         {
417                                 error = 2;
418                         }
419                 }
420         }
421         return error;
422 }
423
424 /** Detach a filter.
425 */
426
427 int mlt_service_detach( mlt_service this, mlt_filter filter )
428 {
429         int error = this == NULL || filter == NULL;
430         if ( error == 0 )
431         {
432                 int i = 0;
433                 mlt_service_base *base = this->local;
434                 mlt_properties properties = mlt_service_properties( this );
435
436                 for ( i = 0; i < base->filter_count; i ++ )
437                         if ( base->filters[ i ] == filter )
438                                 break;
439
440                 if ( i < base->filter_count )
441                 {
442                         base->filters[ i ] = NULL;
443                         for ( i ++ ; i < base->filter_count; i ++ )
444                                 base->filters[ i - 1 ] = base->filters[ i ];
445                         base->filter_count --;
446                         mlt_events_disconnect( mlt_filter_properties( filter ), this );
447                         mlt_filter_close( filter );
448                         mlt_events_fire( properties, "service-changed", NULL );
449                 }
450         }
451         return error;
452 }
453
454 /** Retrieve a filter.
455 */
456
457 mlt_filter mlt_service_filter( mlt_service this, int index )
458 {
459         mlt_filter filter = NULL;
460         if ( this != NULL )
461         {
462                 mlt_service_base *base = this->local;
463                 if ( index >= 0 && index < base->filter_count )
464                         filter = base->filters[ index ];
465         }
466         return filter;
467 }
468
469 /** Close the service.
470 */
471
472 void mlt_service_close( mlt_service this )
473 {
474         if ( this != NULL && mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
475         {
476                 if ( this->close != NULL )
477                 {
478                         this->close( this->close_object );
479                 }
480                 else
481                 {
482                         mlt_service_base *base = this->local;
483                         int i = 0;
484                         int count = base->filter_count;
485                         mlt_events_block( mlt_service_properties( this ), this );
486                         while( count -- )
487                                 mlt_service_detach( this, base->filters[ 0 ] );
488                         free( base->filters );
489                         for ( i = 0; i < base->count; i ++ )
490                                 if ( base->in[ i ] != NULL )
491                                         mlt_service_close( base->in[ i ] );
492                         this->parent.close = NULL;
493                         free( base->in );
494                         free( base );
495                         mlt_properties_close( &this->parent );
496                 }
497         }
498 }
499