Remaining audio handling switched to stacks; Minor corrections to compositing and...
[melted] / src / modules / jackrack / filter_jackrack.c
index 954917d..a8803c8 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <samplerate.h>
-#define __USE_ISOC99 1
-#include <math.h>
 #include <sys/types.h>
 #include <unistd.h>
+
 #include <jack/jack.h>
 #include <jack/ringbuffer.h>
 #include <pthread.h>
@@ -36,7 +34,7 @@
 
 #include "ui.h"
 
-#define BUFFER_LEN 2048 * 20
+#define BUFFER_LEN 2048 * 3
 
 static void *jackrack_thread( void *arg )
 {
@@ -53,7 +51,7 @@ static void *jackrack_thread( void *arg )
        return NULL;
 }
 
-static void initialise_jack_ports( mlt_properties properties, int channels, int samples )
+static void initialise_jack_ports( mlt_properties properties )
 {
        int i;
        char mlt_name[20], rack_name[30];
@@ -62,16 +60,15 @@ static void initialise_jack_ports( mlt_properties properties, int channels, int
        jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
        
        // Propogate these for the Jack processing callback
-       mlt_properties_set_int( properties, "_channels", channels );
-       mlt_properties_set_int( properties, "_samples", samples );
+       int channels = mlt_properties_get_int( properties, "channels" );
 
        // Start JackRack
        if ( mlt_properties_get( properties, "src" ) )
        {
-               pthread_t *jackrack_pthread = calloc( 1, sizeof( pthread_t ) );
+               pthread_t *jackrack_pthread = mlt_pool_alloc( sizeof( pthread_t ) );
 
                snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
-               ui_t *jackrack = ui_new( rack_name, mlt_properties_get_int( properties, "_channels" ), 0, 0 );
+               ui_t *jackrack = ui_new( rack_name, mlt_properties_get_int( properties, "channels" ), 0, 0 );
                jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );               
                
                mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
@@ -90,13 +87,13 @@ static void initialise_jack_ports( mlt_properties properties, int channels, int
        float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
        float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
 
-       // Set properties for self-destruction  
-       mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, mlt_pool_release, NULL );
-       mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, mlt_pool_release, NULL );
+       // Set properties - released inside filter_close
+       mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
+       mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
+       mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
+       mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
+       mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, NULL, NULL );
+       mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, NULL, NULL );
 
        // Start Jack processing - required before registering ports
        jack_activate( jack_client );
@@ -109,9 +106,9 @@ static void initialise_jack_ports( mlt_properties properties, int channels, int
                output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
                input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
                snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
-               mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), ( mlt_destructor )jack_ringbuffer_free, NULL );
+               mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
                snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
-               mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), ( mlt_destructor )jack_ringbuffer_free, NULL );
+               mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
                
                for ( in = 0; in < 2; in++ )
                {
@@ -158,11 +155,12 @@ static int jack_process (jack_nframes_t frames, void * data)
 {
        mlt_filter filter = (mlt_filter) data;
        mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
-       int channels = mlt_properties_get_int( properties, "_channels" );
+       int channels = mlt_properties_get_int( properties, "channels" );
+       int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
+       int sync = mlt_properties_get_int( properties, "_sync" );
        int err = 0;
        int i;
-       static size_t total_size = 0;
-       size_t first_ring_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
+       static int total_size = 0;
   
        jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
        if ( output_buffers == NULL )
@@ -191,31 +189,34 @@ static int jack_process (jack_nframes_t frames, void * data)
                ring_size = jack_ringbuffer_read_space( output_buffers[i] );
                jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
                
+               // Return audio through in port
+               jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
+               if ( ! jack_input_buffers[i] )
+               {
+                       fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
+                       err = 1;
+                       break;
+               }
+               
                // Do not start returning audio until we have sent first mlt frame
-               if ( first_ring_size != -sizeof(float) && i == 0 )
+               if ( sync && i == 0 && frame_size > 0 )
                        total_size += ring_size;
-               if ( first_ring_size == -sizeof(float) || total_size >= first_ring_size )
+               //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
+               
+               if ( ! sync || ( frame_size > 0  && total_size >= frame_size ) )
                {
-                       // Set flag to skip this henceforth
-                       mlt_properties_set_int( properties, "_samples", -1 );
-                       
-                       // Return audio through in port
-                       jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
-                       if ( ! jack_input_buffers[i] )
-                       {
-                               fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
-                               err = 1;
-                               break;
-                       }
-                       
                        ring_size = jack_ringbuffer_write_space( input_buffers[i] );
                        jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
                        
-                       // Tell mlt that audio is available
-                       if ( i == ( channels - 1 ) && pthread_mutex_trylock( output_lock) == 0 )
+                       if ( sync )
                        {
+                               // Tell mlt that audio is available
+                               pthread_mutex_lock( output_lock);
                                pthread_cond_signal( output_ready );
-                               pthread_mutex_unlock( output_lock );
+                               pthread_mutex_unlock( output_lock);
+
+                               // Clear sync phase
+                               mlt_properties_set_int( properties, "_sync", 0 );
                        }
                }
        }
@@ -235,16 +236,19 @@ static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Get the filter properties
        mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
 
-       // Restore the original get_audio
-       frame->get_audio = mlt_frame_pop_audio( frame );
+       int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
 
        // Get the producer's audio
-       mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
-       //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
+       mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
+       
+       // TODO: Deal with sample rate differences
+       if ( *frequency != jack_frequency )
+               fprintf( stderr, "mismatching frequencies in filter jackrack\n" );
+       *frequency = jack_frequency;
 
        // Initialise Jack ports and connections if needed
-       if ( ! mlt_properties_get_data( filter_properties, "jack_output_ports", NULL ) )
-               initialise_jack_ports( filter_properties, *channels, *samples );
+       if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
+               mlt_properties_set_int( filter_properties, "_samples", *samples );
        
        // Get the filter-specific properties
        jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
@@ -260,8 +264,6 @@ static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
        // Convert to floats and write into output ringbuffer
        if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
        {
-               //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
-               //pthread_cond_wait( &output_ready, &output_lock );
                for ( i = 0; i < *samples; i++ )
                        for ( j = 0; j < *channels; j++ )
                        {
@@ -269,24 +271,24 @@ static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                                jack_ringbuffer_write( output_buffers[j], ( char * )&sample, sizeof(float) );
                        }
        }
-       //else
-       //      fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
-       
-       // Read from input ringbuffer and convert from floats
-       while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) )
+
+       // Synchronization phase - wait for signal from Jack process
+       while ( mlt_properties_get_int( filter_properties, "_sync" )
+                   && jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) )
                pthread_cond_wait( output_ready, output_lock );
