Extendable factories; general producer related modifications; westley storage; sdl_st...
[melted] / src / modules / westley / producer_westley.c
index 46fd809..2f3a458 100644 (file)
@@ -54,7 +54,7 @@ enum service_type
        mlt_transition_type,
        mlt_consumer_type,
        mlt_field_type,
-       mlt_service_type,
+       mlt_services_type,
        mlt_dummy_filter_type,
        mlt_dummy_transition_type,
        mlt_dummy_producer_type,
@@ -292,17 +292,9 @@ static void on_start_tractor( deserialise_context context, const xmlChar *name,
 
        track_service( context->destructors, service, (mlt_destructor) mlt_tractor_close );
 
-       mlt_properties_set_position( properties, "length", 0 );
-
        for ( ; atts != NULL && *atts != NULL; atts += 2 )
                mlt_properties_set( mlt_service_properties( service ), (char*) atts[0], (char*) atts[1] );
 
-       if ( mlt_properties_get_position( properties, "length" ) < mlt_properties_get_position( properties, "out" ) )
-       {
-               mlt_position length = mlt_properties_get_position( properties, "out" ) + 1;
-               mlt_properties_set_position( properties, "length", length );
-       }
-
        if ( mlt_properties_get( properties, "id" ) != NULL )
                mlt_properties_set_data( context->producer_map, mlt_properties_get( properties, "id" ), service, 0, NULL, NULL );
        
@@ -348,7 +340,6 @@ static void on_start_multitrack( deserialise_context context, const xmlChar *nam
        {
                mlt_service service = MLT_SERVICE( mlt_tractor_multitrack( MLT_TRACTOR( parent ) ) );
                mlt_properties properties = mlt_service_properties( service );
-               mlt_properties_set_position( properties, "length", 0 );
                for ( ; atts != NULL && *atts != NULL; atts += 2 )
                        mlt_properties_set( properties, (char*) atts[0], (char*) atts[1] );
 
@@ -381,8 +372,6 @@ static void on_start_playlist( deserialise_context context, const xmlChar *name,
 
        track_service( context->destructors, service, (mlt_destructor) mlt_playlist_close );
 
-       mlt_properties_set_position( properties, "length", 0 );
-
        for ( ; atts != NULL && *atts != NULL; atts += 2 )
        {
                mlt_properties_set( properties, ( char* )atts[0], ( char* )atts[1] );
@@ -408,18 +397,8 @@ static void on_end_playlist( deserialise_context context, const xmlChar *name )
        {
                mlt_properties properties = mlt_service_properties( service );
                mlt_position in = mlt_properties_get_position( properties, "in" );
-               mlt_position out;
-
-               if ( mlt_properties_get( properties, "_westley.out" ) != NULL )
-                       out = mlt_properties_get_position( properties, "_westley.out" );
-               else
-                       out = mlt_properties_get_position( properties, "length" ) - 1;
-
-               if ( mlt_properties_get_position( properties, "length" ) < out )
-                       mlt_properties_set_position( properties, "length", out  + 1 );
+               mlt_position out = mlt_properties_get_position( properties, "out" );
 
-               mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
-       
                // See if the playlist should be added to a playlist or multitrack
                if ( add_producer( context, service, in, out ) == 0 )
                        context_push_service( context, service, type );
@@ -550,7 +529,7 @@ static void on_end_producer( deserialise_context context, const xmlChar *name )
                                        else
                                        {
                                                // Otherwise, set in and out on producer directly
-                                               mlt_producer_set_in_and_out( MLT_PRODUCER( service ), in, out );
+                                               mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
                                        }
                                }
                                else
@@ -559,7 +538,7 @@ static void on_end_producer( deserialise_context context, const xmlChar *name )
                                        mlt_producer_set_in_and_out( MLT_PRODUCER( producer ), in, out );
                                }
                        }
-       
+
                        // Push the producer onto the stack
                        context_push_service( context, producer, mlt_producer_type );
                }
@@ -599,7 +578,84 @@ static void on_start_blank( deserialise_context context, const xmlChar *name, co
        }
 }
 
-static void on_start_entry_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
+static void on_start_entry( deserialise_context context, const xmlChar *name, const xmlChar **atts)
+{
+       mlt_producer entry = NULL;
+       mlt_properties temp = mlt_properties_new( );
+
+       for ( ; atts != NULL && *atts != NULL; atts += 2 )
+       {
+               mlt_properties_set( temp, (char*) atts[0], (char*) atts[1] );
+               
+               // Look for the producer attribute
+               if ( strcmp( atts[ 0 ], "producer" ) == 0 )
+               {
+                       mlt_producer producer = mlt_properties_get_data( context->producer_map, (char*) atts[1], NULL );
+                       if ( producer !=  NULL )
+                               mlt_properties_set_data( temp, "producer", producer, 0, NULL, NULL );
+               }
+       }
+
+       // If we have a valid entry
+       if ( mlt_properties_get_data( temp, "producer", NULL ) != NULL )
+       {
+               mlt_playlist_clip_info info;
+               enum service_type parent_type;
+               mlt_service parent = context_pop_service( context, &parent_type );
+               mlt_producer producer = mlt_properties_get_data( temp, "producer", NULL );
+
+               if ( parent_type == mlt_playlist_type )
+               {
+                       // Append the producer to the playlist
+                       if ( mlt_properties_get( temp, "in" ) != NULL || mlt_properties_get( temp, "out" ) != NULL )
+                       {
+                               mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer,
+                                       mlt_properties_get_position( temp, "in" ), 
+                                       mlt_properties_get_position( temp, "out" ) );
+                       }
+                       else
+                       {
+                               mlt_playlist_append( MLT_PLAYLIST( parent ), producer );
+                       }
+
+                       // Handle the repeat property
+                       if ( mlt_properties_get_int( temp, "repeat" ) > 0 )
+                       {
+                               mlt_playlist_repeat_clip( MLT_PLAYLIST( parent ),
+                                                                                 mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1,
+                                                                                 mlt_properties_get_int( temp, "repeat" ) );
+                       }
+
+                       mlt_playlist_get_clip_info( MLT_PLAYLIST( parent ), &info, mlt_playlist_count( MLT_PLAYLIST( parent ) ) - 1 );
+                       entry = info.cut;
+               }
+               else
+               {
+                       fprintf( stderr, "Entry not part of a playlist...\n" );
+               }
+
+               context_push_service( context, parent, parent_type );
+       }
+
+       // Push the cut onto the stack
+       context_push_service( context, mlt_producer_service( entry ), mlt_entry_type );
+
+       mlt_properties_close( temp );
+}
+
+static void on_end_entry( deserialise_context context, const xmlChar *name )
+{
+       // Get the entry from the stack
+       enum service_type entry_type;
+       mlt_service entry = context_pop_service( context, &entry_type );
+
+       if ( entry == NULL && entry_type != mlt_entry_type )
+       {
+               fprintf( stderr, "Invalid state at end of entry\n" );
+       }
+}
+
+static void on_start_track( deserialise_context context, const xmlChar *name, const xmlChar **atts)
 {
        // use a dummy service to hold properties to allow arbitrary nesting
        mlt_service service = calloc( 1, sizeof( struct mlt_service_s ) );
@@ -608,10 +664,7 @@ static void on_start_entry_track( deserialise_context context, const xmlChar *na
        // Push the dummy service onto the stack
        context_push_service( context, service, mlt_entry_type );
        
-       if ( strcmp( name, "entry" ) == 0 )
-               mlt_properties_set( mlt_service_properties( service ), "resource", "<entry>" );
-       else
-               mlt_properties_set( mlt_service_properties( service ), "resource", "<track>" );
+       mlt_properties_set( mlt_service_properties( service ), "resource", "<track>" );
        
        for ( ; atts != NULL && *atts != NULL; atts += 2 )
        {
@@ -652,18 +705,22 @@ static void on_end_track( deserialise_context context, const xmlChar *name )
 
                if ( multitrack != NULL )
                {
-                       // Set the track on the multitrack
-                       mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
-
                        // Set producer i/o if specified
                        if ( mlt_properties_get( track_props, "in" ) != NULL ||
-                               mlt_properties_get( track_props, "out" ) != NULL )
+                                mlt_properties_get( track_props, "out" ) != NULL )
                        {
-                               mlt_producer_set_in_and_out( MLT_PRODUCER( producer ),
+                               mlt_producer cut = mlt_producer_cut( MLT_PRODUCER( producer ),
                                        mlt_properties_get_position( track_props, "in" ),
                                        mlt_properties_get_position( track_props, "out" ) );
+                               mlt_properties_set_int( mlt_producer_properties( cut ), "cut", 1 );
+                               mlt_multitrack_connect( multitrack, cut, mlt_multitrack_count( multitrack ) );
+                               mlt_producer_close( cut );
                        }
-       
+                       else
+                       {
+                               mlt_multitrack_connect( multitrack, producer, mlt_multitrack_count( multitrack ) );
+                       }
+
                        // Set the hide state of the track producer
                        char *hide_s = mlt_properties_get( track_props, "hide" );
                        if ( hide_s != NULL )
@@ -688,50 +745,6 @@ static void on_end_track( deserialise_context context, const xmlChar *name )
        }
 }
 
-static void on_end_entry( deserialise_context context, const xmlChar *name )
-{
-       // Get the entry from the stack
-       enum service_type entry_type;
-       mlt_service entry = context_pop_service( context, &entry_type );
-
-       if ( entry != NULL && entry_type == mlt_entry_type )
-       {
-               mlt_properties entry_props = mlt_service_properties( entry );
-               enum service_type parent_type;
-               mlt_service parent = context_pop_service( context, &parent_type );
-               mlt_producer producer = mlt_properties_get_data( entry_props, "producer", NULL );
-
-               if ( parent_type == mlt_playlist_type )
-               {
-                       // Append the producer to the playlist
-                       if ( mlt_properties_get( mlt_service_properties( entry ), "in" ) != NULL ||
-                               mlt_properties_get( mlt_service_properties( entry ), "out" ) != NULL )
-                       {
-                               mlt_playlist_append_io( MLT_PLAYLIST( parent ), producer,
-                                       mlt_properties_get_position( mlt_service_properties( entry ), "in" ), 
-                                       mlt_properties_get_position( mlt_service_properties( entry ), "out" ) );
-                       }
-                       else
-                       {
-                               mlt_playlist_append( MLT_PLAYLIST( parent ), producer );
-                       }
-               }
-               else
-               {
-                       fprintf( stderr, "Invalid position for an entry...\n" );
-               }
-
-               if ( parent != NULL )
-                       context_push_service( context, parent, parent_type );
-
-               mlt_service_close( entry );
-       }
-       else
-       {
-               fprintf( stderr, "Invalid state at end of entry\n" );
-       }
-}
-
 static void on_start_filter( deserialise_context context, const xmlChar *name, const xmlChar **atts)
 {
        // use a dummy service to hold properties to allow arbitrary nesting
@@ -855,6 +868,8 @@ static void on_end_transition( deserialise_context context, const xmlChar *name
                        if ( parent_type == mlt_tractor_type )
                        {
                                mlt_field field = mlt_tractor_field( MLT_TRACTOR( parent ) );
+                               if ( mlt_properties_get_int( properties, "a_track" ) == mlt_properties_get_int( properties, "b_track" ) )
+                                       mlt_properties_set_int( properties, "b_track", mlt_properties_get_int( properties, "a_track" ) + 1 );
                                mlt_field_plant_transition( field, MLT_TRANSITION( effect ), 
                                                                                        mlt_properties_get_int( properties, "a_track" ),
                                                                                        mlt_properties_get_int( properties, "b_track" ) );
@@ -994,8 +1009,10 @@ static void on_start_element( void *ctx, const xmlChar *name, const xmlChar **at
                on_start_producer( context, name, atts );
        else if ( strcmp( name, "blank" ) == 0 )
                on_start_blank( context, name, atts );
-       else if ( strcmp( name, "entry" ) == 0 || strcmp( name, "track" ) == 0 )
-               on_start_entry_track( context, name, atts );
+       else if ( strcmp( name, "entry" ) == 0 )
+               on_start_entry( context, name, atts );
+       else if ( strcmp( name, "track" ) == 0 )
+               on_start_track( context, name, atts );
        else if ( strcmp( name, "filter" ) == 0 )
                on_start_filter( context, name, atts );
        else if ( strcmp( name, "transition" ) == 0 )
@@ -1229,7 +1246,7 @@ mlt_producer producer_westley_init( int info, char *data )
        struct _xmlParserCtxt *xmlcontext;
        int well_formed = 0;
        char *filename = NULL;
-       
+
        context->producer_map = mlt_properties_new();
        context->destructors = mlt_properties_new();
        context->params = mlt_properties_new();
@@ -1284,6 +1301,18 @@ mlt_producer producer_westley_init( int info, char *data )
        else
                xmlcontext = xmlCreateMemoryParserCtxt( data, strlen( data ) );
 
+       // Invalid context - clean up and return NULL
+       if ( xmlcontext == NULL )
+       {
+               mlt_properties_close( context->producer_map );
+               mlt_properties_close( context->destructors );
+               mlt_properties_close( context->params );
+               free( context );
+               free( sax );
+               free( filename );
+               return NULL;
+       }
+
        xmlcontext->sax = sax;
        xmlcontext->_private = ( void* )context;
        
@@ -1340,12 +1369,12 @@ mlt_producer producer_westley_init( int info, char *data )
                // Set this var to service properties for convenience
                properties = mlt_service_properties( service );
        
-               // make the returned service destroy the connected services
-               mlt_properties_set_data( properties, "__destructors__", context->destructors, 0, (mlt_destructor) mlt_properties_close, NULL );
-
                // Assign the title
                mlt_properties_set( properties, "title", title );
 
+               // Optimise for overlapping producers
+               mlt_producer_optimise( MLT_PRODUCER( service ) );
+
                // Handle deep copies
                if ( getenv( "MLT_WESTLEY_DEEP" ) == NULL )
                {
@@ -1359,6 +1388,7 @@ mlt_producer producer_westley_init( int info, char *data )
                else
                {
                        // Allow the project to be edited
+                       mlt_properties_set( properties, "_westley", "was here" );
                        mlt_properties_set_int( properties, "_mlt_service_hidden", 1 );
                }
        }
@@ -1366,15 +1396,13 @@ mlt_producer producer_westley_init( int info, char *data )
        {
                // Return null if not well formed
                service = NULL;
-               
-               // Clean up
-               mlt_properties_close( context->destructors );
        }
 
        // Clean up
        mlt_properties_close( context->producer_map );
        if ( context->params != NULL )
                mlt_properties_close( context->params );
+       mlt_properties_close( context->destructors );
        free( context );
        free( filename );