1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
2470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
11470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <assert.h>
12a4407329d4e16eab3d2e87cde0824188c06acb5apbos@webrtc.org#include <stdlib.h>
131968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos
14ff761fba8274d93bd73e76c8b8a1f2d0776dd840Henrik Kjellander#include "webrtc/modules/include/module_common_types.h"
152557b86e7648ffebc5781df9f093ca5a84efc219Henrik Kjellander#include "webrtc/modules/video_coding/timestamp_map.h"
16470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
17470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.comnamespace webrtc {
18470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
191968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbosVCMTimestampMap::VCMTimestampMap(size_t capacity)
201968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    : ring_buffer_(new TimestampDataTuple[capacity]),
211968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      capacity_(capacity),
221968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      next_add_idx_(0),
235908c71128aea207e42f86468aedb0a6fce3cccbphilipel      next_pop_idx_(0) {}
24470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
255908c71128aea207e42f86468aedb0a6fce3cccbphilipelVCMTimestampMap::~VCMTimestampMap() {}
26470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
271968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbosvoid VCMTimestampMap::Add(uint32_t timestamp, VCMFrameInformation* data) {
281968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  ring_buffer_[next_add_idx_].timestamp = timestamp;
291968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  ring_buffer_[next_add_idx_].data = data;
301968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  next_add_idx_ = (next_add_idx_ + 1) % capacity_;
31470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
321968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  if (next_add_idx_ == next_pop_idx_) {
331968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    // Circular list full; forget oldest entry.
341968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
351968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  }
36470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
37470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
381968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbosVCMFrameInformation* VCMTimestampMap::Pop(uint32_t timestamp) {
391968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  while (!IsEmpty()) {
401968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    if (ring_buffer_[next_pop_idx_].timestamp == timestamp) {
411968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      // Found start time for this timestamp.
421968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      VCMFrameInformation* data = ring_buffer_[next_pop_idx_].data;
431968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      ring_buffer_[next_pop_idx_].data = nullptr;
441968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
451968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      return data;
461968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    } else if (IsNewerTimestamp(ring_buffer_[next_pop_idx_].timestamp,
471968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos                                timestamp)) {
481968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      // The timestamp we are looking for is not in the list.
491968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos      return nullptr;
50470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
51470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
521968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    // Not in this position, check next (and forget this position).
531968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos    next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
541968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  }
55470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
561968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  // Could not find matching timestamp in list.
571968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  return nullptr;
58470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
59470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
601968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbosbool VCMTimestampMap::IsEmpty() const {
611968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos  return (next_add_idx_ == next_pop_idx_);
621968d3f357f3cb01c4fdd3d9e89e22a1f0d6660apbos}
635908c71128aea207e42f86468aedb0a6fce3cccbphilipel}  // namespace webrtc
64