1 /*
2  * filter_jackrack.c -- filter audio through Jack and/or LADSPA plugins
3  * Copyright (C) 2004-2021 Meltytech, LLC
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 
20 #include <framework/mlt.h>
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <pthread.h>
28 #include <jack/jack.h>
29 #include <jack/ringbuffer.h>
30 #include <string.h>
31 
32 #include "jack_rack.h"
33 
34 extern pthread_mutex_t g_activate_mutex;
35 
36 #define BUFFER_LEN 204800 * 6
37 #define JACKSTATE(x) (x==JackTransportStopped?"stopped":x==JackTransportStarting?"starting":x==JackTransportRolling?"rolling":"unknown")
38 
jack_sync(jack_transport_state_t state,jack_position_t * jack_pos,void * arg)39 static int jack_sync( jack_transport_state_t state, jack_position_t *jack_pos, void *arg )
40 {
41 	mlt_filter filter = (mlt_filter) arg;
42 	mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
43 	mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE(filter) );
44 	mlt_position position = mlt_profile_fps( profile ) * jack_pos->frame / jack_pos->frame_rate + 0.5;
45 	int result = 1;
46 
47 	mlt_log_debug( MLT_FILTER_SERVICE(filter), "%s frame %u rate %u pos %d last_pos %d\n",
48 		JACKSTATE(state), jack_pos->frame, jack_pos->frame_rate, position,
49 		mlt_properties_get_position( properties, "_last_pos" ) );
50 	if ( state == JackTransportStopped )
51 	{
52 		mlt_events_fire( properties, "jack-stopped", mlt_event_data_from_int(position) );
53 		mlt_properties_set_int( properties, "_sync_guard", 0 );
54 	}
55 	else if ( state == JackTransportStarting )
56 	{
57 		result = 0;
58 		if ( !mlt_properties_get_int( properties, "_sync_guard" ) )
59 		{
60 			mlt_properties_set_int( properties, "_sync_guard", 1 );
61 			mlt_events_fire( properties, "jack-started", mlt_event_data_from_int(position) );
62 		}
63 		else if ( position >= mlt_properties_get_position( properties, "_last_pos" ) - 2 )
64 		{
65 			mlt_properties_set_int( properties, "_sync_guard", 0 );
66 			result = 1;
67 		}
68 	}
69 	else
70 	{
71 		mlt_properties_set_int( properties, "_sync_guard", 0 );
72 	}
73 
74 	return result;
75 }
76 
on_jack_start(mlt_properties owner,mlt_properties properties)77 static void on_jack_start( mlt_properties owner, mlt_properties properties )
78 {
79 	mlt_log_verbose( NULL, "%s\n", __FUNCTION__ );
80 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
81 	jack_transport_start( jack_client );
82 }
83 
on_jack_stop(mlt_properties owner,mlt_properties properties)84 static void on_jack_stop( mlt_properties owner, mlt_properties properties )
85 {
86 	mlt_log_verbose( NULL, "%s\n", __FUNCTION__ );
87 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
88 	jack_transport_stop( jack_client );
89 }
90 
on_jack_seek(mlt_properties owner,mlt_filter filter,mlt_event_data event_data)91 static void on_jack_seek( mlt_properties owner, mlt_filter filter, mlt_event_data event_data )
92 {
93 	mlt_position position = mlt_event_data_to_int(event_data);
94 	mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
95 	mlt_log_verbose( MLT_FILTER_SERVICE(filter), "%s: %d\n", __FUNCTION__, position );
96 	mlt_properties_set_int( properties, "_sync_guard", 1 );
97 	mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
98 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
99 	jack_nframes_t jack_frame = jack_get_sample_rate( jack_client );
100 	jack_frame *= position / mlt_profile_fps( profile );
101 	jack_transport_locate( jack_client, jack_frame );
102 }
103 
initialise_jack_ports(mlt_properties properties)104 static void initialise_jack_ports( mlt_properties properties )
105 {
106 	int i;
107 	char mlt_name[67], rack_name[30];
108 	jack_port_t **port = NULL;
109 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
110 	jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
111 
112 	// Propagate these for the Jack processing callback
113 	int channels = mlt_properties_get_int( properties, "channels" );
114 
115 	// Start JackRack
116 	if ( mlt_properties_get( properties, "src" ) )
117 	{
118 		snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
119 		jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
120 		jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );
121 
122 		mlt_properties_set_data( properties, "jackrack", jackrack, 0,
123 			(mlt_destructor) jack_rack_destroy, NULL );
124 		mlt_properties_set( properties, "_rack_client_name", rack_name );
125 	}
126 	else
127 	{
128 		// We have to set this to something to prevent re-initialization.
129 		mlt_properties_set_data( properties, "jackrack", jack_client, 0, NULL, NULL );
130 	}
131 
132 	// Allocate buffers and ports
133 	jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
134 	jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
135 	jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
136 	jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
137 	float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
138 	float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
139 
140 	// Set properties - released inside filter_close
141 	mlt_properties_set_data( properties, "output_buffers", output_buffers,
142 		sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
143 	mlt_properties_set_data( properties, "input_buffers", input_buffers,
144 		sizeof( jack_ringbuffer_t *) * channels, mlt_pool_release, NULL );
145 	mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports,
146 		sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
147 	mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports,
148 		sizeof( jack_port_t *) * channels, mlt_pool_release, NULL );
149 	mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers,
150 		sizeof( float *) * channels, mlt_pool_release, NULL );
151 	mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers,
152 		sizeof( float *) * channels, mlt_pool_release, NULL );
153 
154 	// Register Jack ports
155 	for ( i = 0; i < channels; i++ )
156 	{
157 		int in;
158 
159 		output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
160 		input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
161 		snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
162 		mlt_properties_set_data( properties, mlt_name, output_buffers[i],
163 			BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
164 		snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
165 		mlt_properties_set_data( properties, mlt_name, input_buffers[i],
166 			BUFFER_LEN * sizeof(float), (mlt_destructor) jack_ringbuffer_free, NULL );
167 
168 		for ( in = 0; in < 2; in++ )
169 		{
170 			snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
171 			port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
172 
173 			*port =  jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
174 				( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
175 		}
176 	}
177 
178 	// Start Jack processing
179 	pthread_mutex_lock( &g_activate_mutex );
180 	jack_activate( jack_client );
181 	pthread_mutex_unlock( &g_activate_mutex  );
182 
183 	// Establish connections
184 	for ( i = 0; i < channels; i++ )
185 	{
186 		int in;
187 		for ( in = 0; in < 2; in++ )
188 		{
189 			port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
190 			snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
191 
192 			snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
193 			if ( mlt_properties_get( properties, "_rack_client_name" ) )
194 				snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
195 			else if ( mlt_properties_get( properties, rack_name ) )
196 				snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
197 			else
198 				snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "client_name" ), in ? "out" : "in", i + 1);
199 
200 			if ( in )
201 			{
202 				mlt_log_verbose( NULL, "JACK connect %s to %s\n", rack_name, mlt_name );
203 				jack_connect( jack_client, rack_name, mlt_name );
204 			}
205 			else
206 			{
207 				mlt_log_verbose( NULL, "JACK connect %s to %s\n", mlt_name, rack_name );
208 				jack_connect( jack_client, mlt_name, rack_name );
209 			}
210 		}
211 	}
212 }
213 
jack_process(jack_nframes_t frames,void * data)214 static int jack_process (jack_nframes_t frames, void * data)
215 {
216 	mlt_filter filter = (mlt_filter) data;
217  	mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
218 	int channels = mlt_properties_get_int( properties, "channels" );
219 	int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
220 	int sync = mlt_properties_get_int( properties, "_sync" );
221 	int err = 0;
222 	int i;
223 	static int total_size = 0;
224 
225 	jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
226 	if ( output_buffers == NULL )
227 		return 0;
228 	jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
229 	jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
230 	jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
231 	float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
232 	float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
233 	pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
234 	pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
235 
236 	for ( i = 0; i < channels; i++ )
237 	{
238 		size_t jack_size = ( frames * sizeof(float) );
239 		size_t ring_size;
240 
241 		// Send audio through out port
242 		jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
243 		if ( ! jack_output_buffers[i] )
244 		{
245 			mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for output port %d\n", i );
246 			err = 1;
247 			break;
248 		}
249 		ring_size = jack_ringbuffer_read_space( output_buffers[i] );
250 		jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
251 		if ( ring_size < jack_size )
252 			memset( &jack_output_buffers[i][ring_size], 0, jack_size - ring_size );
253 
254 		// Return audio through in port
255 		jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
256 		if ( ! jack_input_buffers[i] )
257 		{
258 			mlt_log_error( MLT_FILTER_SERVICE(filter), "no buffer for input port %d\n", i );
259 			err = 1;
260 			break;
261 		}
262 
263 		// Do not start returning audio until we have sent first mlt frame
264 		if ( sync && i == 0 && frame_size > 0 )
265 			total_size += ring_size;
266 		mlt_log_debug( MLT_FILTER_SERVICE(filter), "sync %d frame_size %d ring_size %zu jack_size %zu\n", sync, frame_size, ring_size, jack_size );
267 
268 		if ( ! sync || ( frame_size > 0  && total_size >= frame_size ) )
269 		{
270 			ring_size = jack_ringbuffer_write_space( input_buffers[i] );
271 			jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
272 
273 			if ( sync )
274 			{
275 				// Tell mlt that audio is available
276 				pthread_mutex_lock( output_lock);
277 				pthread_cond_signal( output_ready );
278 				pthread_mutex_unlock( output_lock);
279 
280 				// Clear sync phase
281 				mlt_properties_set_int( properties, "_sync", 0 );
282 			}
283 		}
284 	}
285 
286 	// Often jackd does not send the stopped event through the JackSyncCallback
287 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
288 	jack_position_t jack_pos;
289 	jack_transport_state_t state = jack_transport_query( jack_client, &jack_pos );
290 	int transport_state = mlt_properties_get_int( properties, "_transport_state" );
291 	if ( state != transport_state )
292 	{
293 		mlt_properties_set_int( properties, "_transport_state", state );
294 		if ( state == JackTransportStopped )
295 			jack_sync( state, &jack_pos, filter );
296 	}
297 
298 	return err;
299 }
300 
301 
302 /** Get the audio.
303 */
304 
jackrack_get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)305 static int jackrack_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
306 {
307 	// Get the filter service
308 	mlt_filter filter = mlt_frame_pop_audio( frame );
309 
310 	// Get the filter properties
311 	mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
312 
313 	int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
314 
315 	// Get the producer's audio
316 	*format = mlt_audio_float;
317 	mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
318 
319 	// TODO: Deal with sample rate differences
320 	if ( *frequency != jack_frequency )
321 		mlt_log_error( MLT_FILTER_SERVICE( filter ), "mismatching frequencies JACK = %d actual = %d\n",
322 			jack_frequency, *frequency );
323 	*frequency = jack_frequency;
324 
325 	// Initialise Jack ports and connections if needed
326 	if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
327 		mlt_properties_set_int( filter_properties, "_samples", *samples );
328 
329 	// Get the filter-specific properties
330 	jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
331 	jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
332 //	pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
333 //	pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
334 
335 	// Process the audio
336 	float *q = (float*) *buffer;
337 	size_t size = *samples * sizeof(float);
338 	int j;
339 //	struct timespec tm = { 0, 0 };
340 
341 	// Write into output ringbuffer
342 	for ( j = 0; j < *channels; j++ )
343 	{
344 		if ( jack_ringbuffer_write_space( output_buffers[j] ) >= size )
345 			jack_ringbuffer_write( output_buffers[j], (char*)( q + j * *samples ), size );
346 	}
347 
348 	// Synchronization phase - wait for signal from Jack process
349 	while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < size ) ;
350 		//pthread_cond_wait( output_ready, output_lock );
351 
352 	// Read from input ringbuffer
353 	for ( j = 0; j < *channels; j++, q++ )
354 	{
355 		if ( jack_ringbuffer_read_space( input_buffers[j] ) >= size )
356 			jack_ringbuffer_read( input_buffers[j], (char*)( q + j * *samples ), size );
357 	}
358 
359 	// help jack_sync() indicate when we are rolling
360 	mlt_position pos = mlt_frame_get_position( frame );
361 	mlt_properties_set_position( filter_properties, "_last_pos", pos );
362 
363 	return 0;
364 }
365 
366 
367 /** Filter processing.
368 */
369 
filter_process(mlt_filter this,mlt_frame frame)370 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
371 {
372 	{
373 		mlt_properties properties = MLT_FILTER_PROPERTIES( this );
374 		mlt_frame_push_audio( frame, this );
375 		mlt_frame_push_audio( frame, jackrack_get_audio );
376 
377 		if ( !mlt_properties_get_data( properties, "jackrack", NULL ) )
378 			initialise_jack_ports( properties );
379 	}
380 
381 	return frame;
382 }
383 
384 
filter_close(mlt_filter this)385 static void filter_close( mlt_filter this )
386 {
387 	mlt_properties properties = MLT_FILTER_PROPERTIES( this );
388 	jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
389 	jack_deactivate( jack_client );
390 	jack_client_close( jack_client );
391 	this->parent.close = NULL;
392 	mlt_service_close( &this->parent );
393 }
394 
395 /** Constructor for the filter.
396 */
397 
filter_jackrack_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)398 mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
399 {
400 	mlt_filter this = mlt_filter_new( );
401 	if ( this != NULL )
402 	{
403 		char name[61];
404 		char *jack_client_name;
405 		const char *src;
406 		jack_status_t status = 0;
407 
408 		if ( id && arg && !strcmp( id, "jack" ) )
409 		{
410 			snprintf( name, sizeof( name ), "%s", arg );
411 			src = NULL;
412 		}
413 		else
414 		{
415 			snprintf( name, sizeof( name ), "mlt%d", getpid() );
416 			src = arg;
417 		}
418 		jack_client_t *jack_client = jack_client_open( arg, JackNullOption, &status, NULL );
419 		if ( jack_client )
420 		{
421 			if ( status & JackNameNotUnique )
422 			{
423 				jack_client_name = jack_get_client_name ( jack_client );
424 				strcpy( name, jack_client_name );
425 			}
426 
427 			mlt_properties properties = MLT_FILTER_PROPERTIES( this );
428 			pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
429 			pthread_cond_t  *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
430 
431 			jack_set_process_callback( jack_client, jack_process, this );
432 			jack_set_sync_callback( jack_client, jack_sync, this );
433 			jack_set_sync_timeout( jack_client, 5000000 );
434 			//TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
435 			this->process = filter_process;
436 			this->close = filter_close;
437 			pthread_mutex_init( output_lock, NULL );
438 			pthread_cond_init( output_ready, NULL );
439 
440 			mlt_properties_set( properties, "src", src );
441 			mlt_properties_set( properties, "client_name", name );
442 			mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
443 			mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
444 			mlt_properties_set_data( properties, "output_lock", output_lock, 0, mlt_pool_release, NULL );
445 			mlt_properties_set_data( properties, "output_ready", output_ready, 0, mlt_pool_release, NULL );
446 			mlt_properties_set_int( properties, "_sync", 1 );
447 			mlt_properties_set_int( properties, "channels", 2 );
448 
449 			mlt_events_register( properties, "jack-started" );
450 			mlt_events_register( properties, "jack-stopped" );
451 			mlt_events_register( properties, "jack-start" );
452 			mlt_events_register( properties, "jack-stop" );
453 			mlt_events_register( properties, "jack-seek" );
454 			mlt_events_listen( properties, properties, "jack-start", (mlt_listener) on_jack_start );
455 			mlt_events_listen( properties, properties, "jack-stop", (mlt_listener) on_jack_stop );
456 			mlt_events_listen( properties, this, "jack-seek", (mlt_listener) on_jack_seek );
457 			mlt_properties_set_position( properties, "_jack_seek", -1 );
458 		}
459 		else
460 		{
461 			mlt_log_error( NULL, "Failed to connect to JACK server\n" );
462 			mlt_filter_close( this );
463 			this = NULL;
464 		}
465 	}
466 	return this;
467 }
468