saza | c58f8c0 | 2017-07-19 07:39:19 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef AUDIO_TIME_INTERVAL_H_ |
| 12 | #define AUDIO_TIME_INTERVAL_H_ |
saza | c58f8c0 | 2017-07-19 07:39:19 | [diff] [blame] | 13 | |
| 14 | #include <stdint.h> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "api/optional.h" |
saza | c58f8c0 | 2017-07-19 07:39:19 | [diff] [blame] | 17 | |
| 18 | namespace 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 | // } |
| 39 | class 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 Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 65 | #endif // AUDIO_TIME_INTERVAL_H_ |