Simplify VCMTimestampMap.
Fixes code formatting and uses size_t properly. Also makes use of IsNewerTimestamp instead of a simple > check, which should fix an edge-case bug. BUG= R=stefan@webrtc.org Review URL: https://codereview.webrtc.org/1358863002 Cr-Commit-Position: refs/heads/master@{#10094}
This commit is contained in:
@ -53,8 +53,7 @@ int32_t VCMDecodedFrameCallback::Decoded(VideoFrame& decodedImage) {
|
|||||||
VCMReceiveCallback* callback;
|
VCMReceiveCallback* callback;
|
||||||
{
|
{
|
||||||
CriticalSectionScoped cs(_critSect);
|
CriticalSectionScoped cs(_critSect);
|
||||||
frameInfo = static_cast<VCMFrameInformation*>(
|
frameInfo = _timestampMap.Pop(decodedImage.timestamp());
|
||||||
_timestampMap.Pop(decodedImage.timestamp()));
|
|
||||||
callback = _receiveCallback;
|
callback = _receiveCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,10 +102,10 @@ uint64_t VCMDecodedFrameCallback::LastReceivedPictureID() const
|
|||||||
return _lastReceivedPictureID;
|
return _lastReceivedPictureID;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t VCMDecodedFrameCallback::Map(uint32_t timestamp, VCMFrameInformation* frameInfo)
|
void VCMDecodedFrameCallback::Map(uint32_t timestamp,
|
||||||
{
|
VCMFrameInformation* frameInfo) {
|
||||||
CriticalSectionScoped cs(_critSect);
|
CriticalSectionScoped cs(_critSect);
|
||||||
return _timestampMap.Add(timestamp, frameInfo);
|
_timestampMap.Add(timestamp, frameInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp)
|
int32_t VCMDecodedFrameCallback::Pop(uint32_t timestamp)
|
||||||
|
|||||||
@ -46,7 +46,7 @@ public:
|
|||||||
|
|
||||||
uint64_t LastReceivedPictureID() const;
|
uint64_t LastReceivedPictureID() const;
|
||||||
|
|
||||||
int32_t Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
|
void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
|
||||||
int32_t Pop(uint32_t timestamp);
|
int32_t Pop(uint32_t timestamp);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@ -10,90 +10,56 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "webrtc/modules/interface/module_common_types.h"
|
||||||
#include "webrtc/modules/video_coding/main/source/timestamp_map.h"
|
#include "webrtc/modules/video_coding/main/source/timestamp_map.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
// Constructor. Optional parameter specifies maximum number of
|
VCMTimestampMap::VCMTimestampMap(size_t capacity)
|
||||||
// coexisting timers.
|
: ring_buffer_(new TimestampDataTuple[capacity]),
|
||||||
VCMTimestampMap::VCMTimestampMap(int32_t length):
|
capacity_(capacity),
|
||||||
_nextAddIx(0),
|
next_add_idx_(0),
|
||||||
_nextPopIx(0)
|
next_pop_idx_(0) {
|
||||||
{
|
}
|
||||||
if (length <= 0)
|
|
||||||
{
|
VCMTimestampMap::~VCMTimestampMap() {
|
||||||
// default
|
}
|
||||||
length = 10;
|
|
||||||
|
void VCMTimestampMap::Add(uint32_t timestamp, VCMFrameInformation* data) {
|
||||||
|
ring_buffer_[next_add_idx_].timestamp = timestamp;
|
||||||
|
ring_buffer_[next_add_idx_].data = data;
|
||||||
|
next_add_idx_ = (next_add_idx_ + 1) % capacity_;
|
||||||
|
|
||||||
|
if (next_add_idx_ == next_pop_idx_) {
|
||||||
|
// Circular list full; forget oldest entry.
|
||||||
|
next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
VCMFrameInformation* VCMTimestampMap::Pop(uint32_t timestamp) {
|
||||||
|
while (!IsEmpty()) {
|
||||||
|
if (ring_buffer_[next_pop_idx_].timestamp == timestamp) {
|
||||||
|
// Found start time for this timestamp.
|
||||||
|
VCMFrameInformation* data = ring_buffer_[next_pop_idx_].data;
|
||||||
|
ring_buffer_[next_pop_idx_].data = nullptr;
|
||||||
|
next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
|
||||||
|
return data;
|
||||||
|
} else if (IsNewerTimestamp(ring_buffer_[next_pop_idx_].timestamp,
|
||||||
|
timestamp)) {
|
||||||
|
// The timestamp we are looking for is not in the list.
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
_map = new VCMTimestampDataTuple[length];
|
// Not in this position, check next (and forget this position).
|
||||||
_length = length;
|
next_pop_idx_ = (next_pop_idx_ + 1) % capacity_;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Could not find matching timestamp in list.
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destructor.
|
bool VCMTimestampMap::IsEmpty() const {
|
||||||
VCMTimestampMap::~VCMTimestampMap()
|
return (next_add_idx_ == next_pop_idx_);
|
||||||
{
|
|
||||||
delete [] _map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty the list of timers.
|
|
||||||
void
|
|
||||||
VCMTimestampMap::Reset()
|
|
||||||
{
|
|
||||||
_nextAddIx = 0;
|
|
||||||
_nextPopIx = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t
|
|
||||||
VCMTimestampMap::Add(uint32_t timestamp, void* data)
|
|
||||||
{
|
|
||||||
_map[_nextAddIx].timestamp = timestamp;
|
|
||||||
_map[_nextAddIx].data = data;
|
|
||||||
_nextAddIx = (_nextAddIx + 1) % _length;
|
|
||||||
|
|
||||||
if (_nextAddIx == _nextPopIx)
|
|
||||||
{
|
|
||||||
// Circular list full; forget oldest entry
|
|
||||||
_nextPopIx = (_nextPopIx + 1) % _length;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void*
|
|
||||||
VCMTimestampMap::Pop(uint32_t timestamp)
|
|
||||||
{
|
|
||||||
while (!IsEmpty())
|
|
||||||
{
|
|
||||||
if (_map[_nextPopIx].timestamp == timestamp)
|
|
||||||
{
|
|
||||||
// found start time for this timestamp
|
|
||||||
void* data = _map[_nextPopIx].data;
|
|
||||||
_map[_nextPopIx].data = NULL;
|
|
||||||
_nextPopIx = (_nextPopIx + 1) % _length;
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
else if (_map[_nextPopIx].timestamp > timestamp)
|
|
||||||
{
|
|
||||||
// the timestamp we are looking for is not in the list
|
|
||||||
assert(_nextPopIx < _length && _nextPopIx >= 0);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// not in this position, check next (and forget this position)
|
|
||||||
_nextPopIx = (_nextPopIx + 1) % _length;
|
|
||||||
}
|
|
||||||
|
|
||||||
// could not find matching timestamp in list
|
|
||||||
assert(_nextPopIx < _length && _nextPopIx >= 0);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if no timers are currently running
|
|
||||||
bool
|
|
||||||
VCMTimestampMap::IsEmpty() const
|
|
||||||
{
|
|
||||||
return (_nextAddIx == _nextPopIx);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,40 +11,35 @@
|
|||||||
#ifndef WEBRTC_MODULES_VIDEO_CODING_TIMESTAMP_MAP_H_
|
#ifndef WEBRTC_MODULES_VIDEO_CODING_TIMESTAMP_MAP_H_
|
||||||
#define WEBRTC_MODULES_VIDEO_CODING_TIMESTAMP_MAP_H_
|
#define WEBRTC_MODULES_VIDEO_CODING_TIMESTAMP_MAP_H_
|
||||||
|
|
||||||
|
#include "webrtc/base/scoped_ptr.h"
|
||||||
#include "webrtc/typedefs.h"
|
#include "webrtc/typedefs.h"
|
||||||
|
|
||||||
namespace webrtc
|
namespace webrtc {
|
||||||
{
|
|
||||||
|
|
||||||
struct VCMTimestampDataTuple
|
struct VCMFrameInformation;
|
||||||
{
|
|
||||||
uint32_t timestamp;
|
|
||||||
void* data;
|
|
||||||
};
|
|
||||||
|
|
||||||
class VCMTimestampMap
|
class VCMTimestampMap {
|
||||||
{
|
public:
|
||||||
public:
|
explicit VCMTimestampMap(size_t capacity);
|
||||||
// Constructor. Optional parameter specifies maximum number of
|
~VCMTimestampMap();
|
||||||
// timestamps in map.
|
|
||||||
VCMTimestampMap(const int32_t length = 10);
|
|
||||||
|
|
||||||
// Destructor.
|
// Empty the map.
|
||||||
~VCMTimestampMap();
|
void Reset();
|
||||||
|
|
||||||
// Empty the map
|
void Add(uint32_t timestamp, VCMFrameInformation* data);
|
||||||
void Reset();
|
VCMFrameInformation* Pop(uint32_t timestamp);
|
||||||
|
|
||||||
int32_t Add(uint32_t timestamp, void* data);
|
private:
|
||||||
void* Pop(uint32_t timestamp);
|
struct TimestampDataTuple {
|
||||||
|
uint32_t timestamp;
|
||||||
|
VCMFrameInformation* data;
|
||||||
|
};
|
||||||
|
bool IsEmpty() const;
|
||||||
|
|
||||||
private:
|
rtc::scoped_ptr<TimestampDataTuple[]> ring_buffer_;
|
||||||
bool IsEmpty() const;
|
const size_t capacity_;
|
||||||
|
size_t next_add_idx_;
|
||||||
VCMTimestampDataTuple* _map;
|
size_t next_pop_idx_;
|
||||||
int32_t _nextAddIx;
|
|
||||||
int32_t _nextPopIx;
|
|
||||||
int32_t _length;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|||||||
Reference in New Issue
Block a user