mlt_consumer.c: make the realtime frame-dropping heuristic based on actual frame...
[melted] / src / framework / mlt_playlist.c
index 240c523..0e0ba09 100644 (file)
@@ -18,8 +18,6 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
-#include "config.h"
-
 #include "mlt_playlist.h"
 #include "mlt_tractor.h"
 #include "mlt_multitrack.h"
@@ -134,28 +132,31 @@ static int mlt_playlist_virtual_refresh( mlt_playlist this )
        {
                // Get the producer
                mlt_producer producer = this->list[ i ]->producer;
-               int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
-
-               // Check if the length of the producer has changed
-               if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
-                        this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
+               if ( producer )
                {
-                       // This clip should be removed...
-                       if ( current_length < 1 )
-                       {
-                               this->list[ i ]->frame_in = 0;
-                               this->list[ i ]->frame_out = -1;
-                               this->list[ i ]->frame_count = 0;
-                       }
-                       else 
+                       int current_length = mlt_producer_get_out( producer ) - mlt_producer_get_in( producer ) + 1;
+       
+                       // Check if the length of the producer has changed
+                       if ( this->list[ i ]->frame_in != mlt_producer_get_in( producer ) ||
+                               this->list[ i ]->frame_out != mlt_producer_get_out( producer ) )
                        {
-                               this->list[ i ]->frame_in = mlt_producer_get_in( producer );
-                               this->list[ i ]->frame_out = mlt_producer_get_out( producer );
-                               this->list[ i ]->frame_count = current_length;
+                               // This clip should be removed...
+                               if ( current_length < 1 )
+                               {
+                                       this->list[ i ]->frame_in = 0;
+                                       this->list[ i ]->frame_out = -1;
+                                       this->list[ i ]->frame_count = 0;
+                               }
+                               else 
+                               {
+                                       this->list[ i ]->frame_in = mlt_producer_get_in( producer );
+                                       this->list[ i ]->frame_out = mlt_producer_get_out( producer );
+                                       this->list[ i ]->frame_count = current_length;
+                               }
+       
+                               // Update the producer_length
+                               this->list[ i ]->producer_length = current_length;
                        }
-
-                       // Update the producer_length
-                       this->list[ i ]->producer_length = current_length;
                }
 
                // Calculate the frame_count
@@ -335,6 +336,27 @@ static mlt_service mlt_playlist_virtual_seek( mlt_playlist this, int *progressiv
        // Get the properties
        mlt_properties properties = MLT_PLAYLIST_PROPERTIES( this );
 
+       // Automatically close previous producers if requested
+       if ( i > 1 // keep immediate previous in case app wants to get info about what just finished
+               && position < 2 // tolerate off-by-one error on going to next clip
+               && mlt_properties_get_int( properties, "autoclose" ) )
+       {
+               int j;
+               // They might have jumped ahead!
+               for ( j = 0; j < i - 1; j++ )
+               {
+                       mlt_service_lock( MLT_PRODUCER_SERVICE( this->list[ j ]->producer ) );
+                       mlt_producer p = this->list[ j ]->producer;
+                       if ( p )
+                       {
+                               this->list[ j ]->producer = NULL;
+                               mlt_service_unlock( MLT_PRODUCER_SERVICE( p ) );
+                               mlt_producer_close( p );
+                       }
+                       // If p is null, the lock will not have been "taken"
+               }
+       }
+
        // Get the eof handling
        char *eof = mlt_properties_get( properties, "eof" );
 
@@ -1271,7 +1293,7 @@ int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_produc
                if ( clip < this->count && mlt_playlist_is_blank( this, clip ) )
                {
                        // Split and move to new clip if need be
-                       if ( position != info.start && mlt_playlist_split( this, clip, position - info.start ) == 0 )
+                       if ( position != info.start && mlt_playlist_split( this, clip, position - info.start - 1 ) == 0 )
                                mlt_playlist_get_clip_info( this, &info, ++ clip );
 
                        // Split again if need be
@@ -1301,8 +1323,12 @@ int mlt_playlist_insert_at( mlt_playlist this, mlt_position position, mlt_produc
                }
                else 
                {
-                       if ( mode == 1 )
-                               mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) );
+                       if ( mode == 1 ) {
+                               if ( position == info.start ) 
+                                       mlt_playlist_remove( this, clip );
+                               else
+                                       mlt_playlist_blank( this, position - mlt_properties_get_int( properties, "length" ) - 1 );
+                       }
                        mlt_playlist_append( this, producer );
                        ret = this->count - 1;
                }
@@ -1409,8 +1435,8 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
        // Check that we have a producer
        if ( producer == NULL )
        {
-               *frame = mlt_frame_init( );
-               return 0;
+               *frame = NULL;
+               return -1;
        }
 
        // Get this mlt_playlist
@@ -1425,7 +1451,7 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
        // Check that we have a producer
        if ( real == NULL )
        {
-               *frame = mlt_frame_init( );
+               *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
                return 0;
        }
 
@@ -1437,7 +1463,7 @@ static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int i
        else
        {
                mlt_producer parent = mlt_producer_cut_parent( ( mlt_producer )real );
-               *frame = mlt_frame_init( );
+               *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( parent ) );
                mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "fx_cut", 1 );
                mlt_frame_push_service( *frame, NULL );
                mlt_frame_push_audio( *frame, NULL );