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
11#include <assert.h>
12#include <stdlib.h>
13#include "webrtc/modules/video_coding/main/source/timestamp_map.h"
14
15namespace webrtc {
16
17// Constructor. Optional parameter specifies maximum number of
18// coexisting timers.
19VCMTimestampMap::VCMTimestampMap(int32_t length):
20    _nextAddIx(0),
21    _nextPopIx(0)
22{
23    if (length <= 0)
24    {
25        // default
26        length = 10;
27    }
28
29    _map = new VCMTimestampDataTuple[length];
30    _length = length;
31}
32
33// Destructor.
34VCMTimestampMap::~VCMTimestampMap()
35{
36    delete [] _map;
37}
38
39// Empty the list of timers.
40void
41VCMTimestampMap::Reset()
42{
43    _nextAddIx = 0;
44    _nextPopIx = 0;
45}
46
47int32_t
48VCMTimestampMap::Add(uint32_t timestamp, void* data)
49{
50    _map[_nextAddIx].timestamp = timestamp;
51    _map[_nextAddIx].data = data;
52    _nextAddIx = (_nextAddIx + 1) % _length;
53
54    if (_nextAddIx == _nextPopIx)
55    {
56        // Circular list full; forget oldest entry
57        _nextPopIx = (_nextPopIx + 1) % _length;
58        return -1;
59    }
60    return 0;
61}
62
63void*
64VCMTimestampMap::Pop(uint32_t timestamp)
65{
66    while (!IsEmpty())
67    {
68        if (_map[_nextPopIx].timestamp == timestamp)
69        {
70            // found start time for this timestamp
71            void* data = _map[_nextPopIx].data;
72            _map[_nextPopIx].data = NULL;
73            _nextPopIx = (_nextPopIx + 1) % _length;
74            return data;
75        }
76        else if (_map[_nextPopIx].timestamp > timestamp)
77        {
78            // the timestamp we are looking for is not in the list
79            assert(_nextPopIx < _length && _nextPopIx >= 0);
80            return NULL;
81        }
82
83        // not in this position, check next (and forget this position)
84        _nextPopIx = (_nextPopIx + 1) % _length;
85    }
86
87    // could not find matching timestamp in list
88    assert(_nextPopIx < _length && _nextPopIx >= 0);
89    return NULL;
90}
91
92// Check if no timers are currently running
93bool
94VCMTimestampMap::IsEmpty() const
95{
96    return (_nextAddIx == _nextPopIx);
97}
98
99}
100