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>
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.
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.
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.
22 #include "mlt_service.h"
23 #include "mlt_filter.h"
24 #include "mlt_frame.h"
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.
35 PLEASE DO NOT CHANGE THIS BEHAVIOUR!!! OVERRIDE THE METHODS THAT
36 CONTROL THIS IN EXTENDING CLASSES.
39 /** Private service definition.
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
);
65 int mlt_service_init( mlt_service
this, void *child
)
69 // Initialise everything to NULL
70 memset( this, 0, sizeof( struct mlt_service_s
) );
75 // Generate local space
76 this->local
= calloc( sizeof( mlt_service_base
), 1 );
78 // Associate the methods
79 this->get_frame
= service_get_frame
;
81 // Initialise the properties
82 error
= mlt_properties_init( &this->parent
, this );
85 this->parent
.close
= ( mlt_destructor
)mlt_service_close
;
86 this->parent
.close_object
= this;
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
);
96 static void mlt_service_property_changed( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
98 if ( listener
!= NULL
)
99 listener( owner
, this, ( char * )args
[ 0 ] );
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
109 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
113 // Get the service base
114 mlt_service_base
*base
= this->local
;
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
)
122 if ( index
>= base
->size
)
124 int new_size
= base
->size
+ index
+ 10;
125 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
126 if ( base
->in
!= NULL
)
128 for ( i
= base
->size
; i
< new_size
; i
++ )
129 base
->in
[ i
] = NULL
;
130 base
->size
= new_size
;
134 // If we have space, assign the input
135 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
137 // Get the current service
138 mlt_service current
= base
->in
[ index
];
140 // Increment the reference count on this producer
141 if ( producer
!= NULL
)
142 mlt_properties_inc_ref( mlt_service_properties( producer
) );
144 // Now we disconnect the producer service from its consumer
145 mlt_service_disconnect( producer
);
147 // Add the service to index specified
148 base
->in
[ index
] = producer
;
150 // Determine the number of active tracks
151 if ( index
>= base
->count
)
152 base
->count
= index
+ 1;
154 // Now we connect the producer to its connected consumer
155 mlt_service_connect( producer
, this );
157 // Close the current service
158 mlt_service_close( current
);
160 // Inform caller that all went well
169 /** Disconnect this service from its consumer.
172 static void mlt_service_disconnect( mlt_service
this )
176 // Get the service base
177 mlt_service_base
*base
= this->local
;
184 /** Obtain the consumer this service is connected to.
187 mlt_service
mlt_service_consumer( mlt_service
this )
189 // Get the service base
190 mlt_service_base
*base
= this->local
;
192 // Return the connected consumer
196 /** Obtain the producer this service is connected to.
199 mlt_service
mlt_service_producer( mlt_service
this )
201 // Get the service base
202 mlt_service_base
*base
= this->local
;
204 // Return the connected producer
205 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
208 /** Associate this service to the consumer.
211 static void mlt_service_connect( mlt_service
this, mlt_service that
)
215 // Get the service base
216 mlt_service_base
*base
= this->local
;
218 // There's a bit more required here...
223 /** Get the first connected producer service.
226 mlt_service
mlt_service_get_producer( mlt_service
this )
228 mlt_service producer
= NULL
;
230 // Get the service base
231 mlt_service_base
*base
= this->local
;
233 if ( base
->in
!= NULL
)
234 producer
= base
->in
[ 0 ];
239 /** Default implementation of get_frame.
242 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
244 mlt_service_base
*base
= this->local
;
245 if ( index
< base
->count
)
247 mlt_service producer
= base
->in
[ index
];
248 if ( producer
!= NULL
)
249 return mlt_service_get_frame( producer
, frame
, index
);
251 *frame
= mlt_frame_init( );
255 /** Return the properties object.
258 mlt_properties
mlt_service_properties( mlt_service self
)
260 return self
!= NULL ?
&self
->parent
: NULL
;
263 /** Recursively apply attached filters
266 void mlt_service_apply_filters( mlt_service
this, mlt_frame frame
, int index
)
269 mlt_properties frame_properties
= mlt_frame_properties( frame
);
270 mlt_properties filter_properties
= mlt_service_properties( this );
271 mlt_service_base
*base
= this->local
;
272 mlt_position position
= mlt_frame_get_position( frame
);
273 mlt_position this_in
= mlt_properties_get_position( filter_properties
, "in" );
275 // Hmm - special case for cuts - apply filters from the parent first
276 if ( mlt_properties_get_int( filter_properties
, "_cut" ) )
279 mlt_frame_set_position( frame
, position
);
280 mlt_service_apply_filters( ( mlt_service
)mlt_properties_get_data( filter_properties
, "_cut_parent", NULL
), frame
, 0 );
283 if ( index
== 0 || mlt_properties_get_int( filter_properties
, "_filter_private" ) == 0 )
285 // Process the frame with the attached filters
286 for ( i
= 0; i
< base
->filter_count
; i
++ )
288 if ( base
->filters
[ i
] != NULL
)
290 mlt_properties properties
= mlt_filter_properties( base
->filters
[ i
] );
291 mlt_position in
= mlt_filter_get_in( base
->filters
[ i
] );
292 mlt_position out
= mlt_filter_get_out( base
->filters
[ i
] );
293 if ( ( in
== 0 && out
== 0 ) || ( position
>= in
&& ( position
<= out
|| out
== 0 ) ) )
295 mlt_frame_set_position( frame
, position
- in
);
296 mlt_filter_process( base
->filters
[ i
], frame
);
297 mlt_service_apply_filters( mlt_filter_service( base
->filters
[ i
] ), frame
, index
+ 1 );
298 mlt_frame_set_position( frame
, position
+ in
);
304 if ( mlt_properties_get_int( filter_properties
, "_cut" ) )
305 mlt_frame_set_position( frame
, position
+ this_in
);
311 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
315 int result
= this->get_frame( this, frame
, index
);
317 mlt_service_apply_filters( this, *frame
, 1 );
320 *frame
= mlt_frame_init( );
324 static void mlt_service_filter_changed( mlt_service owner
, mlt_service
this )
326 mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL
);
332 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
334 int error
= this == NULL
|| filter
== NULL
;
338 mlt_properties properties
= mlt_service_properties( this );
339 mlt_service_base
*base
= this->local
;
341 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
342 if ( base
->filters
[ i
] == filter
)
347 if ( base
->filter_count
== base
->filter_size
)
349 base
->filter_size
+= 10;
350 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
353 if ( base
->filters
!= NULL
)
355 mlt_properties props
= mlt_filter_properties( filter
);
356 mlt_properties_inc_ref( mlt_filter_properties( filter
) );
357 base
->filters
[ base
->filter_count
++ ] = filter
;
358 mlt_events_fire( properties
, "service-changed", NULL
);
359 mlt_events_listen( props
, this, "service-changed", ( mlt_listener
)mlt_service_filter_changed
);
360 mlt_events_listen( props
, this, "property-changed", ( mlt_listener
)mlt_service_filter_changed
);
374 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
376 int error
= this == NULL
|| filter
== NULL
;
380 mlt_service_base
*base
= this->local
;
381 mlt_properties properties
= mlt_service_properties( this );
383 for ( i
= 0; i
< base
->filter_count
; i
++ )
384 if ( base
->filters
[ i
] == filter
)
387 if ( i
< base
->filter_count
)
389 base
->filters
[ i
] = NULL
;
390 for ( i
++ ; i
< base
->filter_count
; i
++ )
391 base
->filters
[ i
- 1 ] = base
->filters
[ i
];
392 base
->filter_count
--;
393 mlt_events_disconnect( mlt_filter_properties( filter
), this );
394 mlt_filter_close( filter
);
395 mlt_events_fire( properties
, "service-changed", NULL
);
401 /** Retrieve a filter.
404 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
406 mlt_filter filter
= NULL
;
409 mlt_service_base
*base
= this->local
;
410 if ( index
>= 0 && index
< base
->filter_count
)
411 filter
= base
->filters
[ index
];
416 /** Close the service.
419 void mlt_service_close( mlt_service
this )
421 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
423 if ( this->close
!= NULL
)
425 this->close( this->close_object
);
429 mlt_service_base
*base
= this->local
;
431 int count
= base
->filter_count
;
432 mlt_events_block( mlt_service_properties( this ), this );
434 mlt_service_detach( this, base
->filters
[ 0 ] );
435 free( base
->filters
);
436 for ( i
= 0; i
< base
->count
; i
++ )
437 if ( base
->in
[ i
] != NULL
)
438 mlt_service_close( base
->in
[ i
] );
439 this->parent
.close
= NULL
;
442 mlt_properties_close( &this->parent
);