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 // 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 // Get the producer's audio
243 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
244 //fprintf( stderr, "%s: %d frames %d channels\n", __FUNCTION__, *samples, *channels );
246 // Initialise Jack ports and connections if needed
247 if ( ! mlt_properties_get_data( filter_properties
, "jack_output_ports", NULL
) )
248 initialise_jack_ports( filter_properties
, *channels
, *samples
);
250 // Get the filter-specific properties
251 jack_ringbuffer_t
**output_buffers
= mlt_properties_get_data( filter_properties
, "output_buffers", NULL
);
252 jack_ringbuffer_t
**input_buffers
= mlt_properties_get_data( filter_properties
, "input_buffers", NULL
);
253 pthread_mutex_t
*output_lock
= mlt_properties_get_data( filter_properties
, "output_lock", NULL
);
254 pthread_cond_t
*output_ready
= mlt_properties_get_data( filter_properties
, "output_ready", NULL
);
257 int16_t *q
= *buffer
;
261 // Convert to floats and write into output ringbuffer
262 if ( jack_ringbuffer_write_space( output_buffers
[0] ) >= ( *samples
* sizeof(float) ) )
264 //fprintf( stderr, "%s: buffer overrun!\n", __FUNCTION__ );
265 //pthread_cond_wait( &output_ready, &output_lock );
266 for ( i
= 0; i
< *samples
; i
++ )
267 for ( j
= 0; j
< *channels
; j
++ )
269 sample
= ( float )( *q
++ ) / 32768.0;
270 jack_ringbuffer_write( output_buffers
[j
], ( char * )&sample
, sizeof(float) );
274 // fprintf( stderr, "%s: out buffer size %d\n", __FUNCTION__, jack_ringbuffer_write_space( output_buffers[0] ) );
276 // Read from input ringbuffer and convert from floats
277 while ( mlt_properties_get_int( filter_properties
, "_samples" ) != -1
278 && jack_ringbuffer_read_space( input_buffers
[ *channels
- 1 ] ) < ( *samples
* sizeof(float) ) )
279 pthread_cond_wait( output_ready
, output_lock
);
283 // Initialise to silence, but repeat last frame if available in case of
287 for ( i
= 0; i
< *samples
; i
++ )
288 for ( j
= 0; j
< *channels
; j
++ )
290 jack_ringbuffer_read( input_buffers
[j
], ( char * )&sample
, sizeof(float) );
294 else if ( sample
< -1.0 )
298 *q
++ = 32767 * sample
;
300 *q
++ = 32768 * sample
;
307 /** Filter processing.
310 static mlt_frame
filter_process( mlt_filter
this, mlt_frame frame
)
312 if ( frame
->get_audio
!= NULL
)
314 mlt_frame_push_audio( frame
, frame
->get_audio
);
315 mlt_frame_push_audio( frame
, this );
316 frame
->get_audio
= jackrack_get_audio
;
323 void filter_close( mlt_filter
this )
325 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
326 jack_client_t
*jack_client
= mlt_properties_get_data( properties
, "jack_client", NULL
);
328 jack_deactivate( jack_client
);
329 jack_client_close( jack_client
);
330 pthread_t
*jackrack_pthread
= mlt_properties_get_data( properties
, "jackrack_thread", NULL
);
331 if ( jackrack_pthread
!= NULL
)
333 mlt_properties_set_int( properties
, "_done", 1 );
334 pthread_join( *jackrack_pthread
, NULL
);
335 free( jackrack_pthread
);
339 /** Constructor for the filter.
342 mlt_filter
filter_jackrack_init( char *arg
)
344 mlt_filter
this = mlt_filter_new( );
349 snprintf( name
, sizeof( name
), "mlt%d", getpid() );
350 jack_client_t
*jack_client
= jack_client_new( name
);
353 mlt_properties properties
= MLT_FILTER_PROPERTIES( this );
354 pthread_mutex_t
*output_lock
= calloc( 1, sizeof( pthread_mutex_t
) );
355 pthread_cond_t
*output_ready
= calloc( 1, sizeof( pthread_cond_t
) );
357 jack_set_process_callback( jack_client
, jack_process
, this );
358 //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
359 this->process
= filter_process
;
360 pthread_mutex_init( output_lock
, NULL
);
361 pthread_cond_init( output_ready
, NULL
);
363 mlt_properties_set( properties
, "src", arg
);
364 mlt_properties_set( properties
, "_client_name", name
);
365 mlt_properties_set_data( properties
, "jack_client", jack_client
, 0, ( mlt_destructor
)filter_close
, NULL
);
366 mlt_properties_set_int( properties
, "_sample_rate", jack_get_sample_rate( jack_client
) );
367 mlt_properties_set_data( properties
, "output_lock", output_lock
, 0, free
, NULL
);
368 mlt_properties_set_data( properties
, "output_ready", output_ready
, 0, free
, NULL
);