blob: 79fe29d9d569a1056d59131d5876e2c90b96d499 [file] [log] [blame]
sazac58f8c02017-07-19 07:39:191/*
2 * Copyright (c) 2017 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#ifndef AUDIO_TIME_INTERVAL_H_
12#define AUDIO_TIME_INTERVAL_H_
sazac58f8c02017-07-19 07:39:1913
14#include <stdint.h>
15
Danil Chapovalovb9b146c2018-06-15 10:28:0716#include "absl/types/optional.h"
sazac58f8c02017-07-19 07:39:1917
18namespace webrtc {
19
20// This class logs the first and last time its Extend() function is called.
21//
22// This class is not thread-safe; Extend() calls should only be made by a
23// single thread at a time, such as within a lock or destructor.
24//
25// Example usage:
26// // let x < y < z < u < v
27// rtc::TimeInterval interval;
28// ... // interval.Extend(); // at time x
29// ...
30// interval.Extend(); // at time y
31// ...
32// interval.Extend(); // at time u
33// ...
34// interval.Extend(z); // at time v
35// ...
36// if (!interval.Empty()) {
37// int64_t active_time = interval.Length(); // returns (u - x)
38// }
39class TimeInterval {
40 public:
41 TimeInterval();
42 ~TimeInterval();
43 // Extend the interval with the current time.
44 void Extend();
45 // Extend the interval with a given time.
46 void Extend(int64_t time);
47 // Take the convex hull with another interval.
48 void Extend(const TimeInterval& other_interval);
49 // True iff Extend has never been called.
50 bool Empty() const;
51 // Returns the time between the first and the last tick, in milliseconds.
52 int64_t Length() const;
53
54 private:
55 struct Interval {
56 Interval(int64_t first, int64_t last);
57
58 int64_t first, last;
59 };
Danil Chapovalovb9b146c2018-06-15 10:28:0760 absl::optional<Interval> interval_;
sazac58f8c02017-07-19 07:39:1961};
62
63} // namespace webrtc
64
Mirko Bonadei92ea95e2017-09-15 04:47:3165#endif // AUDIO_TIME_INTERVAL_H_