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