1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/system_wrappers/source/event_timer_win.h"
12
13 #include "Mmsystem.h"
14
15 namespace webrtc {
16
17 // static
Create()18 EventTimerWrapper* EventTimerWrapper::Create() {
19 return new EventTimerWin();
20 }
21
EventTimerWin()22 EventTimerWin::EventTimerWin()
23 : event_(::CreateEvent(NULL, // security attributes
24 FALSE, // manual reset
25 FALSE, // initial state
26 NULL)), // name of event
27 timerID_(NULL) {
28 }
29
~EventTimerWin()30 EventTimerWin::~EventTimerWin() {
31 StopTimer();
32 CloseHandle(event_);
33 }
34
Set()35 bool EventTimerWin::Set() {
36 // Note: setting an event that is already set has no effect.
37 return SetEvent(event_) == 1;
38 }
39
Wait(unsigned long max_time)40 EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
41 unsigned long res = WaitForSingleObject(event_, max_time);
42 switch (res) {
43 case WAIT_OBJECT_0:
44 return kEventSignaled;
45 case WAIT_TIMEOUT:
46 return kEventTimeout;
47 default:
48 return kEventError;
49 }
50 }
51
StartTimer(bool periodic,unsigned long time)52 bool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
53 if (timerID_ != NULL) {
54 timeKillEvent(timerID_);
55 timerID_ = NULL;
56 }
57
58 if (periodic) {
59 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
60 TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
61 } else {
62 timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
63 TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
64 }
65
66 return timerID_ != NULL;
67 }
68
StopTimer()69 bool EventTimerWin::StopTimer() {
70 if (timerID_ != NULL) {
71 timeKillEvent(timerID_);
72 timerID_ = NULL;
73 }
74
75 return true;
76 }
77
78 } // namespace webrtc
79