]> git.sesse.net Git - vlc/blob - modules/audio_output/jack.c
* jack.c: fixed non-stereo audio streams
[vlc] / modules / audio_output / jack.c
1 /*****************************************************************************
2  * jack : JACK audio output module
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Cyril Deguet <asmax@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>                                            /* strerror() */
28 #include <unistd.h>                                      /* write(), close() */
29 #include <stdlib.h>                            /* calloc(), malloc(), free() */
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33
34 #include "aout_internal.h"
35
36 #include <jack/jack.h>
37
38 /*****************************************************************************
39  * aout_sys_t: JACK audio output method descriptor
40  *****************************************************************************
41  * This structure is part of the audio output thread descriptor.
42  * It describes some JACK specific variables.
43  *****************************************************************************/
44 struct aout_sys_t
45 {
46     jack_client_t *p_jack_client;
47     jack_port_t   *p_jack_port[2];
48     unsigned int  i_channels;
49 };
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  Open         ( vlc_object_t * );
55 static void Close        ( vlc_object_t * );
56 static void Play         ( aout_instance_t * );
57 static int Process       ( jack_nframes_t i_frames, void *p_arg );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
63    set_shortname( "JACK" );
64    set_description( _("JACK audio output") );
65    set_capability( "audio output", 100 );
66     set_category( CAT_AUDIO );
67     set_subcategory( SUBCAT_AUDIO_AOUT );
68    set_callbacks( Open, Close );
69 vlc_module_end();
70
71 /*****************************************************************************
72  * Open: create a JACK client
73  *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
75 {
76     aout_instance_t *p_aout = (aout_instance_t *)p_this;
77     unsigned int i, i_in_ports;
78     const char **pp_in_ports;
79     struct aout_sys_t * p_sys;
80
81     /* Allocate structure */
82     p_sys = malloc( sizeof( aout_sys_t ) );
83     if( p_sys == NULL )
84     {
85         msg_Err( p_aout, "out of memory" );
86         return VLC_ENOMEM;
87     }
88     p_aout->output.p_sys = p_sys;
89
90     /* Connect to the JACK server */
91     p_sys->p_jack_client = jack_client_new( "vlc" );
92     if( p_sys->p_jack_client == NULL )
93     {
94         msg_Err( p_aout, "failed to connect to JACK server" );
95         free( p_sys );
96         return VLC_EGENERIC;
97     }
98
99     /* Set the process callback */
100     jack_set_process_callback( p_sys->p_jack_client, Process, p_aout );
101
102     p_aout->output.pf_play = Play;
103     aout_VolumeSoftInit( p_aout );
104
105     /* JACK only support fl32 format */
106     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
107     // TODO add buffer size callback
108     p_aout->output.i_nb_samples = jack_get_buffer_size( p_sys->p_jack_client );
109     p_aout->output.output.i_rate = jack_get_sample_rate( p_sys->p_jack_client );
110
111     p_sys->i_channels = aout_FormatNbChannels( &p_aout->output.output );
112
113     /* Create the output ports */
114     for( i = 0; i < p_sys->i_channels; i++ )
115     {
116         char p_name[32];
117         snprintf( p_name, 32, "channel_%d", i + 1);
118         p_sys->p_jack_port[i] = jack_port_register( p_sys->p_jack_client,
119                 p_name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
120
121         if( p_sys->p_jack_port[i] == NULL )
122         {
123             msg_Err( p_aout, "failed to register a JACK port" );
124             jack_client_close( p_sys->p_jack_client );
125             free( p_sys );
126             return VLC_EGENERIC;
127         }
128     }
129
130     /* Tell the JACK server we are ready */
131     if( jack_activate( p_sys->p_jack_client ) )
132     {
133         msg_Err( p_aout, "failed to activate JACK client" );
134         jack_client_close( p_sys->p_jack_client );
135         free( p_sys );
136         return VLC_EGENERIC;
137     }
138
139
140     /* Find input ports to connect to */
141     pp_in_ports = jack_get_ports( p_sys->p_jack_client, NULL, NULL,
142                                   JackPortIsInput );
143     i_in_ports = 0;
144     while( pp_in_ports && pp_in_ports[i_in_ports] )
145     {
146         i_in_ports++;
147     }
148
149     /* Connect the output ports to input ports */
150     if( i_in_ports > 0 )
151     {
152         for( i = 0; i < p_sys->i_channels; i++ )
153         {
154             int i_in = i % i_in_ports;
155             if( jack_connect( p_sys->p_jack_client,
156                               jack_port_name( p_sys->p_jack_port[i] ),
157                               pp_in_ports[i_in]) )
158             {
159                 msg_Err( p_aout, "failed to connect port %s to port %s",
160                          jack_port_name( p_sys->p_jack_port[i] ),
161                          pp_in_ports[i_in] );
162
163             }
164             else
165             {
166                 msg_Dbg( p_aout, "connecting port %s to port %s",
167                          jack_port_name( p_sys->p_jack_port[i] ),
168                          pp_in_ports[i_in] );
169             }
170         }
171     }
172
173     msg_Dbg( p_aout, "JACK audio output initialized (%d channels, buffer "
174              "size=%d, rate=%d)", p_sys->i_channels,
175              p_aout->output.i_nb_samples, p_aout->output.output.i_rate );
176
177     return VLC_SUCCESS;
178 }
179
180
181 /*****************************************************************************
182  * Process: callback for JACK
183  *****************************************************************************/
184 int Process( jack_nframes_t i_frames, void *p_arg )
185 {
186     aout_buffer_t *p_buffer;
187     jack_default_audio_sample_t *p_jack_buffer;
188     unsigned int i, j, i_nb_samples = 0;
189     aout_instance_t *p_aout = (aout_instance_t*) p_arg;
190     unsigned int i_nb_channels = p_aout->output.p_sys->i_channels;
191
192     /* Get the next audio data buffer */
193     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
194
195     if( p_buffer )
196     {
197         i_nb_samples = p_buffer->i_nb_samples;
198     }
199
200     for( i = 0; i < i_nb_channels; i++ )
201     {
202         /* Get an output buffer from JACK */
203         p_jack_buffer = jack_port_get_buffer(
204             p_aout->output.p_sys->p_jack_port[i], i_frames );
205
206         /* Fill the buffer with audio data */
207         for( j = 0; j < i_nb_samples; j++ )
208         {
209             p_jack_buffer[j] = ((float*)p_buffer->p_buffer)[i_nb_channels*j+i];
210         }
211         if (i_nb_samples < i_frames)
212         {
213             memset( p_jack_buffer + i_nb_samples, 0,
214                     sizeof( jack_default_audio_sample_t ) *
215                     (i_frames - i_nb_samples) );
216         }
217     }
218
219     if( p_buffer )
220     {
221         aout_BufferFree( p_buffer );
222     }
223
224     return 0;
225 }
226
227
228 /*****************************************************************************
229  * Play: nothing to do
230  *****************************************************************************/
231 static void Play( aout_instance_t *p_aout )
232 {
233     aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
234 }
235
236 /*****************************************************************************
237  * Close: close the JACK client
238  *****************************************************************************/
239 static void Close( vlc_object_t *p_this )
240 {
241     aout_instance_t *p_aout = (aout_instance_t *)p_this;
242     struct aout_sys_t * p_sys = p_aout->output.p_sys;
243
244     jack_client_close( p_sys->p_jack_client );
245     free( p_sys );
246 }
247