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