]> git.sesse.net Git - mlt/blob - src/modules/jackrack/filter_jackrack.c
add filter/jackrack to services.txt and apply a performance tweak to filter_jackrack
[mlt] / src / modules / jackrack / filter_jackrack.c
1 /*
2  * filter_jackrack.c -- filter audio through Jack and/or LADSPA plugins
3  * Copyright (C) 2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "filter_jackrack.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <samplerate.h>
28 #define __USE_ISOC99 1
29 #include <math.h>
30 #include <sys/types.h>
31 #include <unistd.h>
32 #include <jack/jack.h>
33 #include <jack/ringbuffer.h>
34 #include <pthread.h>
35 #include <string.h>
36
37 #include "ui.h"
38
39 #define BUFFER_LEN 2048 * 20
40
41 static void *jackrack_thread( void *arg )
42 {
43         mlt_properties properties = arg;
44         ui_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
45                 
46         while ( mlt_properties_get_int( properties, "_done" ) == 0 )
47                 if ( ui_loop_iterate( jackrack ) )
48                         break;
49                 
50         ui_quit( jackrack );
51         ui_destroy( jackrack );
52         
53         return NULL;
54 }
55
56 static void initialise_jack_ports( mlt_properties properties, int channels, int samples )
57 {
58         int i;
59         char mlt_name[20], rack_name[30];
60         jack_port_t **port = NULL;
61         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
62         jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
63         
64         // Propogate these for the Jack processing callback
65         mlt_properties_set_int( properties, "_channels", channels );
66         mlt_properties_set_int( properties, "_samples", samples );
67
68         // Start JackRack
69         if ( mlt_properties_get( properties, "src" ) )
70         {
71                 pthread_t *jackrack_pthread = calloc( 1, sizeof( pthread_t ) );
72
73                 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
74                 ui_t *jackrack = ui_new( rack_name, mlt_properties_get_int( properties, "_channels" ), 0, 0 );
75                 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );               
76                 
77                 mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
78                 mlt_properties_set( properties, "_rack_client_name", rack_name );
79                 mlt_properties_set_int( properties, "_done", 0 );
80                 mlt_properties_set_data( properties, "jackrack_pthread", jackrack_pthread, 0, NULL, NULL );
81                 
82                 pthread_create( jackrack_pthread, NULL, jackrack_thread, properties );
83         }
84                 
85         // Allocate buffers and ports
86         jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
87         jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
88         jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
89         jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
90         float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
91         float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
92
93         // Set properties for self-destruction  
94         mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
95         mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
96         mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
97         mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
98         mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, mlt_pool_release, NULL );
99         mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, mlt_pool_release, NULL );
100
101         // Start Jack processing - required before registering ports
102         jack_activate( jack_client );
103         
104         // Register Jack ports
105         for ( i = 0; i < channels; i++ )
106         {
107                 int in;
108                 
109                 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
110                 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
111                 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
112                 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), ( mlt_destructor )jack_ringbuffer_free, NULL );
113                 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
114                 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), ( mlt_destructor )jack_ringbuffer_free, NULL );
115                 
116                 for ( in = 0; in < 2; in++ )
117                 {
118                         snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
119                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
120                         
121                         *port =  jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
122                                 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
123                 }
124         }
125         
126         // Establish connections
127         for ( i = 0; i < channels; i++ )
128         {
129                 int in;
130                 for ( in = 0; in < 2; in++ )
131                 {
132                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
133                         snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
134
135                         snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
136                         if ( mlt_properties_get( properties, "_rack_client_name" ) )
137                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
138                         else if ( mlt_properties_get( properties, rack_name ) )
139                                 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
140                         else
141                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
142                         
143                         if ( in )
144                         {
145                                 fprintf( stderr, "jack connect %s to %s\n", rack_name, mlt_name );
146                                 jack_connect( jack_client, rack_name, mlt_name );
147                         }
148                         else
149                         {
150                                 fprintf( stderr, "jack connect %s to %s\n", mlt_name, rack_name );
151                                 jack_connect( jack_client, mlt_name, rack_name );
152                         }
153                 }
154         }
155 }
156
157 static int jack_process (jack_nframes_t frames, void * data)
158 {
159         mlt_filter filter = (mlt_filter) data;
160         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
161         int channels = mlt_properties_get_int( properties, "_channels" );
162         int err = 0;
163         int i;
164         static size_t total_size = 0;
165         size_t first_ring_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
166   
167         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
168         if ( output_buffers == NULL )
169                 return 0;
170         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
171         jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
172         jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
173         float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
174         float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
175         pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
176         pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
177         
178         for ( i = 0; i < channels; i++ )
179         {
180                 size_t jack_size = ( frames * sizeof(float) );
181                 size_t ring_size;
182                 
183                 // Send audio through out port
184                 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
185                 if ( ! jack_output_buffers[i] )
186                 {
187                         fprintf( stderr, "%s: no jack buffer for output port %d\n", __FUNCTION__, i );
188                         err = 1;
189                         break;
190                 }
191                 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
192                 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
193                 
194                 // Do not start returning audio until we have sent first mlt frame
195                 if ( first_ring_size != -sizeof(float) && i == 0 )
196                         total_size += ring_size;
197                 if ( first_ring_size == -sizeof(float) || total_size >= first_ring_size )
198                 {
199                         // Return audio through in port
200                         jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
201                         if ( ! jack_input_buffers[i] )
202                         {
203                                 fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
204                                 err = 1;
205                                 break;
206                         }
207                         
208                         ring_size = jack_ringbuffer_write_space( input_buffers[i] );
209                         jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
210                         
211                         // Tell mlt that audio is available
212                         if ( first_ring_size != -sizeof(float) && i == ( channels - 1 ) 
213                                  && pthread_mutex_trylock( output_lock) == 0 )
214                         {
215                                 pthread_cond_signal( output_ready );
216                                 pthread_mutex_unlock( output_lock );
217                         }
218                         
219                         // Set flag to skip this henceforth
220                         mlt_properties_set_int( properties, "_samples", -1 );
221                 }
222         }
223
224         return err;
225 }
226
227
228 /** Get the audio.
229 */
230
231 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
232 {
233         // Get the filter service
234         mlt_filter filter = mlt_frame_pop_audio( frame );
235
236         // Get the filter properties
237         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
238
239         // Restore the original get_audio
240         frame->get_audio = mlt_frame_pop_audio( frame );
241
242         // Get the producer's audio
243         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
244         //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
245
246         // Initialise Jack ports and connections if needed
247         if ( ! mlt_properties_get_data( filter_properties, "jack_output_ports", NULL ) )
248                 initialise_jack_ports( filter_properties, *channels, *samples );
249         
250         // Get the filter-specific properties
251         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
252         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
253         pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
254         pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
255         
256         // Process the audio
257         int16_t *q = *buffer;
258         float sample;
259         int i, j;
260
261         // Convert to floats and write into output ringbuffer
262         if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
263         {
264                 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
265                 //pthread_cond_wait( &output_ready, &output_lock );
266                 for ( i = 0; i < *samples; i++ )
267                         for ( j = 0; j < *channels; j++ )
268                         {
269                                 sample = ( float )( *q ++ ) / 32768.0;
270                                 jack_ringbuffer_write( output_buffers[j], ( char * )&sample, sizeof(float) );
271                         }
272         }
273         //else
274         //      fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
275         
276         // Read from input ringbuffer and convert from floats
277         while ( mlt_properties_get_int( filter_properties, "_samples" ) != -1
278                     && jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) )
279                 pthread_cond_wait( output_ready, output_lock );
280
281         q = *buffer;
282                 
283         // Initialise to silence, but repeat last frame if available in case of 
284         // buffer underrun
285         sample = 0;
286         
287         for ( i = 0; i < *samples; i++ )
288                 for ( j = 0; j < *channels; j++ )
289                 {
290                         jack_ringbuffer_read( input_buffers[j], ( char * )&sample, sizeof(float) );
291
292                         if ( sample > 1.0 )
293                                 sample = 1.0;
294                         else if ( sample < -1.0 )
295                                 sample = -1.0;
296                 
297                         if ( sample > 0 )
298                                 *q ++ = 32767 * sample;
299                         else
300                                 *q ++ = 32768 * sample;
301                 }
302
303         return 0;
304 }
305
306
307 /** Filter processing.
308 */
309
310 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
311 {
312         if ( frame->get_audio != NULL )
313         {
314                 mlt_frame_push_audio( frame, frame->get_audio );
315                 mlt_frame_push_audio( frame, this );
316                 frame->get_audio = jackrack_get_audio;
317         }
318
319         return frame;
320 }
321
322
323 void filter_close( mlt_filter this )
324 {
325         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
326         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
327         
328         jack_deactivate( jack_client );
329         jack_client_close( jack_client );
330         pthread_t *jackrack_pthread = mlt_properties_get_data( properties, "jackrack_thread", NULL );
331         if ( jackrack_pthread != NULL )
332         {
333                 mlt_properties_set_int( properties, "_done", 1 );
334                 pthread_join( *jackrack_pthread, NULL );
335                 free( jackrack_pthread );
336         }
337 }
338
339 /** Constructor for the filter.
340 */
341
342 mlt_filter filter_jackrack_init( char *arg )
343 {
344         mlt_filter this = mlt_filter_new( );
345         if ( this != NULL )
346         {
347                 char name[14];
348                 
349                 snprintf( name, sizeof( name ), "mlt%d", getpid() );
350                 jack_client_t *jack_client = jack_client_new( name );
351                 if ( jack_client )
352                 {
353                         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
354                         pthread_mutex_t *output_lock = calloc( 1, sizeof( pthread_mutex_t ) );
355                         pthread_cond_t  *output_ready = calloc( 1, sizeof( pthread_cond_t ) );
356                         
357                         jack_set_process_callback( jack_client, jack_process, this );
358                         //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
359                         this->process = filter_process;
360                         pthread_mutex_init( output_lock, NULL );
361                         pthread_cond_init( output_ready, NULL );
362                         
363                         mlt_properties_set( properties, "src", arg );
364                         mlt_properties_set( properties, "_client_name", name );
365                         mlt_properties_set_data( properties, "jack_client", jack_client, 0, ( mlt_destructor )filter_close, NULL );
366                         mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
367                         mlt_properties_set_data( properties, "output_lock", output_lock, 0, free, NULL );
368                         mlt_properties_set_data( properties, "output_ready", output_ready, 0, free, NULL );
369                 }
370         }
371         return this;
372 }