Customising the miracle server part 1
[melted] / src / framework / mlt_producer.c
index c07b2b2..79038fb 100644 (file)
@@ -31,6 +31,8 @@
 */
 
 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 );
+static void mlt_producer_service_changed( mlt_service owner, mlt_producer this );
 
 /** Constructor
 */
@@ -91,12 +93,33 @@ int mlt_producer_init( mlt_producer this, void *child )
 
                        // Override service get_frame
                        parent->get_frame = producer_get_frame;
+
+                       mlt_events_listen( properties, this, "service-changed", ( mlt_listener )mlt_producer_service_changed );
+                       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 );
+}
+
+/** Listener for service changes.
+*/
+
+static void mlt_producer_service_changed( mlt_service owner, mlt_producer this )
+{
+       mlt_events_fire( mlt_producer_properties( this ), "producer-changed", NULL );
+}
+
 /** Create a new producer.
 */
 
@@ -201,6 +224,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;
@@ -221,8 +246,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;
 }
@@ -296,18 +324,8 @@ 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
@@ -326,41 +344,7 @@ static int producer_get_frame( mlt_service service, mlt_frame_ptr frame, int ind
 
 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;
+       return mlt_service_attach( mlt_producer_service( this ), filter );
 }
 
 /** Detach a filter.
@@ -368,30 +352,7 @@ int mlt_producer_attach( mlt_producer this, mlt_filter 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;
+       return mlt_service_detach( mlt_producer_service( this ), filter );
 }
 
 /** Retrieve a filter.
@@ -399,16 +360,7 @@ int mlt_producer_detach( mlt_producer this, mlt_filter 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;
+       return mlt_service_filter( mlt_producer_service( this ), index );
 }
 
 /** Close the producer.
@@ -418,13 +370,6 @@ void mlt_producer_close( mlt_producer this )
 {
        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 )