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
|
/*
* 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 "AESinkFactoryWin.h"
#include "utils/log.h"
#define ERRTOSTR(err) case err: return #err
const char *WASAPIErrToStr(HRESULT err)
{
switch (err)
{
ERRTOSTR(AUDCLNT_E_NOT_INITIALIZED);
ERRTOSTR(AUDCLNT_E_ALREADY_INITIALIZED);
ERRTOSTR(AUDCLNT_E_WRONG_ENDPOINT_TYPE);
ERRTOSTR(AUDCLNT_E_DEVICE_INVALIDATED);
ERRTOSTR(AUDCLNT_E_NOT_STOPPED);
ERRTOSTR(AUDCLNT_E_BUFFER_TOO_LARGE);
ERRTOSTR(AUDCLNT_E_OUT_OF_ORDER);
ERRTOSTR(AUDCLNT_E_UNSUPPORTED_FORMAT);
ERRTOSTR(AUDCLNT_E_INVALID_SIZE);
ERRTOSTR(AUDCLNT_E_DEVICE_IN_USE);
ERRTOSTR(AUDCLNT_E_BUFFER_OPERATION_PENDING);
ERRTOSTR(AUDCLNT_E_THREAD_NOT_REGISTERED);
ERRTOSTR(AUDCLNT_E_EXCLUSIVE_MODE_NOT_ALLOWED);
ERRTOSTR(AUDCLNT_E_ENDPOINT_CREATE_FAILED);
ERRTOSTR(AUDCLNT_E_SERVICE_NOT_RUNNING);
ERRTOSTR(AUDCLNT_E_EVENTHANDLE_NOT_EXPECTED);
ERRTOSTR(AUDCLNT_E_EXCLUSIVE_MODE_ONLY);
ERRTOSTR(AUDCLNT_E_BUFDURATION_PERIOD_NOT_EQUAL);
ERRTOSTR(AUDCLNT_E_EVENTHANDLE_NOT_SET);
ERRTOSTR(AUDCLNT_E_INCORRECT_BUFFER_SIZE);
ERRTOSTR(AUDCLNT_E_BUFFER_SIZE_ERROR);
ERRTOSTR(AUDCLNT_E_CPUUSAGE_EXCEEDED);
ERRTOSTR(AUDCLNT_E_BUFFER_ERROR);
ERRTOSTR(AUDCLNT_E_BUFFER_SIZE_NOT_ALIGNED);
ERRTOSTR(AUDCLNT_E_INVALID_DEVICE_PERIOD);
ERRTOSTR(E_POINTER);
ERRTOSTR(E_INVALIDARG);
ERRTOSTR(E_OUTOFMEMORY);
default: break;
}
return "Undefined";
}
void CAESinkFactoryWin::AEChannelsFromSpeakerMask(CAEChannelInfo& channelLayout, DWORD speakers)
{
channelLayout.Reset();
for (int i = 0; i < WASAPI_SPEAKER_COUNT; i++)
{
if (speakers & WASAPIChannelOrder[i])
channelLayout += AEChannelNames[i];
}
};
DWORD CAESinkFactoryWin::SpeakerMaskFromAEChannels(const CAEChannelInfo &channels)
{
DWORD mask = 0;
for (unsigned int i = 0; i < channels.Count(); i++)
{
for (unsigned int j = 0; j < WASAPI_SPEAKER_COUNT; j++)
if (channels[i] == AEChannelNames[j])
mask |= WASAPIChannelOrder[j];
}
return mask;
};
DWORD CAESinkFactoryWin::ChLayoutToChMask(const enum AEChannel * layout, unsigned int * numberOfChannels /*= NULL*/)
{
if (numberOfChannels)
*numberOfChannels = 0;
if (!layout)
return 0;
DWORD mask = 0;
unsigned int i;
for (i = 0; layout[i] != AE_CH_NULL; i++)
mask |= WASAPIChannelOrder[layout[i]];
if (numberOfChannels)
*numberOfChannels = i;
return mask;
};
void CAESinkFactoryWin::BuildWaveFormatExtensible(AEAudioFormat &format, WAVEFORMATEXTENSIBLE &wfxex)
{
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
wfxex.Format.cbSize = sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
if (format.m_dataFormat != AE_FMT_RAW) // PCM data
{
wfxex.dwChannelMask = CAESinkFactoryWin::SpeakerMaskFromAEChannels(format.m_channelLayout);
wfxex.Format.nChannels = (WORD)format.m_channelLayout.Count();
wfxex.Format.nSamplesPerSec = format.m_sampleRate;
wfxex.Format.wBitsPerSample = CAEUtil::DataFormatToBits((AEDataFormat)format.m_dataFormat);
wfxex.SubFormat = format.m_dataFormat < AE_FMT_FLOAT ? KSDATAFORMAT_SUBTYPE_PCM : KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
}
else //Raw bitstream
{
wfxex.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
if (format.m_dataFormat == AE_FMT_RAW &&
((format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_AC3) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_EAC3) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTSHD_CORE) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTS_2048) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTS_1024) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTS_512)))
{
if (format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_EAC3)
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL_PLUS;
else
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_DIGITAL;
wfxex.dwChannelMask = bool(format.m_channelLayout.Count() == 2) ? KSAUDIO_SPEAKER_STEREO : KSAUDIO_SPEAKER_5POINT1;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.Format.nChannels = (WORD)format.m_channelLayout.Count();
wfxex.Format.nSamplesPerSec = format.m_sampleRate;
if (format.m_streamInfo.m_sampleRate == 0)
CLog::Log(LOGERROR, "Invalid sample rate supplied for RAW format");
}
else if (format.m_dataFormat == AE_FMT_RAW &&
((format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTSHD_MA) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_TRUEHD) ||
(format.m_streamInfo.m_type == CAEStreamInfo::STREAM_TYPE_DTSHD)))
{
// IEC 61937 transmissions over HDMI
wfxex.Format.nSamplesPerSec = 192000L;
wfxex.Format.wBitsPerSample = 16;
wfxex.Samples.wValidBitsPerSample = 16;
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
switch (format.m_streamInfo.m_type)
{
case CAEStreamInfo::STREAM_TYPE_TRUEHD:
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DOLBY_MLP;
wfxex.Format.nChannels = 8; // Four IEC 60958 Lines.
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
break;
case CAEStreamInfo::STREAM_TYPE_DTSHD_MA:
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD;
wfxex.Format.nChannels = 8; // Four IEC 60958 Lines.
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
break;
case CAEStreamInfo::STREAM_TYPE_DTSHD:
wfxex.SubFormat = KSDATAFORMAT_SUBTYPE_IEC61937_DTS_HD;
wfxex.Format.nChannels = 2; // One IEC 60958 Lines.
wfxex.dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
break;
}
if (format.m_channelLayout.Count() == 8)
wfxex.dwChannelMask = KSAUDIO_SPEAKER_7POINT1_SURROUND;
else
wfxex.dwChannelMask = KSAUDIO_SPEAKER_5POINT1;
}
}
if (format.m_dataFormat == AE_FMT_S24NE4MSB)
wfxex.Samples.wValidBitsPerSample = 24;
else
wfxex.Samples.wValidBitsPerSample = wfxex.Format.wBitsPerSample;
wfxex.Format.nBlockAlign = wfxex.Format.nChannels * (wfxex.Format.wBitsPerSample >> 3);
wfxex.Format.nAvgBytesPerSec = wfxex.Format.nSamplesPerSec * wfxex.Format.nBlockAlign;
};
|