kodi
AESinkWASAPI.h
1 /*
2  * Copyright (C) 2010-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "cores/AudioEngine/Interfaces/AESink.h"
12 #include "cores/AudioEngine/Sinks/windows/AESinkFactoryWin.h"
13 #include "cores/AudioEngine/Utils/AEDeviceInfo.h"
14 
15 #include <stdint.h>
16 #include <vector>
17 
18 #include <Audioclient.h>
19 #include <mmdeviceapi.h>
20 #include <wrl/client.h>
21 
22 class CAESinkWASAPI : public IAESink
23 {
24 public:
25  CAESinkWASAPI();
26  virtual ~CAESinkWASAPI();
27 
28  static void Register();
29  static std::unique_ptr<IAESink> Create(std::string& device, AEAudioFormat& desiredFormat);
30  static void EnumerateDevicesEx(AEDeviceInfoList &deviceInfoList, bool force = false);
31 
32  // IAESink overrides
33  const char *GetName() override { return "WASAPI"; }
34  bool Initialize(AEAudioFormat &format, std::string &device) override;
35  void Deinitialize() override;
36  void GetDelay(AEDelayStatus& status) override;
37  double GetCacheTotal() override;
38  unsigned int AddPackets(uint8_t **data, unsigned int frames, unsigned int offset) override;
39  void Drain() override;
40 
41 private:
42  bool InitializeExclusive(AEAudioFormat &format);
43  static void BuildWaveFormatExtensibleIEC61397(AEAudioFormat &format, WAVEFORMATEXTENSIBLE_IEC61937 &wfxex);
44  bool IsUSBDevice();
45 
46  HANDLE m_needDataEvent{0};
47  IAEWASAPIDevice* m_pDevice{nullptr};
48  Microsoft::WRL::ComPtr<IAudioClient> m_pAudioClient;
49  Microsoft::WRL::ComPtr<IAudioRenderClient> m_pRenderClient;
50  Microsoft::WRL::ComPtr<IAudioClock> m_pAudioClock;
51 
52  AEAudioFormat m_format{};
53  unsigned int m_encodedChannels{0};
54  unsigned int m_encodedSampleRate{0};
55  CAEChannelInfo m_channelLayout;
56  std::string m_device;
57 
58  enum AEDataFormat sinkReqFormat = AE_FMT_INVALID;
59  enum AEDataFormat sinkRetFormat = AE_FMT_INVALID;
60 
61  bool m_running{false};
62  bool m_initialized{false};
63  bool m_isSuspended{false}; // sink is in a suspended state - release audio device
64  bool m_isDirty{false}; // sink output failed - needs re-init or new device
65 
66  // time between next buffer of data from SoftAE and driver call for data
67  double m_avgTimeWaiting{50.0};
68  double m_sinkLatency{0.0}; // time in seconds of total duration of the two WASAPI buffers
69 
70  unsigned int m_uiBufferLen{0}; // wasapi endpoint buffer size, in frames
71  uint64_t m_sinkFrames{0};
72  uint64_t m_clockFreq{0};
73 
74  std::vector<uint8_t> m_buffer;
75  int m_bufferPtr{0};
76 };
Definition: XHandle.h:21
Definition: AESink.h:18
Definition: AESinkFactoryWin.h:187
The audio format structure that fully defines a stream&#39;s audio information.
Definition: AEAudioFormat.h:19
Definition: AEUtil.h:27
void GetDelay(AEDelayStatus &status) override
Return a timestamped status structure with delay and sink info.
Definition: AESinkWASAPI.cpp:222
Definition: AEChannelInfo.h:19
Definition: AESinkWASAPI.h:22
unsigned int AddPackets(uint8_t **data, unsigned int frames, unsigned int offset) override
Adds packets to be sent out, this routine MUST block or sleep.
Definition: AESinkWASAPI.cpp:251