]> git.tdb.fi Git - ext/openal.git/blob - alc/backends/winmm.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / alc / backends / winmm.cpp
1 /**
2  * OpenAL cross platform audio library
3  * Copyright (C) 1999-2007 by authors.
4  * This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Library General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  *  License along with this library; if not, write to the
16  *  Free Software Foundation, Inc.,
17  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * Or go to http://www.gnu.org/copyleft/lgpl.html
19  */
20
21 #include "config.h"
22
23 #include "winmm.h"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
28
29 #include <windows.h>
30 #include <mmsystem.h>
31 #include <mmreg.h>
32
33 #include <array>
34 #include <atomic>
35 #include <thread>
36 #include <vector>
37 #include <string>
38 #include <algorithm>
39 #include <functional>
40
41 #include "alnumeric.h"
42 #include "core/device.h"
43 #include "core/helpers.h"
44 #include "core/logging.h"
45 #include "ringbuffer.h"
46 #include "strutils.h"
47 #include "threads.h"
48
49 #ifndef WAVE_FORMAT_IEEE_FLOAT
50 #define WAVE_FORMAT_IEEE_FLOAT  0x0003
51 #endif
52
53 namespace {
54
55 #define DEVNAME_HEAD "OpenAL Soft on "
56
57
58 al::vector<std::string> PlaybackDevices;
59 al::vector<std::string> CaptureDevices;
60
61 bool checkName(const al::vector<std::string> &list, const std::string &name)
62 { return std::find(list.cbegin(), list.cend(), name) != list.cend(); }
63
64 void ProbePlaybackDevices(void)
65 {
66     PlaybackDevices.clear();
67
68     UINT numdevs{waveOutGetNumDevs()};
69     PlaybackDevices.reserve(numdevs);
70     for(UINT i{0};i < numdevs;++i)
71     {
72         std::string dname;
73
74         WAVEOUTCAPSW WaveCaps{};
75         if(waveOutGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
76         {
77             const std::string basename{DEVNAME_HEAD + wstr_to_utf8(WaveCaps.szPname)};
78
79             int count{1};
80             std::string newname{basename};
81             while(checkName(PlaybackDevices, newname))
82             {
83                 newname = basename;
84                 newname += " #";
85                 newname += std::to_string(++count);
86             }
87             dname = std::move(newname);
88
89             TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
90         }
91         PlaybackDevices.emplace_back(std::move(dname));
92     }
93 }
94
95 void ProbeCaptureDevices(void)
96 {
97     CaptureDevices.clear();
98
99     UINT numdevs{waveInGetNumDevs()};
100     CaptureDevices.reserve(numdevs);
101     for(UINT i{0};i < numdevs;++i)
102     {
103         std::string dname;
104
105         WAVEINCAPSW WaveCaps{};
106         if(waveInGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
107         {
108             const std::string basename{DEVNAME_HEAD + wstr_to_utf8(WaveCaps.szPname)};
109
110             int count{1};
111             std::string newname{basename};
112             while(checkName(CaptureDevices, newname))
113             {
114                 newname = basename;
115                 newname += " #";
116                 newname += std::to_string(++count);
117             }
118             dname = std::move(newname);
119
120             TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
121         }
122         CaptureDevices.emplace_back(std::move(dname));
123     }
124 }
125
126
127 struct WinMMPlayback final : public BackendBase {
128     WinMMPlayback(DeviceBase *device) noexcept : BackendBase{device} { }
129     ~WinMMPlayback() override;
130
131     void CALLBACK waveOutProc(HWAVEOUT device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
132     static void CALLBACK waveOutProcC(HWAVEOUT device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
133     { reinterpret_cast<WinMMPlayback*>(instance)->waveOutProc(device, msg, param1, param2); }
134
135     int mixerProc();
136
137     void open(const char *name) override;
138     bool reset() override;
139     void start() override;
140     void stop() override;
141
142     std::atomic<uint> mWritable{0u};
143     al::semaphore mSem;
144     uint mIdx{0u};
145     std::array<WAVEHDR,4> mWaveBuffer{};
146
147     HWAVEOUT mOutHdl{nullptr};
148
149     WAVEFORMATEX mFormat{};
150
151     std::atomic<bool> mKillNow{true};
152     std::thread mThread;
153
154     DEF_NEWDEL(WinMMPlayback)
155 };
156
157 WinMMPlayback::~WinMMPlayback()
158 {
159     if(mOutHdl)
160         waveOutClose(mOutHdl);
161     mOutHdl = nullptr;
162
163     al_free(mWaveBuffer[0].lpData);
164     std::fill(mWaveBuffer.begin(), mWaveBuffer.end(), WAVEHDR{});
165 }
166
167 /* WinMMPlayback::waveOutProc
168  *
169  * Posts a message to 'WinMMPlayback::mixerProc' everytime a WaveOut Buffer is
170  * completed and returns to the application (for more data)
171  */
172 void CALLBACK WinMMPlayback::waveOutProc(HWAVEOUT, UINT msg, DWORD_PTR, DWORD_PTR) noexcept
173 {
174     if(msg != WOM_DONE) return;
175     mWritable.fetch_add(1, std::memory_order_acq_rel);
176     mSem.post();
177 }
178
179 FORCE_ALIGN int WinMMPlayback::mixerProc()
180 {
181     SetRTPriority();
182     althrd_setname(MIXER_THREAD_NAME);
183
184     while(!mKillNow.load(std::memory_order_acquire)
185         && mDevice->Connected.load(std::memory_order_acquire))
186     {
187         uint todo{mWritable.load(std::memory_order_acquire)};
188         if(todo < 1)
189         {
190             mSem.wait();
191             continue;
192         }
193
194         size_t widx{mIdx};
195         do {
196             WAVEHDR &waveHdr = mWaveBuffer[widx];
197             if(++widx == mWaveBuffer.size()) widx = 0;
198
199             mDevice->renderSamples(waveHdr.lpData, mDevice->UpdateSize, mFormat.nChannels);
200             mWritable.fetch_sub(1, std::memory_order_acq_rel);
201             waveOutWrite(mOutHdl, &waveHdr, sizeof(WAVEHDR));
202         } while(--todo);
203         mIdx = static_cast<uint>(widx);
204     }
205
206     return 0;
207 }
208
209
210 void WinMMPlayback::open(const char *name)
211 {
212     if(PlaybackDevices.empty())
213         ProbePlaybackDevices();
214
215     // Find the Device ID matching the deviceName if valid
216     auto iter = name ?
217         std::find(PlaybackDevices.cbegin(), PlaybackDevices.cend(), name) :
218         PlaybackDevices.cbegin();
219     if(iter == PlaybackDevices.cend())
220         throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
221             name};
222     auto DeviceID = static_cast<UINT>(std::distance(PlaybackDevices.cbegin(), iter));
223
224     DevFmtType fmttype{mDevice->FmtType};
225 retry_open:
226     WAVEFORMATEX format{};
227     if(fmttype == DevFmtFloat)
228     {
229         format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
230         format.wBitsPerSample = 32;
231     }
232     else
233     {
234         format.wFormatTag = WAVE_FORMAT_PCM;
235         if(fmttype == DevFmtUByte || fmttype == DevFmtByte)
236             format.wBitsPerSample = 8;
237         else
238             format.wBitsPerSample = 16;
239     }
240     format.nChannels = ((mDevice->FmtChans == DevFmtMono) ? 1 : 2);
241     format.nBlockAlign = static_cast<WORD>(format.wBitsPerSample * format.nChannels / 8);
242     format.nSamplesPerSec = mDevice->Frequency;
243     format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
244     format.cbSize = 0;
245
246     HWAVEOUT outHandle{};
247     MMRESULT res{waveOutOpen(&outHandle, DeviceID, &format,
248         reinterpret_cast<DWORD_PTR>(&WinMMPlayback::waveOutProcC),
249         reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
250     if(res != MMSYSERR_NOERROR)
251     {
252         if(fmttype == DevFmtFloat)
253         {
254             fmttype = DevFmtShort;
255             goto retry_open;
256         }
257         throw al::backend_exception{al::backend_error::DeviceError, "waveOutOpen failed: %u", res};
258     }
259
260     if(mOutHdl)
261         waveOutClose(mOutHdl);
262     mOutHdl = outHandle;
263     mFormat = format;
264
265     mDevice->DeviceName = PlaybackDevices[DeviceID];
266 }
267
268 bool WinMMPlayback::reset()
269 {
270     mDevice->BufferSize = static_cast<uint>(uint64_t{mDevice->BufferSize} *
271         mFormat.nSamplesPerSec / mDevice->Frequency);
272     mDevice->BufferSize = (mDevice->BufferSize+3) & ~0x3u;
273     mDevice->UpdateSize = mDevice->BufferSize / 4;
274     mDevice->Frequency = mFormat.nSamplesPerSec;
275
276     if(mFormat.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
277     {
278         if(mFormat.wBitsPerSample == 32)
279             mDevice->FmtType = DevFmtFloat;
280         else
281         {
282             ERR("Unhandled IEEE float sample depth: %d\n", mFormat.wBitsPerSample);
283             return false;
284         }
285     }
286     else if(mFormat.wFormatTag == WAVE_FORMAT_PCM)
287     {
288         if(mFormat.wBitsPerSample == 16)
289             mDevice->FmtType = DevFmtShort;
290         else if(mFormat.wBitsPerSample == 8)
291             mDevice->FmtType = DevFmtUByte;
292         else
293         {
294             ERR("Unhandled PCM sample depth: %d\n", mFormat.wBitsPerSample);
295             return false;
296         }
297     }
298     else
299     {
300         ERR("Unhandled format tag: 0x%04x\n", mFormat.wFormatTag);
301         return false;
302     }
303
304     if(mFormat.nChannels >= 2)
305         mDevice->FmtChans = DevFmtStereo;
306     else if(mFormat.nChannels == 1)
307         mDevice->FmtChans = DevFmtMono;
308     else
309     {
310         ERR("Unhandled channel count: %d\n", mFormat.nChannels);
311         return false;
312     }
313     setDefaultWFXChannelOrder();
314
315     uint BufferSize{mDevice->UpdateSize * mFormat.nChannels * mDevice->bytesFromFmt()};
316
317     al_free(mWaveBuffer[0].lpData);
318     mWaveBuffer[0] = WAVEHDR{};
319     mWaveBuffer[0].lpData = static_cast<char*>(al_calloc(16, BufferSize * mWaveBuffer.size()));
320     mWaveBuffer[0].dwBufferLength = BufferSize;
321     for(size_t i{1};i < mWaveBuffer.size();i++)
322     {
323         mWaveBuffer[i] = WAVEHDR{};
324         mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
325         mWaveBuffer[i].dwBufferLength = BufferSize;
326     }
327     mIdx = 0;
328
329     return true;
330 }
331
332 void WinMMPlayback::start()
333 {
334     try {
335         for(auto &waveHdr : mWaveBuffer)
336             waveOutPrepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR));
337         mWritable.store(static_cast<uint>(mWaveBuffer.size()), std::memory_order_release);
338
339         mKillNow.store(false, std::memory_order_release);
340         mThread = std::thread{std::mem_fn(&WinMMPlayback::mixerProc), this};
341     }
342     catch(std::exception& e) {
343         throw al::backend_exception{al::backend_error::DeviceError,
344             "Failed to start mixing thread: %s", e.what()};
345     }
346 }
347
348 void WinMMPlayback::stop()
349 {
350     if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
351         return;
352     mThread.join();
353
354     while(mWritable.load(std::memory_order_acquire) < mWaveBuffer.size())
355         mSem.wait();
356     for(auto &waveHdr : mWaveBuffer)
357         waveOutUnprepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR));
358     mWritable.store(0, std::memory_order_release);
359 }
360
361
362 struct WinMMCapture final : public BackendBase {
363     WinMMCapture(DeviceBase *device) noexcept : BackendBase{device} { }
364     ~WinMMCapture() override;
365
366     void CALLBACK waveInProc(HWAVEIN device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
367     static void CALLBACK waveInProcC(HWAVEIN device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
368     { reinterpret_cast<WinMMCapture*>(instance)->waveInProc(device, msg, param1, param2); }
369
370     int captureProc();
371
372     void open(const char *name) override;
373     void start() override;
374     void stop() override;
375     void captureSamples(al::byte *buffer, uint samples) override;
376     uint availableSamples() override;
377
378     std::atomic<uint> mReadable{0u};
379     al::semaphore mSem;
380     uint mIdx{0};
381     std::array<WAVEHDR,4> mWaveBuffer{};
382
383     HWAVEIN mInHdl{nullptr};
384
385     RingBufferPtr mRing{nullptr};
386
387     WAVEFORMATEX mFormat{};
388
389     std::atomic<bool> mKillNow{true};
390     std::thread mThread;
391
392     DEF_NEWDEL(WinMMCapture)
393 };
394
395 WinMMCapture::~WinMMCapture()
396 {
397     // Close the Wave device
398     if(mInHdl)
399         waveInClose(mInHdl);
400     mInHdl = nullptr;
401
402     al_free(mWaveBuffer[0].lpData);
403     std::fill(mWaveBuffer.begin(), mWaveBuffer.end(), WAVEHDR{});
404 }
405
406 /* WinMMCapture::waveInProc
407  *
408  * Posts a message to 'WinMMCapture::captureProc' everytime a WaveIn Buffer is
409  * completed and returns to the application (with more data).
410  */
411 void CALLBACK WinMMCapture::waveInProc(HWAVEIN, UINT msg, DWORD_PTR, DWORD_PTR) noexcept
412 {
413     if(msg != WIM_DATA) return;
414     mReadable.fetch_add(1, std::memory_order_acq_rel);
415     mSem.post();
416 }
417
418 int WinMMCapture::captureProc()
419 {
420     althrd_setname(RECORD_THREAD_NAME);
421
422     while(!mKillNow.load(std::memory_order_acquire) &&
423           mDevice->Connected.load(std::memory_order_acquire))
424     {
425         uint todo{mReadable.load(std::memory_order_acquire)};
426         if(todo < 1)
427         {
428             mSem.wait();
429             continue;
430         }
431
432         size_t widx{mIdx};
433         do {
434             WAVEHDR &waveHdr = mWaveBuffer[widx];
435             widx = (widx+1) % mWaveBuffer.size();
436
437             mRing->write(waveHdr.lpData, waveHdr.dwBytesRecorded / mFormat.nBlockAlign);
438             mReadable.fetch_sub(1, std::memory_order_acq_rel);
439             waveInAddBuffer(mInHdl, &waveHdr, sizeof(WAVEHDR));
440         } while(--todo);
441         mIdx = static_cast<uint>(widx);
442     }
443
444     return 0;
445 }
446
447
448 void WinMMCapture::open(const char *name)
449 {
450     if(CaptureDevices.empty())
451         ProbeCaptureDevices();
452
453     // Find the Device ID matching the deviceName if valid
454     auto iter = name ?
455         std::find(CaptureDevices.cbegin(), CaptureDevices.cend(), name) :
456         CaptureDevices.cbegin();
457     if(iter == CaptureDevices.cend())
458         throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
459             name};
460     auto DeviceID = static_cast<UINT>(std::distance(CaptureDevices.cbegin(), iter));
461
462     switch(mDevice->FmtChans)
463     {
464     case DevFmtMono:
465     case DevFmtStereo:
466         break;
467
468     case DevFmtQuad:
469     case DevFmtX51:
470     case DevFmtX61:
471     case DevFmtX71:
472     case DevFmtX714:
473     case DevFmtX3D71:
474     case DevFmtAmbi3D:
475         throw al::backend_exception{al::backend_error::DeviceError, "%s capture not supported",
476             DevFmtChannelsString(mDevice->FmtChans)};
477     }
478
479     switch(mDevice->FmtType)
480     {
481     case DevFmtUByte:
482     case DevFmtShort:
483     case DevFmtInt:
484     case DevFmtFloat:
485         break;
486
487     case DevFmtByte:
488     case DevFmtUShort:
489     case DevFmtUInt:
490         throw al::backend_exception{al::backend_error::DeviceError, "%s samples not supported",
491             DevFmtTypeString(mDevice->FmtType)};
492     }
493
494     mFormat = WAVEFORMATEX{};
495     mFormat.wFormatTag = (mDevice->FmtType == DevFmtFloat) ?
496                          WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
497     mFormat.nChannels = static_cast<WORD>(mDevice->channelsFromFmt());
498     mFormat.wBitsPerSample = static_cast<WORD>(mDevice->bytesFromFmt() * 8);
499     mFormat.nBlockAlign = static_cast<WORD>(mFormat.wBitsPerSample * mFormat.nChannels / 8);
500     mFormat.nSamplesPerSec = mDevice->Frequency;
501     mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign;
502     mFormat.cbSize = 0;
503
504     MMRESULT res{waveInOpen(&mInHdl, DeviceID, &mFormat,
505         reinterpret_cast<DWORD_PTR>(&WinMMCapture::waveInProcC),
506         reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
507     if(res != MMSYSERR_NOERROR)
508         throw al::backend_exception{al::backend_error::DeviceError, "waveInOpen failed: %u", res};
509
510     // Ensure each buffer is 50ms each
511     DWORD BufferSize{mFormat.nAvgBytesPerSec / 20u};
512     BufferSize -= (BufferSize % mFormat.nBlockAlign);
513
514     // Allocate circular memory buffer for the captured audio
515     // Make sure circular buffer is at least 100ms in size
516     uint CapturedDataSize{mDevice->BufferSize};
517     CapturedDataSize = static_cast<uint>(maxz(CapturedDataSize, BufferSize*mWaveBuffer.size()));
518
519     mRing = RingBuffer::Create(CapturedDataSize, mFormat.nBlockAlign, false);
520
521     al_free(mWaveBuffer[0].lpData);
522     mWaveBuffer[0] = WAVEHDR{};
523     mWaveBuffer[0].lpData = static_cast<char*>(al_calloc(16, BufferSize * mWaveBuffer.size()));
524     mWaveBuffer[0].dwBufferLength = BufferSize;
525     for(size_t i{1};i < mWaveBuffer.size();++i)
526     {
527         mWaveBuffer[i] = WAVEHDR{};
528         mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
529         mWaveBuffer[i].dwBufferLength = mWaveBuffer[i-1].dwBufferLength;
530     }
531
532     mDevice->DeviceName = CaptureDevices[DeviceID];
533 }
534
535 void WinMMCapture::start()
536 {
537     try {
538         for(size_t i{0};i < mWaveBuffer.size();++i)
539         {
540             waveInPrepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
541             waveInAddBuffer(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
542         }
543
544         mKillNow.store(false, std::memory_order_release);
545         mThread = std::thread{std::mem_fn(&WinMMCapture::captureProc), this};
546
547         waveInStart(mInHdl);
548     }
549     catch(std::exception& e) {
550         throw al::backend_exception{al::backend_error::DeviceError,
551             "Failed to start recording thread: %s", e.what()};
552     }
553 }
554
555 void WinMMCapture::stop()
556 {
557     waveInStop(mInHdl);
558
559     mKillNow.store(true, std::memory_order_release);
560     if(mThread.joinable())
561     {
562         mSem.post();
563         mThread.join();
564     }
565
566     waveInReset(mInHdl);
567     for(size_t i{0};i < mWaveBuffer.size();++i)
568         waveInUnprepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
569
570     mReadable.store(0, std::memory_order_release);
571     mIdx = 0;
572 }
573
574 void WinMMCapture::captureSamples(al::byte *buffer, uint samples)
575 { mRing->read(buffer, samples); }
576
577 uint WinMMCapture::availableSamples()
578 { return static_cast<uint>(mRing->readSpace()); }
579
580 } // namespace
581
582
583 bool WinMMBackendFactory::init()
584 { return true; }
585
586 bool WinMMBackendFactory::querySupport(BackendType type)
587 { return type == BackendType::Playback || type == BackendType::Capture; }
588
589 std::string WinMMBackendFactory::probe(BackendType type)
590 {
591     std::string outnames;
592     auto add_device = [&outnames](const std::string &dname) -> void
593     {
594         /* +1 to also append the null char (to ensure a null-separated list and
595          * double-null terminated list).
596          */
597         if(!dname.empty())
598             outnames.append(dname.c_str(), dname.length()+1);
599     };
600     switch(type)
601     {
602     case BackendType::Playback:
603         ProbePlaybackDevices();
604         std::for_each(PlaybackDevices.cbegin(), PlaybackDevices.cend(), add_device);
605         break;
606
607     case BackendType::Capture:
608         ProbeCaptureDevices();
609         std::for_each(CaptureDevices.cbegin(), CaptureDevices.cend(), add_device);
610         break;
611     }
612     return outnames;
613 }
614
615 BackendPtr WinMMBackendFactory::createBackend(DeviceBase *device, BackendType type)
616 {
617     if(type == BackendType::Playback)
618         return BackendPtr{new WinMMPlayback{device}};
619     if(type == BackendType::Capture)
620         return BackendPtr{new WinMMCapture{device}};
621     return nullptr;
622 }
623
624 BackendFactory &WinMMBackendFactory::getFactory()
625 {
626     static WinMMBackendFactory factory{};
627     return factory;
628 }