-
Notifications
You must be signed in to change notification settings - Fork 341
/
audiooutputoss.cpp
380 lines (311 loc) · 8.96 KB
/
audiooutputoss.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include <sys/ioctl.h>
#include <cerrno>
#include <cstring>
#include <iostream>
#include "config.h"
#if HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#elif HAVE_SOUNDCARD_H
#include <soundcard.h>
#endif
using namespace std;
#define LOC QString("AOOSS: ")
#include "mythcorecontext.h"
#include "audiooutputoss.h"
#include "mythtimer.h"
#include "mythdate.h"
AudioOutputOSS::AudioOutputOSS(const AudioSettings &settings) :
AudioOutputBase(settings),
audiofd(-1), numbadioctls(0),
mixerfd(-1), control(SOUND_MIXER_VOLUME)
{
// Set everything up
InitSettings(settings);
if (settings.init)
Reconfigure(settings);
}
AudioOutputOSS::~AudioOutputOSS()
{
KillAudio();
}
AudioOutputSettings* AudioOutputOSS::GetOutputSettings(bool /*digital*/)
{
AudioOutputSettings *settings = new AudioOutputSettings();
QByteArray device = main_device.toLatin1();
audiofd = open(device.constData(), O_WRONLY | O_NONBLOCK);
AudioFormat fmt;
int rate, formats = 0;
if (audiofd < 0)
{
VBERRENO(QString("Error opening audio device (%1)").arg(main_device));
delete settings;
return NULL;
}
while ((rate = settings->GetNextRate()))
{
int rate2 = rate;
if(ioctl(audiofd, SNDCTL_DSP_SPEED, &rate2) >= 0
&& rate2 == rate)
settings->AddSupportedRate(rate);
}
if(ioctl(audiofd, SNDCTL_DSP_GETFMTS, &formats) < 0)
VBERRENO("Error retrieving formats");
else
{
int ofmt;
while ((fmt = settings->GetNextFormat()))
{
switch (fmt)
{
case FORMAT_U8: ofmt = AFMT_U8; break;
case FORMAT_S16: ofmt = AFMT_S16_NE; break;
default: continue;
}
if (formats & ofmt)
settings->AddSupportedFormat(fmt);
}
}
#if defined(AFMT_AC3)
// Check if drivers supports AC3
settings->setPassthrough(((formats & AFMT_AC3) != 0) - 1);
#endif
for (int i = 1; i <= 2; i++)
{
int channel = i;
if (ioctl(audiofd, SNDCTL_DSP_CHANNELS, &channel) >= 0 &&
channel == i)
{
settings->AddSupportedChannels(i);
}
}
close(audiofd);
audiofd = -1;
return settings;
}
bool AudioOutputOSS::OpenDevice()
{
numbadioctls = 0;
MythTimer timer;
timer.start();
VBAUDIO(QString("Opening OSS audio device '%1'.").arg(main_device));
while (timer.elapsed() < 2000 && audiofd == -1)
{
QByteArray device = main_device.toLatin1();
audiofd = open(device.constData(), O_WRONLY);
if (audiofd < 0 && errno != EAGAIN && errno != EINTR)
{
if (errno == EBUSY)
{
VBWARN(QString("Something is currently using: %1.")
.arg(main_device));
return false;
}
VBERRENO(QString("Error opening audio device (%1)")
.arg(main_device));
}
if (audiofd < 0)
usleep(50);
}
if (audiofd == -1)
{
VBERRENO(QString("Error opening audio device (%1)").arg(main_device));
return false;
}
fcntl(audiofd, F_SETFL, fcntl(audiofd, F_GETFL) & ~O_NONBLOCK);
bool err = false;
int format;
switch (output_format)
{
case FORMAT_U8: format = AFMT_U8; break;
case FORMAT_S16: format = AFMT_S16_NE; break;
default:
VBERROR(QString("Unknown sample format: %1").arg(output_format));
close(audiofd);
audiofd = -1;
return false;
}
#if defined(AFMT_AC3) && defined(SNDCTL_DSP_GETFMTS)
if (passthru)
{
int format_support = 0;
if (!ioctl(audiofd, SNDCTL_DSP_GETFMTS, &format_support) &&
(format_support & AFMT_AC3))
{
format = AFMT_AC3;
}
}
#endif
if (channels > 2)
{
if (ioctl(audiofd, SNDCTL_DSP_CHANNELS, &channels) < 0 ||
ioctl(audiofd, SNDCTL_DSP_SPEED, &samplerate) < 0 ||
ioctl(audiofd, SNDCTL_DSP_SETFMT, &format) < 0)
err = true;
}
else
{
int stereo = channels - 1;
if (ioctl(audiofd, SNDCTL_DSP_STEREO, &stereo) < 0 ||
ioctl(audiofd, SNDCTL_DSP_SPEED, &samplerate) < 0 ||
ioctl(audiofd, SNDCTL_DSP_SETFMT, &format) < 0)
err = true;
}
if (err)
{
VBERRENO(QString("Unable to set audio device (%1) to %2 kHz, %3 bits, "
"%4 channels")
.arg(main_device).arg(samplerate)
.arg(AudioOutputSettings::FormatToBits(output_format))
.arg(channels));
close(audiofd);
audiofd = -1;
return false;
}
audio_buf_info info;
ioctl(audiofd, SNDCTL_DSP_GETOSPACE, &info);
// align by frame size
fragment_size = info.fragsize - (info.fragsize % output_bytes_per_frame);
soundcard_buffer_size = info.bytes;
int caps;
if (ioctl(audiofd, SNDCTL_DSP_GETCAPS, &caps) == 0)
{
if (!(caps & DSP_CAP_REALTIME))
VBWARN("The audio device cannot report buffer state "
"accurately! audio/video sync will be bad, continuing...");
}
else
VBERRENO("Unable to get audio card capabilities");
// Setup volume control
if (internal_vol)
VolumeInit();
// Device opened successfully
return true;
}
void AudioOutputOSS::CloseDevice()
{
if (audiofd != -1)
close(audiofd);
audiofd = -1;
VolumeCleanup();
}
void AudioOutputOSS::WriteAudio(uchar *aubuf, int size)
{
if (audiofd < 0)
return;
uchar *tmpbuf;
int written = 0, lw = 0;
tmpbuf = aubuf;
while ((written < size) &&
((lw = write(audiofd, tmpbuf, size - written)) > 0))
{
written += lw;
tmpbuf += lw;
}
if (lw < 0)
{
VBERRENO(QString("Error writing to audio device (%1)")
.arg(main_device));
return;
}
}
int AudioOutputOSS::GetBufferedOnSoundcard(void) const
{
int soundcard_buffer=0;
//GREG This is needs to be fixed for sure!
#ifdef SNDCTL_DSP_GETODELAY
ioctl(audiofd, SNDCTL_DSP_GETODELAY, &soundcard_buffer); // bytes
#endif
return soundcard_buffer;
}
void AudioOutputOSS::VolumeInit()
{
mixerfd = -1;
QString device = gCoreContext->GetSetting("MixerDevice", "/dev/mixer");
if (device.toLower() == "software")
return;
QByteArray dev = device.toLatin1();
mixerfd = open(dev.constData(), O_RDONLY);
QString controlLabel = gCoreContext->GetSetting("MixerControl", "PCM");
if (controlLabel == "Master")
control = SOUND_MIXER_VOLUME;
else
control = SOUND_MIXER_PCM;
if (mixerfd < 0)
{
VBERROR(QString("Unable to open mixer: '%1'").arg(device));
return;
}
if (set_initial_vol)
{
int tmpVol;
int volume = gCoreContext->GetNumSetting("MasterMixerVolume", 80);
tmpVol = (volume << 8) + volume;
int ret = ioctl(mixerfd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmpVol);
if (ret < 0)
VBERROR(QString("Error Setting initial Master Volume") + ENO);
volume = gCoreContext->GetNumSetting("PCMMixerVolume", 80);
tmpVol = (volume << 8) + volume;
ret = ioctl(mixerfd, MIXER_WRITE(SOUND_MIXER_PCM), &tmpVol);
if (ret < 0)
VBERROR(QString("Error setting initial PCM Volume") + ENO);
}
}
void AudioOutputOSS::VolumeCleanup()
{
if (mixerfd >= 0)
{
close(mixerfd);
mixerfd = -1;
}
}
int AudioOutputOSS::GetVolumeChannel(int channel) const
{
int volume=0;
int tmpVol=0;
if (mixerfd <= 0)
return 100;
int ret = ioctl(mixerfd, MIXER_READ(control), &tmpVol);
if (ret < 0)
{
VBERROR(QString("Error reading volume for channel %1").arg(channel));
return 0;
}
if (channel == 0)
volume = tmpVol & 0xff; // left
else if (channel == 1)
volume = (tmpVol >> 8) & 0xff; // right
else
VBERROR("Invalid channel. Only stereo volume supported");
return volume;
}
void AudioOutputOSS::SetVolumeChannel(int channel, int volume)
{
if (channel > 1)
{
// Don't support more than two channels!
VBERROR(QString("Error setting channel %1. Only 2 ch volume supported")
.arg(channel));
return;
}
if (volume > 100)
volume = 100;
if (volume < 0)
volume = 0;
if (mixerfd >= 0)
{
int tmpVol = 0;
if (channel == 0)
tmpVol = (GetVolumeChannel(1) << 8) + volume;
else
tmpVol = (volume << 8) + GetVolumeChannel(0);
int ret = ioctl(mixerfd, MIXER_WRITE(control), &tmpVol);
if (ret < 0)
VBERROR(QString("Error setting volume on channel %1").arg(channel));
}
}