3 * \brief interface definition for all service classes
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_service.h"
25 #include "mlt_filter.h"
26 #include "mlt_frame.h"
34 The base service implements a null frame producing service - as such,
35 it is functional without extension and will produce test cards frames
36 and PAL sized audio frames.
38 PLEASE DO NOT CHANGE THIS BEHAVIOUR!!! OVERRIDE THE METHODS THAT
39 CONTROL THIS IN EXTENDING CLASSES.
42 /** \brief private service definition */
53 pthread_mutex_t mutex
;
60 static void mlt_service_disconnect( mlt_service
this );
61 static void mlt_service_connect( mlt_service
this, mlt_service that
);
62 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
);
63 static void mlt_service_property_changed( mlt_listener
, mlt_properties owner
, mlt_service
this, void **args
);
65 /** Initialize a service.
67 * \public \memberof mlt_service_s
68 * \param this the service structure to initialize
69 * \param child pointer to the child object for the subclass
70 * \return true if there was an error
73 int mlt_service_init( mlt_service
this, void *child
)
77 // Initialise everything to NULL
78 memset( this, 0, sizeof( struct mlt_service_s
) );
83 // Generate local space
84 this->local
= calloc( sizeof( mlt_service_base
), 1 );
86 // Associate the methods
87 this->get_frame
= service_get_frame
;
89 // Initialise the properties
90 error
= mlt_properties_init( &this->parent
, this );
93 this->parent
.close
= ( mlt_destructor
)mlt_service_close
;
94 this->parent
.close_object
= this;
96 mlt_events_init( &this->parent
);
97 mlt_events_register( &this->parent
, "service-changed", NULL
);
98 mlt_events_register( &this->parent
, "property-changed", ( mlt_transmitter
)mlt_service_property_changed
);
99 pthread_mutex_init( &( ( mlt_service_base
* )this->local
)->mutex
, NULL
);
105 /** The transmitter for property changes.
107 * Invokes the listener.
109 * \private \memberof mlt_service_s
110 * \param listener a function pointer that will be invoked
111 * \param owner a properties list that will be passed to \p listener
112 * \param this a service that will be passed to \p listener
113 * \param args an array of pointers - the first entry is passed as a string to \p listener
116 static void mlt_service_property_changed( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
118 if ( listener
!= NULL
)
119 listener( owner
, this, ( char * )args
[ 0 ] );
122 /** Acquire a mutual exclusion lock on this service.
124 * \public \memberof mlt_service_s
125 * \param this the service to lock
128 void mlt_service_lock( mlt_service
this )
131 pthread_mutex_lock( &( ( mlt_service_base
* )this->local
)->mutex
);
134 /** Release a mutual exclusion lock on this service.
136 * \public \memberof mlt_service_s
137 * \param this the service to unlock
140 void mlt_service_unlock( mlt_service
this )
143 pthread_mutex_unlock( &( ( mlt_service_base
* )this->local
)->mutex
);
146 /** Identify the subclass of the service.
148 * \public \memberof mlt_service_s
149 * \param this a service
150 * \return the subclass
153 mlt_service_type
mlt_service_identify( mlt_service
this )
155 mlt_service_type type
= invalid_type
;
158 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
159 char *mlt_type
= mlt_properties_get( properties
, "mlt_type" );
160 char *resource
= mlt_properties_get( properties
, "resource" );
161 if ( mlt_type
== NULL
)
163 else if (resource
!= NULL
&& !strcmp( resource
, "<playlist>" ) )
164 type
= playlist_type
;
165 else if (resource
!= NULL
&& !strcmp( resource
, "<tractor>" ) )
167 else if (resource
!= NULL
&& !strcmp( resource
, "<multitrack>" ) )
168 type
= multitrack_type
;
169 else if ( !strcmp( mlt_type
, "producer" ) )
170 type
= producer_type
;
171 else if ( !strcmp( mlt_type
, "filter" ) )
173 else if ( !strcmp( mlt_type
, "transition" ) )
174 type
= transition_type
;
175 else if ( !strcmp( mlt_type
, "consumer" ) )
176 type
= consumer_type
;
183 /** Connect a producer to the service.
185 * \public \memberof mlt_service_s
186 * \param this a service
187 * \param producer a producer
188 * \param index which of potentially multiple producers to this service (0 based)
189 * \return > 0 warning, == 0 success, < 0 serious error,
190 * 1 = this service does not accept input,
191 * 2 = the producer is invalid,
192 * 3 = the producer is already registered with this consumer
195 int mlt_service_connect_producer( mlt_service
this, mlt_service producer
, int index
)
199 // Get the service base
200 mlt_service_base
*base
= this->local
;
202 // Special case 'track' index - only works for last filter(s) in a particular chain
203 // but allows a filter to apply to the output frame regardless of which track it comes from
207 // Check if the producer is already registered with this service
208 for ( i
= 0; i
< base
->count
; i
++ )
209 if ( base
->in
[ i
] == producer
)
213 if ( index
>= base
->size
)
215 int new_size
= base
->size
+ index
+ 10;
216 base
->in
= realloc( base
->in
, new_size
* sizeof( mlt_service
) );
217 if ( base
->in
!= NULL
)
219 for ( i
= base
->size
; i
< new_size
; i
++ )
220 base
->in
[ i
] = NULL
;
221 base
->size
= new_size
;
225 // If we have space, assign the input
226 if ( base
->in
!= NULL
&& index
>= 0 && index
< base
->size
)
228 // Get the current service
229 mlt_service current
= base
->in
[ index
];
231 // Increment the reference count on this producer
232 if ( producer
!= NULL
)
233 mlt_properties_inc_ref( MLT_SERVICE_PROPERTIES( producer
) );
235 // Now we disconnect the producer service from its consumer
236 mlt_service_disconnect( producer
);
238 // Add the service to index specified
239 base
->in
[ index
] = producer
;
241 // Determine the number of active tracks
242 if ( index
>= base
->count
)
243 base
->count
= index
+ 1;
245 // Now we connect the producer to its connected consumer
246 mlt_service_connect( producer
, this );
248 // Close the current service
249 mlt_service_close( current
);
251 // Inform caller that all went well
260 /** Disconnect this service from its consumer.
262 * \public \memberof mlt_service_s
263 * \param this a service
266 static void mlt_service_disconnect( mlt_service
this )
270 // Get the service base
271 mlt_service_base
*base
= this->local
;
278 /** Obtain the consumer this service is connected to.
280 * \public \memberof mlt_service_s
281 * \param this a service
282 * \return the consumer
285 mlt_service
mlt_service_consumer( mlt_service
this )
287 // Get the service base
288 mlt_service_base
*base
= this->local
;
290 // Return the connected consumer
294 /** Obtain the producer this service is connected to.
296 * \public \memberof mlt_service_s
297 * \param this a service
298 * \return the last-most producer
301 mlt_service
mlt_service_producer( mlt_service
this )
303 // Get the service base
304 mlt_service_base
*base
= this->local
;
306 // Return the connected producer
307 return base
->count
> 0 ? base
->in
[ base
->count
- 1 ] : NULL
;
310 /** Associate this service to a consumer.
312 * Overwrites connection to any existing consumer.
313 * \private \memberof mlt_service_s
314 * \param this a service
315 * \param that a consumer
318 static void mlt_service_connect( mlt_service
this, mlt_service that
)
322 // Get the service base
323 mlt_service_base
*base
= this->local
;
325 // There's a bit more required here...
330 /** Get the first connected producer.
332 * \public \memberof mlt_service_s
333 * \param this a service
334 * \return the first producer
337 mlt_service
mlt_service_get_producer( mlt_service
this )
339 mlt_service producer
= NULL
;
341 // Get the service base
342 mlt_service_base
*base
= this->local
;
344 if ( base
->in
!= NULL
)
345 producer
= base
->in
[ 0 ];
350 /** Default implementation of the get_frame virtual function.
352 * \private \memberof mlt_service_s
353 * \param this a service
354 * \param[out] frame a frame by reference
355 * \param index as determined by the producer
359 static int service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
361 mlt_service_base
*base
= this->local
;
362 if ( index
< base
->count
)
364 mlt_service producer
= base
->in
[ index
];
365 if ( producer
!= NULL
)
366 return mlt_service_get_frame( producer
, frame
, index
);
368 *frame
= mlt_frame_init( this );
372 /** Return the properties object.
374 * \public \memberof mlt_service_s
375 * \param this a service
376 * \return the properties
379 mlt_properties
mlt_service_properties( mlt_service
this )
381 return this != NULL ?
&this->parent
: NULL
;
384 /** Recursively apply attached filters.
386 * \public \memberof mlt_service_s
387 * \param this a service
388 * \param frame a frame
389 * \param index used to track depth of recursion, top caller should supply 0
392 void mlt_service_apply_filters( mlt_service
this, mlt_frame frame
, int index
)
395 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
396 mlt_properties service_properties
= MLT_SERVICE_PROPERTIES( this );
397 mlt_service_base
*base
= this->local
;
398 mlt_position position
= mlt_frame_get_position( frame
);
399 mlt_position this_in
= mlt_properties_get_position( service_properties
, "in" );
400 mlt_position this_out
= mlt_properties_get_position( service_properties
, "out" );
402 if ( index
== 0 || mlt_properties_get_int( service_properties
, "_filter_private" ) == 0 )
404 // Process the frame with the attached filters
405 for ( i
= 0; i
< base
->filter_count
; i
++ )
407 if ( base
->filters
[ i
] != NULL
)
409 mlt_position in
= mlt_filter_get_in( base
->filters
[ i
] );
410 mlt_position out
= mlt_filter_get_out( base
->filters
[ i
] );
411 int disable
= mlt_properties_get_int( MLT_FILTER_PROPERTIES( base
->filters
[ i
] ), "disable" );
412 if ( !disable
&& ( ( in
== 0 && out
== 0 ) || ( position
>= in
&& ( position
<= out
|| out
== 0 ) ) ) )
414 mlt_properties_set_position( frame_properties
, "in", in
== 0 ? this_in
: in
);
415 mlt_properties_set_position( frame_properties
, "out", out
== 0 ? this_out
: out
);
416 mlt_filter_process( base
->filters
[ i
], frame
);
417 mlt_service_apply_filters( MLT_FILTER_SERVICE( base
->filters
[ i
] ), frame
, index
+ 1 );
426 * \public \memberof mlt_service_s
427 * \param this a service
428 * \param[out] frame a frame by reference
429 * \param index as determined by the producer
430 * \return true if there was an error
433 int mlt_service_get_frame( mlt_service
this, mlt_frame_ptr frame
, int index
)
438 mlt_service_lock( this );
440 // Ensure that the frame is NULL
443 // Only process if we have a valid service
444 if ( this != NULL
&& this->get_frame
!= NULL
)
446 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
447 mlt_position in
= mlt_properties_get_position( properties
, "in" );
448 mlt_position out
= mlt_properties_get_position( properties
, "out" );
450 result
= this->get_frame( this, frame
, index
);
454 mlt_properties_inc_ref( properties
);
455 properties
= MLT_FRAME_PROPERTIES( *frame
);
456 if ( in
>=0 && out
> 0 )
458 mlt_properties_set_position( properties
, "in", in
);
459 mlt_properties_set_position( properties
, "out", out
);
461 mlt_service_apply_filters( this, *frame
, 1 );
462 mlt_deque_push_back( MLT_FRAME_SERVICE_STACK( *frame
), this );
466 // Make sure we return a frame
467 if ( *frame
== NULL
)
468 *frame
= mlt_frame_init( this );
470 // Unlock the service
471 mlt_service_unlock( this );
476 /** The service-changed event handler.
478 * \private \memberof mlt_service_s
479 * \param owner ignored
480 * \param this the service on which the "service-changed" event is fired
483 static void mlt_service_filter_changed( mlt_service owner
, mlt_service
this )
485 mlt_events_fire( MLT_SERVICE_PROPERTIES( this ), "service-changed", NULL
);
490 * \public \memberof mlt_service_s
491 * \param this a service
492 * \param filter the filter to attach
493 * \return true if there was an error
496 int mlt_service_attach( mlt_service
this, mlt_filter filter
)
498 int error
= this == NULL
|| filter
== NULL
;
502 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
503 mlt_service_base
*base
= this->local
;
505 for ( i
= 0; error
== 0 && i
< base
->filter_count
; i
++ )
506 if ( base
->filters
[ i
] == filter
)
511 if ( base
->filter_count
== base
->filter_size
)
513 base
->filter_size
+= 10;
514 base
->filters
= realloc( base
->filters
, base
->filter_size
* sizeof( mlt_filter
) );
517 if ( base
->filters
!= NULL
)
519 mlt_properties props
= MLT_FILTER_PROPERTIES( filter
);
520 mlt_properties_inc_ref( MLT_FILTER_PROPERTIES( filter
) );
521 base
->filters
[ base
->filter_count
++ ] = filter
;
522 mlt_events_fire( properties
, "service-changed", NULL
);
523 mlt_events_listen( props
, this, "service-changed", ( mlt_listener
)mlt_service_filter_changed
);
524 mlt_events_listen( props
, this, "property-changed", ( mlt_listener
)mlt_service_filter_changed
);
537 * \public \memberof mlt_service_s
538 * \param this a service
539 * \param filter the filter to detach
540 * \return true if there was an error
543 int mlt_service_detach( mlt_service
this, mlt_filter filter
)
545 int error
= this == NULL
|| filter
== NULL
;
549 mlt_service_base
*base
= this->local
;
550 mlt_properties properties
= MLT_SERVICE_PROPERTIES( this );
552 for ( i
= 0; i
< base
->filter_count
; i
++ )
553 if ( base
->filters
[ i
] == filter
)
556 if ( i
< base
->filter_count
)
558 base
->filters
[ i
] = NULL
;
559 for ( i
++ ; i
< base
->filter_count
; i
++ )
560 base
->filters
[ i
- 1 ] = base
->filters
[ i
];
561 base
->filter_count
--;
562 mlt_events_disconnect( MLT_FILTER_PROPERTIES( filter
), this );
563 mlt_filter_close( filter
);
564 mlt_events_fire( properties
, "service-changed", NULL
);
570 /** Retrieve a filter.
572 * \public \memberof mlt_service_s
573 * \param this a service
574 * \param index which one of potentially multiple filters
575 * \return the filter or null if there was an error
578 mlt_filter
mlt_service_filter( mlt_service
this, int index
)
580 mlt_filter filter
= NULL
;
583 mlt_service_base
*base
= this->local
;
584 if ( index
>= 0 && index
< base
->filter_count
)
585 filter
= base
->filters
[ index
];
590 /** Retrieve the profile.
592 * \public \memberof mlt_service_s
593 * \param this a service
594 * \return the profile
597 mlt_profile
mlt_service_profile( mlt_service
this )
599 return mlt_properties_get_data( MLT_SERVICE_PROPERTIES( this ), "_profile", NULL
);
602 /** Destroy a service.
604 * \public \memberof mlt_service_s
605 * \param this the service to destroy
608 void mlt_service_close( mlt_service
this )
610 if ( this != NULL
&& mlt_properties_dec_ref( MLT_SERVICE_PROPERTIES( this ) ) <= 0 )
612 if ( this->close
!= NULL
)
614 this->close( this->close_object
);
618 mlt_service_base
*base
= this->local
;
620 int count
= base
->filter_count
;
621 mlt_events_block( MLT_SERVICE_PROPERTIES( this ), this );
623 mlt_service_detach( this, base
->filters
[ 0 ] );
624 free( base
->filters
);
625 for ( i
= 0; i
< base
->count
; i
++ )
626 if ( base
->in
[ i
] != NULL
)
627 mlt_service_close( base
->in
[ i
] );
628 this->parent
.close
= NULL
;
630 pthread_mutex_destroy( &base
->mutex
);
632 mlt_properties_close( &this->parent
);
637 mlt_service_unlock( this );