blob: 4c586988df65af8dff43721eee9aaae06104b776 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
phoglund@webrtc.org9d9ad882012-02-07 16:16:52 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Peter Boström64c03662015-04-08 11:24:19 +020011#include "webrtc/system_wrappers/source/event_timer_win.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
13#include "Mmsystem.h"
14
15namespace webrtc {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000016
Peter Boström64c03662015-04-08 11:24:19 +020017// static
18EventTimerWrapper* EventTimerWrapper::Create() {
19 return new EventTimerWin();
20}
21
22EventTimerWin::EventTimerWin()
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000023 : event_(::CreateEvent(NULL, // security attributes
24 FALSE, // manual reset
25 FALSE, // initial state
26 NULL)), // name of event
27 timerID_(NULL) {
niklase@google.com470e71d2011-07-07 08:21:25 +000028}
29
Peter Boström64c03662015-04-08 11:24:19 +020030EventTimerWin::~EventTimerWin() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000031 StopTimer();
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000032 CloseHandle(event_);
niklase@google.com470e71d2011-07-07 08:21:25 +000033}
34
Peter Boström64c03662015-04-08 11:24:19 +020035bool EventTimerWin::Set() {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000036 // Note: setting an event that is already set has no effect.
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000037 return SetEvent(event_) == 1;
niklase@google.com470e71d2011-07-07 08:21:25 +000038}
39
Peter Boström64c03662015-04-08 11:24:19 +020040EventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000041 unsigned long res = WaitForSingleObject(event_, max_time);
42 switch (res) {
niklase@google.com470e71d2011-07-07 08:21:25 +000043 case WAIT_OBJECT_0:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000044 return kEventSignaled;
niklase@google.com470e71d2011-07-07 08:21:25 +000045 case WAIT_TIMEOUT:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000046 return kEventTimeout;
niklase@google.com470e71d2011-07-07 08:21:25 +000047 default:
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000048 return kEventError;
49 }
niklase@google.com470e71d2011-07-07 08:21:25 +000050}
51
Peter Boström64c03662015-04-08 11:24:19 +020052bool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000053 if (timerID_ != NULL) {
54 timeKillEvent(timerID_);
55 timerID_ = NULL;
56 }
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000057
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000058 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 }
niklase@google.com470e71d2011-07-07 08:21:25 +000065
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000066 return timerID_ != NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +000067}
68
Peter Boström64c03662015-04-08 11:24:19 +020069bool EventTimerWin::StopTimer() {
pbos@webrtc.orgc0167702013-10-02 13:11:15 +000070 if (timerID_ != NULL) {
71 timeKillEvent(timerID_);
72 timerID_ = NULL;
73 }
74
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000075 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +000076}
phoglund@webrtc.org5bbe0692012-12-10 10:44:37 +000077
78} // namespace webrtc