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();
40 virtual bool IsCompatible(const AEAudioFormat &format, const std::string &device);
42 virtual double GetDelay ();
43 virtual double GetCacheTime ();
44 virtual double GetCacheTotal ();
45 virtual unsigned int AddPackets (uint8_t *data, unsigned int frames, bool hasAudio, bool blocking = false);
46 virtual bool SoftSuspend ();
47 virtual bool SoftResume ();
48 virtual void Drain ();
49 static void EnumerateDevicesEx (AEDeviceInfoList &deviceInfoList, bool force = false);
51 bool InitializeExclusive(AEAudioFormat &format);
52 void AEChannelsFromSpeakerMask(DWORD speakers);
53 static DWORD SpeakerMaskFromAEChannels(const CAEChannelInfo &channels);
54 static void BuildWaveFormatExtensible(AEAudioFormat &format, WAVEFORMATEXTENSIBLE &wfxex);
55 static void BuildWaveFormatExtensibleIEC61397(AEAudioFormat &format, WAVEFORMATEXTENSIBLE_IEC61937 &wfxex);
57 static const char *WASAPIErrToStr(HRESULT err);
59 HANDLE m_needDataEvent;
61 IAudioClient *m_pAudioClient;
62 IAudioRenderClient *m_pRenderClient;
64 AEAudioFormat m_format;
65 enum AEDataFormat m_encodedFormat;
66 unsigned int m_encodedChannels;
67 unsigned int m_encodedSampleRate;
68 CAEChannelInfo m_channelLayout;
71 enum AEDataFormat sinkReqFormat;
72 enum AEDataFormat sinkRetFormat;
76 bool m_isSuspended; /* sink is in a suspended state - release audio device */
77 bool m_isDirty; /* sink output failed - needs re-init or new device */
79 unsigned int m_uiBufferLen; /* wasapi endpoint buffer size, in frames */
80 double m_avgTimeWaiting; /* time between next buffer of data from SoftAE and driver call for data */
81 double m_sinkLatency; /* time in seconds of total duration of the two WASAPI buffers */
82 unsigned int m_lastWriteToBuffer;