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>
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.
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.
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.
21 #include "filter_jackrack.h"
23 #include <framework/mlt_frame.h>
27 #include <samplerate.h>
28 #define __USE_ISOC99 1
30 #include <sys/types.h>
32 #include <jack/jack.h>
33 #include <jack/ringbuffer.h>
39 #define BUFFER_LEN 2048 * 20
41 static void *jackrack_thread( void *arg
)
43 mlt_properties properties
= arg
;
44 ui_t
*jackrack
= mlt_properties_get_data( properties
, "jackrack", NULL
);
46 while ( mlt_properties_get_int( properties
, "_done" ) == 0 )
47 if ( ui_loop_iterate( jackrack
) )
51 ui_destroy( jackrack
);
56 static void initialise_jack_ports( mlt_properties properties
, int channels
, int samples
)
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
);
64 // Propogate these for the Jack processing callback
65 mlt_properties_set_int( properties
, "_channels", channels
);
66 mlt_properties_set_int( properties
, "_samples", samples
);
69 if ( mlt_properties_get( properties
, "src" ) )
71 pthread_t
*jackrack_pthread
= mlt_pool_alloc( sizeof( pthread_t
) );
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" ) );
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
);
82 pthread_create( jackrack_pthread
, NULL
, jackrack_thread
, properties
);
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
);
93 // Set properties for self-destruction
94 mlt_properties_set_data( properties
, "output_buffers", output_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
95 mlt_properties_set_data( properties
, "input_buffers", input_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
96 mlt_properties_set_data( properties
, "jack_output_ports", jack_output_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
97 mlt_properties_set_data( properties
, "jack_input_ports", jack_input_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
98 mlt_properties_set_data( properties
, "jack_output_buffers", jack_output_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
99 mlt_properties_set_data( properties
, "jack_input_buffers", jack_input_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
101 // Start Jack processing - required before registering ports
102 jack_activate( jack_client
);
104 // Register Jack ports
105 for ( i
= 0; i
< channels
; i
++ )
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), NULL
, 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), NULL
, NULL
);
116 for ( in
= 0; in
< 2; in
++ )
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
] );
121 *port
= jack_port_register( jack_client
, mlt_name
, JACK_DEFAULT_AUDIO_TYPE
,
122 ( in ? JackPortIsInput
: JackPortIsOutput
) | JackPortIsTerminal
, 0 );
126 // Establish connections
127 for ( i
= 0; i
< channels
; i
++ )
130 for ( in
= 0; in
< 2; in
++ )
132 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
133 snprintf( mlt_name
, sizeof( mlt_name
), "%s", jack_port_name( *port
) );
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
) );
141 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_client_name" ), in ?
"out" : "in", i
+ 1);
145 fprintf( stderr
, "jack connect %s to %s\n", rack_name
, mlt_name
);
146 jack_connect( jack_client
, rack_name
, mlt_name
);
150 fprintf( stderr
, "jack connect %s to %s\n", mlt_name
, rack_name
);
151 jack_connect( jack_client
, mlt_name
, rack_name
);
157 static int jack_process (jack_nframes_t frames
, void * data
)
159 mlt_filter filter
= (mlt_filter
) data
;
160 mlt_properties properties
= MLT_FILTER_PROPERTIES( filter
);
161 int channels
= mlt_properties_get_int( properties
, "_channels" );
164 static size_t total_size
= 0;
165 size_t first_ring_size
= mlt_properties_get_int( properties
, "_samples" ) * sizeof(float);
167 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( properties
, "output_buffers", NULL
);
168 if ( output_buffers
== NULL
)
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
);
178 for ( i
= 0; i
< channels
; i
++ )
180 size_t jack_size
= ( frames
* sizeof(float) );
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
] )
187 fprintf( stderr
, "%s: no jack buffer for output port %d\n", __FUNCTION__
, i
);
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
);
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
)
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
] )
203 fprintf( stderr
, "%s: no jack buffer for input port %d\n", __FUNCTION__
, i
);
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
);
211 // Tell mlt that audio is available
212 if ( first_ring_size
!= -sizeof(float) && i
== ( channels
- 1 )
213 && pthread_mutex_trylock( output_lock
) == 0 )
215 pthread_cond_signal( output_ready
);
216 pthread_mutex_unlock( output_lock
);
219 // Set flag to skip this henceforth
220 mlt_properties_set_int( properties
, "_samples", -1 );
231 static int jackrack_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
233 // Get the filter service
234 mlt_filter filter
= mlt_frame_pop_audio( frame
);
236 // Get the filter properties
237 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
239 // Restore the original get_audio
240 frame
->get_audio
= mlt_frame_pop_audio( frame
);
242 int jack_frequency
= mlt_properties_get_int( filter_properties
, "_sample_rate" );
244 // Get the producer's audio
245 mlt_frame_get_audio( frame
, buffer
, format
, &jack_frequency
, channels
, samples
);
246 //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
248 // Deal with sample rate differences
249 if ( *frequency
!= jack_frequency
)
250 fprintf( stderr
, "mismatching frequencies in filter jackrack\n" );
251 *frequency
= jack_frequency
;
253 // Initialise Jack ports and connections if needed
254 if ( ! mlt_properties_get_data( filter_properties
, "jack_output_ports", NULL
) )
255 initialise_jack_ports( filter_properties
, *channels
, *samples
);
257 // Get the filter-specific properties
258 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
259 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
260 pthread_mutex_t
*output_lock
= mlt_properties_get_data( filter_properties
, "output_lock", NULL
);
261 pthread_cond_t
*output_ready
= mlt_properties_get_data( filter_properties
, "output_ready", NULL
);
264 int16_t *q
= *buffer
;
268 // Convert to floats and write into output ringbuffer
269 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
271 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
272 //pthread_cond_wait( &output_ready, &output_lock );
273 for ( i
= 0; i
< *samples
; i
++ )
274 for ( j
= 0; j
< *channels
; j
++ )
276 sample
= ( float )( *q
++ ) / 32768.0;
277 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
281 // fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
283 // Read from input ringbuffer and convert from floats
284 while ( mlt_properties_get_int( filter_properties
, "_samples" ) != -1
285 && jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) )
286 pthread_cond_wait( output_ready
, output_lock
);
288 // Initialise to silence, but repeat last frame if available in case of
292 for ( i
= 0; i
< *samples
; i
++ )
293 for ( j
= 0; j
< *channels
; j
++ )
295 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
299 else if ( sample
< -1.0 )
303 *q
++ = 32767 * sample
;
305 *q
++ = 32768 * sample
;
312 /** Filter processing.
315 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
317 if ( frame
->get_audio
!= NULL
)
319 mlt_frame_push_audio( frame
, frame
->get_audio
);
320 mlt_frame_push_audio( frame
, this );
321 frame
->get_audio
= jackrack_get_audio
;
328 void filter_close( mlt_filter
this )
332 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
333 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
335 jack_deactivate( jack_client
);
336 jack_client_close( jack_client
);
337 for ( i
= 0; i
< mlt_properties_get_int( properties
, "_channels" ); i
++ )
339 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
340 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
341 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
342 jack_ringbuffer_free( mlt_properties_get_data( properties
, mlt_name
, NULL
) );
344 mlt_pool_release( mlt_properties_get_data( properties
, "output_buffers", NULL
) );
345 mlt_pool_release( mlt_properties_get_data( properties
, "input_buffers", NULL
) );
346 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_ports", NULL
) );
347 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_ports", NULL
) );
348 mlt_pool_release( mlt_properties_get_data( properties
, "jack_output_buffers", NULL
) );
349 mlt_pool_release( mlt_properties_get_data( properties
, "jack_input_buffers", NULL
) );
350 mlt_pool_release( mlt_properties_get_data( properties
, "output_lock", NULL
) );
351 mlt_pool_release( mlt_properties_get_data( properties
, "output_ready", NULL
) );
353 pthread_t
*jackrack_pthread
= mlt_properties_get_data( properties
, "jackrack_thread", NULL
);
354 if ( jackrack_pthread
!= NULL
)
356 mlt_properties_set_int( properties
, "_done", 1 );
357 pthread_join( *jackrack_pthread
, NULL
);
358 mlt_pool_release( jackrack_pthread
);
362 /** Constructor for the filter.
365 mlt_filter
filter_jackrack_init( char *arg
)
367 mlt_filter
this = mlt_filter_new( );
372 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
373 jack_client_t
*jack_client
= jack_client_new( name
);
376 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
377 pthread_mutex_t
*output_lock
= mlt_pool_alloc( sizeof( pthread_mutex_t
) );
378 pthread_cond_t
*output_ready
= mlt_pool_alloc( sizeof( pthread_cond_t
) );
380 jack_set_process_callback( jack_client
, jack_process
, this );
381 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
382 this->process
= filter_process
;
383 pthread_mutex_init( output_lock
, NULL
);
384 pthread_cond_init( output_ready
, NULL
);
386 mlt_properties_set( properties
, "src", arg
);
387 mlt_properties_set( properties
, "_client_name", name
);
388 mlt_properties_set_data( properties
, "jack_client", jack_client
, 0, ( mlt_destructor
)filter_close
, NULL
);
389 mlt_properties_set_int( properties
, "_sample_rate", jack_get_sample_rate( jack_client
) );
390 mlt_properties_set_data( properties
, "output_lock", output_lock
, 0, NULL
, NULL
);
391 mlt_properties_set_data( properties
, "output_ready", output_ready
, 0, NULL
, NULL
);