+/** The property-changed event handler.
+ *
+ * \private \memberof mlt_service_s
+ * \param owner ignored
+ * \param self the service on which the "property-changed" event is fired
+ * \param name the name of the property that changed
+ */
+
+static void mlt_service_filter_property_changed( mlt_service owner, mlt_service self, char *name )
+{
+ mlt_events_fire( MLT_SERVICE_PROPERTIES( self ), "property-changed", name, NULL );
+}
+
+/** Attach a filter.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param filter the filter to attach
+ * \return true if there was an error
+ */
+
+int mlt_service_attach( mlt_service self, mlt_filter filter )
+{
+ int error = self == NULL || filter == NULL;
+ if ( error == 0 )
+ {
+ int i = 0;
+ mlt_properties properties = MLT_SERVICE_PROPERTIES( self );
+ mlt_service_base *base = self->local;
+
+ for ( i = 0; error == 0 && i < base->filter_count; i ++ )
+ if ( base->filters[ i ] == filter )
+ error = 1;
+
+ if ( error == 0 )
+ {
+ if ( base->filter_count == base->filter_size )
+ {
+ base->filter_size += 10;
+ base->filters = realloc( base->filters, base->filter_size * sizeof( mlt_filter ) );
+ }
+
+ if ( base->filters != NULL )
+ {
+ mlt_properties props = MLT_FILTER_PROPERTIES( filter );
+ mlt_properties_inc_ref( MLT_FILTER_PROPERTIES( filter ) );
+ base->filters[ base->filter_count ++ ] = filter;
+ mlt_properties_set_data( props, "service", self, 0, NULL, NULL );
+ mlt_events_fire( properties, "service-changed", NULL );
+ mlt_events_fire( props, "service-changed", NULL );
+ mlt_service cp = mlt_properties_get_data( properties, "_cut_parent", NULL );
+ if ( cp )
+ mlt_events_fire( MLT_SERVICE_PROPERTIES(cp), "service-changed", NULL );
+ mlt_events_listen( props, self, "service-changed", ( mlt_listener )mlt_service_filter_changed );
+ mlt_events_listen( props, self, "property-changed", ( mlt_listener )mlt_service_filter_property_changed );
+ }
+ else
+ {
+ error = 2;
+ }
+ }
+ }
+ return error;
+}
+
+/** Detach a filter.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param filter the filter to detach
+ * \return true if there was an error
+ */
+
+int mlt_service_detach( mlt_service self, mlt_filter filter )
+{
+ int error = self == NULL || filter == NULL;
+ if ( error == 0 )
+ {
+ int i = 0;
+ mlt_service_base *base = self->local;
+ mlt_properties properties = MLT_SERVICE_PROPERTIES( self );
+
+ for ( i = 0; i < base->filter_count; i ++ )
+ if ( base->filters[ i ] == filter )
+ break;
+
+ if ( i < base->filter_count )
+ {
+ base->filters[ i ] = NULL;
+ for ( i ++ ; i < base->filter_count; i ++ )
+ base->filters[ i - 1 ] = base->filters[ i ];
+ base->filter_count --;
+ mlt_events_disconnect( MLT_FILTER_PROPERTIES( filter ), self );
+ mlt_filter_close( filter );
+ mlt_events_fire( properties, "service-changed", NULL );
+ }
+ }
+ return error;
+}
+
+/** Get the number of filters attached.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \return the number of attached filters or -1 if there was an error
+ */
+
+int mlt_service_filter_count( mlt_service self )
+{
+ int result = -1;
+ if ( self )
+ {
+ mlt_service_base *base = self->local;
+ result = base->filter_count;
+ }
+ return result;
+}
+
+/** Reorder the attached filters.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param from the current index value of the filter to move
+ * \param to the new index value for the filter specified in \p from
+ * \return true if there was an error
+ */
+
+int mlt_service_move_filter( mlt_service self, int from, int to )
+{
+ int error = -1;
+ if ( self )
+ {
+ mlt_service_base *base = self->local;
+ if ( from < 0 ) from = 0;
+ if ( from >= base->filter_count ) from = base->filter_count - 1;
+ if ( to < 0 ) to = 0;
+ if ( to >= base->filter_count ) to = base->filter_count - 1;
+ if ( from != to && base->filter_count > 1 )
+ {
+ mlt_filter filter = base->filters[from];
+ int i;
+ if ( from > to )
+ {
+ for ( i = from; i > to; i-- )
+ base->filters[i] = base->filters[i - 1];
+ }
+ else
+ {
+ for ( i = from; i < to; i++ )
+ base->filters[i] = base->filters[i + 1];
+ }
+ base->filters[to] = filter;
+ mlt_events_fire( MLT_SERVICE_PROPERTIES(self), "service-changed", NULL );
+ error = 0;
+ }
+ }
+ return error;
+}
+
+/** Retrieve an attached filter.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param index which one of potentially multiple filters
+ * \return the filter or null if there was an error
+ */
+
+mlt_filter mlt_service_filter( mlt_service self, int index )
+{
+ mlt_filter filter = NULL;
+ if ( self != NULL )
+ {
+ mlt_service_base *base = self->local;
+ if ( index >= 0 && index < base->filter_count )
+ filter = base->filters[ index ];
+ }
+ return filter;
+}
+
+/** Retrieve the profile.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \return the profile
+ */
+
+mlt_profile mlt_service_profile( mlt_service self )
+{
+ return self? mlt_properties_get_data( MLT_SERVICE_PROPERTIES( self ), "_profile", NULL ) : NULL;
+}
+
+/** Set the profile for a service.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param profile the profile to set onto the service
+ */
+
+void mlt_service_set_profile( mlt_service self, mlt_profile profile )
+{
+ mlt_properties_set_data( MLT_SERVICE_PROPERTIES( self ), "_profile", profile, 0, NULL, NULL );
+}
+
+/** Destroy a service.
+ *
+ * \public \memberof mlt_service_s
+ * \param self the service to destroy
+ */
+
+void mlt_service_close( mlt_service self )
+{
+ if ( self != NULL && mlt_properties_dec_ref( MLT_SERVICE_PROPERTIES( self ) ) <= 0 )
+ {
+ if ( self->close != NULL )
+ {
+ self->close( self->close_object );
+ }
+ else
+ {
+ mlt_service_base *base = self->local;
+ int i = 0;
+ int count = base->filter_count;
+ mlt_events_block( MLT_SERVICE_PROPERTIES( self ), self );
+ while( count -- )
+ mlt_service_detach( self, base->filters[ 0 ] );
+ free( base->filters );
+ for ( i = 0; i < base->count; i ++ )
+ if ( base->in[ i ] != NULL )
+ mlt_service_close( base->in[ i ] );
+ self->parent.close = NULL;
+ free( base->in );
+ pthread_mutex_destroy( &base->mutex );
+ free( base );
+ mlt_properties_close( &self->parent );
+ }
+ }
+}
+
+/** Release a service's cache items.
+ *
+ * \private \memberof mlt_service_s
+ * \param self a service
+ */
+
+void mlt_service_cache_purge( mlt_service self )
+{
+ mlt_properties caches = mlt_properties_get_data( mlt_global_properties(), "caches", NULL );
+
+ if ( caches )
+ {
+ int i = mlt_properties_count( caches );
+ while ( i-- )
+ {
+ mlt_cache_purge( mlt_properties_get_data_at( caches, i, NULL ), self );
+ mlt_properties_set_data( mlt_global_properties(), mlt_properties_get_name( caches, i ), NULL, 0, NULL, NULL );
+ }
+ }
+}
+
+/** Lookup the cache object for a service.
+ *
+ * \private \memberof mlt_service_s
+ * \param self a service
+ * \param name a name for the object
+ * \return a cache
+ */
+
+static mlt_cache get_cache( mlt_service self, const char *name )
+{
+ mlt_cache result = NULL;
+ mlt_properties caches = mlt_properties_get_data( mlt_global_properties(), "caches", NULL );
+
+ if ( !caches )
+ {
+ caches = mlt_properties_new();
+ mlt_properties_set_data( mlt_global_properties(), "caches", caches, 0, ( mlt_destructor )mlt_properties_close, NULL );
+ }
+ if ( caches )
+ {
+ result = mlt_properties_get_data( caches, name, NULL );
+ if ( !result )
+ {
+ result = mlt_cache_init();
+ mlt_properties_set_data( caches, name, result, 0, ( mlt_destructor )mlt_cache_close, NULL );
+ }
+ }
+
+ return result;
+}
+
+/** Put an object into a service's cache.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param name a name for the object that is unique to the service class, but not to the instance
+ * \param data an opaque pointer to the object to put into the cache
+ * \param size the number of bytes pointed to by data
+ * \param destructor a function that releases the data
+ */
+
+void mlt_service_cache_put( mlt_service self, const char *name, void* data, int size, mlt_destructor destructor )
+{
+ mlt_log( self, MLT_LOG_DEBUG, "%s: name %s object %p data %p\n", __FUNCTION__, name, self, data );
+ mlt_cache cache = get_cache( self, name );
+
+ if ( cache )
+ mlt_cache_put( cache, self, data, size, destructor );
+}
+
+/** Get an object from a service's cache.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param name a name for the object that is unique to the service class, but not to the instance
+ * \return a cache item or NULL if an object is not found
+ * \see mlt_cache_item_data
+ */
+
+mlt_cache_item mlt_service_cache_get( mlt_service self, const char *name )
+{
+ mlt_log( self, MLT_LOG_DEBUG, "%s: name %s object %p\n", __FUNCTION__, name, self );
+ mlt_cache_item result = NULL;
+ mlt_cache cache = get_cache( self, name );
+
+ if ( cache )
+ result = mlt_cache_get( cache, self );
+
+ return result;
+}
+
+/** Set the number of items to cache for the named cache.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param name a name for the object that is unique to the service class, but not to the instance
+ * \param size the number of items to cache
+ */
+
+void mlt_service_cache_set_size( mlt_service self, const char *name, int size )
+{
+ mlt_cache cache = get_cache( self, name );
+ if ( cache )
+ mlt_cache_set_size( cache, size );
+}
+
+/** Get the current maximum size of the named cache.
+ *
+ * \public \memberof mlt_service_s
+ * \param self a service
+ * \param name a name for the object that is unique to the service class, but not to the instance
+ * \return the current maximum number of items to cache or zero if there is an error
+ */
+
+int mlt_service_cache_get_size( mlt_service self, const char *name )
+{
+ mlt_cache cache = get_cache( self, name );
+ if ( cache )
+ return mlt_cache_get_size( cache );
+ else
+ return 0;
+}