| 6 |
| 7 | #include "AudioOutputWave.h" |
| 8 | #include "../../VoIPController.h" |
| 9 | #include "../../logging.h" |
| 10 | #include <cassert> |
| 11 |
| 12 | #define BUFFER_SIZE 960 |
| 13 | #define CHECK_ERROR(res, msg) \ |
| 14 | if (res != MMSYSERR_NOERROR) \ |
| 15 | { \ |
| 16 | wchar_t _buf[1024]; \ |
| 17 | waveOutGetErrorTextW(res, _buf, 1024); \ |
| 18 | LOGE(msg ": %ws (MMRESULT=0x%08X)", _buf, res); \ |
| 19 | failed = true; \ |
| 20 | } |
| 21 |
| 22 | using namespace tgvoip::audio; |
| 23 |
| 24 | AudioOutputWave::AudioOutputWave(std::string deviceID) |
| 25 | { |
| 26 | isPlaying = false; |
| 27 | hWaveOut = nullptr; |
| 28 |
| 29 | for (int i = 0; i < 4; i++) |
| 30 | { |
| 31 | ZeroMemory(&buffers[i], sizeof(WAVEHDR)); |
| 32 | buffers[i].dwBufferLength = 960 * 2; |
| 33 | buffers[i].lpData = reinterpret_cast<char*>(std::malloc(960 * 2)); |
| 34 | } |
| 35 |
| 36 | SetCurrentDevice(deviceID); |
| 37 | } |
| 38 |
| 39 | AudioOutputWave::~AudioOutputWave() |
| 40 | { |
| 41 | for (int i = 0; i < 4; i++) |
| 42 | { |
| 43 | std::free(buffers[i].lpData); |
| 44 | } |
| 45 | waveOutClose(hWaveOut); |
| 46 | } |
| 47 |
| 48 | void AudioOutputWave::Start() |
| 49 | { |
| 50 | if (!isPlaying) |
| 51 | { |
| 52 | isPlaying = true; |
| 53 |
| 54 | for (int i = 0; i < 4; i++) |
| 55 | { |
| 56 | MMRESULT res = waveOutPrepareHeader(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 57 | CHECK_ERROR(res, "waveOutPrepareHeader failed"); |
| 58 | //InvokeCallback((unsigned char*)buffers[i].lpData, buffers[i].dwBufferLength); |
| 59 | ZeroMemory(buffers[i].lpData, buffers[i].dwBufferLength); |
| 60 | res = waveOutWrite(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 61 | CHECK_ERROR(res, "waveOutWrite failed"); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 |
| 66 | void AudioOutputWave::Stop() |
| 67 | { |
| 68 | if (isPlaying) |
| 69 | { |
| 70 | isPlaying = false; |
| 71 |
| 72 | MMRESULT res = waveOutReset(hWaveOut); |
| 73 | CHECK_ERROR(res, "waveOutReset failed"); |
| 74 | for (int i = 0; i < 4; i++) |
| 75 | { |
| 76 | res = waveOutUnprepareHeader(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 77 | CHECK_ERROR(res, "waveOutUnprepareHeader failed"); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 |
| 82 | bool AudioOutputWave::IsPlaying() |
| 83 | { |
| 84 | return isPlaying; |
| 85 | } |
| 86 |
| 87 | void AudioOutputWave::WaveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2) |
| 88 | { |
| 89 | if (uMsg == WOM_DONE) |
| 90 | { |
| 91 | ((AudioOutputWave*)dwInstance)->OnBufferDone((WAVEHDR*)dwParam1); |
| 92 | } |
| 93 | } |
| 94 |
| 95 | void AudioOutputWave::OnBufferDone(WAVEHDR* hdr) |
| 96 | { |
| 97 | if (!isPlaying) |
| 98 | return; |
| 99 |
| 100 | InvokeCallback(reinterpret_cast<std::uint8_t*>(hdr->lpData), hdr->dwBufferLength); |
| 101 | hdr->dwFlags &= ~WHDR_DONE; |
| 102 | MMRESULT res = waveOutWrite(hWaveOut, hdr, sizeof(WAVEHDR)); |
| 103 | } |
| 104 |
| 105 | void AudioOutputWave::EnumerateDevices(std::vector<tgvoip::AudioOutputDevice>& devs) |
| 106 | { |
| 107 | UINT num = waveOutGetNumDevs(); |
| 108 | WAVEOUTCAPSW caps; |
| 109 | char nameBuf[512]; |
| 110 | for (UINT i = 0; i < num; i++) |
| 111 | { |
| 112 | waveOutGetDevCapsW(i, &caps, sizeof(caps)); |
| 113 | AudioOutputDevice dev; |
| 114 | WideCharToMultiByte(CP_UTF8, 0, caps.szPname, -1, nameBuf, sizeof(nameBuf), nullptr, nullptr); |
| 115 | dev.displayName = std::string(nameBuf); |
| 116 | dev.id = std::string(nameBuf); |
| 117 | devs.emplace_back(dev); |
| 118 | } |
| 119 | } |
| 120 |
| 121 | void AudioOutputWave::SetCurrentDevice(std::string deviceID) |
| 122 | { |
| 123 | m_currentDevice = deviceID; |
| 124 |
| 125 | bool wasPlaying = isPlaying; |
| 126 | isPlaying = false; |
| 127 | LOGV("closing, hWaveOut=%d", (int)hWaveOut); |
| 128 | if (hWaveOut) |
| 129 | { |
| 130 | MMRESULT res; |
| 131 | if (isPlaying) |
| 132 | { |
| 133 | res = waveOutReset(hWaveOut); |
| 134 | CHECK_ERROR(res, "waveOutReset failed"); |
| 135 | for (int i = 0; i < 4; i++) |
| 136 | { |
| 137 | res = waveOutUnprepareHeader(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 138 | CHECK_ERROR(res, "waveOutUnprepareHeader failed"); |
| 139 | } |
| 140 | } |
| 141 | res = waveOutClose(hWaveOut); |
| 142 | CHECK_ERROR(res, "waveOutClose failed"); |
| 143 | } |
| 144 |
| 145 | ZeroMemory(&format, sizeof(format)); |
| 146 | format.cbSize = 0; |
| 147 | format.wFormatTag = WAVE_FORMAT_PCM; |
| 148 | format.nSamplesPerSec = 48000; |
| 149 | format.wBitsPerSample = 16; |
| 150 | format.nChannels = 1; |
| 151 | format.nBlockAlign = 2; |
| 152 |
| 153 | LOGV("before open device %s", deviceID.c_str()); |
| 154 |
| 155 | if (deviceID == "default") |
| 156 | { |
| 157 | MMRESULT res = waveOutOpen(&hWaveOut, WAVE_MAPPER, &format, (DWORD_PTR)AudioOutputWave::WaveOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION); |
| 158 | CHECK_ERROR(res, "waveOutOpen failed"); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | UINT num = waveOutGetNumDevs(); |
| 163 | WAVEOUTCAPSW caps; |
| 164 | char nameBuf[512]; |
| 165 | hWaveOut = nullptr; |
| 166 | for (UINT i = 0; i < num; i++) |
| 167 | { |
| 168 | waveOutGetDevCapsW(i, &caps, sizeof(caps)); |
| 169 | WideCharToMultiByte(CP_UTF8, 0, caps.szPname, -1, nameBuf, sizeof(nameBuf), nullptr, nullptr); |
| 170 | std::string name = std::string(nameBuf); |
| 171 | if (name == deviceID) |
| 172 | { |
| 173 | MMRESULT res = waveOutOpen(&hWaveOut, i, &format, (DWORD_PTR)AudioOutputWave::WaveOutProc, (DWORD_PTR)this, CALLBACK_FUNCTION | WAVE_MAPPED); |
| 174 | CHECK_ERROR(res, "waveOutOpen failed"); |
| 175 | LOGD("Opened device %s", nameBuf); |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (!hWaveOut) |
| 180 | { |
| 181 | SetCurrentDevice("default"); |
| 182 | return; |
| 183 | } |
| 184 | } |
| 185 |
| 186 | isPlaying = wasPlaying; |
| 187 |
| 188 | if (isPlaying) |
| 189 | { |
| 190 | MMRESULT res; |
| 191 | for (int i = 0; i < 4; i++) |
| 192 | { |
| 193 | res = waveOutPrepareHeader(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 194 | CHECK_ERROR(res, "waveOutPrepareHeader failed"); |
| 195 | res = waveOutWrite(hWaveOut, &buffers[i], sizeof(WAVEHDR)); |
| 196 | CHECK_ERROR(res, "waveOutWrite failed"); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 |