blob: 88b2f7dd4f53563bedbe32e0d524b72f6db8c903 [file] [log] [blame]
sazac58f8c02017-07-19 00:39:19 -07001/*
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 06:47:31 +020011#ifndef AUDIO_TIME_INTERVAL_H_
12#define AUDIO_TIME_INTERVAL_H_
sazac58f8c02017-07-19 00:39:19 -070013
14#include <stdint.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/optional.h"
sazac58f8c02017-07-19 00:39:19 -070017
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 };
60 rtc::Optional<Interval> interval_;
61};
62
63} // namespace webrtc
64
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020065#endif // AUDIO_TIME_INTERVAL_H_