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
, "property-changed", ( mlt_transmitter
)mlt_service_property_changed
);
95 static void mlt_service_property_changed( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
97 if ( listener
!= NULL
)
98 listener( owner
, this, ( char * )args
[ 0 ] );
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
108 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
112 // Get the service base
113 mlt_service_base
*base
= this->local
;
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
)
121 if ( index
>= base
->size
)
123 int new_size
= base
->size
+ index
+ 10;
124 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
125 if ( base
->in
!= NULL
)
127 for ( i
= base
->size
; i
< new_size
; i
++ )
128 base
->in
[ i
] = NULL
;
129 base
->size
= new_size
;
133 // If we have space, assign the input
134 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
136 // Get the current service
137 mlt_service current
= base
->in
[ index
];
139 // Increment the reference count on this producer
140 if ( producer
!= NULL
)
141 mlt_properties_inc_ref( mlt_service_properties( producer
) );
143 // Now we disconnect the producer service from its consumer
144 mlt_service_disconnect( producer
);
146 // Add the service to index specified
147 base
->in
[ index
] = producer
;
149 // Determine the number of active tracks
150 if ( index
>= base
->count
)
151 base
->count
= index
+ 1;
153 // Now we connect the producer to its connected consumer
154 mlt_service_connect( producer
, this );
156 // Close the current service
157 mlt_service_close( current
);
159 // Inform caller that all went well
168 /** Disconnect this service from its consumer.
171 static void mlt_service_disconnect( mlt_service
this )
175 // Get the service base
176 mlt_service_base
*base
= this->local
;
183 /** Obtain the consumer this service is connected to.
186 mlt_service
mlt_service_consumer( mlt_service
this )
188 // Get the service base
189 mlt_service_base
*base
= this->local
;
191 // Return the connected consumer
195 /** Obtain the producer this service is connected to.
198 mlt_service
mlt_service_producer( mlt_service
this )
200 // Get the service base
201 mlt_service_base
*base
= this->local
;
203 // Return the connected producer
204 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
207 /** Associate this service to the consumer.
210 static void mlt_service_connect( mlt_service
this, mlt_service that
)
214 // Get the service base
215 mlt_service_base
*base
= this->local
;
217 // There's a bit more required here...
222 /** Get the first connected producer service.
225 mlt_service
mlt_service_get_producer( mlt_service
this )
227 mlt_service producer
= NULL
;
229 // Get the service base
230 mlt_service_base
*base
= this->local
;
232 if ( base
->in
!= NULL
)
233 producer
= base
->in
[ 0 ];
238 /** Default implementation of get_frame.
241 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
243 mlt_service_base
*base
= this->local
;
244 if ( index
< base
->count
)
246 mlt_service producer
= base
->in
[ index
];
247 if ( producer
!= NULL
)
248 return mlt_service_get_frame( producer
, frame
, index
);
250 *frame
= mlt_frame_init( );
254 /** Return the properties object.
257 mlt_properties
mlt_service_properties( mlt_service self
)
259 return self
!= NULL ?
&self
->parent
: NULL
;
262 /** Recursively apply attached filters
265 static void apply_filters( mlt_service
this, mlt_frame frame
, int index
)
267 mlt_properties properties
= mlt_service_properties( this );
268 mlt_service_base
*base
= this->local
;
271 if ( mlt_properties_get_int( properties
, "_filter_private" ) == 0 )
273 // Process the frame with the attached filters
274 for ( i
= 0; i
< base
->filter_count
; i
++ )
276 if ( base
->filters
[ i
] != NULL
)
278 mlt_filter_process( base
->filters
[ i
], frame
);
279 apply_filters( mlt_filter_service( base
->filters
[ i
] ), frame
, index
);
288 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
292 int result
= this->get_frame( this, frame
, index
);
294 apply_filters( this, *frame
, index
);
297 *frame
= mlt_frame_init( );
304 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
306 int error
= this == NULL
|| filter
== NULL
;
310 mlt_properties properties
= mlt_service_properties( this );
311 mlt_service_base
*base
= this->local
;
313 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
314 if ( base
->filters
[ i
] == filter
)
319 if ( base
->filter_count
== base
->filter_size
)
321 base
->filter_size
+= 10;
322 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
325 if ( base
->filters
!= NULL
)
327 mlt_properties_inc_ref( mlt_filter_properties( filter
) );
328 base
->filters
[ base
->filter_count
++ ] = filter
;
329 mlt_events_fire( properties
, "service-changed", NULL
);
343 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
345 int error
= this == NULL
|| filter
== NULL
;
349 mlt_service_base
*base
= this->local
;
350 mlt_properties properties
= mlt_service_properties( this );
352 for ( i
= 0; i
< base
->filter_count
; i
++ )
353 if ( base
->filters
[ i
] == filter
)
356 if ( i
< base
->filter_count
)
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
);
369 /** Retrieve a filter.
372 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
374 mlt_filter filter
= NULL
;
377 mlt_service_base
*base
= this->local
;
378 if ( index
>= 0 && index
< base
->filter_count
)
379 filter
= base
->filters
[ index
];
384 /** Close the service.
387 void mlt_service_close( mlt_service
this )
389 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
391 if ( this->close
!= NULL
)
393 this->close( this->close_object
);
397 mlt_service_base
*base
= this->local
;
399 int count
= base
->filter_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
;
409 mlt_properties_close( &this->parent
);