blob: d93293704df3290c6b7c9f9dd50a718aec98f5e7 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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 Olssona4d87372019-07-05 19:08:33 +020011#include "modules/video_coding/timestamp_map.h"
12
pbos@webrtc.orga4407322013-07-16 12:32:05 +000013#include <stdlib.h>
pbos1968d3f2015-09-28 08:52:18 -070014
Yves Gerey3e707812018-11-28 16:47:49 +010015#include "modules/include/module_common_types_public.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
17namespace webrtc {
18
pbos1968d3f2015-09-28 08:52:18 -070019VCMTimestampMap::VCMTimestampMap(size_t capacity)
20 : ring_buffer_(new TimestampDataTuple[capacity]),
21 capacity_(capacity),
22 next_add_idx_(0),
philipel5908c712015-12-21 08:23:20 -080023 next_pop_idx_(0) {}
pbos1968d3f2015-09-28 08:52:18 -070024
philipel5908c712015-12-21 08:23:20 -080025VCMTimestampMap::~VCMTimestampMap() {}
pbos1968d3f2015-09-28 08:52:18 -070026
27void VCMTimestampMap::Add(uint32_t timestamp, VCMFrameInformation* data) {
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
38VCMFrameInformation* VCMTimestampMap::Pop(uint32_t timestamp) {
39 while (!IsEmpty()) {
40 if (ring_buffer_[next_pop_idx_].timestamp == timestamp) {
41 // Found start time for this timestamp.
42 VCMFrameInformation* data = ring_buffer_[next_pop_idx_].data;
43 ring_buffer_[next_pop_idx_].data = nullptr;
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.
49 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000050 }
51
pbos1968d3f2015-09-28 08:52:18 -070052 // 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.
57 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000058}
59
pbos1968d3f2015-09-28 08:52:18 -070060bool VCMTimestampMap::IsEmpty() const {
61 return (next_add_idx_ == next_pop_idx_);
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
philipel5908c712015-12-21 08:23:20 -080063} // namespace webrtc