1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
29d9ad88ba51682d91ed3dade6d711fb26c9073a2phoglund@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
1164c0366908f7a966cbb28a4b07a810f2597a888aPeter Boström#include "webrtc/system_wrappers/source/event_timer_win.h"
12470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
13470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include "Mmsystem.h"
14470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
15470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.comnamespace webrtc {
165bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org
1764c0366908f7a966cbb28a4b07a810f2597a888aPeter Boström// static
1864c0366908f7a966cbb28a4b07a810f2597a888aPeter BoströmEventTimerWrapper* EventTimerWrapper::Create() {
1964c0366908f7a966cbb28a4b07a810f2597a888aPeter Boström  return new EventTimerWin();
2064c0366908f7a966cbb28a4b07a810f2597a888aPeter Boström}
2164c0366908f7a966cbb28a4b07a810f2597a888aPeter Boström
2264c0366908f7a966cbb28a4b07a810f2597a888aPeter BoströmEventTimerWin::EventTimerWin()
235bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    : event_(::CreateEvent(NULL,    // security attributes
245bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org                           FALSE,   // manual reset
255bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org                           FALSE,   // initial state
265bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org                           NULL)),  // name of event
275bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    timerID_(NULL) {
28470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
29470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
3064c0366908f7a966cbb28a4b07a810f2597a888aPeter BoströmEventTimerWin::~EventTimerWin() {
31c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org  StopTimer();
325bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  CloseHandle(event_);
33470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
34470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
3564c0366908f7a966cbb28a4b07a810f2597a888aPeter Boströmbool EventTimerWin::Set() {
365bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  // Note: setting an event that is already set has no effect.
37c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org  return SetEvent(event_) == 1;
38470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
39470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
4064c0366908f7a966cbb28a4b07a810f2597a888aPeter BoströmEventTypeWrapper EventTimerWin::Wait(unsigned long max_time) {
415bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  unsigned long res = WaitForSingleObject(event_, max_time);
425bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  switch (res) {
43470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    case WAIT_OBJECT_0:
445bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org      return kEventSignaled;
45470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    case WAIT_TIMEOUT:
465bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org      return kEventTimeout;
47470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    default:
485bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org      return kEventError;
495bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  }
50470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
51470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
5264c0366908f7a966cbb28a4b07a810f2597a888aPeter Boströmbool EventTimerWin::StartTimer(bool periodic, unsigned long time) {
535bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  if (timerID_ != NULL) {
545bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    timeKillEvent(timerID_);
555bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    timerID_ = NULL;
565bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  }
57c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org
585bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  if (periodic) {
595bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
605bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org                            TIME_PERIODIC | TIME_CALLBACK_EVENT_PULSE);
615bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  } else {
625bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org    timerID_ = timeSetEvent(time, 0, (LPTIMECALLBACK)HANDLE(event_), 0,
635bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org                            TIME_ONESHOT | TIME_CALLBACK_EVENT_SET);
645bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  }
65470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
66c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org  return timerID_ != NULL;
67470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
68470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
6964c0366908f7a966cbb28a4b07a810f2597a888aPeter Boströmbool EventTimerWin::StopTimer() {
70c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org  if (timerID_ != NULL) {
71c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org    timeKillEvent(timerID_);
72c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org    timerID_ = NULL;
73c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org  }
74c0167702d3db7acb76933db4a3deda281705518apbos@webrtc.org
755bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org  return true;
76470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
775bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org
785bbe069f28417fb37e193147f01778ca9968d4a5phoglund@webrtc.org}  // namespace webrtc
79