[WASAPI] fixed: use the more accurate GetDelay() for Drain().
[vuplus_xbmc] / xbmc / cores / AudioEngine / Sinks / AESinkWASAPI.h
1 #pragma once
2 /*
3 *      Copyright (C) 2010-2013 Team XBMC
4 *      http://xbmc.org
5 *
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)
9 *  any later version.
10 *
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.
15 *
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/>.
19 *
20 */
21
22 #include "../Interfaces/AESink.h"
23 #include <stdint.h>
24 #include <mmdeviceapi.h>
25 #include <Audioclient.h>
26 #include "../Utils/AEDeviceInfo.h"
27
28 #include "threads/CriticalSection.h"
29
30 class CAESinkWASAPI : public IAESink
31 {
32 public:
33     virtual const char *GetName() { return "WASAPI"; }
34
35     CAESinkWASAPI();
36     virtual ~CAESinkWASAPI();
37
38     virtual bool Initialize  (AEAudioFormat &format, std::string &device);
39     virtual void Deinitialize();
40     virtual bool IsCompatible(const AEAudioFormat &format, const std::string &device);
41
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);
50 private:
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);
56
57     static const char  *WASAPIErrToStr(HRESULT err);
58
59     HANDLE              m_needDataEvent;
60     IMMDevice          *m_pDevice;
61     IAudioClient       *m_pAudioClient;
62     IAudioRenderClient *m_pRenderClient;
63
64     AEAudioFormat       m_format;
65     enum AEDataFormat   m_encodedFormat;
66     unsigned int        m_encodedChannels;
67     unsigned int        m_encodedSampleRate;
68     CAEChannelInfo      m_channelLayout;
69     std::string         m_device;
70
71     enum AEDataFormat   sinkReqFormat;
72     enum AEDataFormat   sinkRetFormat;
73
74     bool                m_running;
75     bool                m_initialized;
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 */
78
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;
83
84     uint8_t            *m_pBuffer;
85     int                 m_bufferPtr;
86 };