blob: 14b16cd60ccd62850d7c0505176b771a11bdbb45 [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
niklase@google.com470e71d2011-07-07 08:21:25 +000011#include <assert.h>
pbos@webrtc.orga4407322013-07-16 12:32:05 +000012#include <stdlib.h>
pbos1968d3f2015-09-28 08:52:18 -070013
Henrik Kjellanderff761fb2015-11-04 08:31:52 +010014#include "webrtc/modules/include/module_common_types.h"
Henrik Kjellander2557b862015-11-18 22:00:21 +010015#include "webrtc/modules/video_coding/timestamp_map.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),
23 next_pop_idx_(0) {
24}
25
26VCMTimestampMap::~VCMTimestampMap() {
27}
28
29void VCMTimestampMap::Add(uint32_t timestamp, VCMFrameInformation* data) {
30 ring_buffer_[next_add_idx_].timestamp = timestamp;
31 ring_buffer_[next_add_idx_].data = data;
32 next_add_idx_ = (next_add_idx_ + 1) % capacity_;
33
34 if (next_add_idx_ == next_pop_idx_) {
35 // Circular list full; forget oldest entry.
36 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
37 }
38}
39
40VCMFrameInformation* VCMTimestampMap::Pop(uint32_t timestamp) {
41 while (!IsEmpty()) {
42 if (ring_buffer_[next_pop_idx_].timestamp == timestamp) {
43 // Found start time for this timestamp.
44 VCMFrameInformation* data = ring_buffer_[next_pop_idx_].data;
45 ring_buffer_[next_pop_idx_].data = nullptr;
46 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
47 return data;
48 } else if (IsNewerTimestamp(ring_buffer_[next_pop_idx_].timestamp,
49 timestamp)) {
50 // The timestamp we are looking for is not in the list.
51 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000052 }
53
pbos1968d3f2015-09-28 08:52:18 -070054 // Not in this position, check next (and forget this position).
55 next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
56 }
57
58 // Could not find matching timestamp in list.
59 return nullptr;
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
pbos1968d3f2015-09-28 08:52:18 -070062bool VCMTimestampMap::IsEmpty() const {
63 return (next_add_idx_ == next_pop_idx_);
niklase@google.com470e71d2011-07-07 08:21:25 +000064}
niklase@google.com470e71d2011-07-07 08:21:25 +000065}