niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 11 | #include "modules/video_coding/timestamp_map.h" |
| 12 | |
pbos@webrtc.org | a440732 | 2013-07-16 12:32:05 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 14 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include "modules/include/module_common_types_public.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 19 | VCMTimestampMap::VCMTimestampMap(size_t capacity) |
| 20 | : ring_buffer_(new TimestampDataTuple[capacity]), |
| 21 | capacity_(capacity), |
| 22 | next_add_idx_(0), |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 23 | next_pop_idx_(0) {} |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 24 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 25 | VCMTimestampMap::~VCMTimestampMap() {} |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 26 | |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 27 | void VCMTimestampMap::Add(uint32_t timestamp, const VCMFrameInformation& data) { |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 28 | ring_buffer_[next_add_idx_].timestamp = timestamp; |
| 29 | ring_buffer_[next_add_idx_].data = data; |
| 30 | next_add_idx_ = (next_add_idx_ + 1) % capacity_; |
| 31 | |
| 32 | if (next_add_idx_ == next_pop_idx_) { |
| 33 | // Circular list full; forget oldest entry. |
| 34 | next_pop_idx_ = (next_pop_idx_ + 1) % capacity_; |
| 35 | } |
| 36 | } |
| 37 | |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 38 | absl::optional<VCMFrameInformation> VCMTimestampMap::Pop(uint32_t timestamp) { |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 39 | while (!IsEmpty()) { |
| 40 | if (ring_buffer_[next_pop_idx_].timestamp == timestamp) { |
| 41 | // Found start time for this timestamp. |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 42 | const VCMFrameInformation& data = ring_buffer_[next_pop_idx_].data; |
| 43 | ring_buffer_[next_pop_idx_].timestamp = 0; |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 44 | next_pop_idx_ = (next_pop_idx_ + 1) % capacity_; |
| 45 | return data; |
| 46 | } else if (IsNewerTimestamp(ring_buffer_[next_pop_idx_].timestamp, |
| 47 | timestamp)) { |
| 48 | // The timestamp we are looking for is not in the list. |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 49 | return absl::nullopt; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | } |
| 51 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 52 | // Not in this position, check next (and forget this position). |
| 53 | next_pop_idx_ = (next_pop_idx_ + 1) % capacity_; |
| 54 | } |
| 55 | |
| 56 | // Could not find matching timestamp in list. |
Johannes Kron | b6b782d | 2021-03-03 14:39:44 +0100 | [diff] [blame] | 57 | return absl::nullopt; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | } |
| 59 | |
pbos | 1968d3f | 2015-09-28 08:52:18 -0700 | [diff] [blame] | 60 | bool VCMTimestampMap::IsEmpty() const { |
| 61 | return (next_add_idx_ == next_pop_idx_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | } |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 63 | |
| 64 | size_t VCMTimestampMap::Size() const { |
| 65 | // The maximum number of elements in the list is |capacity_| - 1. The list is |
| 66 | // empty if the add and pop indices are equal. |
| 67 | return next_add_idx_ >= next_pop_idx_ |
| 68 | ? next_add_idx_ - next_pop_idx_ |
| 69 | : next_add_idx_ + capacity_ - next_pop_idx_; |
| 70 | } |
| 71 | |
philipel | 5908c71 | 2015-12-21 08:23:20 -0800 | [diff] [blame] | 72 | } // namespace webrtc |