]> git.sesse.net Git - mlt/blob - src/modules/jackrack/filter_jackrack.c
0715ec153b64eaebbc47ce19b17b96f9a09409b4
[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 <sys/types.h>
28 #include <unistd.h>
29
30 #include <jack/jack.h>
31 #include <jack/ringbuffer.h>
32 #include <pthread.h>
33 #include <string.h>
34
35 #include "jack_rack.h"
36
37 #define BUFFER_LEN 204800 * 3
38
39 static void initialise_jack_ports( mlt_properties properties )
40 {
41         int i;
42         char mlt_name[20], rack_name[30];
43         jack_port_t **port = NULL;
44         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
45         jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
46         
47         // Propogate these for the Jack processing callback
48         int channels = mlt_properties_get_int( properties, "channels" );
49
50         // Start JackRack
51         if ( mlt_properties_get( properties, "src" ) )
52         {
53                 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
54                 jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
55                 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );               
56                 
57                 mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
58                 mlt_properties_set( properties, "_rack_client_name", rack_name );
59         }
60                 
61         // Allocate buffers and ports
62         jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
63         jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
64         jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
65         jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
66         float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
67         float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
68
69         // Set properties - released inside filter_close
70         mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
71         mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
72         mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
73         mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
74         mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, NULL, NULL );
75         mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, NULL, NULL );
76
77         // Start Jack processing - required before registering ports
78         jack_activate( jack_client );
79         
80         // Register Jack ports
81         for ( i = 0; i < channels; i++ )
82         {
83                 int in;
84                 
85                 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
86                 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
87                 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
88                 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
89                 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
90                 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
91                 
92                 for ( in = 0; in < 2; in++ )
93                 {
94                         snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
95                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
96                         
97                         *port =  jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
98                                 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
99                 }
100         }
101         
102         // Establish connections
103         for ( i = 0; i < channels; i++ )
104         {
105                 int in;
106                 for ( in = 0; in < 2; in++ )
107                 {
108                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
109                         snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
110
111                         snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
112                         if ( mlt_properties_get( properties, "_rack_client_name" ) )
113                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
114                         else if ( mlt_properties_get( properties, rack_name ) )
115                                 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
116                         else
117                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
118                         
119                         if ( in )
120                         {
121                                 fprintf( stderr, "jack connect %s to %s\n", rack_name, mlt_name );
122                                 jack_connect( jack_client, rack_name, mlt_name );
123                         }
124                         else
125                         {
126                                 fprintf( stderr, "jack connect %s to %s\n", mlt_name, rack_name );
127                                 jack_connect( jack_client, mlt_name, rack_name );
128                         }
129                 }
130         }
131 }
132
133 static int jack_process (jack_nframes_t frames, void * data)
134 {
135         mlt_filter filter = (mlt_filter) data;
136         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
137         int channels = mlt_properties_get_int( properties, "channels" );
138         int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
139         int sync = mlt_properties_get_int( properties, "_sync" );
140         int err = 0;
141         int i;
142         static int total_size = 0;
143   
144         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
145         if ( output_buffers == NULL )
146                 return 0;
147         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
148         jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
149         jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
150         float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
151         float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
152         pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
153         pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
154         
155         for ( i = 0; i < channels; i++ )
156         {
157                 size_t jack_size = ( frames * sizeof(float) );
158                 size_t ring_size;
159                 
160                 // Send audio through out port
161                 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
162                 if ( ! jack_output_buffers[i] )
163                 {
164                         fprintf( stderr, "%s: no jack buffer for output port %d\n", __FUNCTION__, i );
165                         err = 1;
166                         break;
167                 }
168                 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
169                 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
170                 
171                 // Return audio through in port
172                 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
173                 if ( ! jack_input_buffers[i] )
174                 {
175                         fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
176                         err = 1;
177                         break;
178                 }
179                 
180                 // Do not start returning audio until we have sent first mlt frame
181                 if ( sync && i == 0 && frame_size > 0 )
182                         total_size += ring_size;
183                 //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
184                 
185                 if ( ! sync || ( frame_size > 0  && total_size >= frame_size ) )
186                 {
187                         ring_size = jack_ringbuffer_write_space( input_buffers[i] );
188                         jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
189                         
190                         if ( sync )
191                         {
192                                 // Tell mlt that audio is available
193                                 pthread_mutex_lock( output_lock);
194                                 pthread_cond_signal( output_ready );
195                                 pthread_mutex_unlock( output_lock);
196
197                                 // Clear sync phase
198                                 mlt_properties_set_int( properties, "_sync", 0 );
199                         }
200                 }
201         }
202
203         return err;
204 }
205
206
207 /** Get the audio.
208 */
209
210 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
211 {
212         // Get the filter service
213         mlt_filter filter = mlt_frame_pop_audio( frame );
214
215         // Get the filter properties
216         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
217
218         int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
219
220         // Get the producer's audio
221         mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
222         
223         // TODO: Deal with sample rate differences
224         if ( *frequency != jack_frequency )
225                 fprintf( stderr, "mismatching frequencies in filter jackrack\n" );
226         *frequency = jack_frequency;
227
228         // Initialise Jack ports and connections if needed
229         if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
230                 mlt_properties_set_int( filter_properties, "_samples", *samples );
231         
232         // Get the filter-specific properties
233         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
234         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
235 //      pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
236 //      pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
237         
238         // Process the audio
239         int16_t *q = *buffer;
240         float sample[ 2 ][ 10000 ];
241         int i, j;
242 //      struct timespec tm = { 0, 0 };
243
244         // Convert to floats and write into output ringbuffer
245         if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
246         {
247                 for ( i = 0; i < *samples; i++ )
248                         for ( j = 0; j < *channels; j++ )
249                                 sample[ j ][ i ] = ( float )( *q ++ ) / 32768.0;
250
251                 for ( j = 0; j < *channels; j++ )
252                         jack_ringbuffer_write( output_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
253         }
254
255         // Synchronization phase - wait for signal from Jack process
256         while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) ) ;
257                 //pthread_cond_wait( output_ready, output_lock );
258                 
259         // Read from input ringbuffer and convert from floats
260         if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
261         {
262                 // Initialise to silence, but repeat last frame if available in case of 
263                 // buffer underrun
264                 for ( j = 0; j < *channels; j++ )
265                         jack_ringbuffer_read( input_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
266
267                 q = *buffer;
268                 for ( i = 0; i < *samples; i++ )
269                         for ( j = 0; j < *channels; j++ )
270                         {
271                                 if ( sample[ j ][ i ] > 1.0 )
272                                         sample[ j ][ i ] = 1.0;
273                                 else if ( sample[ j ][ i ] < -1.0 )
274                                         sample[ j ][ i ] = -1.0;
275                         
276                                 if ( sample[ j ][ i ] > 0 )
277                                         *q ++ = 32767 * sample[ j ][ i ];
278                                 else
279                                         *q ++ = 32768 * sample[ j ][ i ];
280                         }
281         }
282
283         return 0;
284 }
285
286
287 /** Filter processing.
288 */
289
290 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
291 {
292         {
293                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
294                 mlt_frame_push_audio( frame, this );
295                 mlt_frame_push_audio( frame, jackrack_get_audio );
296                 
297                 if ( mlt_properties_get_int( properties, "_sync" ) )
298                         initialise_jack_ports( properties );
299         }
300
301         return frame;
302 }
303
304
305 static void filter_close( mlt_filter this )
306 {
307         int i;
308         char mlt_name[20];
309         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
310         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
311         
312         jack_deactivate( jack_client );
313         jack_client_close( jack_client );
314         for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
315         {
316                 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
317                 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
318                 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
319                 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
320         }
321         mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
322         mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
323         mlt_pool_release( mlt_properties_get_data( properties, "jack_output_ports", NULL ) );
324         mlt_pool_release( mlt_properties_get_data( properties, "jack_input_ports", NULL ) );
325         mlt_pool_release( mlt_properties_get_data( properties, "jack_output_buffers", NULL ) );
326         mlt_pool_release( mlt_properties_get_data( properties, "jack_input_buffers", NULL ) );
327         mlt_pool_release( mlt_properties_get_data( properties, "output_lock", NULL ) );
328         mlt_pool_release( mlt_properties_get_data( properties, "output_ready", NULL ) );
329
330         jack_rack_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
331         jack_rack_destroy( jackrack );
332         
333         this->parent.close = NULL;
334         mlt_service_close( &this->parent );
335 }
336
337 /** Constructor for the filter.
338 */
339
340 mlt_filter filter_jackrack_init( char *arg )
341 {
342         mlt_filter this = mlt_filter_new( );
343         if ( this != NULL )
344         {
345                 char name[14];
346                 
347                 snprintf( name, sizeof( name ), "mlt%d", getpid() );
348                 jack_client_t *jack_client = jack_client_new( name );
349                 if ( jack_client )
350                 {
351                         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
352                         pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
353                         pthread_cond_t  *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
354                         
355                         jack_set_process_callback( jack_client, jack_process, this );
356                         //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
357                         this->process = filter_process;
358                         this->close = filter_close;
359                         pthread_mutex_init( output_lock, NULL );
360                         pthread_cond_init( output_ready, NULL );
361                         
362                         mlt_properties_set( properties, "src", arg );
363                         mlt_properties_set( properties, "_client_name", name );
364                         mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
365                         mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
366                         mlt_properties_set_data( properties, "output_lock", output_lock, 0, NULL, NULL );
367                         mlt_properties_set_data( properties, "output_ready", output_ready, 0, NULL, NULL );
368                         mlt_properties_set_int( properties, "_sync", 1 );
369                         mlt_properties_set_int( properties, "channels", 2 );
370                 }
371         }
372         return this;
373 }