Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include <assert.h> | 11 #include <assert.h> |
| 12 #include <stdlib.h> | 12 #include <stdlib.h> |
| 13 | |
| 14 #include "webrtc/modules/interface/module_common_types.h" | |
| 13 #include "webrtc/modules/video_coding/main/source/timestamp_map.h" | 15 #include "webrtc/modules/video_coding/main/source/timestamp_map.h" |
| 14 | 16 |
| 15 namespace webrtc { | 17 namespace webrtc { |
| 16 | 18 |
| 17 // Constructor. Optional parameter specifies maximum number of | 19 VCMTimestampMap::VCMTimestampMap(size_t length) |
|
stefan-webrtc
2015/09/21 15:15:46
Named length here, size in the .h file.
pbos-webrtc
2015/09/23 13:51:55
Done.
| |
| 18 // coexisting timers. | 20 : ring_buffer_(new TimestampDataTuple[length]), |
| 19 VCMTimestampMap::VCMTimestampMap(int32_t length): | 21 length_(length), |
| 20 _nextAddIx(0), | 22 next_add_idx_(0), |
| 21 _nextPopIx(0) | 23 next_pop_idx_(0) { |
| 22 { | 24 } |
| 23 if (length <= 0) | 25 |
| 24 { | 26 VCMTimestampMap::~VCMTimestampMap() { |
| 25 // default | 27 } |
| 26 length = 10; | 28 |
| 29 void VCMTimestampMap::Add(uint32_t timestamp, void* 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) % length_; | |
| 33 | |
| 34 if (next_add_idx_ == next_pop_idx_) { | |
| 35 // Circular list full; forget oldest entry | |
|
stefan-webrtc
2015/09/21 15:15:46
Feel free to properly format the comments in this
pbos-webrtc
2015/09/23 13:51:55
Done.
| |
| 36 next_pop_idx_ = (next_pop_idx_ + 1) % length_; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void* 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 void* data = ring_buffer_[next_pop_idx_].data; | |
| 45 ring_buffer_[next_pop_idx_].data = NULL; | |
| 46 next_pop_idx_ = (next_pop_idx_ + 1) % length_; | |
| 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 NULL; | |
| 27 } | 52 } |
| 28 | 53 |
| 29 _map = new VCMTimestampDataTuple[length]; | 54 // not in this position, check next (and forget this position) |
| 30 _length = length; | 55 next_pop_idx_ = (next_pop_idx_ + 1) % length_; |
| 56 } | |
| 57 | |
| 58 // could not find matching timestamp in list | |
| 59 return NULL; | |
| 31 } | 60 } |
| 32 | 61 |
| 33 // Destructor. | 62 bool VCMTimestampMap::IsEmpty() const { |
| 34 VCMTimestampMap::~VCMTimestampMap() | 63 return (next_add_idx_ == next_pop_idx_); |
| 35 { | |
| 36 delete [] _map; | |
| 37 } | 64 } |
| 38 | |
| 39 // Empty the list of timers. | |
| 40 void | |
| 41 VCMTimestampMap::Reset() | |
| 42 { | |
| 43 _nextAddIx = 0; | |
| 44 _nextPopIx = 0; | |
| 45 } | 65 } |
| 46 | |
| 47 int32_t | |
| 48 VCMTimestampMap::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 | |
| 63 void* | |
| 64 VCMTimestampMap::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 | |
| 93 bool | |
| 94 VCMTimestampMap::IsEmpty() const | |
| 95 { | |
| 96 return (_nextAddIx == _nextPopIx); | |
| 97 } | |
| 98 | |
| 99 } | |
| OLD | NEW |