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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "mlt_service.h"
22 #include "mlt_filter.h"
23 #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.
51 pthread_mutex_t mutex
;
58 static void mlt_service_disconnect( mlt_service
this );
59 static void mlt_service_connect( mlt_service
this, mlt_service that
);
60 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
);
61 static void mlt_service_property_changed( mlt_listener
, mlt_properties owner
, mlt_service
this, void **args
);
66 int mlt_service_init( mlt_service
this, void *child
)
70 // Initialise everything to NULL
71 memset( this, 0, sizeof( struct mlt_service_s
) );
76 // Generate local space
77 this->local
= calloc( sizeof( mlt_service_base
), 1 );
79 // Associate the methods
80 this->get_frame
= service_get_frame
;
82 // Initialise the properties
83 error
= mlt_properties_init( &this->parent
, this );
86 this->parent
.close
= ( mlt_destructor
)mlt_service_close
;
87 this->parent
.close_object
= this;
89 mlt_events_init( &this->parent
);
90 mlt_events_register( &this->parent
, "service-changed", NULL
);
91 mlt_events_register( &this->parent
, "property-changed", ( mlt_transmitter
)mlt_service_property_changed
);
92 pthread_mutex_init( &( ( mlt_service_base
* )this->local
)->mutex
, NULL
);
98 static void mlt_service_property_changed( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
100 if ( listener
!= NULL
)
101 listener( owner
, this, ( char * )args
[ 0 ] );
104 void mlt_service_lock( mlt_service
this )
107 pthread_mutex_lock( &( ( mlt_service_base
* )this->local
)->mutex
);
110 void mlt_service_unlock( mlt_service
this )
113 pthread_mutex_unlock( &( ( mlt_service_base
* )this->local
)->mutex
);
116 mlt_service_type
mlt_service_identify( mlt_service
this )
118 mlt_service_type type
= invalid_type
;
121 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
122 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
123 char *resource
= mlt_properties_get( properties
, "resource" );
124 if ( mlt_type
== NULL
)
126 else if (resource
!= NULL
&& !strcmp( resource
, "<playlist>" ) )
127 type
= playlist_type
;
128 else if (resource
!= NULL
&& !strcmp( resource
, "<tractor>" ) )
130 else if (resource
!= NULL
&& !strcmp( resource
, "<multitrack>" ) )
131 type
= multitrack_type
;
132 else if ( !strcmp( mlt_type
, "producer" ) )
133 type
= producer_type
;
134 else if ( !strcmp( mlt_type
, "filter" ) )
136 else if ( !strcmp( mlt_type
, "transition" ) )
137 type
= transition_type
;
138 else if ( !strcmp( mlt_type
, "consumer" ) )
139 type
= consumer_type
;
146 /** Connect a producer service.
147 Returns: > 0 warning, == 0 success, < 0 serious error
148 1 = this service does not accept input
149 2 = the producer is invalid
150 3 = the producer is already registered with this consumer
153 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
157 // Get the service base
158 mlt_service_base
*base
= this->local
;
160 // Special case 'track' index - only works for last filter(s) in a particular chain
161 // but allows a filter to apply to the output frame regardless of which track it comes from
165 // Check if the producer is already registered with this service
166 for ( i
= 0; i
< base
->count
; i
++ )
167 if ( base
->in
[ i
] == producer
)
171 if ( index
>= base
->size
)
173 int new_size
= base
->size
+ index
+ 10;
174 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
175 if ( base
->in
!= NULL
)
177 for ( i
= base
->size
; i
< new_size
; i
++ )
178 base
->in
[ i
] = NULL
;
179 base
->size
= new_size
;
183 // If we have space, assign the input
184 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
186 // Get the current service
187 mlt_service current
= base
->in
[ index
];
189 // Increment the reference count on this producer
190 if ( producer
!= NULL
)
192 mlt_service_lock( producer
);
193 mlt_properties_inc_ref( MLT_SERVICE_PROPERTIES( producer
) );
194 mlt_service_unlock( producer
);
197 // Now we disconnect the producer service from its consumer
198 mlt_service_disconnect( producer
);
200 // Add the service to index specified
201 base
->in
[ index
] = producer
;
203 // Determine the number of active tracks
204 if ( index
>= base
->count
)
205 base
->count
= index
+ 1;
207 // Now we connect the producer to its connected consumer
208 mlt_service_connect( producer
, this );
210 // Close the current service
211 mlt_service_close( current
);
213 // Inform caller that all went well
222 /** Disconnect this service from its consumer.
225 static void mlt_service_disconnect( mlt_service
this )
229 // Get the service base
230 mlt_service_base
*base
= this->local
;
237 /** Obtain the consumer this service is connected to.
240 mlt_service
mlt_service_consumer( mlt_service
this )
242 // Get the service base
243 mlt_service_base
*base
= this->local
;
245 // Return the connected consumer
249 /** Obtain the producer this service is connected to.
252 mlt_service
mlt_service_producer( mlt_service
this )
254 // Get the service base
255 mlt_service_base
*base
= this->local
;
257 // Return the connected producer
258 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
261 /** Associate this service to the consumer.
264 static void mlt_service_connect( mlt_service
this, mlt_service that
)
268 // Get the service base
269 mlt_service_base
*base
= this->local
;
271 // There's a bit more required here...
276 /** Get the first connected producer service.
279 mlt_service
mlt_service_get_producer( mlt_service
this )
281 mlt_service producer
= NULL
;
283 // Get the service base
284 mlt_service_base
*base
= this->local
;
286 if ( base
->in
!= NULL
)
287 producer
= base
->in
[ 0 ];
292 /** Default implementation of get_frame.
295 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
297 mlt_service_base
*base
= this->local
;
298 if ( index
< base
->count
)
300 mlt_service producer
= base
->in
[ index
];
301 if ( producer
!= NULL
)
302 return mlt_service_get_frame( producer
, frame
, index
);
304 *frame
= mlt_frame_init( this );
308 /** Return the properties object.
311 mlt_properties
mlt_service_properties( mlt_service self
)
313 return self
!= NULL ?
&self
->parent
: NULL
;
316 /** Recursively apply attached filters
319 void mlt_service_apply_filters( mlt_service
this, mlt_frame frame
, int index
)
322 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
323 mlt_properties service_properties
= MLT_SERVICE_PROPERTIES( this );
324 mlt_service_base
*base
= this->local
;
325 mlt_position position
= mlt_frame_get_position( frame
);
326 mlt_position this_in
= mlt_properties_get_position( service_properties
, "in" );
327 mlt_position this_out
= mlt_properties_get_position( service_properties
, "out" );
329 if ( index
== 0 || mlt_properties_get_int( service_properties
, "_filter_private" ) == 0 )
331 // Process the frame with the attached filters
332 for ( i
= 0; i
< base
->filter_count
; i
++ )
334 if ( base
->filters
[ i
] != NULL
)
336 mlt_position in
= mlt_filter_get_in( base
->filters
[ i
] );
337 mlt_position out
= mlt_filter_get_out( base
->filters
[ i
] );
338 int disable
= mlt_properties_get_int( MLT_FILTER_PROPERTIES( base
->filters
[ i
] ), "disable" );
339 if ( !disable
&& ( ( in
== 0 && out
== 0 ) || ( position
>= in
&& ( position
<= out
|| out
== 0 ) ) ) )
341 mlt_properties_set_position( frame_properties
, "in", in
== 0 ? this_in
: in
);
342 mlt_properties_set_position( frame_properties
, "out", out
== 0 ? this_out
: out
);
343 mlt_filter_process( base
->filters
[ i
], frame
);
344 mlt_service_apply_filters( MLT_FILTER_SERVICE( base
->filters
[ i
] ), frame
, index
+ 1 );
354 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
359 mlt_service_lock( this );
361 // Ensure that the frame is NULL
364 // Only process if we have a valid service
365 if ( this != NULL
&& this->get_frame
!= NULL
)
367 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
368 mlt_position in
= mlt_properties_get_position( properties
, "in" );
369 mlt_position out
= mlt_properties_get_position( properties
, "out" );
371 result
= this->get_frame( this, frame
, index
);
375 mlt_properties_inc_ref( properties
);
376 properties
= MLT_FRAME_PROPERTIES( *frame
);
377 if ( in
>=0 && out
> 0 )
379 mlt_properties_set_position( properties
, "in", in
);
380 mlt_properties_set_position( properties
, "out", out
);
382 mlt_service_apply_filters( this, *frame
, 1 );
383 mlt_deque_push_back( MLT_FRAME_SERVICE_STACK( *frame
), this );
387 // Make sure we return a frame
388 if ( *frame
== NULL
)
389 *frame
= mlt_frame_init( this );
391 // Unlock the service
392 mlt_service_unlock( this );
397 static void mlt_service_filter_changed( mlt_service owner
, mlt_service
this )
399 mlt_events_fire( MLT_SERVICE_PROPERTIES( this ), "service-changed", NULL
);
405 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
407 int error
= this == NULL
|| filter
== NULL
;
411 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
412 mlt_service_base
*base
= this->local
;
414 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
415 if ( base
->filters
[ i
] == filter
)
420 if ( base
->filter_count
== base
->filter_size
)
422 base
->filter_size
+= 10;
423 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
426 if ( base
->filters
!= NULL
)
428 mlt_properties props
= MLT_FILTER_PROPERTIES( filter
);
429 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES( filter
) );
430 base
->filters
[ base
->filter_count
++ ] = filter
;
431 mlt_events_fire( properties
, "service-changed", NULL
);
432 mlt_events_listen( props
, this, "service-changed", ( mlt_listener
)mlt_service_filter_changed
);
433 mlt_events_listen( props
, this, "property-changed", ( mlt_listener
)mlt_service_filter_changed
);
447 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
449 int error
= this == NULL
|| filter
== NULL
;
453 mlt_service_base
*base
= this->local
;
454 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
456 for ( i
= 0; i
< base
->filter_count
; i
++ )
457 if ( base
->filters
[ i
] == filter
)
460 if ( i
< base
->filter_count
)
462 base
->filters
[ i
] = NULL
;
463 for ( i
++ ; i
< base
->filter_count
; i
++ )
464 base
->filters
[ i
- 1 ] = base
->filters
[ i
];
465 base
->filter_count
--;
466 mlt_events_disconnect( MLT_FILTER_PROPERTIES( filter
), this );
467 mlt_filter_close( filter
);
468 mlt_events_fire( properties
, "service-changed", NULL
);
474 /** Retrieve a filter.
477 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
479 mlt_filter filter
= NULL
;
482 mlt_service_base
*base
= this->local
;
483 if ( index
>= 0 && index
< base
->filter_count
)
484 filter
= base
->filters
[ index
];
489 /** Retrieve the profile.
492 mlt_profile
mlt_service_profile( mlt_service
this )
494 return mlt_properties_get_data( MLT_SERVICE_PROPERTIES( this ), "_profile", NULL
);
497 /** Close the service.
500 void mlt_service_close( mlt_service
this )
502 mlt_service_lock( this );
503 if ( this != NULL
&& mlt_properties_dec_ref( MLT_SERVICE_PROPERTIES( this ) ) <= 0 )
505 mlt_service_unlock( this );
506 if ( this->close
!= NULL
)
508 this->close( this->close_object
);
512 mlt_service_base
*base
= this->local
;
514 int count
= base
->filter_count
;
515 mlt_events_block( MLT_SERVICE_PROPERTIES( this ), this );
517 mlt_service_detach( this, base
->filters
[ 0 ] );
518 free( base
->filters
);
519 for ( i
= 0; i
< base
->count
; i
++ )
520 if ( base
->in
[ i
] != NULL
)
521 mlt_service_close( base
->in
[ i
] );
522 this->parent
.close
= NULL
;
524 pthread_mutex_destroy( &base
->mutex
);
526 mlt_properties_close( &this->parent
);
531 mlt_service_unlock( this );