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