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