forked from alexmarsev/sanear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyClock.cpp
217 lines (165 loc) · 5.97 KB
/
MyClock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "pch.h"
#include "MyClock.h"
#include "AudioRenderer.h"
namespace SaneAudioRenderer
{
MyClock::MyClock(IUnknown* pUnknown, const std::unique_ptr<AudioRenderer>& renderer, HRESULT& result)
: CBaseReferenceClock(L"SaneAudioRenderer::MyClock", pUnknown, &result)
, m_renderer(renderer)
, m_performanceFrequency(GetPerformanceFrequency())
{
}
STDMETHODIMP MyClock::NonDelegatingQueryInterface(REFIID riid, void** ppv)
{
if (riid == __uuidof(IGuidedReclock))
return GetInterface(static_cast<IGuidedReclock*>(this), ppv);
return CBaseReferenceClock::NonDelegatingQueryInterface(riid, ppv);
}
REFERENCE_TIME MyClock::GetPrivateTime()
{
CAutoLock lock(this);
#ifndef NDEBUG
const int64_t oldCounterOffset = m_counterOffset;
#endif
if (m_guidedReclockSlaving && !CanDoGuidedReclock())
UnslaveClock();
REFERENCE_TIME audioClockTime, counterTime, clockTime;
if (m_guidedReclockSlaving)
{
auto getGuidedReclockTime = [this](int64_t counterTime)
{
int64_t progress = (int64_t)((counterTime - m_guidedReclockStartTime) * m_guidedReclockMultiplier);
return m_guidedReclockStartClock + progress;
};
if (SUCCEEDED(GetAudioClockTime(&audioClockTime, &counterTime)))
{
clockTime = getGuidedReclockTime(counterTime);
int64_t diff = clockTime - audioClockTime;
m_audioOffset += diff;
m_renderer->TakeGuidedReclock(diff);
}
else
{
counterTime = GetCounterTime();
clockTime = getGuidedReclockTime(counterTime);
}
m_counterOffset = clockTime - counterTime;
}
else if (SUCCEEDED(GetAudioClockTime(&audioClockTime, &counterTime)))
{
clockTime = audioClockTime;
m_counterOffset = audioClockTime - counterTime;
}
else
{
clockTime = m_counterOffset + GetCounterTime();
}
#ifndef NDEBUG
int64_t counterOffsetDiff = m_counterOffset - oldCounterOffset;
if (std::abs(counterOffsetDiff) > OneMillisecond / 2)
DebugOut(ClassName(this), "observed clock warp of", counterOffsetDiff / 10000., "ms");
#endif
return clockTime;
}
void MyClock::SlaveClockToAudio(IAudioClock* pAudioClock, int64_t audioStart)
{
assert(pAudioClock);
CAutoLock lock(this);
DebugOut(ClassName(this), "slave clock to audio device (delayed until it progresses)");
m_audioClock = pAudioClock;
m_audioStart = audioStart;
m_audioInitialPosition = 0;
m_audioClock->GetPosition(&m_audioInitialPosition, nullptr);
m_audioOffset = 0;
}
void MyClock::UnslaveClockFromAudio()
{
CAutoLock lock(this);
DebugOut(ClassName(this), "unslave clock from audio device");
m_audioClock = nullptr;
}
void MyClock::OffsetAudioClock(REFERENCE_TIME offsetTime)
{
CAutoLock lock(this);
m_audioOffset += offsetTime;
}
HRESULT MyClock::GetAudioClockTime(REFERENCE_TIME* pAudioTime, REFERENCE_TIME* pCounterTime)
{
CheckPointer(pAudioTime, E_POINTER);
CAutoLock lock(this);
if (m_audioClock)
{
uint64_t audioFrequency, audioPosition, audioTime;
if (SUCCEEDED(m_audioClock->GetFrequency(&audioFrequency)) &&
SUCCEEDED(m_audioClock->GetPosition(&audioPosition, &audioTime)) &&
audioPosition > m_audioInitialPosition)
{
int64_t counterTime = GetCounterTime();
int64_t clockTime = llMulDiv(audioPosition, OneSecond, audioFrequency, 0) +
m_audioStart + m_audioOffset + counterTime - audioTime;
*pAudioTime = clockTime;
if (pCounterTime)
*pCounterTime = counterTime;
return S_OK;
}
return E_FAIL;
}
return E_FAIL;
}
HRESULT MyClock::GetAudioClockStartTime(REFERENCE_TIME* pStartTime)
{
CheckPointer(pStartTime, E_POINTER);
CAutoLock lock(this);
if (m_audioClock)
{
*pStartTime = m_audioStart;
return S_OK;
}
return E_FAIL;
}
STDMETHODIMP MyClock::SlaveClock(DOUBLE multiplier)
{
CAutoLock lock(this);
if (!CanDoGuidedReclock())
return E_FAIL;
int64_t time;
ReturnIfFailed(GetTime(&time));
m_guidedReclockSlaving = true;
m_guidedReclockMultiplier = multiplier;
m_guidedReclockStartTime = GetCounterTime();
m_guidedReclockStartClock = time;
return S_OK;
}
STDMETHODIMP MyClock::UnslaveClock()
{
CAutoLock lock(this);
if (!m_guidedReclockSlaving)
return S_FALSE;
GetPrivateTime();
m_guidedReclockSlaving = false;
return S_OK;
}
STDMETHODIMP MyClock::OffsetClock(LONGLONG offset)
{
CAutoLock lock(this);
if (!CanDoGuidedReclock())
return E_FAIL;
m_audioOffset += offset;
m_counterOffset += offset;
m_guidedReclockStartClock += offset;
m_renderer->TakeGuidedReclock(offset);
return S_OK;
}
STDMETHODIMP MyClock::GetImmediateTime(LONGLONG* pTime)
{
CheckPointer(pTime, E_POINTER);
*pTime = GetPrivateTime();
return S_OK;
}
bool MyClock::CanDoGuidedReclock()
{
return !m_renderer->IsBitstreaming() &&
!m_renderer->OnExternalClock() &&
!m_renderer->IsLive();
}
}