1/*
2 *  Copyright (c) 2013 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#ifndef WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_LOW_LATENCY_EVENT_H_
12#define WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_LOW_LATENCY_EVENT_H_
13
14#include <errno.h>
15#include <limits.h>
16#include <fcntl.h>
17#include <stdio.h>
18#include <sys/ioctl.h>
19#include <sys/socket.h>
20#include <sys/types.h>
21
22namespace webrtc {
23
24// Implementation of event for single waiter, single signal threads. Event
25// is sticky.
26class LowLatencyEvent {
27 public:
28  LowLatencyEvent();
29  ~LowLatencyEvent();
30
31  // Readies the event. Must be called before signaling or waiting for event.
32  // Returns true on success.
33  bool Start();
34  // Shuts down the event and releases threads calling WaitOnEvent. Once
35  // stopped SignalEvent and WaitOnEvent will have no effect. Start can be
36  // called to re-enable the event.
37  // Returns true on success.
38  bool Stop();
39
40  // Releases thread calling WaitOnEvent in a sticky fashion.
41  void SignalEvent(int event_id, int event_msg);
42  // Waits until SignalEvent or Stop is called.
43  void WaitOnEvent(int* event_id, int* event_msg);
44
45 private:
46  typedef int Handle;
47  static const Handle kInvalidHandle;
48  static const int kReadHandle;
49  static const int kWriteHandle;
50
51  // Closes the handle. Returns true on success.
52  static bool Close(Handle* handle);
53
54  // SignalEvent and WaitOnEvent are actually read/write to file descriptors.
55  // Write is signal.
56  void WriteFd(int message_id, int message);
57  // Read is wait.
58  void ReadFd(int* message_id, int* message);
59
60  Handle handles_[2];
61};
62
63}  // namespace webrtc
64
65#endif  // WEBRTC_MODULES_AUDIO_DEVICE_ANDROID_LOW_LATENCY_EVENT_H_
66