]> git.sesse.net Git - mlt/blob - src/modules/jackrack/filter_jackrack.c
954917d783458836d61ba99386b1f5917adc0419
[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                         // Set flag to skip this henceforth
200                         mlt_properties_set_int( properties, "_samples", -1 );
201                         
202                         // Return audio through in port
203                         jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
204                         if ( ! jack_input_buffers[i] )
205                         {
206                                 fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
207                                 err = 1;
208                                 break;
209                         }
210                         
211                         ring_size = jack_ringbuffer_write_space( input_buffers[i] );
212                         jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
213                         
214                         // Tell mlt that audio is available
215                         if ( i == ( channels - 1 ) && pthread_mutex_trylock( output_lock) == 0 )
216                         {
217                                 pthread_cond_signal( output_ready );
218                                 pthread_mutex_unlock( output_lock );
219                         }
220                 }
221         }
222
223         return err;
224 }
225
226
227 /** Get the audio.
228 */
229
230 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
231 {
232         // Get the filter service
233         mlt_filter filter = mlt_frame_pop_audio( frame );
234
235         // Get the filter properties
236         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
237
238         // Restore the original get_audio
239         frame->get_audio = mlt_frame_pop_audio( frame );
240
241         // Get the producer's audio
242         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
243         //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
244
245         // Initialise Jack ports and connections if needed
246         if ( ! mlt_properties_get_data( filter_properties, "jack_output_ports", NULL ) )
247                 initialise_jack_ports( filter_properties, *channels, *samples );
248         
249         // Get the filter-specific properties
250         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
251         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
252         pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
253         pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
254         
255         // Process the audio
256         int16_t *q = *buffer;
257         float sample;
258         int i, j;
259
260         // Convert to floats and write into output ringbuffer
261         if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
262         {
263                 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
264                 //pthread_cond_wait( &output_ready, &output_lock );
265                 for ( i = 0; i < *samples; i++ )
266                         for ( j = 0; j < *channels; j++ )
267                         {
268                                 sample = ( float )( *q ++ ) / 32768.0;
269                                 jack_ringbuffer_write( output_buffers[j], ( char * )&sample, sizeof(float) );
270                         }
271         }
272         //else
273         //      fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
274         
275         // Read from input ringbuffer and convert from floats
276         while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) )
277                 pthread_cond_wait( output_ready, output_lock );
278         {
279                 q = *buffer;
280                 
281                 // Initialise to silence, but repeat last frame if available in case of 
282                 // buffer underrun
283                 sample = 0;
284                 
285                 for ( i = 0; i < *samples; i++ )
286                         for ( j = 0; j < *channels; j++ )
287                         {
288                                 jack_ringbuffer_read( input_buffers[j], ( char * )&sample, sizeof(float) );
289
290                                 if ( sample > 1.0 )
291                                         sample = 1.0;
292                                 else if ( sample < -1.0 )
293                                         sample = -1.0;
294                         
295                                 if ( sample > 0 )
296                                         *q ++ = 32767 * sample;
297                                 else
298                                         *q ++ = 32768 * sample;
299                         }
300         }
301         //else
302         //      fprintf( stderr, "%s: in buffer size %d\n", __FUNCTION__, jack_ringbuffer_read_space( output_buffers[0] ) );
303
304         return 0;
305 }
306
307
308 /** Filter processing.
309 */
310
311 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
312 {
313         if ( frame->get_audio != NULL )
314         {
315                 mlt_frame_push_audio( frame, frame->get_audio );
316                 mlt_frame_push_audio( frame, this );
317                 frame->get_audio = jackrack_get_audio;
318         }
319
320         return frame;
321 }
322
323
324 void filter_close( mlt_filter this )
325 {
326         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
327         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
328         
329         jack_deactivate( jack_client );
330         jack_client_close( jack_client );
331         pthread_t *jackrack_pthread = mlt_properties_get_data( properties, "jackrack_thread", NULL );
332         if ( jackrack_pthread != NULL )
333         {
334                 mlt_properties_set_int( properties, "_done", 1 );
335                 pthread_join( *jackrack_pthread, NULL );
336                 free( jackrack_pthread );
337         }
338 }
339
340 /** Constructor for the filter.
341 */
342
343 mlt_filter filter_jackrack_init( char *arg )
344 {
345         mlt_filter this = mlt_filter_new( );
346         if ( this != NULL )
347         {
348                 char name[14];
349                 
350                 snprintf( name, sizeof( name ), "mlt%d", getpid() );
351                 jack_client_t *jack_client = jack_client_new( name );
352                 if ( jack_client )
353                 {
354                         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
355                         pthread_mutex_t *output_lock = calloc( 1, sizeof( pthread_mutex_t ) );
356                         pthread_cond_t  *output_ready = calloc( 1, sizeof( pthread_cond_t ) );
357                         
358                         jack_set_process_callback( jack_client, jack_process, this );
359                         //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
360                         this->process = filter_process;
361                         pthread_mutex_init( output_lock, NULL );
362                         pthread_cond_init( output_ready, NULL );
363                         
364                         mlt_properties_set( properties, "src", arg );
365                         mlt_properties_set( properties, "_client_name", name );
366                         mlt_properties_set_data( properties, "jack_client", jack_client, 0, ( mlt_destructor )filter_close, NULL );
367                         mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
368                         mlt_properties_set_data( properties, "output_lock", output_lock, 0, free, NULL );
369                         mlt_properties_set_data( properties, "output_ready", output_ready, 0, free, NULL );
370                 }
371         }
372         return this;
373 }