blob: 4ddb173639e970d35ba879deb8c9b70c5e775f0a [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
pbos@webrtc.orga4407322013-07-16 12:32:05 +000011#include <stdlib.h>
pbos1968d3f2015-09-28 08:52:18 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include "modules/include/module_common_types_public.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/timestamp_map.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16namespace webrtc {
17
pbos1968d3f2015-09-28 08:52:18 -070018VCMTimestampMap::VCMTimestampMap(size_t capacity)
19 : ring_buffer_(new TimestampDataTuple[capacity]),
20 capacity_(capacity),
21 next_add_idx_(0),
philipel5908c712015-12-21 08:23:20 -080022 next_pop_idx_(0) {}
pbos1968d3f2015-09-28 08:52:18 -070023
philipel5908c712015-12-21 08:23:20 -080024VCMTimestampMap::~VCMTimestampMap() {}
pbos1968d3f2015-09-28 08:52:18 -070025
26void VCMTimestampMap::Add(uint32_t timestamp, VCMFrameInformation* data) {
27 ring_buffer_[next_add_idx_].timestamp = timestamp;
28 ring_buffer_[next_add_idx_].data = data;
29 next_add_idx_ = (next_add_idx_ + 1) % capacity_;
30
31 if (next_add_idx_ == next_pop_idx_) {
32 // Circular list full; forget oldest entry.
33 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
34 }
35}
36
37VCMFrameInformation* VCMTimestampMap::Pop(uint32_t timestamp) {
38 while (!IsEmpty()) {
39 if (ring_buffer_[next_pop_idx_].timestamp == timestamp) {
40 // Found start time for this timestamp.
41 VCMFrameInformation* data = ring_buffer_[next_pop_idx_].data;
42 ring_buffer_[next_pop_idx_].data = nullptr;
43 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
44 return data;
45 } else if (IsNewerTimestamp(ring_buffer_[next_pop_idx_].timestamp,
46 timestamp)) {
47 // The timestamp we are looking for is not in the list.
48 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000049 }
50
pbos1968d3f2015-09-28 08:52:18 -070051 // Not in this position, check next (and forget this position).
52 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
53 }
54
55 // Could not find matching timestamp in list.
56 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
pbos1968d3f2015-09-28 08:52:18 -070059bool VCMTimestampMap::IsEmpty() const {
60 return (next_add_idx_ == next_pop_idx_);
niklase@google.com470e71d2011-07-07 08:21:25 +000061}
philipel5908c712015-12-21 08:23:20 -080062} // namespace webrtc