1 | /***************************************************************************** |
---|
2 | * roar-plugin.c : RoarAudio module for VLC |
---|
3 | ***************************************************************************** |
---|
4 | * Copyright (C) 2010 the VideoLAN team |
---|
5 | * Copyright (C) 2011 - Hans-Kristian Arntzen |
---|
6 | * Copyright (C) 2011 - Philipp Schafft |
---|
7 | * |
---|
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 | |
---|
28 | #ifdef HAVE_CONFIG_H |
---|
29 | # include "config.h" |
---|
30 | #endif |
---|
31 | |
---|
32 | //#include <stdio.h> |
---|
33 | #include <roaraudio.h> |
---|
34 | |
---|
35 | #include <vlc_common.h> |
---|
36 | #include <vlc_plugin.h> |
---|
37 | #include <vlc_fs.h> |
---|
38 | |
---|
39 | #include <vlc_aout.h> |
---|
40 | |
---|
41 | #define N_(str) (str) |
---|
42 | |
---|
43 | /***************************************************************************** |
---|
44 | * aout_sys_t: RoarAudio audio output method descriptor |
---|
45 | ***************************************************************************** |
---|
46 | * This structure is part of the audio output thread descriptor. |
---|
47 | *****************************************************************************/ |
---|
48 | struct aout_sys_t |
---|
49 | { |
---|
50 | roar_vs_t *vss; |
---|
51 | vlc_thread_t thread; |
---|
52 | }; |
---|
53 | |
---|
54 | /***************************************************************************** |
---|
55 | * Local prototypes |
---|
56 | *****************************************************************************/ |
---|
57 | static int Open ( vlc_object_t * ); |
---|
58 | static void Close ( vlc_object_t * ); |
---|
59 | |
---|
60 | static void Play ( aout_instance_t * ); |
---|
61 | static void* ROARThread ( void * ); |
---|
62 | |
---|
63 | static mtime_t BufferDuration( aout_instance_t * p_aout ); |
---|
64 | |
---|
65 | #define CONNECT_STRING_OPTION_SERVER "roar-connect-server" |
---|
66 | #define CONNECT_STRING_SERVER N_("RoarAudio server:") |
---|
67 | #define CONNECT_STRING_LONGTEXT_SERVER N_("Defines which server to connect to. A blank field will connect to localhost.") |
---|
68 | |
---|
69 | #define CONNECT_STRING_OPTION_NAME "roar-connect-name" |
---|
70 | #define CONNECT_STRING_NAME N_("Client name:") |
---|
71 | #define CONNECT_STRING_LONGTEXT_NAME N_("Defines a name for this client.") |
---|
72 | |
---|
73 | #define CONNECT_STRING_OPTION_ROLE "roar-connect-role" |
---|
74 | #define CONNECT_STRING_ROLE N_("Role:") |
---|
75 | #define CONNECT_STRING_LONGTEXT_ROLE N_("Defines the role for this client.") |
---|
76 | |
---|
77 | /***************************************************************************** |
---|
78 | * Module descriptor |
---|
79 | *****************************************************************************/ |
---|
80 | |
---|
81 | vlc_module_begin () |
---|
82 | set_shortname( "RoarAudio" ) |
---|
83 | set_description( N_("RoarAudio audio output") ) |
---|
84 | |
---|
85 | set_category( CAT_AUDIO ) |
---|
86 | set_subcategory( SUBCAT_AUDIO_AOUT ) |
---|
87 | |
---|
88 | add_string(CONNECT_STRING_OPTION_SERVER, NULL, CONNECT_STRING_SERVER, |
---|
89 | CONNECT_STRING_LONGTEXT_SERVER, true) |
---|
90 | add_string(CONNECT_STRING_OPTION_NAME, NULL, CONNECT_STRING_NAME, |
---|
91 | CONNECT_STRING_LONGTEXT_NAME, true) |
---|
92 | add_string(CONNECT_STRING_OPTION_ROLE, NULL, CONNECT_STRING_ROLE, |
---|
93 | CONNECT_STRING_LONGTEXT_ROLE, true) |
---|
94 | |
---|
95 | set_capability( "audio output", 160 ) |
---|
96 | set_callbacks( Open, Close ) |
---|
97 | vlc_module_end () |
---|
98 | |
---|
99 | |
---|
100 | /***************************************************************************** |
---|
101 | * Open: open the connection |
---|
102 | *****************************************************************************/ |
---|
103 | static int Open( vlc_object_t *p_this ) |
---|
104 | { |
---|
105 | aout_instance_t * p_aout = (aout_instance_t *)p_this; |
---|
106 | struct aout_sys_t * p_sys; |
---|
107 | |
---|
108 | /* Allocate structure */ |
---|
109 | p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) ); |
---|
110 | if( p_sys == NULL ) |
---|
111 | return VLC_ENOMEM; |
---|
112 | |
---|
113 | int channels = aout_FormatNbChannels ( &p_aout->output.output ); |
---|
114 | int rate = p_aout->output.output.i_rate; |
---|
115 | |
---|
116 | char *server = var_InheritString ( p_aout, CONNECT_STRING_OPTION_SERVER ); |
---|
117 | char *name = var_InheritString ( p_aout, CONNECT_STRING_OPTION_NAME ); |
---|
118 | char *role = var_InheritString ( p_aout, CONNECT_STRING_OPTION_ROLE ); |
---|
119 | |
---|
120 | /* Sets up channel mapping */ |
---|
121 | switch(channels) |
---|
122 | { |
---|
123 | case 8: |
---|
124 | p_aout->output.output.i_physical_channels |
---|
125 | = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
---|
126 | | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT |
---|
127 | | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
---|
128 | | AOUT_CHAN_LFE; |
---|
129 | break; |
---|
130 | case 6: |
---|
131 | p_aout->output.output.i_physical_channels |
---|
132 | = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER |
---|
133 | | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT |
---|
134 | | AOUT_CHAN_LFE; |
---|
135 | break; |
---|
136 | |
---|
137 | case 4: |
---|
138 | p_aout->output.output.i_physical_channels |
---|
139 | = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
---|
140 | | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT; |
---|
141 | break; |
---|
142 | |
---|
143 | case 2: |
---|
144 | p_aout->output.output.i_physical_channels |
---|
145 | = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT; |
---|
146 | break; |
---|
147 | |
---|
148 | case 1: |
---|
149 | p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER; |
---|
150 | break; |
---|
151 | |
---|
152 | default: |
---|
153 | msg_Err(p_aout,"Invalid number of channels"); |
---|
154 | free(p_sys); |
---|
155 | return VLC_EGENERIC; |
---|
156 | } |
---|
157 | |
---|
158 | // Hardcode for now. TODO: Fix this! |
---|
159 | p_aout->output.output.i_format = VLC_CODEC_S16N; |
---|
160 | p_aout->output.i_nb_samples = 4096; // Just pick something relatively small. Anything is fine. |
---|
161 | p_aout->output.pf_play = Play; |
---|
162 | |
---|
163 | // Use softvolume. TODO: Use Roar mixer! |
---|
164 | aout_VolumeSoftInit( p_aout ); |
---|
165 | |
---|
166 | if ( (p_sys->vss = roar_vs_new_simple (server, name, rate, channels, ROAR_CODEC_PCM_S_LE, 16, ROAR_DIR_PLAY, NULL)) == NULL ) |
---|
167 | { |
---|
168 | msg_Err ( p_aout, "Cannot connect to server."); |
---|
169 | free( p_sys ); |
---|
170 | |
---|
171 | if ( server ) |
---|
172 | free( server ); |
---|
173 | if ( name ) |
---|
174 | free( name ); |
---|
175 | |
---|
176 | return VLC_EGENERIC; |
---|
177 | } |
---|
178 | |
---|
179 | if ( role ) |
---|
180 | { |
---|
181 | roar_vs_role ( p_sys->vss, roar_str2role( role ), NULL ); |
---|
182 | free ( role ); |
---|
183 | } |
---|
184 | |
---|
185 | if ( server ) |
---|
186 | free( server ); |
---|
187 | if ( name ) |
---|
188 | free( name ); |
---|
189 | |
---|
190 | /* Create RSound thread and wait for its readiness. */ |
---|
191 | if( vlc_clone( &p_sys->thread, ROARThread, p_aout, |
---|
192 | VLC_THREAD_PRIORITY_OUTPUT ) ) |
---|
193 | { |
---|
194 | msg_Err( p_aout, "cannot create Roar thread (%m)" ); |
---|
195 | roar_vs_close(p_sys->vss, ROAR_VS_TRUE, NULL); |
---|
196 | free( p_sys ); |
---|
197 | return VLC_ENOMEM; |
---|
198 | } |
---|
199 | |
---|
200 | return VLC_SUCCESS; |
---|
201 | } |
---|
202 | |
---|
203 | /***************************************************************************** |
---|
204 | * Play: nothing to do |
---|
205 | *****************************************************************************/ |
---|
206 | static void Play( aout_instance_t *p_aout ) |
---|
207 | { |
---|
208 | VLC_UNUSED(p_aout); |
---|
209 | } |
---|
210 | |
---|
211 | /***************************************************************************** |
---|
212 | * Close: close the connection |
---|
213 | *****************************************************************************/ |
---|
214 | static void Close( vlc_object_t * p_this ) |
---|
215 | { |
---|
216 | aout_instance_t *p_aout = (aout_instance_t *)p_this; |
---|
217 | struct aout_sys_t * p_sys = p_aout->output.p_sys; |
---|
218 | |
---|
219 | vlc_object_kill( p_aout ); |
---|
220 | vlc_join(p_sys->thread, NULL); |
---|
221 | p_aout->b_die = false; |
---|
222 | |
---|
223 | roar_vs_close(p_sys->vss, ROAR_VS_TRUE, NULL); |
---|
224 | free( p_sys ); |
---|
225 | } |
---|
226 | |
---|
227 | /***************************************************************************** |
---|
228 | * BufferDuration: buffer status query |
---|
229 | ***************************************************************************** |
---|
230 | * This function returns the duration in microseconds of the current buffer. |
---|
231 | *****************************************************************************/ |
---|
232 | static mtime_t BufferDuration( aout_instance_t * p_aout ) |
---|
233 | { |
---|
234 | struct aout_sys_t * p_sys = p_aout->output.p_sys; |
---|
235 | return (mtime_t)(roar_vs_latency(p_sys->vss, ROAR_VS_BACKEND_DEFAULT, NULL)); |
---|
236 | } |
---|
237 | |
---|
238 | /***************************************************************************** |
---|
239 | * Thread loop |
---|
240 | ****************************************************************************/ |
---|
241 | |
---|
242 | static void* ROARThread( void *p_this ) |
---|
243 | { |
---|
244 | aout_instance_t * p_aout = p_this; |
---|
245 | struct aout_sys_t * p_sys = p_aout->output.p_sys; |
---|
246 | int canc = vlc_savecancel(); |
---|
247 | |
---|
248 | while ( vlc_object_alive( p_aout ) ) |
---|
249 | { |
---|
250 | aout_buffer_t * p_buffer = NULL; |
---|
251 | uint8_t * p_bytes; |
---|
252 | int i_size; |
---|
253 | |
---|
254 | mtime_t buffered = BufferDuration( p_aout ); |
---|
255 | |
---|
256 | p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered, false ); |
---|
257 | |
---|
258 | // Here we will underrun ALOT if we pause :( |
---|
259 | if ( p_buffer == NULL ) |
---|
260 | { |
---|
261 | msleep(BufferDuration(p_aout)/8); |
---|
262 | continue; |
---|
263 | } |
---|
264 | |
---|
265 | p_bytes = p_buffer->p_buffer; |
---|
266 | i_size = p_buffer->i_buffer; |
---|
267 | |
---|
268 | if ( roar_vs_write(p_sys->vss, p_bytes, i_size, NULL) <= 0 ) |
---|
269 | { |
---|
270 | msg_Err(p_aout, "roar_vs_write() failed. Connection was closed?"); |
---|
271 | } |
---|
272 | |
---|
273 | aout_BufferFree(p_buffer); |
---|
274 | } |
---|
275 | |
---|
276 | vlc_restorecancel(canc); |
---|
277 | return NULL; |
---|
278 | } |
---|