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 // Hmm - special case for cuts - apply filters from the parent first
312 if ( mlt_properties_get_int( service_properties
, "_cut" ) )
314 mlt_service_apply_filters( ( mlt_service
)mlt_properties_get_data( service_properties
, "_cut_parent", NULL
), frame
, 0 );
316 mlt_frame_set_position( frame
, position
);
319 if ( index
== 0 || mlt_properties_get_int( service_properties
, "_filter_private" ) == 0 )
321 // Process the frame with the attached filters
322 for ( i
= 0; i
< base
->filter_count
; i
++ )
324 if ( base
->filters
[ i
] != NULL
)
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 ) ) )
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
);
341 if ( mlt_properties_get_int( service_properties
, "_cut" ) )
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
);
352 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
354 if ( this != NULL
&& this->get_frame
!= NULL
)
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
);
363 if ( in
>=0 && out
> 0 )
365 properties
= mlt_frame_properties( *frame
);
366 mlt_properties_set_position( properties
, "in", in
);
367 mlt_properties_set_position( properties
, "out", out
);
369 mlt_service_apply_filters( this, *frame
, 1 );
373 *frame
= mlt_frame_init( );
377 static void mlt_service_filter_changed( mlt_service owner
, mlt_service
this )
379 mlt_events_fire( mlt_service_properties( this ), "service-changed", NULL
);
385 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
387 int error
= this == NULL
|| filter
== NULL
;
391 mlt_properties properties
= mlt_service_properties( this );
392 mlt_service_base
*base
= this->local
;
394 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
395 if ( base
->filters
[ i
] == filter
)
400 if ( base
->filter_count
== base
->filter_size
)
402 base
->filter_size
+= 10;
403 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
406 if ( base
->filters
!= NULL
)
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
);
427 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
429 int error
= this == NULL
|| filter
== NULL
;
433 mlt_service_base
*base
= this->local
;
434 mlt_properties properties
= mlt_service_properties( this );
436 for ( i
= 0; i
< base
->filter_count
; i
++ )
437 if ( base
->filters
[ i
] == filter
)
440 if ( i
< base
->filter_count
)
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
);
454 /** Retrieve a filter.
457 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
459 mlt_filter filter
= NULL
;
462 mlt_service_base
*base
= this->local
;
463 if ( index
>= 0 && index
< base
->filter_count
)
464 filter
= base
->filters
[ index
];
469 /** Close the service.
472 void mlt_service_close( mlt_service
this )
474 if ( this != NULL
&& mlt_properties_dec_ref( mlt_service_properties( this ) ) <= 0 )
476 if ( this->close
!= NULL
)
478 this->close( this->close_object
);
482 mlt_service_base
*base
= this->local
;
484 int count
= base
->filter_count
;
485 mlt_events_block( mlt_service_properties( this ), this );
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
;
495 mlt_properties_close( &this->parent
);