include: Mark imported functions with hidden visibility.
[wine/multimedia.git] / dlls / quartz / systemclock.c
blob005a5c705c09a9e65611ea89c75327cdd8a7fea5
1 /*
2 * Implementation of IReferenceClock
4 * Copyright 2004 Raphael Junqueira
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "quartz_private.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
25 #include <assert.h>
27 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
29 typedef struct SystemClockAdviseEntry SystemClockAdviseEntry;
30 struct SystemClockAdviseEntry {
31 SystemClockAdviseEntry* next;
32 SystemClockAdviseEntry* prev;
34 HANDLE hEvent;
35 REFERENCE_TIME rtBaseTime;
36 REFERENCE_TIME rtIntervalTime;
39 typedef struct SystemClockImpl {
40 const IReferenceClockVtbl *lpVtbl;
41 LONG ref;
43 /** IReferenceClock */
44 HANDLE adviseThread;
45 DWORD adviseThreadId;
46 BOOL adviseThreadActive;
47 REFERENCE_TIME lastRefTime;
48 DWORD lastTimeTickCount;
49 CRITICAL_SECTION safe;
51 SystemClockAdviseEntry* pSingleShotAdvise;
52 SystemClockAdviseEntry* pPeriodicAdvise;
53 } SystemClockImpl;
56 static void QUARTZ_RemoveAviseEntryFromQueue(SystemClockImpl* This, SystemClockAdviseEntry* pEntry) {
57 if (pEntry->prev) pEntry->prev->next = pEntry->next;
58 if (pEntry->next) pEntry->next->prev = pEntry->prev;
59 if (This->pSingleShotAdvise == pEntry) This->pSingleShotAdvise = pEntry->next;
60 if (This->pPeriodicAdvise == pEntry) This->pPeriodicAdvise = pEntry->next;
63 static void QUARTZ_InsertAviseEntryFromQueue(SystemClockImpl* This, SystemClockAdviseEntry* pEntry, SystemClockAdviseEntry** pQueue) {
64 SystemClockAdviseEntry* prev_it = NULL;
65 SystemClockAdviseEntry* it = NULL;
66 REFERENCE_TIME bornTime = pEntry->rtBaseTime + pEntry->rtIntervalTime;
68 for (it = *pQueue; NULL != it && (it->rtBaseTime + it->rtIntervalTime) < bornTime; it = it->next) {
69 prev_it = it;
71 if (NULL == prev_it) {
72 pEntry->prev = NULL;
73 if (NULL != (*pQueue)) pEntry->next = (*pQueue)->next;
74 /*assert( NULL == pEntry->next->prev );*/
75 if (NULL != pEntry->next) pEntry->next->prev = pEntry;
76 (*pQueue) = pEntry;
77 } else {
78 pEntry->prev = prev_it;
79 pEntry->next = prev_it->next;
80 prev_it->next = pEntry;
81 if (NULL != pEntry->next) pEntry->next->prev = pEntry;
85 #define MAX_REFTIME (REFERENCE_TIME)(0x7FFFFFFFFFFFFFFF)
86 #define ADVISE_EXIT (WM_APP + 0)
87 #define ADVISE_REMOVE (WM_APP + 2)
88 #define ADVISE_ADD_SINGLESHOT (WM_APP + 4)
89 #define ADVISE_ADD_PERIODIC (WM_APP + 8)
91 static DWORD WINAPI SystemClockAdviseThread(LPVOID lpParam) {
92 SystemClockImpl* This = (SystemClockImpl*) lpParam;
93 DWORD timeOut = INFINITE;
94 DWORD tmpTimeOut;
95 MSG msg;
96 HRESULT hr;
97 REFERENCE_TIME curTime;
98 SystemClockAdviseEntry* it = NULL;
100 TRACE("(%p): Main Loop\n", This);
102 while (TRUE) {
103 if (timeOut > 0) MsgWaitForMultipleObjects(0, NULL, FALSE, timeOut, QS_POSTMESSAGE|QS_SENDMESSAGE|QS_TIMER);
105 EnterCriticalSection(&This->safe);
106 /*timeOut = IReferenceClock_OnTimerUpdated(This); */
107 hr = IReferenceClock_GetTime((IReferenceClock*) This, &curTime);
108 if (FAILED(hr)) {
109 timeOut = INFINITE;
110 goto outrefresh;
113 /** First SingleShots Advice: sorted list */
114 for (it = This->pSingleShotAdvise; NULL != it && (it->rtBaseTime + it->rtIntervalTime) <= curTime; it = it->next) {
115 /** send event ... */
116 SetEvent((HANDLE) it->hEvent);
117 /** ... and Release it */
118 QUARTZ_RemoveAviseEntryFromQueue(This, it);
119 CoTaskMemFree(it);
121 if (NULL != it) timeOut = (DWORD) ((it->rtBaseTime + it->rtIntervalTime) - curTime) / (REFERENCE_TIME)10000;
123 /** Now Periodics Advice: semi sorted list (sort cannot be used) */
124 for (it = This->pPeriodicAdvise; NULL != it; it = it->next) {
125 if (it->rtBaseTime <= curTime) {
126 DWORD nPeriods = (DWORD) ((curTime - it->rtBaseTime) / it->rtIntervalTime);
127 /** Release the semaphore ... */
128 ReleaseSemaphore((HANDLE) it->hEvent, nPeriods, NULL);
129 /** ... and refresh time */
130 it->rtBaseTime += nPeriods * it->rtIntervalTime;
131 /*assert( it->rtBaseTime + it->rtIntervalTime < curTime );*/
133 tmpTimeOut = (DWORD) ((it->rtBaseTime + it->rtIntervalTime) - curTime) / (REFERENCE_TIME)10000;
134 if (timeOut > tmpTimeOut) timeOut = tmpTimeOut;
137 outrefresh:
138 LeaveCriticalSection(&This->safe);
140 while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
141 /** if hwnd we suppose that is a windows event ... */
142 if (NULL != msg.hwnd) {
143 TranslateMessage(&msg);
144 DispatchMessageA(&msg);
145 } else {
146 switch (msg.message) {
147 case WM_QUIT:
148 case ADVISE_EXIT:
149 goto outofthread;
150 case ADVISE_ADD_SINGLESHOT:
151 case ADVISE_ADD_PERIODIC:
152 /** set timeout to 0 to do a rescan now */
153 timeOut = 0;
154 break;
155 case ADVISE_REMOVE:
156 /** hmmmm what we can do here ... */
157 timeOut = INFINITE;
158 break;
159 default:
160 ERR("Unhandled message %u. Critical Path\n", msg.message);
161 break;
167 outofthread:
168 TRACE("(%p): Exiting\n", This);
169 return 0;
171 /*static DWORD WINAPI SystemClockAdviseThread(LPVOID lpParam) { */
173 static BOOL SystemClockPostMessageToAdviseThread(SystemClockImpl* This, UINT iMsg) {
174 if (FALSE == This->adviseThreadActive) {
175 BOOL res;
176 This->adviseThread = CreateThread(NULL, 0, SystemClockAdviseThread, This, 0, &This->adviseThreadId);
177 if (NULL == This->adviseThread) return FALSE;
178 SetThreadPriority(This->adviseThread, THREAD_PRIORITY_TIME_CRITICAL);
179 This->adviseThreadActive = TRUE;
180 while(1) {
181 res = PostThreadMessageA(This->adviseThreadId, iMsg, 0, 0);
182 /* Let the thread creates its message queue (with MsgWaitForMultipleObjects call) by yielding and retrying */
183 if (!res && (GetLastError() == ERROR_INVALID_THREAD_ID))
184 Sleep(0);
185 else
186 break;
188 return res;
190 return PostThreadMessageA(This->adviseThreadId, iMsg, 0, 0);
193 static ULONG WINAPI SystemClockImpl_AddRef(IReferenceClock* iface) {
194 SystemClockImpl *This = (SystemClockImpl *)iface;
195 ULONG ref = InterlockedIncrement(&This->ref);
197 TRACE("(%p): AddRef from %d\n", This, ref - 1);
199 return ref;
202 static HRESULT WINAPI SystemClockImpl_QueryInterface(IReferenceClock* iface, REFIID riid, void** ppobj) {
203 SystemClockImpl *This = (SystemClockImpl *)iface;
204 TRACE("(%p, %s,%p)\n", This, debugstr_guid(riid), ppobj);
206 if (IsEqualIID (riid, &IID_IUnknown) ||
207 IsEqualIID (riid, &IID_IReferenceClock)) {
208 SystemClockImpl_AddRef(iface);
209 *ppobj = This;
210 return S_OK;
213 WARN("(%p, %s,%p): not found\n", This, debugstr_guid(riid), ppobj);
214 return E_NOINTERFACE;
217 static ULONG WINAPI SystemClockImpl_Release(IReferenceClock* iface) {
218 SystemClockImpl *This = (SystemClockImpl *)iface;
219 ULONG ref = InterlockedDecrement(&This->ref);
220 TRACE("(%p): ReleaseRef to %d\n", This, ref);
221 if (ref == 0) {
222 if (SystemClockPostMessageToAdviseThread(This, ADVISE_EXIT)) {
223 WaitForSingleObject(This->adviseThread, INFINITE);
224 CloseHandle(This->adviseThread);
226 This->safe.DebugInfo->Spare[0] = 0;
227 DeleteCriticalSection(&This->safe);
228 CoTaskMemFree(This);
230 return ref;
233 static HRESULT WINAPI SystemClockImpl_GetTime(IReferenceClock* iface, REFERENCE_TIME* pTime) {
234 SystemClockImpl *This = (SystemClockImpl *)iface;
235 DWORD curTimeTickCount;
236 HRESULT hr = S_OK;
238 TRACE("(%p, %p)\n", This, pTime);
240 if (NULL == pTime) {
241 return E_POINTER;
244 curTimeTickCount = GetTickCount();
246 EnterCriticalSection(&This->safe);
247 if (This->lastTimeTickCount == curTimeTickCount) hr = S_FALSE;
248 This->lastRefTime += (REFERENCE_TIME) (DWORD) (curTimeTickCount - This->lastTimeTickCount) * (REFERENCE_TIME) 10000;
249 This->lastTimeTickCount = curTimeTickCount;
250 *pTime = This->lastRefTime;
251 LeaveCriticalSection(&This->safe);
252 return hr;
255 static HRESULT WINAPI SystemClockImpl_AdviseTime(IReferenceClock* iface, REFERENCE_TIME rtBaseTime, REFERENCE_TIME rtStreamTime, HEVENT hEvent, DWORD_PTR* pdwAdviseCookie) {
256 SystemClockImpl *This = (SystemClockImpl *)iface;
257 SystemClockAdviseEntry* pEntry = NULL;
259 TRACE("(%p, 0x%s, 0x%s, %ld, %p)\n", This, wine_dbgstr_longlong(rtBaseTime),
260 wine_dbgstr_longlong(rtStreamTime), hEvent, pdwAdviseCookie);
262 if ((HEVENT) 0 == hEvent) {
263 return E_INVALIDARG;
265 if (0 >= rtBaseTime + rtStreamTime) {
266 return E_INVALIDARG;
268 if (NULL == pdwAdviseCookie) {
269 return E_POINTER;
271 pEntry = CoTaskMemAlloc(sizeof(SystemClockAdviseEntry));
272 if (NULL == pEntry) {
273 return E_OUTOFMEMORY;
275 ZeroMemory(pEntry, sizeof(SystemClockAdviseEntry));
277 pEntry->hEvent = (HANDLE) hEvent;
278 pEntry->rtBaseTime = rtBaseTime + rtStreamTime;
279 pEntry->rtIntervalTime = 0;
281 EnterCriticalSection(&This->safe);
282 QUARTZ_InsertAviseEntryFromQueue(This, pEntry, &This->pSingleShotAdvise);
283 LeaveCriticalSection(&This->safe);
285 SystemClockPostMessageToAdviseThread(This, ADVISE_ADD_SINGLESHOT);
287 *pdwAdviseCookie = (DWORD_PTR) (pEntry);
288 return S_OK;
291 static HRESULT WINAPI SystemClockImpl_AdvisePeriodic(IReferenceClock* iface, REFERENCE_TIME rtStartTime, REFERENCE_TIME rtPeriodTime, HSEMAPHORE hSemaphore, DWORD_PTR* pdwAdviseCookie) {
292 SystemClockImpl *This = (SystemClockImpl *)iface;
293 SystemClockAdviseEntry* pEntry = NULL;
295 TRACE("(%p, 0x%s, 0x%s, %ld, %p)\n", This, wine_dbgstr_longlong(rtStartTime),
296 wine_dbgstr_longlong(rtPeriodTime), hSemaphore, pdwAdviseCookie);
298 if ((HSEMAPHORE) 0 == hSemaphore) {
299 return E_INVALIDARG;
301 if (0 >= rtStartTime || 0 >= rtPeriodTime) {
302 return E_INVALIDARG;
304 if (NULL == pdwAdviseCookie) {
305 return E_POINTER;
307 pEntry = CoTaskMemAlloc(sizeof(SystemClockAdviseEntry));
308 if (NULL == pEntry) {
309 return E_OUTOFMEMORY;
311 ZeroMemory(pEntry, sizeof(SystemClockAdviseEntry));
313 pEntry->hEvent = (HANDLE) hSemaphore;
314 pEntry->rtBaseTime = rtStartTime;
315 pEntry->rtIntervalTime = rtPeriodTime;
317 EnterCriticalSection(&This->safe);
318 QUARTZ_InsertAviseEntryFromQueue(This, pEntry, &This->pPeriodicAdvise);
319 LeaveCriticalSection(&This->safe);
321 SystemClockPostMessageToAdviseThread(This, ADVISE_ADD_PERIODIC);
323 *pdwAdviseCookie = (DWORD_PTR) (pEntry);
324 return S_OK;
327 static HRESULT WINAPI SystemClockImpl_Unadvise(IReferenceClock* iface, DWORD_PTR dwAdviseCookie) {
328 SystemClockImpl *This = (SystemClockImpl *)iface;
329 SystemClockAdviseEntry* pEntry = NULL;
330 SystemClockAdviseEntry* it = NULL;
331 HRESULT ret = S_OK;
332 TRACE("(%p, %lu)\n", This, dwAdviseCookie);
334 pEntry = (SystemClockAdviseEntry*) dwAdviseCookie;
336 EnterCriticalSection(&This->safe);
337 for (it = This->pPeriodicAdvise; NULL != it && it != pEntry; it = it->next) ;
338 if (it != pEntry) {
339 for (it = This->pSingleShotAdvise; NULL != it && it != pEntry; it = it->next) ;
340 if (it != pEntry) {
341 ret = S_FALSE;
342 goto out;
346 QUARTZ_RemoveAviseEntryFromQueue(This, pEntry);
347 CoTaskMemFree(pEntry);
349 SystemClockPostMessageToAdviseThread(This, ADVISE_REMOVE);
351 out:
352 LeaveCriticalSection(&This->safe);
353 return ret;
356 static const IReferenceClockVtbl SystemClock_Vtbl =
358 SystemClockImpl_QueryInterface,
359 SystemClockImpl_AddRef,
360 SystemClockImpl_Release,
361 SystemClockImpl_GetTime,
362 SystemClockImpl_AdviseTime,
363 SystemClockImpl_AdvisePeriodic,
364 SystemClockImpl_Unadvise
367 HRESULT QUARTZ_CreateSystemClock(IUnknown * pUnkOuter, LPVOID * ppv) {
368 SystemClockImpl* obj = NULL;
370 TRACE("(%p,%p)\n", ppv, pUnkOuter);
372 obj = CoTaskMemAlloc(sizeof(SystemClockImpl));
373 if (NULL == obj) {
374 *ppv = NULL;
375 return E_OUTOFMEMORY;
377 ZeroMemory(obj, sizeof(SystemClockImpl));
379 obj->lpVtbl = &SystemClock_Vtbl;
380 obj->ref = 0; /* will be inited by QueryInterface */
382 obj->lastTimeTickCount = GetTickCount();
383 InitializeCriticalSection(&obj->safe);
384 obj->safe.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": SystemClockImpl.safe");
386 return SystemClockImpl_QueryInterface((IReferenceClock*) obj, &IID_IReferenceClock, ppv);