]> git.sesse.net Git - mlt/blob - src/modules/jackrack/filter_jackrack.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[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_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <jack/jack.h>
30 #include <jack/ringbuffer.h>
31 #include <pthread.h>
32 #include <string.h>
33
34 #include "jack_rack.h"
35
36 #define BUFFER_LEN 204800 * 3
37
38 static void initialise_jack_ports( mlt_properties properties )
39 {
40         int i;
41         char mlt_name[20], rack_name[30];
42         jack_port_t **port = NULL;
43         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
44         jack_nframes_t jack_buffer_size = jack_get_buffer_size( jack_client );
45         
46         // Propogate these for the Jack processing callback
47         int channels = mlt_properties_get_int( properties, "channels" );
48
49         // Start JackRack
50         if ( mlt_properties_get( properties, "src" ) )
51         {
52                 snprintf( rack_name, sizeof( rack_name ), "jackrack%d", getpid() );
53                 jack_rack_t *jackrack = jack_rack_new( rack_name, mlt_properties_get_int( properties, "channels" ) );
54                 jack_rack_open_file( jackrack, mlt_properties_get( properties, "src" ) );               
55                 
56                 mlt_properties_set_data( properties, "jackrack", jackrack, 0, NULL, NULL );
57                 mlt_properties_set( properties, "_rack_client_name", rack_name );
58         }
59                 
60         // Allocate buffers and ports
61         jack_ringbuffer_t **output_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
62         jack_ringbuffer_t **input_buffers = mlt_pool_alloc( sizeof( jack_ringbuffer_t *) * channels );
63         jack_port_t **jack_output_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
64         jack_port_t **jack_input_ports = mlt_pool_alloc( sizeof(jack_port_t *) * channels );
65         float **jack_output_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
66         float **jack_input_buffers = mlt_pool_alloc( sizeof(float *) * jack_buffer_size );
67
68         // Set properties - released inside filter_close
69         mlt_properties_set_data( properties, "output_buffers", output_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
70         mlt_properties_set_data( properties, "input_buffers", input_buffers, sizeof( jack_ringbuffer_t *) * channels, NULL, NULL );
71         mlt_properties_set_data( properties, "jack_output_ports", jack_output_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
72         mlt_properties_set_data( properties, "jack_input_ports", jack_input_ports, sizeof( jack_port_t *) * channels, NULL, NULL );
73         mlt_properties_set_data( properties, "jack_output_buffers", jack_output_buffers, sizeof( float *) * channels, NULL, NULL );
74         mlt_properties_set_data( properties, "jack_input_buffers", jack_input_buffers, sizeof( float *) * channels, NULL, NULL );
75
76         // Start Jack processing - required before registering ports
77         jack_activate( jack_client );
78         
79         // Register Jack ports
80         for ( i = 0; i < channels; i++ )
81         {
82                 int in;
83                 
84                 output_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
85                 input_buffers[i] = jack_ringbuffer_create( BUFFER_LEN * sizeof(float) );
86                 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
87                 mlt_properties_set_data( properties, mlt_name, output_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
88                 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
89                 mlt_properties_set_data( properties, mlt_name, input_buffers[i], BUFFER_LEN * sizeof(float), NULL, NULL );
90                 
91                 for ( in = 0; in < 2; in++ )
92                 {
93                         snprintf( mlt_name, sizeof( mlt_name ), "%s_%d", in ? "in" : "out", i + 1);
94                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
95                         
96                         *port =  jack_port_register( jack_client, mlt_name, JACK_DEFAULT_AUDIO_TYPE,
97                                 ( in ? JackPortIsInput : JackPortIsOutput ) | JackPortIsTerminal, 0 );
98                 }
99         }
100         
101         // Establish connections
102         for ( i = 0; i < channels; i++ )
103         {
104                 int in;
105                 for ( in = 0; in < 2; in++ )
106                 {
107                         port = ( in ? &jack_input_ports[i] : &jack_output_ports[i] );
108                         snprintf( mlt_name, sizeof( mlt_name ), "%s", jack_port_name( *port ) );
109
110                         snprintf( rack_name, sizeof( rack_name ), "%s_%d", in ? "in" : "out", i + 1 );
111                         if ( mlt_properties_get( properties, "_rack_client_name" ) )
112                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_rack_client_name" ), in ? "out" : "in", i + 1);
113                         else if ( mlt_properties_get( properties, rack_name ) )
114                                 snprintf( rack_name, sizeof( rack_name ), "%s", mlt_properties_get( properties, rack_name ) );
115                         else
116                                 snprintf( rack_name, sizeof( rack_name ), "%s:%s_%d", mlt_properties_get( properties, "_client_name" ), in ? "out" : "in", i + 1);
117                         
118                         if ( in )
119                         {
120                                 fprintf( stderr, "jack connect %s to %s\n", rack_name, mlt_name );
121                                 jack_connect( jack_client, rack_name, mlt_name );
122                         }
123                         else
124                         {
125                                 fprintf( stderr, "jack connect %s to %s\n", mlt_name, rack_name );
126                                 jack_connect( jack_client, mlt_name, rack_name );
127                         }
128                 }
129         }
130 }
131
132 static int jack_process (jack_nframes_t frames, void * data)
133 {
134         mlt_filter filter = (mlt_filter) data;
135         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
136         int channels = mlt_properties_get_int( properties, "channels" );
137         int frame_size = mlt_properties_get_int( properties, "_samples" ) * sizeof(float);
138         int sync = mlt_properties_get_int( properties, "_sync" );
139         int err = 0;
140         int i;
141         static int total_size = 0;
142   
143         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( properties, "output_buffers", NULL );
144         if ( output_buffers == NULL )
145                 return 0;
146         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( properties, "input_buffers", NULL );
147         jack_port_t **jack_output_ports = mlt_properties_get_data( properties, "jack_output_ports", NULL );
148         jack_port_t **jack_input_ports = mlt_properties_get_data( properties, "jack_input_ports", NULL );
149         float **jack_output_buffers = mlt_properties_get_data( properties, "jack_output_buffers", NULL );
150         float **jack_input_buffers = mlt_properties_get_data( properties, "jack_input_buffers", NULL );
151         pthread_mutex_t *output_lock = mlt_properties_get_data( properties, "output_lock", NULL );
152         pthread_cond_t *output_ready = mlt_properties_get_data( properties, "output_ready", NULL );
153         
154         for ( i = 0; i < channels; i++ )
155         {
156                 size_t jack_size = ( frames * sizeof(float) );
157                 size_t ring_size;
158                 
159                 // Send audio through out port
160                 jack_output_buffers[i] = jack_port_get_buffer( jack_output_ports[i], frames );
161                 if ( ! jack_output_buffers[i] )
162                 {
163                         fprintf( stderr, "%s: no jack buffer for output port %d\n", __FUNCTION__, i );
164                         err = 1;
165                         break;
166                 }
167                 ring_size = jack_ringbuffer_read_space( output_buffers[i] );
168                 jack_ringbuffer_read( output_buffers[i], ( char * )jack_output_buffers[i], ring_size < jack_size ? ring_size : jack_size );
169                 
170                 // Return audio through in port
171                 jack_input_buffers[i] = jack_port_get_buffer( jack_input_ports[i], frames );
172                 if ( ! jack_input_buffers[i] )
173                 {
174                         fprintf( stderr, "%s: no jack buffer for input port %d\n", __FUNCTION__, i );
175                         err = 1;
176                         break;
177                 }
178                 
179                 // Do not start returning audio until we have sent first mlt frame
180                 if ( sync && i == 0 && frame_size > 0 )
181                         total_size += ring_size;
182                 //fprintf(stderr, "sync %d frame_size %d ring_size %d jack_size %d\n", sync, frame_size, ring_size, jack_size );
183                 
184                 if ( ! sync || ( frame_size > 0  && total_size >= frame_size ) )
185                 {
186                         ring_size = jack_ringbuffer_write_space( input_buffers[i] );
187                         jack_ringbuffer_write( input_buffers[i], ( char * )jack_input_buffers[i], ring_size < jack_size ? ring_size : jack_size );
188                         
189                         if ( sync )
190                         {
191                                 // Tell mlt that audio is available
192                                 pthread_mutex_lock( output_lock);
193                                 pthread_cond_signal( output_ready );
194                                 pthread_mutex_unlock( output_lock);
195
196                                 // Clear sync phase
197                                 mlt_properties_set_int( properties, "_sync", 0 );
198                         }
199                 }
200         }
201
202         return err;
203 }
204
205
206 /** Get the audio.
207 */
208
209 static int jackrack_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
210 {
211         // Get the filter service
212         mlt_filter filter = mlt_frame_pop_audio( frame );
213
214         // Get the filter properties
215         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
216
217         int jack_frequency = mlt_properties_get_int( filter_properties, "_sample_rate" );
218
219         // Get the producer's audio
220         mlt_frame_get_audio( frame, buffer, format, &jack_frequency, channels, samples );
221         
222         // TODO: Deal with sample rate differences
223         if ( *frequency != jack_frequency )
224                 fprintf( stderr, "mismatching frequencies in filter jackrack\n" );
225         *frequency = jack_frequency;
226
227         // Initialise Jack ports and connections if needed
228         if ( mlt_properties_get_int( filter_properties, "_samples" ) == 0 )
229                 mlt_properties_set_int( filter_properties, "_samples", *samples );
230         
231         // Get the filter-specific properties
232         jack_ringbuffer_t **output_buffers = mlt_properties_get_data( filter_properties, "output_buffers", NULL );
233         jack_ringbuffer_t **input_buffers = mlt_properties_get_data( filter_properties, "input_buffers", NULL );
234 //      pthread_mutex_t *output_lock = mlt_properties_get_data( filter_properties, "output_lock", NULL );
235 //      pthread_cond_t *output_ready = mlt_properties_get_data( filter_properties, "output_ready", NULL );
236         
237         // Process the audio
238         int16_t *q = *buffer;
239         float sample[ 2 ][ 10000 ];
240         int i, j;
241 //      struct timespec tm = { 0, 0 };
242
243         // Convert to floats and write into output ringbuffer
244         if ( jack_ringbuffer_write_space( output_buffers[0] ) >= ( *samples * sizeof(float) ) )
245         {
246                 for ( i = 0; i < *samples; i++ )
247                         for ( j = 0; j < *channels; j++ )
248                                 sample[ j ][ i ] = ( float )( *q ++ ) / 32768.0;
249
250                 for ( j = 0; j < *channels; j++ )
251                         jack_ringbuffer_write( output_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
252         }
253
254         // Synchronization phase - wait for signal from Jack process
255         while ( jack_ringbuffer_read_space( input_buffers[ *channels - 1 ] ) < ( *samples * sizeof(float) ) ) ;
256                 //pthread_cond_wait( output_ready, output_lock );
257                 
258         // Read from input ringbuffer and convert from floats
259         if ( jack_ringbuffer_read_space( input_buffers[0] ) >= ( *samples * sizeof(float) ) )
260         {
261                 // Initialise to silence, but repeat last frame if available in case of 
262                 // buffer underrun
263                 for ( j = 0; j < *channels; j++ )
264                         jack_ringbuffer_read( input_buffers[j], ( char * )sample[ j ], *samples * sizeof(float) );
265
266                 q = *buffer;
267                 for ( i = 0; i < *samples; i++ )
268                         for ( j = 0; j < *channels; j++ )
269                         {
270                                 if ( sample[ j ][ i ] > 1.0 )
271                                         sample[ j ][ i ] = 1.0;
272                                 else if ( sample[ j ][ i ] < -1.0 )
273                                         sample[ j ][ i ] = -1.0;
274                         
275                                 if ( sample[ j ][ i ] > 0 )
276                                         *q ++ = 32767 * sample[ j ][ i ];
277                                 else
278                                         *q ++ = 32768 * sample[ j ][ i ];
279                         }
280         }
281
282         return 0;
283 }
284
285
286 /** Filter processing.
287 */
288
289 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
290 {
291         {
292                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
293                 mlt_frame_push_audio( frame, this );
294                 mlt_frame_push_audio( frame, jackrack_get_audio );
295                 
296                 if ( mlt_properties_get_int( properties, "_sync" ) )
297                         initialise_jack_ports( properties );
298         }
299
300         return frame;
301 }
302
303
304 static void filter_close( mlt_filter this )
305 {
306         int i;
307         char mlt_name[20];
308         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
309         jack_client_t *jack_client = mlt_properties_get_data( properties, "jack_client", NULL );
310         
311         jack_deactivate( jack_client );
312         jack_client_close( jack_client );
313         for ( i = 0; i < mlt_properties_get_int( properties, "channels" ); i++ )
314         {
315                 snprintf( mlt_name, sizeof( mlt_name ), "obuf%d", i );
316                 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
317                 snprintf( mlt_name, sizeof( mlt_name ), "ibuf%d", i );
318                 jack_ringbuffer_free( mlt_properties_get_data( properties, mlt_name, NULL ) );
319         }
320         mlt_pool_release( mlt_properties_get_data( properties, "output_buffers", NULL ) );
321         mlt_pool_release( mlt_properties_get_data( properties, "input_buffers", NULL ) );
322         mlt_pool_release( mlt_properties_get_data( properties, "jack_output_ports", NULL ) );
323         mlt_pool_release( mlt_properties_get_data( properties, "jack_input_ports", NULL ) );
324         mlt_pool_release( mlt_properties_get_data( properties, "jack_output_buffers", NULL ) );
325         mlt_pool_release( mlt_properties_get_data( properties, "jack_input_buffers", NULL ) );
326         mlt_pool_release( mlt_properties_get_data( properties, "output_lock", NULL ) );
327         mlt_pool_release( mlt_properties_get_data( properties, "output_ready", NULL ) );
328
329         jack_rack_t *jackrack = mlt_properties_get_data( properties, "jackrack", NULL );
330         jack_rack_destroy( jackrack );
331         
332         this->parent.close = NULL;
333         mlt_service_close( &this->parent );
334 }
335
336 /** Constructor for the filter.
337 */
338
339 mlt_filter filter_jackrack_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
340 {
341         mlt_filter this = mlt_filter_new( );
342         if ( this != NULL )
343         {
344                 char name[14];
345                 
346                 snprintf( name, sizeof( name ), "mlt%d", getpid() );
347                 jack_client_t *jack_client = jack_client_new( name );
348                 if ( jack_client )
349                 {
350                         mlt_properties properties = MLT_FILTER_PROPERTIES( this );
351                         pthread_mutex_t *output_lock = mlt_pool_alloc( sizeof( pthread_mutex_t ) );
352                         pthread_cond_t  *output_ready = mlt_pool_alloc( sizeof( pthread_cond_t ) );
353                         
354                         jack_set_process_callback( jack_client, jack_process, this );
355                         //TODO: jack_on_shutdown( jack_client, jack_shutdown_cb, this );
356                         this->process = filter_process;
357                         this->close = filter_close;
358                         pthread_mutex_init( output_lock, NULL );
359                         pthread_cond_init( output_ready, NULL );
360                         
361                         mlt_properties_set( properties, "src", arg );
362                         mlt_properties_set( properties, "_client_name", name );
363                         mlt_properties_set_data( properties, "jack_client", jack_client, 0, NULL, NULL );
364                         mlt_properties_set_int( properties, "_sample_rate", jack_get_sample_rate( jack_client ) );
365                         mlt_properties_set_data( properties, "output_lock", output_lock, 0, NULL, NULL );
366                         mlt_properties_set_data( properties, "output_ready", output_ready, 0, NULL, NULL );
367                         mlt_properties_set_int( properties, "_sync", 1 );
368                         mlt_properties_set_int( properties, "channels", 2 );
369                 }
370         }
371         return this;
372 }