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
= calloc( 1, 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
, 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
);
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), ( 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
);
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 // Set flag to skip this henceforth
200 mlt_properties_set_int( properties
, "_samples", -1 );
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
] )
206 fprintf( stderr
, "%s: no jack buffer for input port %d\n", __FUNCTION__
, i
);
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
);
214 // Tell mlt that audio is available
215 if ( i
== ( channels
- 1 ) && pthread_mutex_trylock( output_lock
) == 0 )
217 pthread_cond_signal( output_ready
);
218 pthread_mutex_unlock( output_lock
);
230 static int jackrack_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
232 // Get the filter service
233 mlt_filter filter
= mlt_frame_pop_audio( frame
);
235 // Get the filter properties
236 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
238 // Restore the original get_audio
239 frame
->get_audio
= mlt_frame_pop_audio( frame
);
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 );
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
);
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
);
256 int16_t *q
= *buffer
;
260 // Convert to floats and write into output ringbuffer
261 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
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
++ )
268 sample
= ( float )( *q
++ ) / 32768.0;
269 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
273 // fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
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
);
281 // Initialise to silence, but repeat last frame if available in case of
285 for ( i
= 0; i
< *samples
; i
++ )
286 for ( j
= 0; j
< *channels
; j
++ )
288 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
292 else if ( sample
< -1.0 )
296 *q
++ = 32767 * sample
;
298 *q
++ = 32768 * sample
;
302 // fprintf( stderr, "%s: in buffer size %d\n", __FUNCTION__, jack_ringbuffer_read_space( output_buffers[0] ) );
308 /** Filter processing.
311 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
313 if ( frame
->get_audio
!= NULL
)
315 mlt_frame_push_audio( frame
, frame
->get_audio
);
316 mlt_frame_push_audio( frame
, this );
317 frame
->get_audio
= jackrack_get_audio
;
324 void filter_close( mlt_filter
this )
326 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
327 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
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
)
334 mlt_properties_set_int( properties
, "_done", 1 );
335 pthread_join( *jackrack_pthread
, NULL
);
336 free( jackrack_pthread
);
340 /** Constructor for the filter.
343 mlt_filter
filter_jackrack_init( char *arg
)
345 mlt_filter
this = mlt_filter_new( );
350 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
351 jack_client_t
*jack_client
= jack_client_new( name
);
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
) );
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
);
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
);