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 <sys/types.h>
30 #include <jack/jack.h>
31 #include <jack/ringbuffer.h>
37 #define BUFFER_LEN 2048 * 20
39 static void *jackrack_thread( void *arg
)
41 mlt_properties properties
= arg
;
42 ui_t
*jackrack
= mlt_properties_get_data( properties
, "jackrack", NULL
);
44 while ( mlt_properties_get_int( properties
, "_done" ) == 0 )
45 if ( ui_loop_iterate( jackrack
) )
49 ui_destroy( jackrack
);
54 static void initialise_jack_ports( mlt_properties properties
, int channels
, int samples
)
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
);
62 // Propogate these for the Jack processing callback
63 mlt_properties_set_int( properties
, "_channels", channels
);
64 mlt_properties_set_int( properties
, "_samples", samples
);
67 if ( mlt_properties_get( properties
, "src" ) )
69 pthread_t
*jackrack_pthread
= mlt_pool_alloc( sizeof( pthread_t
) );
71 snprintf( rack_name
, sizeof( rack_name
), "jackrack%d", getpid() );
72 ui_t
*jackrack
= ui_new( rack_name
, mlt_properties_get_int( properties
, "_channels" ), 0, 0 );
73 jack_rack_open_file( jackrack
, mlt_properties_get( properties
, "src" ) );
75 mlt_properties_set_data( properties
, "jackrack", jackrack
, 0, NULL
, NULL
);
76 mlt_properties_set( properties
, "_rack_client_name", rack_name
);
77 mlt_properties_set_int( properties
, "_done", 0 );
78 mlt_properties_set_data( properties
, "jackrack_pthread", jackrack_pthread
, 0, NULL
, NULL
);
80 pthread_create( jackrack_pthread
, NULL
, jackrack_thread
, properties
);
83 // Allocate buffers and ports
84 jack_ringbuffer_t
**output_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
85 jack_ringbuffer_t
**input_buffers
= mlt_pool_alloc( sizeof( jack_ringbuffer_t
*) * channels
);
86 jack_port_t
**jack_output_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
87 jack_port_t
**jack_input_ports
= mlt_pool_alloc( sizeof(jack_port_t
*) * channels
);
88 float **jack_output_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
89 float **jack_input_buffers
= mlt_pool_alloc( sizeof(float *) * jack_buffer_size
);
91 // Set properties for self-destruction
92 mlt_properties_set_data( properties
, "output_buffers", output_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
93 mlt_properties_set_data( properties
, "input_buffers", input_buffers
, sizeof( jack_ringbuffer_t
*) * channels
, NULL
, NULL
);
94 mlt_properties_set_data( properties
, "jack_output_ports", jack_output_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
95 mlt_properties_set_data( properties
, "jack_input_ports", jack_input_ports
, sizeof( jack_port_t
*) * channels
, NULL
, NULL
);
96 mlt_properties_set_data( properties
, "jack_output_buffers", jack_output_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
97 mlt_properties_set_data( properties
, "jack_input_buffers", jack_input_buffers
, sizeof( float *) * channels
, NULL
, NULL
);
99 // Start Jack processing - required before registering ports
100 jack_activate( jack_client
);
102 // Register Jack ports
103 for ( i
= 0; i
< channels
; i
++ )
107 output_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
108 input_buffers
[i
] = jack_ringbuffer_create( BUFFER_LEN
* sizeof(float) );
109 snprintf( mlt_name
, sizeof( mlt_name
), "obuf%d", i
);
110 mlt_properties_set_data( properties
, mlt_name
, output_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
111 snprintf( mlt_name
, sizeof( mlt_name
), "ibuf%d", i
);
112 mlt_properties_set_data( properties
, mlt_name
, input_buffers
[i
], BUFFER_LEN
* sizeof(float), NULL
, NULL
);
114 for ( in
= 0; in
< 2; in
++ )
116 snprintf( mlt_name
, sizeof( mlt_name
), "%s_%d", in ?
"in" : "out", i
+ 1);
117 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
119 *port
= jack_port_register( jack_client
, mlt_name
, JACK_DEFAULT_AUDIO_TYPE
,
120 ( in ? JackPortIsInput
: JackPortIsOutput
) | JackPortIsTerminal
, 0 );
124 // Establish connections
125 for ( i
= 0; i
< channels
; i
++ )
128 for ( in
= 0; in
< 2; in
++ )
130 port
= ( in ?
&jack_input_ports
[i
] : &jack_output_ports
[i
] );
131 snprintf( mlt_name
, sizeof( mlt_name
), "%s", jack_port_name( *port
) );
133 snprintf( rack_name
, sizeof( rack_name
), "%s_%d", in ?
"in" : "out", i
+ 1 );
134 if ( mlt_properties_get( properties
, "_rack_client_name" ) )
135 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_rack_client_name" ), in ?
"out" : "in", i
+ 1);
136 else if ( mlt_properties_get( properties
, rack_name
) )
137 snprintf( rack_name
, sizeof( rack_name
), "%s", mlt_properties_get( properties
, rack_name
) );
139 snprintf( rack_name
, sizeof( rack_name
), "%s:%s_%d", mlt_properties_get( properties
, "_client_name" ), in ?
"out" : "in", i
+ 1);
143 fprintf( stderr
, "jack connect %s to %s\n", rack_name
, mlt_name
);
144 jack_connect( jack_client
, rack_name
, mlt_name
);
148 fprintf( stderr
, "jack connect %s to %s\n", mlt_name
, rack_name
);
149 jack_connect( jack_client
, mlt_name
, rack_name
);
155 static int jack_process (jack_nframes_t frames
, void * data
)
157 mlt_filter filter
= (mlt_filter
) data
;
158 mlt_properties properties
= MLT_FILTER_PROPERTIES( filter
);
159 int channels
= mlt_properties_get_int( properties
, "_channels" );
162 static size_t total_size
= 0;
163 size_t first_ring_size
= mlt_properties_get_int( properties
, "_samples" ) * sizeof(float);
165 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( properties
, "output_buffers", NULL
);
166 if ( output_buffers
== NULL
)
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
);
176 for ( i
= 0; i
< channels
; i
++ )
178 size_t jack_size
= ( frames
* sizeof(float) );
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
] )
185 fprintf( stderr
, "%s: no jack buffer for output port %d\n", __FUNCTION__
, i
);
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
);
192 // Do not start returning audio until we have sent first mlt frame
193 if ( first_ring_size
!= -sizeof(float) && i
== 0 )
194 total_size
+= ring_size
;
195 if ( first_ring_size
== -sizeof(float) || total_size
>= first_ring_size
)
197 // Return audio through in port
198 jack_input_buffers
[i
] = jack_port_get_buffer( jack_input_ports
[i
], frames
);
199 if ( ! jack_input_buffers
[i
] )
201 fprintf( stderr
, "%s: no jack buffer for input port %d\n", __FUNCTION__
, i
);
206 ring_size
= jack_ringbuffer_write_space( input_buffers
[i
] );
207 jack_ringbuffer_write( input_buffers
[i
], ( char * )jack_input_buffers
[i
], ring_size
< jack_size ? ring_size
: jack_size
);
209 // Tell mlt that audio is available
210 if ( first_ring_size
!= -sizeof(float) && i
== ( channels
- 1 )
211 && pthread_mutex_trylock( output_lock
) == 0 )
213 pthread_cond_signal( output_ready
);
214 pthread_mutex_unlock( output_lock
);
217 // Set flag to skip this henceforth
218 mlt_properties_set_int( properties
, "_samples", -1 );
229 static int jackrack_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
231 // Get the filter service
232 mlt_filter filter
= mlt_frame_pop_audio( frame
);
234 // Get the filter properties
235 mlt_properties filter_properties
= MLT_FILTER_PROPERTIES( filter
);
237 // Restore the original get_audio
238 frame
->get_audio
= mlt_frame_pop_audio( frame
);
240 int jack_frequency
= mlt_properties_get_int( filter_properties
, "_sample_rate" );
242 // Get the producer's audio
243 mlt_frame_get_audio( frame
, buffer
, format
, &jack_frequency
, channels
, samples
);
244 //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
246 // Deal with sample rate differences
247 if ( *frequency
!= jack_frequency
)
248 fprintf( stderr
, "mismatching frequencies in filter jackrack\n" );
249 *frequency
= jack_frequency
;
251 // Initialise Jack ports and connections if needed
252 if ( ! mlt_properties_get_data( filter_properties
, "jack_output_ports", NULL
) )
253 initialise_jack_ports( filter_properties
, *channels
, *samples
);
255 // Get the filter-specific properties
256 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
257 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
258 pthread_mutex_t
*output_lock
= mlt_properties_get_data( filter_properties
, "output_lock", NULL
);
259 pthread_cond_t
*output_ready
= mlt_properties_get_data( filter_properties
, "output_ready", NULL
);
262 int16_t *q
= *buffer
;
266 // Convert to floats and write into output ringbuffer
267 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
269 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
270 //pthread_cond_wait( &output_ready, &output_lock );
271 for ( i
= 0; i
< *samples
; i
++ )
272 for ( j
= 0; j
< *channels
; j
++ )
274 sample
= ( float )( *q
++ ) / 32768.0;
275 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
279 // fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
281 // Read from input ringbuffer and convert from floats
282 while ( mlt_properties_get_int( filter_properties
, "_samples" ) != -1
283 && jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) )
284 pthread_cond_wait( output_ready
, output_lock
);
286 // Initialise to silence, but repeat last frame if available in case of
290 for ( i
= 0; i
< *samples
; i
++ )
291 for ( j
= 0; j
< *channels
; j
++ )
293 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
297 else if ( sample
< -1.0 )
301 *q
++ = 32767 * sample
;
303 *q
++ = 32768 * sample
;
310 /** Filter processing.
313 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
315 if ( frame
->get_audio
!= NULL
)
317 mlt_frame_push_audio( frame
, frame
->get_audio
);
318 mlt_frame_push_audio( frame
, this );
319 frame
->get_audio
= jackrack_get_audio
;
326 void filter_close( mlt_filter
this )
330 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
331 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
333 jack_deactivate( jack_client
);
334 jack_client_close( jack_client
);
335 for ( i
= 0; i
< mlt_properties_get_int( properties
, "_channels" ); i
++ )
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
) );
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
) );
351 pthread_t
*jackrack_pthread
= mlt_properties_get_data( properties
, "jackrack_thread", NULL
);
352 if ( jackrack_pthread
!= NULL
)
354 mlt_properties_set_int( properties
, "_done", 1 );
355 pthread_join( *jackrack_pthread
, NULL
);
356 mlt_pool_release( jackrack_pthread
);
359 this->parent
.close
= NULL
;
360 mlt_service_close( &this->parent
);
363 /** Constructor for the filter.
366 mlt_filter
filter_jackrack_init( char *arg
)
368 mlt_filter
this = mlt_filter_new( );
373 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
374 jack_client_t
*jack_client
= jack_client_new( name
);
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
) );
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
);
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
);