referenceclock.cpp
9.0 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* akvirtualcamera, virtual camera for Mac and Windows.
* Copyright (C) 2020 Gonzalo Exequiel Pedone
*
* akvirtualcamera is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* akvirtualcamera is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with akvirtualcamera. If not, see <http://www.gnu.org/licenses/>.
*
* Web-Site: http://webcamoid.github.io/
*/
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <limits>
#include <mutex>
#include <thread>
#include <vector>
#include "referenceclock.h"
#include "PlatformUtils/src/utils.h"
#include "VCamUtils/src/utils.h"
namespace AkVCam
{
class AdviseCookiePrivate
{
public:
IReferenceClock *m_clock;
std::thread m_thread;
std::mutex m_mutex;
std::condition_variable_any m_timeout;
std::atomic<bool> m_run {false};
explicit AdviseCookiePrivate(IReferenceClock *clock);
void adviseTime(REFERENCE_TIME baseTime,
REFERENCE_TIME streamTime,
HEVENT hEvent);
void adviseTimeTh(REFERENCE_TIME baseTime,
REFERENCE_TIME streamTime,
HEVENT hEvent);
void advisePeriodic(REFERENCE_TIME startTime,
REFERENCE_TIME periodTime,
HSEMAPHORE hSemaphore);
void advisePeriodicTh(REFERENCE_TIME startTime,
REFERENCE_TIME periodTime,
HSEMAPHORE hSemaphore);
void unadvise();
};
class ReferenceClockPrivate
{
public:
ReferenceClock *self;
std::vector<DWORD_PTR> m_cookies;
REFERENCE_TIME m_lastTime {0};
explicit ReferenceClockPrivate(ReferenceClock *self);
void cleanup();
};
}
AkVCam::ReferenceClock::ReferenceClock():
CUnknown(this, IID_IReferenceClock)
{
this->d = new ReferenceClockPrivate(this);
}
AkVCam::ReferenceClock::~ReferenceClock()
{
for (auto &cookie: this->d->m_cookies) {
auto adviseCookie = reinterpret_cast<AdviseCookiePrivate *>(cookie);
adviseCookie->unadvise();
delete adviseCookie;
}
delete this->d;
}
HRESULT AkVCam::ReferenceClock::GetTime(REFERENCE_TIME *pTime)
{
AkLogFunction();
if (!pTime)
return E_POINTER;
*pTime = REFERENCE_TIME(TIME_BASE * timeGetTime() / 1e3);
if (*pTime <= this->d->m_lastTime)
return S_FALSE;
this->d->m_lastTime = *pTime;
return S_OK;
}
HRESULT AkVCam::ReferenceClock::AdviseTime(REFERENCE_TIME baseTime,
REFERENCE_TIME streamTime,
HEVENT hEvent,
DWORD_PTR *pdwAdviseCookie)
{
AkLogFunction();
this->d->cleanup();
if (!pdwAdviseCookie)
return E_POINTER;
*pdwAdviseCookie = 0;
const REFERENCE_TIME time = baseTime + streamTime;
if (time <= 0 || time == (std::numeric_limits<LONGLONG>::max)())
return E_INVALIDARG;
auto adviseCookie = new AdviseCookiePrivate(this);
*pdwAdviseCookie = DWORD_PTR(adviseCookie);
this->d->m_cookies.push_back(*pdwAdviseCookie);
adviseCookie->adviseTime(baseTime, streamTime, hEvent);
return S_OK;
}
HRESULT AkVCam::ReferenceClock::AdvisePeriodic(REFERENCE_TIME startTime,
REFERENCE_TIME periodTime,
HSEMAPHORE hSemaphore,
DWORD_PTR *pdwAdviseCookie)
{
AkLogFunction();
this->d->cleanup();
if (!pdwAdviseCookie)
return E_POINTER;
*pdwAdviseCookie = 0;
if (startTime <= 0
|| periodTime <= 0
|| startTime == (std::numeric_limits<LONGLONG>::max)())
return E_INVALIDARG;
auto adviseCookie = new AdviseCookiePrivate(this);
adviseCookie->advisePeriodic(startTime, periodTime, hSemaphore);
*pdwAdviseCookie = DWORD_PTR(adviseCookie);
this->d->m_cookies.push_back(*pdwAdviseCookie);
return S_OK;
}
HRESULT AkVCam::ReferenceClock::Unadvise(DWORD_PTR dwAdviseCookie)
{
AkLogFunction();
auto it = std::find(this->d->m_cookies.begin(),
this->d->m_cookies.end(),
dwAdviseCookie);
if (it == this->d->m_cookies.end())
return S_FALSE;
auto adviseCookie = reinterpret_cast<AdviseCookiePrivate *>(*it);
adviseCookie->unadvise();
delete adviseCookie;
this->d->m_cookies.erase(it);
this->d->cleanup();
return S_OK;
}
AkVCam::AdviseCookiePrivate::AdviseCookiePrivate(IReferenceClock *clock):
m_clock(clock)
{
}
void AkVCam::AdviseCookiePrivate::adviseTime(REFERENCE_TIME baseTime,
REFERENCE_TIME streamTime,
HEVENT hEvent)
{
AkLogFunction();
this->m_run = true;
this->m_thread = std::thread(&AdviseCookiePrivate::adviseTimeTh,
this,
baseTime,
streamTime,
hEvent);
AkLogInfo() << "Launching thread " << this->m_thread.get_id() << std::endl;
}
void AkVCam::AdviseCookiePrivate::adviseTimeTh(REFERENCE_TIME baseTime,
REFERENCE_TIME streamTime,
HEVENT hEvent)
{
AkLogFunction();
REFERENCE_TIME clockTime;
this->m_clock->GetTime(&clockTime);
auto startSleep =
REFERENCE_TIME(1e3
* double(baseTime + streamTime - clockTime)
/ TIME_BASE);
if (startSleep > 0) {
std::chrono::milliseconds start(startSleep);
this->m_mutex.lock();
this->m_timeout.wait_for(this->m_mutex, start);
this->m_mutex.unlock();
}
if (this->m_run)
SetEvent(HANDLE(hEvent));
this->m_run = false;
AkLogInfo() << "Thread "
<< std::this_thread::get_id()
<< " finnished"
<< std::endl;
}
void AkVCam::AdviseCookiePrivate::advisePeriodic(REFERENCE_TIME startTime,
REFERENCE_TIME periodTime,
HSEMAPHORE hSemaphore)
{
AkLogFunction();
this->m_run = true;
this->m_thread = std::thread(&AdviseCookiePrivate::advisePeriodicTh,
this,
startTime,
periodTime,
hSemaphore);
AkLogInfo() << "Launching thread " << this->m_thread.get_id() << std::endl;
}
void AkVCam::AdviseCookiePrivate::advisePeriodicTh(REFERENCE_TIME startTime,
REFERENCE_TIME periodTime,
HSEMAPHORE hSemaphore)
{
AkLogFunction();
REFERENCE_TIME clockTime;
this->m_clock->GetTime(&clockTime);
auto startSleep =
REFERENCE_TIME(1e3
* double(startTime - clockTime)
/ TIME_BASE);
if (startSleep > 0) {
std::chrono::milliseconds start(startSleep);
this->m_mutex.lock();
this->m_timeout.wait_for(this->m_mutex, start);
this->m_mutex.unlock();
}
auto periodSleep = REFERENCE_TIME(1e3 * double(periodTime) / TIME_BASE);
std::chrono::milliseconds period(periodSleep);
while (this->m_run) {
ReleaseSemaphore(HANDLE(hSemaphore), 1, nullptr);
this->m_mutex.lock();
this->m_timeout.wait_for(this->m_mutex, period);
this->m_mutex.unlock();
}
AkLogInfo() << "Thread "
<< std::this_thread::get_id()
<< " finnished"
<< std::endl;
}
void AkVCam::AdviseCookiePrivate::unadvise()
{
AkLogFunction();
this->m_run = false;
this->m_mutex.lock();
this->m_timeout.notify_one();
this->m_mutex.unlock();
this->m_thread.join();
}
AkVCam::ReferenceClockPrivate::ReferenceClockPrivate(ReferenceClock *self):
self(self)
{
}
void AkVCam::ReferenceClockPrivate::cleanup()
{
std::vector<DWORD_PTR> cookies;
for (auto &cookie: this->m_cookies) {
auto adviseCookie = reinterpret_cast<AdviseCookiePrivate *>(cookie);
if (!adviseCookie->m_run)
cookies.push_back(cookie);
}
for (auto &cookie: cookies)
this->self->Unadvise(cookie);
}