-       {
-               q = *buffer;
                
+       // Read from input ringbuffer and convert from floats
+       //if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
+       {
                // Initialise to silence, but repeat last frame if available in case of 
                // buffer underrun
                sample = 0;
-               
+               q = *buffer;
                for ( i = 0; i < *samples; i++ )
                        for ( j = 0; j < *channels; j++ )
                        {
                                jack_ringbuffer_read( input_buffers[j], ( char * )&sample, sizeof(float) );
-
+       
                                if ( sample > 1.0 )
                                        sample = 1.0;
                                else if ( sample < -1.0 )
@@ -298,8 +300,6 @@ static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
                                        *q ++ = 32768 * sample;
                        }
        }
-       //else
-       //      fprintf( stderr, "%s: in buffer size %d\n", __FUNCTION__, jack_ringbuffer_read_space( output_buffers[0] ) );
 
        return 0;
 }
@@ -310,11 +310,14 @@ static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_form
 
 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 {
-       if ( frame->get_audio != NULL )
+       if ( mlt_frame_is_test_audio( frame ) != 0 )
        {
-               mlt_frame_push_audio( frame, frame->get_audio );
+               mlt_properties properties = MLT_FILTER_PROPERTIES( this );
                mlt_frame_push_audio( frame, this );
-               frame->get_audio = jackrack_get_audio;
+               mlt_frame_push_audio( frame, jackrack_get_audio );
+               
+               if ( mlt_properties_get_int( properties, "_sync" ) )
+                       initialise_jack_ports( properties );
        }
 
        return frame;
@@ -323,18 +326,39 @@ static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
 
 void filter_close( mlt_filter this )
 {
+       int i;
+       char mlt_name[20];
        mlt_properties properties = MLT_FILTER_PROPERTIES( this );
        jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
        
        jack_deactivate( jack_client );
        jack_client_close( jack_client );
+       for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
+       {
+               snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
+               jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
+               snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
+               jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
+       }
+       mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "jack_output_ports", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "jack_input_ports", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "jack_output_buffers", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "jack_input_buffers", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "output_lock", NULL ) );
+       mlt_pool_release( mlt_properties_get_data( properties, "output_ready", NULL ) );
+       
        pthread_t *jackrack_pthread = mlt_properties_get_data( properties, "jackrack_thread", NULL );
        if ( jackrack_pthread != NULL )
        {
                mlt_properties_set_int( properties, "_done", 1 );
                pthread_join( *jackrack_pthread, NULL );
-               free( jackrack_pthread );
+               mlt_pool_release( jackrack_pthread );
        }
+       
+       this->parent.close = NULL;
+       mlt_service_close( &this->parent );
 }
 
 /** Constructor for the filter.
@@ -352,21 +376,24 @@ mlt_filter filter_jackrack_init( char *arg )
                if ( jack_client )
                {
                        mlt_properties properties = MLT_FILTER_PROPERTIES( this );
-                       pthread_mutex_t *output_lock = calloc( 1, sizeof( pthread_mutex_t ) );
-                       pthread_cond_t  *output_ready = calloc( 1, sizeof( pthread_cond_t ) );
+                       pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
+                       pthread_cond_t  *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
                        
                        jack_set_process_callback( jack_client, jack_process, this );
                        //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
                        this->process = filter_process;
+                       this->close = filter_close;
                        pthread_mutex_init( output_lock, NULL );
                        pthread_cond_init( output_ready, NULL );
                        
                        mlt_properties_set( properties, "src", arg );
                        mlt_properties_set( properties, "_client_name", name );
-                       mlt_properties_set_data( properties, "jack_client", jack_client, 0, ( mlt_destructor )filter_close, NULL );
+                       mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
                        mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
-                       mlt_properties_set_data( properties, "output_lock", output_lock, 0, free, NULL );
-                       mlt_properties_set_data( properties, "output_ready", output_ready, 0, free, NULL );
+                       mlt_properties_set_data( properties, "output_lock", output_lock, 0, NULL, NULL );
+                       mlt_properties_set_data( properties, "output_ready", output_ready, 0, NULL, NULL );
+                       mlt_properties_set_int( properties, "_sync", 1 );
+                       mlt_properties_set_int( properties, "channels", 2 );
                }
        }
        return this;