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 mlt_service_type
mlt_service_identify( mlt_service
this )
104 mlt_service_type type
= invalid_type
;
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
)
112 else if ( !strcmp( mlt_type
, "producer" ) )
113 type
= producer_type
;
114 else if ( !strcmp( mlt_type
, "mlt_producer" ) )
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>" ) )
122 else if ( !strcmp( resource
, "<multitrack>" ) )
123 type
= multitrack_type
;
125 else if ( !strcmp( mlt_type
, "filter" ) )
127 else if ( !strcmp( mlt_type
, "transition" ) )
128 type
= transition_type
;
129 else if ( !strcmp( mlt_type
, "consumer" ) )
130 type
= consumer_type
;
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
144 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
148 // Get the service base
149 mlt_service_base
*base
= this->local
;
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
)
157 if ( index
>= base
->size
)
159 int new_size
= base
->size
+ index
+ 10;
160 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
161 if ( base
->in
!= NULL
)
163 for ( i
= base
->size
; i
< new_size
; i
++ )
164 base
->in
[ i
] = NULL
;
165 base
->size
= new_size
;
169 // If we have space, assign the input
170 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
172 // Get the current service
173 mlt_service current
= base
->in
[ index
];
175 // Increment the reference count on this producer
176 if ( producer
!= NULL
)
177 mlt_properties_inc_ref( mlt_service_properties( producer
) );
179 // Now we disconnect the producer service from its consumer
180 mlt_service_disconnect( producer
);
182 // Add the service to index specified
183 base
->in
[ index
] = producer
;
185 // Determine the number of active tracks
186 if ( index
>= base
->count
)
187 base
->count
= index
+ 1;
189 // Now we connect the producer to its connected consumer
190 mlt_service_connect( producer
, this );
192 // Close the current service
193 mlt_service_close( current
);
195 // Inform caller that all went well
204 /** Disconnect this service from its consumer.
207 static void mlt_service_disconnect( mlt_service
this )
211 // Get the service base
212 mlt_service_base
*base
= this->local
;
219 /** Obtain the consumer this service is connected to.
222 mlt_service
mlt_service_consumer( mlt_service
this )
224 // Get the service base
225 mlt_service_base
*base
= this->local
;
227 // Return the connected consumer
231 /** Obtain the producer this service is connected to.
234 mlt_service
mlt_service_producer( mlt_service
this )
236 // Get the service base
237 mlt_service_base
*base
= this->local
;
239 // Return the connected producer
240 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
243 /** Associate this service to the consumer.
246 static void mlt_service_connect( mlt_service
this, mlt_service that
)
250 // Get the service base
251 mlt_service_base
*base
= this->local
;
253 // There's a bit more required here...
258 /** Get the first connected producer service.
261 mlt_service
mlt_service_get_producer( mlt_service
this )
263 mlt_service producer
= NULL
;
265 // Get the service base
266 mlt_service_base
*base
= this->local
;
268 if ( base
->in
!= NULL
)
269 producer
= base
->in
[ 0 ];
274 /** Default implementation of get_frame.
277 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
279 mlt_service_base
*base
= this->local
;
280 if ( index
< base
->count
)
282 mlt_service producer
= base
->in
[ index
];
283 if ( producer
!= NULL
)
284 return mlt_service_get_frame( producer
, frame
, index
);
286 *frame
= mlt_frame_init( );
290 /** Return the properties object.
293 mlt_properties
mlt_service_properties( mlt_service self
)
295 return self
!= NULL ?
&self
->parent
: NULL
;
298 /** Recursively apply attached filters
301 void mlt_service_apply_filters( mlt_service
this, mlt_frame frame
, int index
)
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" );
311 if ( index
== 0 || mlt_properties_get_int( service_properties
, "_filter_private" ) == 0 )
313 // Process the frame with the attached filters
314 for ( i
= 0; i
< base
->filter_count
; i
++ )
316 if ( base
->filters
[ i
] != NULL
)
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 ) ) )
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 );
335 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
337 if ( this != NULL
&& this->get_frame
!= NULL
)
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
);
346 if ( in
>=0 && out
> 0 )
348 properties
= mlt_frame_properties( *frame
);
349 mlt_properties_set_position( properties
, "in", in
);
350 mlt_properties_set_position( properties
, "out", out
);
352 mlt_service_apply_filters( this, *frame
, 1 );
356 *frame
= mlt_frame_init( );
360 static void mlt_service_filter_changed( mlt_service owner
, mlt_service
this )
362 mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL
);
368 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
370 int error
= this == NULL
|| filter
== NULL
;
374 mlt_properties properties
= mlt_service_properties( this );
375 mlt_service_base
*base
= this->local
;
377 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
378 if ( base
->filters
[ i
] == filter
)
383 if ( base
->filter_count
== base
->filter_size
)
385 base
->filter_size
+= 10;
386 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
389 if ( base
->filters
!= NULL
)
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
);
410 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
412 int error
= this == NULL
|| filter
== NULL
;
416 mlt_service_base
*base
= this->local
;
417 mlt_properties properties
= mlt_service_properties( this );
419 for ( i
= 0; i
< base
->filter_count
; i
++ )
420 if ( base
->filters
[ i
] == filter
)
423 if ( i
< base
->filter_count
)
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
);
437 /** Retrieve a filter.
440 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
442 mlt_filter filter
= NULL
;
445 mlt_service_base
*base
= this->local
;
446 if ( index
>= 0 && index
< base
->filter_count
)
447 filter
= base
->filters
[ index
];
452 /** Close the service.
455 void mlt_service_close( mlt_service
this )
457 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
459 if ( this->close
!= NULL
)
461 this->close( this->close_object
);
465 mlt_service_base
*base
= this->local
;
467 int count
= base
->filter_count
;
468 mlt_events_block( mlt_service_properties( this ), this );
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
;
478 mlt_properties_close( &this->parent
);