]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
direct3d11: catch texture mapping errors
[vlc] / modules / audio_output / jack.c
1 /*****************************************************************************
2  * jack.c : JACK audio output module
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax _at_ videolan.org>
8  *          Jon Griffiths <jon_p_griffiths _At_ yahoo _DOT_ com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 /**
25  * \file modules/audio_output/jack.c
26  * \brief JACK audio output functions
27  */
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include <unistd.h>                                      /* write(), close() */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_aout.h>
40
41 #include <jack/jack.h>
42 #include <jack/ringbuffer.h>
43
44 typedef jack_default_audio_sample_t jack_sample_t;
45
46 /*****************************************************************************
47  * aout_sys_t: JACK audio output method descriptor
48  *****************************************************************************
49  * This structure is part of the audio output thread descriptor.
50  * It describes some JACK specific variables.
51  *****************************************************************************/
52 struct aout_sys_t
53 {
54     jack_ringbuffer_t *p_jack_ringbuffer;
55     jack_client_t  *p_jack_client;
56     jack_port_t   **p_jack_ports;
57     jack_sample_t **p_jack_buffers;
58     unsigned int    i_channels;
59     unsigned int    i_rate;
60     jack_nframes_t latency;
61     float soft_gain;
62     bool soft_mute;
63     mtime_t paused; /**< Time when (last) paused */
64 };
65
66 /*****************************************************************************
67  * Local prototypes
68  *****************************************************************************/
69 static int  Open         ( vlc_object_t * );
70 static void Close        ( vlc_object_t * );
71 static void Play         ( audio_output_t * p_aout, block_t * p_block );
72 static void Pause        ( audio_output_t *aout, bool paused, mtime_t date );
73 static void Flush        ( audio_output_t *p_aout, bool wait );
74 static int  TimeGet      ( audio_output_t *, mtime_t * );
75 static int  Process      ( jack_nframes_t i_frames, void *p_arg );
76 static int  GraphChange  ( void *p_arg );
77
78 #include "audio_output/volume.h"
79
80 #define AUTO_CONNECT_OPTION "jack-auto-connect"
81 #define AUTO_CONNECT_TEXT N_("Automatically connect to writable clients")
82 #define AUTO_CONNECT_LONGTEXT N_( \
83     "If enabled, this option will automatically connect sound output to the " \
84     "first writable JACK clients found." )
85
86 #define CONNECT_REGEX_OPTION "jack-connect-regex"
87 #define CONNECT_REGEX_TEXT N_("Connect to clients matching")
88 #define CONNECT_REGEX_LONGTEXT N_( \
89     "If automatic connection is enabled, only JACK clients whose names " \
90     "match this regular expression will be considered for connection." )
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 vlc_module_begin ()
96     set_shortname( "JACK" )
97     set_description( N_("JACK audio output") )
98     set_capability( "audio output", 100 )
99     set_category( CAT_AUDIO )
100     set_subcategory( SUBCAT_AUDIO_AOUT )
101     add_bool( AUTO_CONNECT_OPTION, true, AUTO_CONNECT_TEXT,
102               AUTO_CONNECT_LONGTEXT, false )
103     add_string( CONNECT_REGEX_OPTION, "system", CONNECT_REGEX_TEXT,
104                 CONNECT_REGEX_LONGTEXT, false )
105     add_sw_gain( )
106     set_callbacks( Open, Close )
107 vlc_module_end ()
108
109
110 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
111 {
112     char psz_name[32];
113     struct aout_sys_t *p_sys = p_aout->sys;
114     int status = VLC_SUCCESS;
115     unsigned int i;
116     int i_error;
117
118     p_sys->latency = 0;
119     p_sys->paused = VLC_TS_INVALID;
120
121     /* Connect to the JACK server */
122     snprintf( psz_name, sizeof(psz_name), "vlc_%d", getpid());
123     psz_name[sizeof(psz_name) - 1] = '\0';
124     p_sys->p_jack_client = jack_client_open( psz_name,
125                                              JackNullOption | JackNoStartServer,
126                                              NULL );
127     if( p_sys->p_jack_client == NULL )
128     {
129         msg_Err( p_aout, "failed to connect to JACK server" );
130         status = VLC_EGENERIC;
131         goto error_out;
132     }
133
134     /* Set the process callback */
135     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
136     jack_set_graph_order_callback ( p_sys->p_jack_client, GraphChange, p_aout );
137
138     /* JACK only supports fl32 format */
139     fmt->i_format = VLC_CODEC_FL32;
140     // TODO add buffer size callback
141     p_sys->i_rate = fmt->i_rate = jack_get_sample_rate( p_sys->p_jack_client );
142
143     p_aout->play = Play;
144     p_aout->pause = Pause;
145     p_aout->flush = Flush;
146     p_aout->time_get = TimeGet;
147     aout_SoftVolumeStart( p_aout );
148
149     p_sys->i_channels = aout_FormatNbChannels( fmt );
150     aout_FormatPrepare(fmt);
151
152     p_sys->p_jack_ports = malloc( p_sys->i_channels *
153                                   sizeof(jack_port_t *) );
154     if( p_sys->p_jack_ports == NULL )
155     {
156         status = VLC_ENOMEM;
157         goto error_out;
158     }
159
160     p_sys->p_jack_buffers = malloc( p_sys->i_channels *
161                                     sizeof(jack_sample_t *) );
162     if( p_sys->p_jack_buffers == NULL )
163     {
164         status = VLC_ENOMEM;
165         goto error_out;
166     }
167
168     const size_t buf_sz = AOUT_MAX_ADVANCE_TIME * fmt->i_rate *
169         fmt->i_bytes_per_frame / CLOCK_FREQ;
170     p_sys->p_jack_ringbuffer = jack_ringbuffer_create( buf_sz );
171
172     if( p_sys->p_jack_ringbuffer == NULL )
173     {
174         status = VLC_ENOMEM;
175         goto error_out;
176     }
177
178     if( jack_ringbuffer_mlock( p_sys->p_jack_ringbuffer ))
179     {
180         msg_Warn( p_aout, "failed to lock JACK ringbuffer in memory" );
181     }
182
183     /* Create the output ports */
184     for( i = 0; i < p_sys->i_channels; i++ )
185     {
186         snprintf( psz_name, sizeof(psz_name), "out_%d", i + 1);
187         psz_name[sizeof(psz_name) - 1] = '\0';
188         p_sys->p_jack_ports[i] = jack_port_register( p_sys->p_jack_client,
189                 psz_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
190
191         if( p_sys->p_jack_ports[i] == NULL )
192         {
193             msg_Err( p_aout, "failed to register a JACK port" );
194             status = VLC_EGENERIC;
195             goto error_out;
196         }
197     }
198
199     /* Tell the JACK server we are ready */
200     i_error = jack_activate( p_sys->p_jack_client );
201     if( i_error )
202     {
203         msg_Err( p_aout, "failed to activate JACK client (error %d)", i_error );
204         status = VLC_EGENERIC;
205         goto error_out;
206     }
207
208     /* Auto connect ports if we were asked to */
209     if( var_InheritBool( p_aout, AUTO_CONNECT_OPTION ) )
210     {
211         unsigned int i_in_ports;
212         char *psz_regex = var_InheritString( p_aout, CONNECT_REGEX_OPTION );
213         const char **pp_in_ports = jack_get_ports( p_sys->p_jack_client,
214                                                    psz_regex, NULL,
215                                                    JackPortIsInput );
216         free( psz_regex );
217         /* Count the number of returned ports */
218         i_in_ports = 0;
219         while( pp_in_ports && pp_in_ports[i_in_ports] )
220         {
221             i_in_ports++;
222         }
223
224         /* Tie the output ports to JACK input ports */
225         for( i = 0; i_in_ports > 0 && i < p_sys->i_channels; i++ )
226         {
227             const char* psz_in = pp_in_ports[i % i_in_ports];
228             const char* psz_out = jack_port_name( p_sys->p_jack_ports[i] );
229
230             i_error = jack_connect( p_sys->p_jack_client, psz_out, psz_in );
231             if( i_error )
232             {
233                 msg_Err( p_aout, "failed to connect port %s to port %s (error %d)",
234                          psz_out, psz_in, i_error );
235             }
236             else
237             {
238                 msg_Dbg( p_aout, "connecting port %s to port %s",
239                          psz_out, psz_in );
240             }
241         }
242         free( pp_in_ports );
243     }
244
245     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, rate=%d)",
246              p_sys->i_channels, fmt->i_rate );
247
248 error_out:
249     /* Clean up, if an error occurred */
250     if( status != VLC_SUCCESS )
251     {
252         if( p_sys->p_jack_client )
253         {
254             jack_deactivate( p_sys->p_jack_client );
255             jack_client_close( p_sys->p_jack_client );
256         }
257         if( p_sys->p_jack_ringbuffer )
258             jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
259
260         free( p_sys->p_jack_ports );
261         free( p_sys->p_jack_buffers );
262     }
263     return status;
264 }
265
266 static void Play (audio_output_t * p_aout, block_t * p_block)
267 {
268     struct aout_sys_t *p_sys = p_aout->sys;
269     jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
270     const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t);
271
272     while (p_block->i_buffer > 0) {
273
274         /* move data to buffer */
275         const size_t write_space = jack_ringbuffer_write_space(rb);
276         const size_t bytes = p_block->i_buffer < write_space ?
277             p_block->i_buffer : write_space;
278
279         /* If our audio thread is not reading fast enough */
280         if( unlikely( bytes == 0 ) ) {
281             msg_Warn( p_aout, "%"PRIuPTR " frames of audio dropped",
282                     p_block->i_buffer /  bytes_per_frame );
283             break;
284         }
285
286         jack_ringbuffer_write( rb, (const char *) p_block->p_buffer, bytes );
287
288         p_block->p_buffer += bytes;
289         p_block->i_buffer -= bytes;
290     }
291
292     block_Release(p_block);
293 }
294
295 /**
296  * Pause or unpause playback
297  */
298 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
299 {
300     aout_sys_t *sys = aout->sys;
301
302     if( paused ) {
303         sys->paused = date;
304     } else {
305         date -= sys->paused;
306         msg_Dbg(aout, "resuming after %"PRId64" us", date);
307         sys->paused = VLC_TS_INVALID;
308     }
309 }
310
311 static void Flush(audio_output_t *p_aout, bool wait)
312 {
313     struct aout_sys_t * p_sys = p_aout->sys;
314     jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
315
316     /* Sleep if wait was requested */
317     if( wait )
318     {
319         mtime_t delay;
320         if (!TimeGet(p_aout, &delay))
321             msleep(delay);
322     }
323
324     /* reset ringbuffer read and write pointers */
325     jack_ringbuffer_reset(rb);
326 }
327
328 static int TimeGet(audio_output_t *p_aout, mtime_t *delay)
329 {
330     struct aout_sys_t * p_sys = p_aout->sys;
331     jack_ringbuffer_t *rb = p_sys->p_jack_ringbuffer;
332     const size_t bytes_per_frame = p_sys->i_channels * sizeof(jack_sample_t);
333
334     *delay = (p_sys->latency +
335             (jack_ringbuffer_read_space(rb) / bytes_per_frame)) *
336         CLOCK_FREQ / p_sys->i_rate;
337
338     return 0;
339 }
340
341 /*****************************************************************************
342  * Process: callback for JACK
343  *****************************************************************************/
344 int Process( jack_nframes_t i_frames, void *p_arg )
345 {
346     unsigned int i, j, frames_from_rb = 0;
347     size_t bytes_read = 0;
348     size_t frames_read;
349     audio_output_t *p_aout = (audio_output_t*) p_arg;
350     struct aout_sys_t *p_sys = p_aout->sys;
351
352     /* Get the next audio data buffer unless paused */
353
354     if( p_sys->paused == VLC_TS_INVALID )
355         frames_from_rb = i_frames;
356
357     /* Get the JACK buffers to write to */
358     for( i = 0; i < p_sys->i_channels; i++ )
359     {
360         p_sys->p_jack_buffers[i] = jack_port_get_buffer( p_sys->p_jack_ports[i],
361                                                          i_frames );
362     }
363
364     /* Copy in the audio data */
365     for( j = 0; j < frames_from_rb; j++ )
366     {
367         for( i = 0; i < p_sys->i_channels; i++ )
368         {
369             jack_sample_t *p_dst = p_sys->p_jack_buffers[i] + j;
370             bytes_read += jack_ringbuffer_read( p_sys->p_jack_ringbuffer,
371                     (char *) p_dst, sizeof(jack_sample_t) );
372         }
373     }
374
375     /* Fill any remaining buffer with silence */
376     frames_read = (bytes_read / sizeof(jack_sample_t)) / p_sys->i_channels;
377     if( frames_read < i_frames )
378     {
379         for( i = 0; i < p_sys->i_channels; i++ )
380         {
381             memset( p_sys->p_jack_buffers[i] + frames_read, 0,
382                     sizeof( jack_sample_t ) * (i_frames - frames_read) );
383         }
384     }
385
386     return 0;
387 }
388
389 /*****************************************************************************
390  * GraphChange: callback when JACK reorders it's process graph.
391                 We update latency information.
392  *****************************************************************************/
393
394 static int GraphChange( void *p_arg )
395 {
396   audio_output_t *p_aout = (audio_output_t*) p_arg;
397   struct aout_sys_t *p_sys = p_aout->sys;
398   unsigned int i;
399   jack_latency_range_t port_latency;
400
401   p_sys->latency = 0;
402
403   for( i = 0; i < p_sys->i_channels; ++i )
404   {
405     jack_port_get_latency_range( p_sys->p_jack_ports[i], JackPlaybackLatency,
406                                  &port_latency );
407     p_sys->latency = __MAX( p_sys->latency, port_latency.max );
408   }
409
410   msg_Dbg(p_aout, "JACK graph reordered. Our maximum latency=%d.",
411           p_sys->latency);
412
413   return 0;
414 }
415
416 /*****************************************************************************
417  * Close: close the JACK client
418  *****************************************************************************/
419 static void Stop( audio_output_t *p_aout )
420 {
421     int i_error;
422     struct aout_sys_t *p_sys = p_aout->sys;
423
424     i_error = jack_deactivate( p_sys->p_jack_client );
425     if( i_error )
426     {
427         msg_Err( p_aout, "jack_deactivate failed (error %d)", i_error );
428     }
429
430     i_error = jack_client_close( p_sys->p_jack_client );
431     if( i_error )
432     {
433         msg_Err( p_aout, "jack_client_close failed (error %d)", i_error );
434     }
435     free( p_sys->p_jack_ports );
436     free( p_sys->p_jack_buffers );
437     jack_ringbuffer_free( p_sys->p_jack_ringbuffer );
438 }
439
440 static int Open(vlc_object_t *obj)
441 {
442     audio_output_t *aout = (audio_output_t *)obj;
443     aout_sys_t *sys = calloc(1, sizeof (*sys));
444
445     if( unlikely( sys == NULL ) )
446         return VLC_ENOMEM;
447     aout->sys = sys;
448     aout->start = Start;
449     aout->stop = Stop;
450     aout_SoftVolumeInit(aout);
451     return VLC_SUCCESS;
452 }
453
454 static void Close(vlc_object_t *obj)
455 {
456     audio_output_t *aout = (audio_output_t *)obj;
457     aout_sys_t *sys = aout->sys;
458
459     free(sys);
460 }