PacketBuffer is now ref counted.
Since all FrameObjects have a reference to its PacketBuffer and since the PacketBuffer can be thrown away at any moment the PacketBuffer has to be ref counted in order to avoid FrameObjects dereferencing a potentially destroyed object. BUG=webrtc:5514 R=danilchap@webrtc.org, mflodman@webrtc.org, stefan@webrtc.org Review URL: https://codereview.webrtc.org/2199133004 . Cr-Commit-Position: refs/heads/master@{#13725}
This commit is contained in:
@ -75,7 +75,7 @@ class RtpFrameObject : public FrameObject {
|
||||
RTPVideoTypeHeader* GetCodecHeader() const;
|
||||
|
||||
private:
|
||||
PacketBuffer* packet_buffer_;
|
||||
rtc::scoped_refptr<PacketBuffer> packet_buffer_;
|
||||
enum FrameType frame_type_;
|
||||
VideoCodecType codec_type_;
|
||||
uint16_t first_seq_num_;
|
||||
|
||||
@ -12,7 +12,9 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
#include "webrtc/base/atomicops.h"
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/logging.h"
|
||||
#include "webrtc/modules/video_coding/frame_object.h"
|
||||
@ -21,10 +23,19 @@
|
||||
namespace webrtc {
|
||||
namespace video_coding {
|
||||
|
||||
rtc::scoped_refptr<PacketBuffer> PacketBuffer::Create(
|
||||
Clock* clock,
|
||||
size_t start_buffer_size,
|
||||
size_t max_buffer_size,
|
||||
OnReceivedFrameCallback* received_frame_callback) {
|
||||
return rtc::scoped_refptr<PacketBuffer>(new PacketBuffer(
|
||||
clock, start_buffer_size, max_buffer_size, received_frame_callback));
|
||||
}
|
||||
|
||||
PacketBuffer::PacketBuffer(Clock* clock,
|
||||
size_t start_buffer_size,
|
||||
size_t max_buffer_size,
|
||||
OnCompleteFrameCallback* frame_callback)
|
||||
OnReceivedFrameCallback* received_frame_callback)
|
||||
: clock_(clock),
|
||||
size_(start_buffer_size),
|
||||
max_size_(max_buffer_size),
|
||||
@ -33,13 +44,15 @@ PacketBuffer::PacketBuffer(Clock* clock,
|
||||
first_packet_received_(false),
|
||||
data_buffer_(start_buffer_size),
|
||||
sequence_buffer_(start_buffer_size),
|
||||
reference_finder_(frame_callback) {
|
||||
received_frame_callback_(received_frame_callback) {
|
||||
RTC_DCHECK_LE(start_buffer_size, max_buffer_size);
|
||||
// Buffer size must always be a power of 2.
|
||||
RTC_DCHECK((start_buffer_size & (start_buffer_size - 1)) == 0);
|
||||
RTC_DCHECK((max_buffer_size & (max_buffer_size - 1)) == 0);
|
||||
}
|
||||
|
||||
PacketBuffer::~PacketBuffer() {}
|
||||
|
||||
bool PacketBuffer::InsertPacket(const VCMPacket& packet) {
|
||||
rtc::CritScope lock(&crit_);
|
||||
uint16_t seq_num = packet.seqNum;
|
||||
@ -69,12 +82,6 @@ bool PacketBuffer::InsertPacket(const VCMPacket& packet) {
|
||||
if (AheadOf(seq_num, last_seq_num_))
|
||||
last_seq_num_ = seq_num;
|
||||
|
||||
// If this is a padding or FEC packet, don't insert it.
|
||||
if (packet.sizeBytes == 0) {
|
||||
reference_finder_.PaddingReceived(packet.seqNum);
|
||||
return true;
|
||||
}
|
||||
|
||||
sequence_buffer_[index].frame_begin = packet.isFirstPacket;
|
||||
sequence_buffer_[index].frame_end = packet.markerBit;
|
||||
sequence_buffer_[index].seq_num = packet.seqNum;
|
||||
@ -169,7 +176,8 @@ void PacketBuffer::FindFrames(uint16_t seq_num) {
|
||||
std::unique_ptr<RtpFrameObject> frame(
|
||||
new RtpFrameObject(this, start_seq_num, seq_num, frame_size,
|
||||
max_nack_count, clock_->TimeInMilliseconds()));
|
||||
reference_finder_.ManageFrame(std::move(frame));
|
||||
|
||||
received_frame_callback_->OnReceivedFrame(std::move(frame));
|
||||
}
|
||||
|
||||
index = (index + 1) % size_;
|
||||
@ -239,5 +247,17 @@ void PacketBuffer::Clear() {
|
||||
first_packet_received_ = false;
|
||||
}
|
||||
|
||||
int PacketBuffer::AddRef() const {
|
||||
return rtc::AtomicOps::Increment(&ref_count_);
|
||||
}
|
||||
|
||||
int PacketBuffer::Release() const {
|
||||
int count = rtc::AtomicOps::Decrement(&ref_count_);
|
||||
if (!count) {
|
||||
delete this;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
} // namespace video_coding
|
||||
} // namespace webrtc
|
||||
|
||||
@ -12,8 +12,10 @@
|
||||
#define WEBRTC_MODULES_VIDEO_CODING_PACKET_BUFFER_H_
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "webrtc/base/criticalsection.h"
|
||||
#include "webrtc/base/scoped_ref_ptr.h"
|
||||
#include "webrtc/base/thread_annotations.h"
|
||||
#include "webrtc/modules/include/module_common_types.h"
|
||||
#include "webrtc/modules/video_coding/packet.h"
|
||||
@ -29,23 +31,37 @@ namespace video_coding {
|
||||
class FrameObject;
|
||||
class RtpFrameObject;
|
||||
|
||||
class OnCompleteFrameCallback {
|
||||
// A received frame is a frame which has received all its packets.
|
||||
class OnReceivedFrameCallback {
|
||||
public:
|
||||
virtual ~OnCompleteFrameCallback() {}
|
||||
virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
|
||||
virtual ~OnReceivedFrameCallback() {}
|
||||
virtual void OnReceivedFrame(std::unique_ptr<RtpFrameObject> frame) = 0;
|
||||
};
|
||||
|
||||
class PacketBuffer {
|
||||
public:
|
||||
static rtc::scoped_refptr<PacketBuffer> Create(
|
||||
Clock* clock,
|
||||
size_t start_buffer_size,
|
||||
size_t max_buffer_size,
|
||||
OnReceivedFrameCallback* frame_callback);
|
||||
|
||||
virtual ~PacketBuffer();
|
||||
|
||||
// Made virtual for testing.
|
||||
virtual bool InsertPacket(const VCMPacket& packet);
|
||||
void ClearTo(uint16_t seq_num);
|
||||
void Clear();
|
||||
|
||||
int AddRef() const;
|
||||
int Release() const;
|
||||
|
||||
protected:
|
||||
// Both |start_buffer_size| and |max_buffer_size| must be a power of 2.
|
||||
PacketBuffer(Clock* clock,
|
||||
size_t start_buffer_size,
|
||||
size_t max_buffer_size,
|
||||
OnCompleteFrameCallback* frame_callback);
|
||||
|
||||
bool InsertPacket(const VCMPacket& packet);
|
||||
void ClearTo(uint16_t seq_num);
|
||||
void Clear();
|
||||
OnReceivedFrameCallback* frame_callback);
|
||||
|
||||
private:
|
||||
friend RtpFrameObject;
|
||||
@ -85,13 +101,16 @@ class PacketBuffer {
|
||||
void FindFrames(uint16_t seq_num) EXCLUSIVE_LOCKS_REQUIRED(crit_);
|
||||
|
||||
// Copy the bitstream for |frame| to |destination|.
|
||||
bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
|
||||
// Virtual for testing.
|
||||
virtual bool GetBitstream(const RtpFrameObject& frame, uint8_t* destination);
|
||||
|
||||
// Get the packet with sequence number |seq_num|.
|
||||
VCMPacket* GetPacket(uint16_t seq_num);
|
||||
// Virtual for testing.
|
||||
virtual VCMPacket* GetPacket(uint16_t seq_num);
|
||||
|
||||
// Mark all slots used by |frame| as not used.
|
||||
void ReturnFrame(RtpFrameObject* frame);
|
||||
// Virtual for testing.
|
||||
virtual void ReturnFrame(RtpFrameObject* frame);
|
||||
|
||||
rtc::CriticalSection crit_;
|
||||
|
||||
@ -115,9 +134,10 @@ class PacketBuffer {
|
||||
// and information needed to determine the continuity between packets.
|
||||
std::vector<ContinuityInfo> sequence_buffer_ GUARDED_BY(crit_);
|
||||
|
||||
// Frames that have received all their packets are handed off to the
|
||||
// |reference_finder_| which finds the dependencies between the frames.
|
||||
RtpFrameReferenceFinder reference_finder_;
|
||||
// Called when a received frame is found.
|
||||
OnReceivedFrameCallback* const received_frame_callback_;
|
||||
|
||||
mutable volatile int ref_count_ = 0;
|
||||
};
|
||||
|
||||
} // namespace video_coding
|
||||
|
||||
@ -26,8 +26,16 @@
|
||||
namespace webrtc {
|
||||
namespace video_coding {
|
||||
|
||||
class FrameObject;
|
||||
class RtpFrameObject;
|
||||
class OnCompleteFrameCallback;
|
||||
|
||||
// A complete frame is a frame which has received all its packets and all its
|
||||
// references are known.
|
||||
class OnCompleteFrameCallback {
|
||||
public:
|
||||
virtual ~OnCompleteFrameCallback() {}
|
||||
virtual void OnCompleteFrame(std::unique_ptr<FrameObject> frame) = 0;
|
||||
};
|
||||
|
||||
class RtpFrameReferenceFinder {
|
||||
public:
|
||||
|
||||
1195
webrtc/modules/video_coding/rtp_frame_reference_finder_unittest.cc
Normal file
1195
webrtc/modules/video_coding/rtp_frame_reference_finder_unittest.cc
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user