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 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
|
/*
* 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 "AESinkWASAPI.h"
#include "cores/AudioEngine/AESinkFactory.h"
#include "cores/AudioEngine/Utils/AEDeviceInfo.h"
#include "cores/AudioEngine/Utils/AEUtil.h"
#include "utils/StringUtils.h"
#include "utils/SystemInfo.h"
#include "utils/TimeUtils.h"
#include "utils/XTimeUtils.h"
#include "utils/log.h"
#include <algorithm>
#include <stdint.h>
#include <Audioclient.h>
#include <Mmreg.h>
#ifdef TARGET_WINDOWS_DESKTOP
# pragma comment(lib, "Avrt.lib")
#endif // TARGET_WINDOWS_DESKTOP
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
const IID IID_IAudioClock = __uuidof(IAudioClock);
DEFINE_PROPERTYKEY(PKEY_Device_FriendlyName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 14);
DEFINE_PROPERTYKEY(PKEY_Device_EnumeratorName, 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0, 24);
extern const char *WASAPIErrToStr(HRESULT err);
#define EXIT_ON_FAILURE(hr, reason) \
if (FAILED(hr)) \
{ \
CLog::LogF(LOGERROR, reason " - HRESULT = {} ErrorMessage = {}", hr, WASAPIErrToStr(hr)); \
goto failed; \
}
template<class T>
inline void SafeRelease(T **ppT)
{
if (*ppT)
{
(*ppT)->Release();
*ppT = nullptr;
}
}
using namespace Microsoft::WRL;
CAESinkWASAPI::CAESinkWASAPI()
{
m_channelLayout.Reset();
}
CAESinkWASAPI::~CAESinkWASAPI()
{
}
void CAESinkWASAPI::Register()
{
AE::AESinkRegEntry reg;
reg.sinkName = "WASAPI";
reg.createFunc = CAESinkWASAPI::Create;
reg.enumerateFunc = CAESinkWASAPI::EnumerateDevicesEx;
AE::CAESinkFactory::RegisterSink(reg);
}
IAESink* CAESinkWASAPI::Create(std::string &device, AEAudioFormat &desiredFormat)
{
IAESink *sink = new CAESinkWASAPI();
if (sink->Initialize(desiredFormat, device))
return sink;
delete sink;
return nullptr;
}
bool CAESinkWASAPI::Initialize(AEAudioFormat &format, std::string &device)
{
if (m_initialized)
return false;
m_device = device;
bool bdefault = false;
HRESULT hr = S_FALSE;
/* Save requested format */
/* Clear returned format */
sinkReqFormat = format.m_dataFormat;
sinkRetFormat = AE_FMT_INVALID;
if(StringUtils::EndsWithNoCase(device, std::string("default")))
bdefault = true;
if(!bdefault)
{
hr = CAESinkFactoryWin::ActivateWASAPIDevice(device, &m_pDevice);
EXIT_ON_FAILURE(hr, "Retrieval of WASAPI endpoint failed.")
}
if (!m_pDevice)
{
if(!bdefault)
{
CLog::LogF(LOGINFO,
"Could not locate the device named \"{}\" in the list of WASAPI endpoint devices. "
" Trying the default device...",
device);
}
std::string defaultId = CAESinkFactoryWin::GetDefaultDeviceId();
if (defaultId.empty())
{
CLog::LogF(LOGINFO, "Could not locate the default device id in the list of WASAPI endpoint devices.");
goto failed;
}
hr = CAESinkFactoryWin::ActivateWASAPIDevice(defaultId, &m_pDevice);
EXIT_ON_FAILURE(hr, "Could not retrieve the default WASAPI audio endpoint.")
device = defaultId;
}
hr = m_pDevice->Activate(m_pAudioClient.ReleaseAndGetAddressOf());
EXIT_ON_FAILURE(hr, "Activating the WASAPI endpoint device failed.")
if (!InitializeExclusive(format))
{
CLog::LogF(LOGINFO, "Could not Initialize Exclusive with that format");
goto failed;
}
/* get the buffer size and calculate the frames for AE */
m_pAudioClient->GetBufferSize(&m_uiBufferLen);
format.m_frames = m_uiBufferLen;
m_format = format;
sinkRetFormat = format.m_dataFormat;
hr = m_pAudioClient->GetService(IID_IAudioRenderClient, reinterpret_cast<void**>(m_pRenderClient.ReleaseAndGetAddressOf()));
EXIT_ON_FAILURE(hr, "Could not initialize the WASAPI render client interface.")
hr = m_pAudioClient->GetService(IID_IAudioClock, reinterpret_cast<void**>(m_pAudioClock.ReleaseAndGetAddressOf()));
EXIT_ON_FAILURE(hr, "Could not initialize the WASAPI audio clock interface.")
hr = m_pAudioClock->GetFrequency(&m_clockFreq);
EXIT_ON_FAILURE(hr, "Retrieval of IAudioClock::GetFrequency failed.")
m_needDataEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
hr = m_pAudioClient->SetEventHandle(m_needDataEvent);
EXIT_ON_FAILURE(hr, "Could not set the WASAPI event handler.");
m_initialized = true;
m_isDirty = false;
// allow feeding less samples than buffer size
// if the device is opened exclusive and event driven, provided samples must match buffersize
// ActiveAE tries to align provided samples with buffer size but cannot guarantee (e.g. transcoding)
// this can be avoided by dropping the event mode which has not much benefit; SoftAE polls anyway
m_buffer.resize(format.m_frames * format.m_frameSize);
m_bufferPtr = 0;
return true;
failed:
CLog::LogF(LOGERROR, "WASAPI initialization failed.");
SafeRelease(&m_pDevice);
if(m_needDataEvent)
{
CloseHandle(m_needDataEvent);
m_needDataEvent = 0;
}
return false;
}
void CAESinkWASAPI::Deinitialize()
{
if (!m_initialized && !m_isDirty)
return;
if (m_running)
{
try
{
m_pAudioClient->Stop(); //stop the audio output
m_pAudioClient->Reset(); //flush buffer and reset audio clock stream position
m_sinkFrames = 0;
}
catch (...)
{
CLog::LogF(LOGDEBUG, "Invalidated AudioClient - Releasing");
}
}
m_running = false;
CloseHandle(m_needDataEvent);
m_pRenderClient = nullptr;
m_pAudioClient = nullptr;
m_pAudioClock = nullptr;
SafeRelease(&m_pDevice);
m_initialized = false;
m_bufferPtr = 0;
}
/**
* @brief rescale uint64_t without overflowing on large values
*/
static uint64_t rescale_u64(uint64_t val, uint64_t num, uint64_t den)
{
return ((val / den) * num) + (((val % den) * num) / den);
}
void CAESinkWASAPI::GetDelay(AEDelayStatus& status)
{
HRESULT hr;
uint64_t pos, tick;
int retries = 0;
if (!m_initialized)
goto failed;
do {
hr = m_pAudioClock->GetPosition(&pos, &tick);
} while (hr != S_OK && ++retries < 100);
EXIT_ON_FAILURE(hr, "Retrieval of IAudioClock::GetPosition failed.")
status.delay = (double)(m_sinkFrames + m_bufferPtr) / m_format.m_sampleRate - (double)pos / m_clockFreq;
status.tick = rescale_u64(tick, CurrentHostFrequency(), 10000000); /* convert from 100ns back to qpc ticks */
return;
failed:
status.SetDelay(0);
}
double CAESinkWASAPI::GetCacheTotal()
{
if (!m_initialized)
return 0.0;
return m_sinkLatency;
}
unsigned int CAESinkWASAPI::AddPackets(uint8_t **data, unsigned int frames, unsigned int offset)
{
if (!m_initialized)
return 0;
HRESULT hr;
BYTE *buf;
DWORD flags = 0;
#ifndef _DEBUG
LARGE_INTEGER timerStart;
LARGE_INTEGER timerStop;
LARGE_INTEGER timerFreq;
#endif
unsigned int NumFramesRequested = m_format.m_frames;
unsigned int FramesToCopy = std::min(m_format.m_frames - m_bufferPtr, frames);
uint8_t *buffer = data[0]+offset*m_format.m_frameSize;
if (m_bufferPtr != 0 || frames != m_format.m_frames)
{
memcpy(m_buffer.data() + m_bufferPtr * m_format.m_frameSize, buffer,
FramesToCopy * m_format.m_frameSize);
m_bufferPtr += FramesToCopy;
if (m_bufferPtr != m_format.m_frames)
return frames;
}
if (!m_running) //first time called, pre-fill buffer then start audio client
{
hr = m_pAudioClient->Reset();
if (FAILED(hr))
{
CLog::LogF(LOGERROR, " AudioClient reset failed due to {}", WASAPIErrToStr(hr));
return 0;
}
hr = m_pRenderClient->GetBuffer(NumFramesRequested, &buf);
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::LogF(LOGERROR, "GetBuffer failed due to {}", WASAPIErrToStr(hr));
#endif
m_isDirty = true; //flag new device or re-init needed
return INT_MAX;
}
memset(buf, 0, NumFramesRequested * m_format.m_frameSize); //fill buffer with silence
hr = m_pRenderClient->ReleaseBuffer(NumFramesRequested, flags); //pass back to audio driver
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::LogF(LOGDEBUG, "ReleaseBuffer failed due to {}.", WASAPIErrToStr(hr));
#endif
m_isDirty = true; //flag new device or re-init needed
return INT_MAX;
}
m_sinkFrames += NumFramesRequested;
hr = m_pAudioClient->Start(); //start the audio driver running
if (FAILED(hr))
CLog::LogF(LOGERROR, "AudioClient Start Failed");
m_running = true; //signal that we're processing frames
return 0U;
}
#ifndef _DEBUG
/* Get clock time for latency checks */
QueryPerformanceFrequency(&timerFreq);
QueryPerformanceCounter(&timerStart);
#endif
/* Wait for Audio Driver to tell us it's got a buffer available */
DWORD eventAudioCallback;
eventAudioCallback = WaitForSingleObject(m_needDataEvent, 1100);
if(eventAudioCallback != WAIT_OBJECT_0 || !&buf)
{
CLog::LogF(LOGERROR, "Endpoint Buffer timed out");
return INT_MAX;
}
if (!m_running)
return 0;
#ifndef _DEBUG
QueryPerformanceCounter(&timerStop);
LONGLONG timerDiff = timerStop.QuadPart - timerStart.QuadPart;
double timerElapsed = (double) timerDiff * 1000.0 / (double) timerFreq.QuadPart;
m_avgTimeWaiting += (timerElapsed - m_avgTimeWaiting) * 0.5;
if (m_avgTimeWaiting < 3.0)
{
CLog::LogF(LOGDEBUG, "Possible AQ Loss: Avg. Time Waiting for Audio Driver callback : {}msec",
(int)m_avgTimeWaiting);
}
#endif
hr = m_pRenderClient->GetBuffer(NumFramesRequested, &buf);
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::LogF(LOGERROR, "GetBuffer failed due to {}", WASAPIErrToStr(hr));
#endif
return INT_MAX;
}
// fill buffer
memcpy(buf, m_bufferPtr == 0 ? buffer : m_buffer.data(),
NumFramesRequested * m_format.m_frameSize);
m_bufferPtr = 0;
hr = m_pRenderClient->ReleaseBuffer(NumFramesRequested, flags); //pass back to audio driver
if (FAILED(hr))
{
#ifdef _DEBUG
CLog::LogF(LOGDEBUG, "ReleaseBuffer failed due to {}.", WASAPIErrToStr(hr));
#endif
return INT_MAX;
}
m_sinkFrames += NumFramesRequested;
if (FramesToCopy != frames)
{
m_bufferPtr = frames-FramesToCopy;
memcpy(m_buffer.data(), buffer + FramesToCopy * m_format.m_frameSize,
m_bufferPtr * m_format.m_frameSize);
}
return frames;
}
void CAESinkWASAPI::EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bool force)
{
CAEDeviceInfo deviceInfo;
CAEChannelInfo deviceChannels;
bool add192 = false;
bool add48 = false;
WAVEFORMATEXTENSIBLE wfxex = {};
HRESULT hr;
const bool onlyPT = (CSysInfo::GetWindowsDeviceFamily() == CSysInfo::WindowsDeviceFamily::Xbox);
for(RendererDetail& details : CAESinkFactoryWin::GetRendererDetails())
{
deviceInfo.m_channels.Reset();
deviceInfo.m_dataFormats.clear();
deviceInfo.m_sampleRates.clear();
deviceChannels.Reset();
for (unsigned int c = 0; c < WASAPI_SPEAKER_COUNT; c++)
{
if (details.uiChannelMask & WASAPIChannelOrder[c])
deviceChannels += AEChannelNames[c];
}
IAEWASAPIDevice* pDevice;
hr = CAESinkFactoryWin::ActivateWASAPIDevice(details.strDeviceId, &pDevice);
if (FAILED(hr))
{
CLog::LogF(LOGERROR, "Retrieval of WASAPI endpoint failed.");
goto failed;
}
ComPtr<IAudioClient> pClient = nullptr;
hr = pDevice->Activate(pClient.GetAddressOf());
if (SUCCEEDED(hr))
{
/* Test format DTS-HD-HR */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.Format.nSamplesPerSec = 192000;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
{
CLog::LogF(LOGINFO,
"Exclusive mode is not allowed on device \"{}\", check device settings.",
details.strDescription);
SafeRelease(&pDevice);
continue;
}
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
CAEUtil::StreamTypeToStr(CAEStreamInfo::STREAM_TYPE_DTSHD),
details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD);
add192 = true;
}
/* Test format DTS-HD */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.Format.nSamplesPerSec = 192000;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 8;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (hr == AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED)
{
CLog::LogF(LOGINFO,
"Exclusive mode is not allowed on device \"{}\", check device settings.",
details.strDescription);
SafeRelease(&pDevice);
continue;
}
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
CAEUtil::StreamTypeToStr(CAEStreamInfo::STREAM_TYPE_DTSHD_MA),
details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_MA);
add192 = true;
}
/* Test format Dolby TrueHD */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_MLP;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
CAEUtil::StreamTypeToStr(CAEStreamInfo::STREAM_TYPE_TRUEHD),
details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_TRUEHD);
add192 = true;
}
/* Test format Dolby EAC3 */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
CAEUtil::StreamTypeToStr(CAEStreamInfo::STREAM_TYPE_EAC3),
details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_EAC3);
add192 = true;
}
/* Test format DTS */
wfxex.Format.nSamplesPerSec = 48000;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
"STREAM_TYPE_DTS", details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTSHD_CORE);
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_2048);
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_1024);
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_DTS_512);
add48 = true;
}
/* Test format Dolby AC3 */
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr) || details.eDeviceType == AE_DEVTYPE_HDMI)
{
if(FAILED(hr))
{
CLog::LogF(LOGINFO, "stream type \"{}\" on device \"{}\" seems to be not supported.",
CAEUtil::StreamTypeToStr(CAEStreamInfo::STREAM_TYPE_AC3),
details.strDescription);
}
deviceInfo.m_streamTypes.push_back(CAEStreamInfo::STREAM_TYPE_AC3);
add48 = true;
}
/* Test format for PCM format iteration */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.dwChannelMask = KSAUDIO_SPEAKER_STEREO;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
for (int p = AE_FMT_FLOAT; p > AE_FMT_INVALID; p--)
{
if (p < AE_FMT_FLOAT)
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.wBitsPerSample = CAEUtil::DataFormatToBits((AEDataFormat) p);
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
if (p == AE_FMT_S24NE4MSB)
{
wfxex.Samples.wValidBitsPerSample = 24;
}
else if (p <= AE_FMT_S24NE4 && p >= AE_FMT_S24BE4)
{
// not supported
continue;
}
else
{
wfxex.Samples.wValidBitsPerSample = wfxex.Format.wBitsPerSample;
}
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_dataFormats.push_back((AEDataFormat) p);
}
/* Test format for sample rate iteration */
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE)-sizeof(WAVEFORMATEX);
wfxex.dwChannelMask = KSAUDIO_SPEAKER_STEREO;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = 2;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
for (int j = 0; j < WASAPISampleRateCount; j++)
{
wfxex.Format.nSamplesPerSec = WASAPISampleRates[j];
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
hr = pClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
deviceInfo.m_sampleRates.push_back(WASAPISampleRates[j]);
else if (wfxex.Format.nSamplesPerSec == 192000 && add192)
{
deviceInfo.m_sampleRates.push_back(WASAPISampleRates[j]);
CLog::LogF(LOGINFO, "sample rate 192khz on device \"{}\" seems to be not supported.",
details.strDescription);
}
else if (wfxex.Format.nSamplesPerSec == 48000 && add48)
{
deviceInfo.m_sampleRates.push_back(WASAPISampleRates[j]);
CLog::LogF(LOGINFO, "sample rate 48khz on device \"{}\" seems to be not supported.",
details.strDescription);
}
}
pClient = nullptr;
}
else
{
CLog::LogF(LOGDEBUG, "Failed to activate device for passthrough capability testing.");
}
deviceInfo.m_deviceName = details.strDeviceId;
deviceInfo.m_displayName = details.strWinDevType.append(details.strDescription);
deviceInfo.m_displayNameExtra = std::string("WASAPI: ").append(details.strDescription);
deviceInfo.m_deviceType = details.eDeviceType;
deviceInfo.m_channels = deviceChannels;
/* Store the device info */
deviceInfo.m_wantsIECPassthrough = true;
deviceInfo.m_onlyPassthrough = onlyPT;
if (!deviceInfo.m_streamTypes.empty())
deviceInfo.m_dataFormats.push_back(AE_FMT_RAW);
deviceInfoList.push_back(deviceInfo);
if (details.bDefault)
{
deviceInfo.m_deviceName = std::string("default");
deviceInfo.m_displayName = std::string("default");
deviceInfo.m_displayNameExtra = std::string("");
deviceInfo.m_wantsIECPassthrough = true;
deviceInfo.m_onlyPassthrough = onlyPT;
deviceInfoList.push_back(deviceInfo);
}
SafeRelease(&pDevice);
}
return;
failed:
if (FAILED(hr))
CLog::LogF(LOGERROR, "Failed to enumerate WASAPI endpoint devices ({}).", WASAPIErrToStr(hr));
}
//Private utility functions////////////////////////////////////////////////////
void CAESinkWASAPI::BuildWaveFormatExtensibleIEC61397(AEAudioFormat &format, WAVEFORMATEXTENSIBLE_IEC61937 &wfxex)
{
/* Fill the common structure */
CAESinkFactoryWin::BuildWaveFormatExtensible(format, wfxex.FormatExt);
/* Code below kept for future use - preferred for later Windows versions */
/* but can cause problems on older Windows versions and drivers */
/*
wfxex.FormatExt.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE_IEC61937)-sizeof(WAVEFORMATEX);
wfxex.dwEncodedChannelCount = format.m_channelLayout.Count();
wfxex.dwEncodedSamplesPerSec = bool(format.m_dataFormat == AE_FMT_TRUEHD ||
format.m_dataFormat == AE_FMT_DTSHD ||
format.m_dataFormat == AE_FMT_EAC3) ? 96000L : 48000L;
wfxex.dwAverageBytesPerSec = 0; //Ignored */
}
bool CAESinkWASAPI::InitializeExclusive(AEAudioFormat &format)
{
WAVEFORMATEXTENSIBLE_IEC61937 wfxex_iec61937;
WAVEFORMATEXTENSIBLE &wfxex = wfxex_iec61937.FormatExt;
if (format.m_dataFormat <= AE_FMT_FLOAT)
CAESinkFactoryWin::BuildWaveFormatExtensible(format, wfxex);
else if (format.m_dataFormat == AE_FMT_RAW)
BuildWaveFormatExtensibleIEC61397(format, wfxex_iec61937);
else
{
// planar formats are currently not supported by this sink
format.m_dataFormat = AE_FMT_FLOAT;
CAESinkFactoryWin::BuildWaveFormatExtensible(format, wfxex);
}
// Prevents NULL speaker mask. To do: debug exact cause.
// When this happens requested AE format is AE_FMT_FLOAT + channel layout
// RAW, RAW, RAW... (6 channels). Only happens at end of playback PT
// stream, force to defaults does not affect functionality or user
// experience. Only avoids crash.
if (!wfxex.dwChannelMask && format.m_dataFormat <= AE_FMT_FLOAT)
{
CLog::LogF(LOGWARNING, "NULL Channel Mask detected. Default values are enforced.");
format.m_sampleRate = 0; // force defaults in following code
}
/* Test for incomplete format and provide defaults */
if (format.m_sampleRate == 0 ||
format.m_channelLayout == CAEChannelInfo(nullptr) ||
format.m_dataFormat <= AE_FMT_INVALID ||
format.m_dataFormat >= AE_FMT_MAX ||
format.m_channelLayout.Count() == 0)
{
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.Format.nChannels = 2;
wfxex.Format.nSamplesPerSec = 44100L;
wfxex.Format.wBitsPerSample = 16;
wfxex.Format.nBlockAlign = 4;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nBlockAlign * wfxex.Format.nSamplesPerSec;
wfxex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
}
HRESULT hr = m_pAudioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
int closestMatch = 0;
unsigned int requestedChannels = 0;
unsigned int noOfCh = 0;
uint64_t desired_map = 0;
if (SUCCEEDED(hr))
{
CLog::LogF(LOGINFO, "Format is Supported - will attempt to Initialize");
goto initialize;
}
else if (hr != AUDCLNT_E_UNSUPPORTED_FORMAT) //It failed for a reason unrelated to an unsupported format.
{
CLog::LogF(LOGERROR, "IsFormatSupported failed ({})", WASAPIErrToStr(hr));
return false;
}
else if (format.m_dataFormat == AE_FMT_RAW) //No sense in trying other formats for passthrough.
return false;
CLog::LogFC(LOGDEBUG, LOGAUDIO,
"IsFormatSupported failed ({}) - trying to find a compatible format",
WASAPIErrToStr(hr));
requestedChannels = wfxex.Format.nChannels;
desired_map = CAESinkFactoryWin::SpeakerMaskFromAEChannels(format.m_channelLayout);
/* The requested format is not supported by the device. Find something that works */
CLog::Log(LOGDEBUG,
"AESinkWASAPI: Input channels are [{}] - Trying to find a matching output layout",
std::string(format.m_channelLayout));
for (int layout = -1; layout <= (int)ARRAYSIZE(layoutsList); layout++)
{
// if requested layout is not supported, try standard layouts which contain
// at least the same channels as the input source
// as the last resort try stereo
if (layout == ARRAYSIZE(layoutsList))
{
wfxex.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
wfxex.Format.nChannels = 2;
}
else if (layout >= 0)
{
wfxex.dwChannelMask = CAESinkFactoryWin::ChLayoutToChMask(layoutsList[layout], &noOfCh);
wfxex.Format.nChannels = noOfCh;
int res = desired_map & wfxex.dwChannelMask;
if (res != desired_map)
continue; // output channel layout doesn't match input channels
}
CAEChannelInfo foundChannels;
CAESinkFactoryWin::AEChannelsFromSpeakerMask(foundChannels, wfxex.dwChannelMask);
CLog::Log(LOGDEBUG, "AESinkWASAPI: Trying matching channel layout [{}]",
std::string(foundChannels));
for (int j = 0; j < sizeof(testFormats)/sizeof(sampleFormat); j++)
{
closestMatch = -1;
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.SubFormat = testFormats[j].subFormat;
wfxex.Format.wBitsPerSample = testFormats[j].bitsPerSample;
wfxex.Samples.wValidBitsPerSample = testFormats[j].validBitsPerSample;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
for (int i = 0 ; i < WASAPISampleRateCount; i++)
{
wfxex.Format.nSamplesPerSec = WASAPISampleRates[i];
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
/* Trace format match iteration loop via log */
#if 0
CLog::Log(LOGDEBUG, "WASAPI: Trying Format: {}, {}, {}, {}", CAEUtil::DataFormatToStr(testFormats[j].subFormatType),
wfxex.Format.nSamplesPerSec,
wfxex.Format.wBitsPerSample,
wfxex.Samples.wValidBitsPerSample);
#endif
hr = m_pAudioClient->IsFormatSupported(AUDCLNT_SHAREMODE_EXCLUSIVE, &wfxex.Format, NULL);
if (SUCCEEDED(hr))
{
/* If the current sample rate matches the source then stop looking and use it */
if ((WASAPISampleRates[i] == format.m_sampleRate) && (testFormats[j].subFormatType <= format.m_dataFormat))
goto initialize;
/* If this rate is closer to the source then the previous one, save it */
else if (closestMatch < 0 || abs((int)WASAPISampleRates[i] - (int)format.m_sampleRate) < abs((int)WASAPISampleRates[closestMatch] - (int)format.m_sampleRate))
closestMatch = i;
}
else if (hr != AUDCLNT_E_UNSUPPORTED_FORMAT)
CLog::LogF(LOGERROR, "IsFormatSupported failed ({})", WASAPIErrToStr(hr));
}
if (closestMatch >= 0)
{
wfxex.Format.nSamplesPerSec = WASAPISampleRates[closestMatch];
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
goto initialize;
}
}
CLog::Log(LOGDEBUG, "AESinkWASAPI: Format [{}] not supported by driver",
std::string(foundChannels));
}
CLog::LogF(LOGERROR, "Unable to locate a supported output format for the device. "
"Check the speaker settings in the control panel.");
/* We couldn't find anything supported. This should never happen */
/* unless the user set the wrong speaker setting in the control panel */
return false;
initialize:
CAESinkFactoryWin::AEChannelsFromSpeakerMask(m_channelLayout, wfxex.dwChannelMask);
format.m_channelLayout = m_channelLayout;
/* When the stream is raw, the values in the format structure are set to the link */
/* parameters, so store the encoded stream values here for the IsCompatible function */
m_encodedChannels = wfxex.Format.nChannels;
m_encodedSampleRate = (format.m_dataFormat == AE_FMT_RAW) ? format.m_streamInfo.m_sampleRate : format.m_sampleRate;
wfxex_iec61937.dwEncodedChannelCount = wfxex.Format.nChannels;
wfxex_iec61937.dwEncodedSamplesPerSec = m_encodedSampleRate;
/* Set up returned sink format for engine */
if (format.m_dataFormat != AE_FMT_RAW)
{
if (wfxex.Format.wBitsPerSample == 32)
{
if (wfxex.SubFormat == KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)
format.m_dataFormat = AE_FMT_FLOAT;
else if (wfxex.Samples.wValidBitsPerSample == 32)
format.m_dataFormat = AE_FMT_S32NE;
else
format.m_dataFormat = AE_FMT_S24NE4MSB;
}
else if (wfxex.Format.wBitsPerSample == 24)
format.m_dataFormat = AE_FMT_S24NE3;
else
format.m_dataFormat = AE_FMT_S16NE;
}
format.m_sampleRate = wfxex.Format.nSamplesPerSec; //PCM: Sample rate. RAW: Link speed
format.m_frameSize = (wfxex.Format.wBitsPerSample >> 3) * wfxex.Format.nChannels;
REFERENCE_TIME audioSinkBufferDurationMsec, hnsLatency;
audioSinkBufferDurationMsec = (REFERENCE_TIME)500000;
if (IsUSBDevice())
{
CLog::LogF(LOGDEBUG, "detected USB device, increasing buffer size");
audioSinkBufferDurationMsec = (REFERENCE_TIME)1000000;
}
audioSinkBufferDurationMsec = (REFERENCE_TIME)((audioSinkBufferDurationMsec / format.m_frameSize) * format.m_frameSize); //even number of frames
if (format.m_dataFormat == AE_FMT_RAW)
format.m_dataFormat = AE_FMT_S16NE;
hr = m_pAudioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
audioSinkBufferDurationMsec, audioSinkBufferDurationMsec, &wfxex.Format, NULL);
if (hr == AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED)
{
/* WASAPI requires aligned buffer */
/* Get the next aligned frame */
hr = m_pAudioClient->GetBufferSize(&m_uiBufferLen);
if (FAILED(hr))
{
CLog::LogF(LOGERROR, "GetBufferSize Failed : {}", WASAPIErrToStr(hr));
return false;
}
audioSinkBufferDurationMsec = (REFERENCE_TIME) ((10000.0 * 1000 / wfxex.Format.nSamplesPerSec * m_uiBufferLen) + 0.5);
/* Release the previous allocations */
/* Create a new audio client */
hr = m_pDevice->Activate(m_pAudioClient.ReleaseAndGetAddressOf());
if (FAILED(hr))
{
CLog::LogF(LOGERROR, "Device Activation Failed : {}", WASAPIErrToStr(hr));
return false;
}
/* Open the stream and associate it with an audio session */
hr = m_pAudioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, AUDCLNT_STREAMFLAGS_EVENTCALLBACK | AUDCLNT_STREAMFLAGS_NOPERSIST,
audioSinkBufferDurationMsec, audioSinkBufferDurationMsec, &wfxex.Format, NULL);
}
if (FAILED(hr))
{
CLog::LogF(LOGERROR, "Failed to initialize WASAPI in exclusive mode {} - ({}).", HRESULT(hr),
WASAPIErrToStr(hr));
CLog::Log(LOGDEBUG, " Sample Rate : {}", wfxex.Format.nSamplesPerSec);
CLog::Log(LOGDEBUG, " Sample Format : {}", CAEUtil::DataFormatToStr(format.m_dataFormat));
CLog::Log(LOGDEBUG, " Bits Per Sample : {}", wfxex.Format.wBitsPerSample);
CLog::Log(LOGDEBUG, " Valid Bits/Samp : {}", wfxex.Samples.wValidBitsPerSample);
CLog::Log(LOGDEBUG, " Channel Count : {}", wfxex.Format.nChannels);
CLog::Log(LOGDEBUG, " Block Align : {}", wfxex.Format.nBlockAlign);
CLog::Log(LOGDEBUG, " Avg. Bytes Sec : {}", wfxex.Format.nAvgBytesPerSec);
CLog::Log(LOGDEBUG, " Samples/Block : {}", wfxex.Samples.wSamplesPerBlock);
CLog::Log(LOGDEBUG, " Format cBSize : {}", wfxex.Format.cbSize);
CLog::Log(LOGDEBUG, " Channel Layout : {}", ((std::string)format.m_channelLayout));
CLog::Log(LOGDEBUG, " Enc. Channels : {}", wfxex_iec61937.dwEncodedChannelCount);
CLog::Log(LOGDEBUG, " Enc. Samples/Sec: {}", wfxex_iec61937.dwEncodedSamplesPerSec);
CLog::Log(LOGDEBUG, " Channel Mask : {}", wfxex.dwChannelMask);
CLog::Log(LOGDEBUG, " Periodicty : {}", audioSinkBufferDurationMsec);
return false;
}
/* Latency of WASAPI buffers in event-driven mode is equal to the returned value */
/* of GetStreamLatency converted from 100ns intervals to seconds then multiplied */
/* by two as there are two equally-sized buffers and playback starts when the */
/* second buffer is filled. Multiplying the returned 100ns intervals by 0.0000002 */
/* is handles both the unit conversion and twin buffers. */
hr = m_pAudioClient->GetStreamLatency(&hnsLatency);
if (FAILED(hr))
{
CLog::LogF(LOGERROR, "GetStreamLatency Failed : {}", WASAPIErrToStr(hr));
return false;
}
m_sinkLatency = hnsLatency * 0.0000002;
CLog::LogF(LOGINFO, "WASAPI Exclusive Mode Sink Initialized using: {}, {}, {}",
CAEUtil::DataFormatToStr(format.m_dataFormat), wfxex.Format.nSamplesPerSec,
wfxex.Format.nChannels);
return true;
}
void CAESinkWASAPI::Drain()
{
if(!m_pAudioClient)
return;
AEDelayStatus status;
GetDelay(status);
KODI::TIME::Sleep(std::chrono::milliseconds(static_cast<int>(status.GetDelay() * 500)));
if (m_running)
{
try
{
m_pAudioClient->Stop(); //stop the audio output
m_pAudioClient->Reset(); //flush buffer and reset audio clock stream position
m_sinkFrames = 0;
}
catch (...)
{
CLog::LogF(LOGDEBUG, "Invalidated AudioClient - Releasing");
}
}
m_running = false;
}
bool CAESinkWASAPI::IsUSBDevice()
{
return m_pDevice && m_pDevice->IsUSBDevice();
}
|