WASAPIStream.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #ifdef _WIN32
  5. // clang-format off
  6. #include <Windows.h>
  7. #include <mmreg.h>
  8. #include <objbase.h>
  9. #include <wil/resource.h>
  10. // clang-format on
  11. #include <atomic>
  12. #include <string>
  13. #include <thread>
  14. #include <vector>
  15. #include <wrl/client.h>
  16. #include "AudioCommon/SoundStream.h"
  17. struct IAudioClient;
  18. struct IAudioRenderClient;
  19. struct IMMDevice;
  20. struct IMMDeviceEnumerator;
  21. #endif
  22. class WASAPIStream final : public SoundStream
  23. {
  24. #ifdef _WIN32
  25. public:
  26. explicit WASAPIStream();
  27. ~WASAPIStream();
  28. bool Init() override;
  29. bool SetRunning(bool running) override;
  30. static bool IsValid();
  31. static std::vector<std::string> GetAvailableDevices();
  32. static Microsoft::WRL::ComPtr<IMMDevice> GetDeviceByName(std::string_view name);
  33. private:
  34. void SoundLoop();
  35. u32 m_frames_in_buffer = 0;
  36. std::atomic<bool> m_running = false;
  37. std::thread m_thread;
  38. // CoUninitialize must be called after all WASAPI COM objects have been destroyed,
  39. // therefore this member must be located before them, as first class fields are destructed last
  40. wil::unique_couninitialize_call m_coinitialize{false};
  41. Microsoft::WRL::ComPtr<IMMDeviceEnumerator> m_enumerator;
  42. Microsoft::WRL::ComPtr<IAudioClient> m_audio_client;
  43. Microsoft::WRL::ComPtr<IAudioRenderClient> m_audio_renderer;
  44. wil::unique_event_nothrow m_need_data_event;
  45. WAVEFORMATEXTENSIBLE m_format;
  46. #endif // _WIN32
  47. };