3 * Copyright (C) 2010-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "../Interfaces/AESink.h"
24 #include <mmdeviceapi.h>
25 #include <Audioclient.h>
26 #include "../Utils/AEDeviceInfo.h"
28 #include "threads/CriticalSection.h"
30 class CAESinkWASAPI : public IAESink
33 virtual const char *GetName() { return "WASAPI"; }
36 virtual ~CAESinkWASAPI();
38 virtual bool Initialize (AEAudioFormat &format, std::string &device);
39 virtual void Deinitialize();
41 virtual double GetDelay ();
42 virtual double GetCacheTotal ();
43 virtual unsigned int AddPackets (uint8_t *data, unsigned int frames, bool hasAudio, bool blocking = false);
44 virtual void Drain ();
45 static void EnumerateDevicesEx (AEDeviceInfoList &deviceInfoList, bool force = false);
47 bool InitializeExclusive(AEAudioFormat &format);
48 void AEChannelsFromSpeakerMask(DWORD speakers);
49 static DWORD SpeakerMaskFromAEChannels(const CAEChannelInfo &channels);
50 static void BuildWaveFormatExtensible(AEAudioFormat &format, WAVEFORMATEXTENSIBLE &wfxex);
51 static void BuildWaveFormatExtensibleIEC61397(AEAudioFormat &format, WAVEFORMATEXTENSIBLE_IEC61937 &wfxex);
53 static const char *WASAPIErrToStr(HRESULT err);
55 HANDLE m_needDataEvent;
57 IAudioClient *m_pAudioClient;
58 IAudioRenderClient *m_pRenderClient;
60 AEAudioFormat m_format;
61 enum AEDataFormat m_encodedFormat;
62 unsigned int m_encodedChannels;
63 unsigned int m_encodedSampleRate;
64 CAEChannelInfo m_channelLayout;
67 enum AEDataFormat sinkReqFormat;
68 enum AEDataFormat sinkRetFormat;
72 bool m_isSuspended; /* sink is in a suspended state - release audio device */
73 bool m_isDirty; /* sink output failed - needs re-init or new device */
75 unsigned int m_uiBufferLen; /* wasapi endpoint buffer size, in frames */
76 double m_avgTimeWaiting; /* time between next buffer of data from SoftAE and driver call for data */
77 double m_sinkLatency; /* time in seconds of total duration of the two WASAPI buffers */
78 unsigned int m_lastWriteToBuffer;