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