First draft of event handling
[melted] / src / framework / mlt_producer.c
index fc161f5..4fda305 100644 (file)
@@ -31,6 +31,7 @@
 */
 
 static int producer_get_frame( mlt_service this, mlt_frame_ptr frame, int index );
+static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name );
 
 /** Constructor
 */
@@ -58,6 +59,13 @@ int mlt_producer_init( mlt_producer this, void *child )
                        // The parent is the service
                        mlt_service parent = &this->parent;
        
+                       // Define the parent close
+                       parent->close = ( mlt_destructor )mlt_producer_close;
+                       parent->close_object = this;
+
+                       // For convenience, we'll assume the close_object is this
+                       this->close_object = this;
+
                        // Get the properties of the parent
                        mlt_properties properties = mlt_service_properties( parent );
        
@@ -84,12 +92,24 @@ int mlt_producer_init( mlt_producer this, void *child )
 
                        // Override service get_frame
                        parent->get_frame = producer_get_frame;
+
+                       mlt_events_listen( properties, this, "property-changed", ( mlt_listener )mlt_producer_property_changed );
+                       mlt_events_register( properties, "producer-changed", NULL );
                }
        }
 
        return error;
 }
 
+/** Listener for property changes.
+*/
+
+static void mlt_producer_property_changed( mlt_service owner, mlt_producer this, char *name )
+{
+       if ( !strcmp( name, "in" ) || !strcmp( name, "out" ) || !strcmp( name, "length" ) )
+               mlt_events_fire( mlt_producer_properties( this ), "producer-changed", NULL );
+}
+
 /** Create a new producer.
 */
 
@@ -194,6 +214,8 @@ double mlt_producer_get_fps( mlt_producer this )
 
 int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_position out )
 {
+       mlt_properties properties = mlt_producer_properties( this );
+
        // Correct ins and outs if necessary
        if ( in < 0 )
                in = 0;
@@ -214,8 +236,11 @@ int mlt_producer_set_in_and_out( mlt_producer this, mlt_position in, mlt_positio
        }
 
        // Set the values
-       mlt_properties_set_position( mlt_producer_properties( this ), "in", in );
-       mlt_properties_set_position( mlt_producer_properties( this ), "out", out );
+       mlt_events_block( properties, properties );
+       mlt_properties_set_position( properties, "in", in );
+       mlt_properties_set_position( properties, "out", out );
+       mlt_events_unblock( properties, properties );
+       mlt_events_fire( properties, "producer-changed", NULL );
 
        return 0;
 }
@@ -289,8 +314,18 @@ static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int ind
        }
        else
        {
+               mlt_properties properties = mlt_producer_properties( this );
+               mlt_filter *filters = mlt_properties_get_data( properties, "_filters", NULL );
+               int count = mlt_properties_get_int( properties, "_filter_count" );
+               int i;
+
                // Get the frame from the implementation
                result = this->get_frame( this, frame, index );
+
+               // Process the frame with the attached filters
+               for ( i = 0; i < count; i ++ )
+                       if ( filters[ i ] != NULL )
+                               mlt_filter_process( filters[ i ], *frame );
        }
 
        // Copy the fps and speed of the producer onto the frame
@@ -304,13 +339,115 @@ static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int ind
        return 0;
 }
 
+/** Attach a filter.
+*/
+
+int mlt_producer_attach( mlt_producer this, mlt_filter filter )
+{
+       int error = this == NULL || filter == NULL;
+       if ( error == 0 )
+       {
+               int i = 0;
+               int size = 0;
+               mlt_properties properties = mlt_producer_properties( this );
+               mlt_filter *filters = mlt_properties_get_data( properties, "_filters", &size );
+               int count = mlt_properties_get_int( properties, "_filter_count" );
+
+               for ( i = 0; error == 0 && i < count; i ++ )
+                       if ( filters[ i ] == filter )
+                               error = 1;
+
+               if ( error == 0 )
+               {
+                       if ( count == size )
+                       {
+                               size += 10;
+                               filters = realloc( filters, size * sizeof( mlt_filter ) );
+                               mlt_properties_set_data( properties, "_filters", filters, size, NULL, NULL );
+                       }
+
+                       if ( filters != NULL )
+                       {
+                               mlt_properties_inc_ref( mlt_filter_properties( filter ) );
+                               filters[ count ++ ] = filter;
+                               mlt_properties_set_int( properties, "_filter_count", count );
+                       }
+                       else
+                       {
+                               error = 2;
+                       }
+               }
+       }
+       return error;
+}
+
+/** Detach a filter.
+*/
+
+int mlt_producer_detach( mlt_producer this, mlt_filter filter )
+{
+       int error = this == NULL || filter == NULL;
+       if ( error == 0 )
+       {
+               int i = 0;
+               int size = 0;
+               mlt_properties properties = mlt_producer_properties( this );
+               mlt_filter *filters = mlt_properties_get_data( properties, "_filters", &size );
+               int count = mlt_properties_get_int( properties, "_filter_count" );
+
+               for ( i = 0; i < count; i ++ )
+                       if ( filters[ i ] == filter )
+                               break;
+
+               if ( i < count )
+               {
+                       mlt_filter filter = filters[ i ];
+                       filters[ i ] = NULL;
+                       for ( i ++ ; i < count; i ++ )
+                               filters[ i - 1 ] = filters[ i ];
+                       mlt_properties_set_int( properties, "_filter_count", -- count );
+                       mlt_filter_close( filter );
+               }
+       }
+       return error;
+}
+
+/** Retrieve a filter.
+*/
+
+mlt_filter mlt_producer_filter( mlt_producer this, int index )
+{
+       mlt_filter filter = NULL;
+       if ( this != NULL )
+       {
+               mlt_properties properties = mlt_producer_properties( this );
+               mlt_filter *filters = mlt_properties_get_data( properties, "_filters", NULL );
+               int count = mlt_properties_get_int( properties, "_filter_count" );
+               if ( index >= 0 && index < count )
+                       filter = filters[ index ];
+       }
+       return filter;
+}
+
 /** Close the producer.
 */
 
 void mlt_producer_close( mlt_producer this )
 {
-       if ( this->close != NULL )
-               this->close( this );
-       else
-               mlt_service_close( &this->parent );
+       if ( this != NULL && mlt_properties_dec_ref( mlt_producer_properties( this ) ) <= 0 )
+       {
+               mlt_properties properties = mlt_producer_properties( this );
+               mlt_filter *filters = mlt_properties_get_data( properties, "_filters", NULL );
+               int count = mlt_properties_get_int( properties, "_filter_count" );
+
+               while( count -- )
+                       mlt_producer_detach( this, filters[ 0 ] );
+
+               this->parent.close = NULL;
+
+               if ( this->close != NULL )
+                       this->close( this->close_object );
+               else
+                       mlt_service_close( &this->parent );
+       }
 }