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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
/*
* Copyright (C) 2010-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "AESinkOSS.h"
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include "cores/AudioEngine/AESinkFactory.h"
#include "cores/AudioEngine/Utils/AEUtil.h"
#include "utils/log.h"
#include "threads/SingleLock.h"
#include <sstream>
#include <sys/ioctl.h>
#include <sys/fcntl.h>
#if defined(OSS4) || defined(TARGET_FREEBSD)
#include <sys/soundcard.h>
#else
#include <linux/soundcard.h>
#endif
#define OSS_FRAMES 256
static enum AEChannel OSSChannelMap[9] =
{AE_CH_FL, AE_CH_FR, AE_CH_BL, AE_CH_BR, AE_CH_FC, AE_CH_LFE, AE_CH_SL, AE_CH_SR, AE_CH_NULL};
#if defined(SNDCTL_SYSINFO) && defined(SNDCTL_CARDINFO)
static int OSSSampleRateList[] =
{
5512,
8000,
11025,
16000,
22050,
32000,
44100,
48000,
64000,
88200,
96000,
176400,
192000,
384000,
0
};
#endif
CAESinkOSS::CAESinkOSS()
{
m_fd = 0;
}
CAESinkOSS::~CAESinkOSS()
{
Deinitialize();
}
void CAESinkOSS::Register()
{
AE::AESinkRegEntry entry;
entry.sinkName = "OSS";
entry.createFunc = CAESinkOSS::Create;
entry.enumerateFunc = CAESinkOSS::EnumerateDevicesEx;
AE::CAESinkFactory::RegisterSink(entry);
}
IAESink* CAESinkOSS::Create(std::string &device, AEAudioFormat& desiredFormat)
{
IAESink* sink = new CAESinkOSS();
if (sink->Initialize(desiredFormat, device))
return sink;
delete sink;
return nullptr;
}
std::string CAESinkOSS::GetDeviceUse(const AEAudioFormat& format, const std::string &device)
{
#ifdef OSS4
if (AE_IS_RAW(format.m_dataFormat))
{
if (device.find_first_of('/') != 0)
return "/dev/dsp_ac3";
return device;
}
if (device.find_first_of('/') != 0)
return "/dev/dsp_multich";
#else
if (device.find_first_of('/') != 0)
return "/dev/dsp";
#endif
return device;
}
bool CAESinkOSS::Initialize(AEAudioFormat &format, std::string &device)
{
m_initFormat = format;
format.m_channelLayout = GetChannelLayout(format);
device = GetDeviceUse(format, device);
#ifdef __linux__
/* try to open in exclusive mode first (no software mixing) */
m_fd = open(device.c_str(), O_WRONLY | O_EXCL, 0);
if (m_fd == -1)
#endif
m_fd = open(device.c_str(), O_WRONLY, 0);
if (m_fd == -1)
{
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to open the audio device: {}", device);
return false;
}
int format_mask;
if (ioctl(m_fd, SNDCTL_DSP_GETFMTS, &format_mask) == -1)
{
close(m_fd);
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to get supported formats, assuming AFMT_S16_NE");
return false;
}
#ifdef OSS4
bool useCooked = true;
#endif
int oss_fmt = 0;
#ifdef AFMT_FLOAT
if ((format.m_dataFormat == AE_FMT_FLOAT) && (format_mask & AFMT_FLOAT ))
oss_fmt = AFMT_FLOAT;
else
#endif
#ifdef AFMT_S32_NE
if ((format.m_dataFormat == AE_FMT_S32NE) && (format_mask & AFMT_S32_NE))
oss_fmt = AFMT_S32_NE;
else if ((format.m_dataFormat == AE_FMT_S32BE) && (format_mask & AFMT_S32_BE))
oss_fmt = AFMT_S32_BE;
else if ((format.m_dataFormat == AE_FMT_S32LE) && (format_mask & AFMT_S32_LE))
oss_fmt = AFMT_S32_LE;
else
#endif
if ((format.m_dataFormat == AE_FMT_S16NE) && (format_mask & AFMT_S16_NE))
oss_fmt = AFMT_S16_NE;
else if ((format.m_dataFormat == AE_FMT_S16BE) && (format_mask & AFMT_S16_BE))
oss_fmt = AFMT_S16_BE;
else if ((format.m_dataFormat == AE_FMT_S16LE) && (format_mask & AFMT_S16_LE))
oss_fmt = AFMT_S16_LE;
else if ((format.m_dataFormat == AE_FMT_U8 ) && (format_mask & AFMT_U8 ))
oss_fmt = AFMT_U8;
else if (((format.m_dataFormat == AE_FMT_RAW) ) && (format_mask & AFMT_AC3 ))
{
oss_fmt = AFMT_AC3;
format.m_dataFormat = AE_FMT_S16NE;
}
else if (format.m_dataFormat == AE_FMT_RAW)
{
close(m_fd);
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to find a suitable RAW output format");
return false;
}
else
{
CLog::Log(LOGINFO,
"CAESinkOSS::Initialize - Your hardware does not support {}, trying other formats",
CAEUtil::DataFormatToStr(format.m_dataFormat));
/* fallback to the best supported format */
#ifdef AFMT_FLOAT
if (format_mask & AFMT_FLOAT )
{
oss_fmt = AFMT_FLOAT;
format.m_dataFormat = AE_FMT_FLOAT;
}
else
#endif
#ifdef AFMT_S32_NE
if (format_mask & AFMT_S32_NE)
{
oss_fmt = AFMT_S32_NE;
format.m_dataFormat = AE_FMT_S32NE;
}
else if (format_mask & AFMT_S32_BE)
{
oss_fmt = AFMT_S32_BE;
format.m_dataFormat = AE_FMT_S32BE;
}
else if (format_mask & AFMT_S32_LE)
{
oss_fmt = AFMT_S32_LE;
format.m_dataFormat = AE_FMT_S32LE;
}
else
#endif
if (format_mask & AFMT_S16_NE)
{
oss_fmt = AFMT_S16_NE;
format.m_dataFormat = AE_FMT_S16NE;
}
else if (format_mask & AFMT_S16_BE)
{
oss_fmt = AFMT_S16_BE;
format.m_dataFormat = AE_FMT_S16BE;
}
else if (format_mask & AFMT_S16_LE)
{
oss_fmt = AFMT_S16_LE;
format.m_dataFormat = AE_FMT_S16LE;
}
else if (format_mask & AFMT_U8 )
{
oss_fmt = AFMT_U8;
format.m_dataFormat = AE_FMT_U8;
}
else
{
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to find a suitable native output format, will try to use AE_FMT_S16NE anyway");
oss_fmt = AFMT_S16_NE;
format.m_dataFormat = AE_FMT_S16NE;
#ifdef OSS4
/* dont use cooked if we did not find a native format, OSS might be able to convert */
useCooked = false;
#endif
}
}
#ifdef OSS4
if (useCooked)
{
int oss_cooked = 1;
if (ioctl(m_fd, SNDCTL_DSP_COOKEDMODE, &oss_cooked) == -1)
CLog::Log(LOGWARNING, "CAESinkOSS::Initialize - Failed to set cooked mode");
}
#endif
if (ioctl(m_fd, SNDCTL_DSP_SETFMT, &oss_fmt) == -1)
{
close(m_fd);
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to set the data format ({})",
CAEUtil::DataFormatToStr(format.m_dataFormat));
return false;
}
/* find the number we need to open to access the channels we need */
bool found = false;
int oss_ch = 0;
for (int ch = format.m_channelLayout.Count(); ch < 9; ++ch)
{
oss_ch = ch;
if (ioctl(m_fd, SNDCTL_DSP_CHANNELS, &oss_ch) != -1 && oss_ch >= (int)format.m_channelLayout.Count())
{
found = true;
break;
}
}
if (!found)
CLog::Log(LOGWARNING, "CAESinkOSS::Initialize - Failed to access the number of channels required, falling back");
#if defined(TARGET_FREEBSD)
/* fix hdmi 8 channels order */
if ((oss_fmt != AFMT_AC3) && 8 == oss_ch)
{
unsigned long long order = 0x0000000087346521ULL;
if (ioctl(m_fd, SNDCTL_DSP_SET_CHNORDER, &order) == -1)
CLog::Log(LOGWARNING, "CAESinkOSS::Initialize - Failed to set the channel order");
}
#elif defined(OSS4)
unsigned long long order = 0;
for (unsigned int i = 0; i < format.m_channelLayout.Count(); ++i)
switch (format.m_channelLayout[i])
{
case AE_CH_FL : order = (order << 4) | CHID_L ; break;
case AE_CH_FR : order = (order << 4) | CHID_R ; break;
case AE_CH_FC : order = (order << 4) | CHID_C ; break;
case AE_CH_LFE: order = (order << 4) | CHID_LFE; break;
case AE_CH_SL : order = (order << 4) | CHID_LS ; break;
case AE_CH_SR : order = (order << 4) | CHID_RS ; break;
case AE_CH_BL : order = (order << 4) | CHID_LR ; break;
case AE_CH_BR : order = (order << 4) | CHID_RR ; break;
default:
continue;
}
if (ioctl(m_fd, SNDCTL_DSP_SET_CHNORDER, &order) == -1)
{
if (ioctl(m_fd, SNDCTL_DSP_GET_CHNORDER, &order) == -1)
{
CLog::Log(LOGWARNING, "CAESinkOSS::Initialize - Failed to get the channel order, assuming CHNORDER_NORMAL");
}
}
#endif
int tmp = (CAEUtil::DataFormatToBits(format.m_dataFormat) >> 3) * format.m_channelLayout.Count() * OSS_FRAMES;
int pos = 0;
while ((tmp & 0x1) == 0x0)
{
tmp = tmp >> 1;
++pos;
}
int oss_frag = (4 << 16) | pos;
if (ioctl(m_fd, SNDCTL_DSP_SETFRAGMENT, &oss_frag) == -1)
CLog::Log(LOGWARNING, "CAESinkOSS::Initialize - Failed to set the fragment size");
int oss_sr = format.m_sampleRate;
if (ioctl(m_fd, SNDCTL_DSP_SPEED, &oss_sr) == -1)
{
close(m_fd);
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to set the sample rate");
return false;
}
audio_buf_info bi;
if (ioctl(m_fd, SNDCTL_DSP_GETOSPACE, &bi) == -1)
{
close(m_fd);
CLog::Log(LOGERROR, "CAESinkOSS::Initialize - Failed to get the output buffer size");
return false;
}
format.m_sampleRate = oss_sr;
format.m_frameSize = (CAEUtil::DataFormatToBits(format.m_dataFormat) >> 3) * format.m_channelLayout.Count();
format.m_frames = bi.fragsize / format.m_frameSize;
m_device = device;
m_format = format;
return true;
}
void CAESinkOSS::Deinitialize()
{
Stop();
if (m_fd != -1)
close(m_fd);
}
inline CAEChannelInfo CAESinkOSS::GetChannelLayout(const AEAudioFormat& format)
{
unsigned int count = 0;
if (format.m_dataFormat == AE_FMT_RAW)
{
switch (format.m_streamInfo.m_type)
{
case CAEStreamInfo::STREAM_TYPE_DTSHD_MA:
case CAEStreamInfo::STREAM_TYPE_TRUEHD:
count = 8;
break;
case CAEStreamInfo::STREAM_TYPE_DTSHD_CORE:
case CAEStreamInfo::STREAM_TYPE_DTS_512:
case CAEStreamInfo::STREAM_TYPE_DTS_1024:
case CAEStreamInfo::STREAM_TYPE_DTS_2048:
case CAEStreamInfo::STREAM_TYPE_AC3:
case CAEStreamInfo::STREAM_TYPE_EAC3:
case CAEStreamInfo::STREAM_TYPE_DTSHD:
count = 2;
break;
default:
count = 0;
break;
}
}
else
{
for (unsigned int c = 0; c < 8; ++c)
for (unsigned int i = 0; i < format.m_channelLayout.Count(); ++i)
if (format.m_channelLayout[i] == OSSChannelMap[c])
{
count = c + 1;
break;
}
}
CAEChannelInfo info;
for (unsigned int i = 0; i < count; ++i)
info += OSSChannelMap[i];
return info;
}
void CAESinkOSS::Stop()
{
#ifdef SNDCTL_DSP_RESET
if (m_fd != -1)
ioctl(m_fd, SNDCTL_DSP_RESET, NULL);
#endif
}
void CAESinkOSS::GetDelay(AEDelayStatus& status)
{
if (m_fd == -1)
{
status.SetDelay(0);
return;
}
int delay;
if (ioctl(m_fd, SNDCTL_DSP_GETODELAY, &delay) == -1)
{
status.SetDelay(0);
return;
}
status.SetDelay((double)delay / (m_format.m_frameSize * m_format.m_sampleRate));
}
unsigned int CAESinkOSS::AddPackets(uint8_t **data, unsigned int frames, unsigned int offset)
{
int size = frames * m_format.m_frameSize;
if (m_fd == -1)
{
CLog::Log(LOGERROR, "CAESinkOSS::AddPackets - Failed to write");
return INT_MAX;
}
void *buffer = data[0]+offset*m_format.m_frameSize;
int wrote = write(m_fd, buffer, size);
if (wrote < 0)
{
CLog::Log(LOGERROR, "CAESinkOSS::AddPackets - Failed to write");
return INT_MAX;
}
return wrote / m_format.m_frameSize;
}
void CAESinkOSS::Drain()
{
if (m_fd == -1)
return;
if(ioctl(m_fd, SNDCTL_DSP_SYNC, NULL) == -1)
{
CLog::Log(LOGERROR, "CAESinkOSS::Drain - Draining the Sink failed");
}
}
void CAESinkOSS::EnumerateDevicesEx(AEDeviceInfoList &list, bool force)
{
int mixerfd;
const char * mixerdev = "/dev/mixer";
if ((mixerfd = open(mixerdev, O_RDWR, 0)) == -1)
{
CLog::Log(LOGINFO, "CAESinkOSS::EnumerateDevicesEx - No OSS mixer device present: {}",
mixerdev);
return;
}
#if defined(SNDCTL_SYSINFO) && defined(SNDCTL_CARDINFO)
oss_sysinfo sysinfo;
if (ioctl(mixerfd, SNDCTL_SYSINFO, &sysinfo) == -1)
{
// hardware not supported
// OSSv4 required ?
close(mixerfd);
return;
}
for (int i = 0; i < sysinfo.numcards; ++i)
{
std::stringstream devicepath;
std::stringstream devicename;
CAEDeviceInfo info;
oss_card_info cardinfo;
devicepath << "/dev/dsp" << i;
info.m_deviceName = devicepath.str();
cardinfo.card = i;
if (ioctl(mixerfd, SNDCTL_CARDINFO, &cardinfo) == -1)
break;
devicename << cardinfo.shortname << " " << cardinfo.longname;
info.m_displayName = devicename.str();
info.m_dataFormats.push_back(AE_FMT_S16NE);
info.m_dataFormats.push_back(AE_FMT_S32NE);
if (info.m_displayName.find("HDMI") != std::string::npos
|| info.m_displayName.find("DisplayPort") != std::string::npos)
{
info.m_deviceType = AE_DEVTYPE_HDMI;
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_AC3);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_CORE);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_1024);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_2048);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_512);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_EAC3);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_MA);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_TRUEHD);
info.m_dataFormats.push_back(AE_FMT_RAW);
}
else if (info.m_displayName.find("Digital") != std::string::npos)
{
info.m_deviceType = AE_DEVTYPE_IEC958;
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_AC3);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_CORE);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_1024);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_2048);
info.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_512);
info.m_dataFormats.push_back(AE_FMT_RAW);
}
else
{
info.m_deviceType = AE_DEVTYPE_PCM;
}
oss_audioinfo ainfo = {};
ainfo.dev = i;
if (ioctl(mixerfd, SNDCTL_AUDIOINFO, &ainfo) != -1) {
#if 0
if (ainfo.oformats & AFMT_S32_LE)
info.m_dataFormats.push_back(AE_FMT_S32LE);
if (ainfo.oformats & AFMT_S16_LE)
info.m_dataFormats.push_back(AE_FMT_S16LE);
#endif
for (int j = 0;
j < ainfo.max_channels && AE_CH_NULL != OSSChannelMap[j];
++j)
info.m_channels += OSSChannelMap[j];
for (int *rate = OSSSampleRateList; *rate != 0; ++rate)
if (*rate >= ainfo.min_rate && *rate <= ainfo.max_rate)
info.m_sampleRates.push_back(*rate);
}
info.m_wantsIECPassthrough = true;
list.push_back(info);
}
#endif
close(mixerfd);
}
|