[feat] use smart pointer for ReceivedFrame when send it into decoder

This commit is contained in:
dijunkun
2025-04-14 16:11:25 +08:00
parent bc3dd680f9
commit df6f4321e8
15 changed files with 80 additions and 65 deletions

View File

@@ -66,10 +66,10 @@ int AomAv1Decoder::Init() {
}
int AomAv1Decoder::Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame) {
const uint8_t *data = received_frame.Buffer();
size_t size = received_frame.Size();
const uint8_t *data = received_frame->Buffer();
size_t size = received_frame->Size();
#ifdef SAVE_RECEIVED_AV1_STREAM
fwrite((unsigned char *)data, 1, size, file_av1_);
@@ -138,8 +138,8 @@ int AomAv1Decoder::Decode(
DecodedFrame decode_frame(nv12_data.data(), nv12_size, img_->d_w,
img_->d_h);
decode_frame.SetReceivedTimestamp(received_frame.ReceivedTimestamp());
decode_frame.SetCapturedTimestamp(received_frame.CapturedTimestamp());
decode_frame.SetReceivedTimestamp(received_frame->ReceivedTimestamp());
decode_frame.SetCapturedTimestamp(received_frame->CapturedTimestamp());
decode_frame.SetDecodedTimestamp(clock_->CurrentTime());
#ifdef SAVE_DECODED_NV12_STREAM

View File

@@ -23,7 +23,7 @@ class AomAv1Decoder : public VideoDecoder {
int Init();
int Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame);
std::string GetDecoderName() { return "AomAv1"; }

View File

@@ -107,10 +107,10 @@ int Dav1dAv1Decoder::Init() {
}
int Dav1dAv1Decoder::Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame) {
const uint8_t *data = received_frame.Buffer();
size_t size = received_frame.Size();
const uint8_t *data = received_frame->Buffer();
size_t size = received_frame->Size();
#ifdef SAVE_RECEIVED_AV1_STREAM
fwrite((unsigned char *)data, 1, size, file_av1_);
@@ -190,8 +190,8 @@ int Dav1dAv1Decoder::Decode(
DecodedFrame decoded_frame(nv12_frame_, nv12_frame_capacity_, frame_width_,
frame_height_);
decoded_frame.SetReceivedTimestamp(received_frame.ReceivedTimestamp());
decoded_frame.SetCapturedTimestamp(received_frame.CapturedTimestamp());
decoded_frame.SetReceivedTimestamp(received_frame->ReceivedTimestamp());
decoded_frame.SetCapturedTimestamp(received_frame->CapturedTimestamp());
decoded_frame.SetDecodedTimestamp(clock_->CurrentTime());
#ifdef SAVE_DECODED_NV12_STREAM

View File

@@ -21,7 +21,7 @@ class Dav1dAv1Decoder : public VideoDecoder {
int Init();
int Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame);
std::string GetDecoderName() { return "Dav1dAv1"; }

View File

@@ -69,14 +69,14 @@ int NvidiaVideoDecoder::Init() {
}
int NvidiaVideoDecoder::Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame) {
if (!decoder) {
return -1;
}
const uint8_t *data = received_frame.Buffer();
size_t size = received_frame.Size();
const uint8_t *data = received_frame->Buffer();
size_t size = received_frame->Size();
#ifdef SAVE_RECEIVED_H264_STREAM
fwrite((unsigned char *)data, 1, size, file_h264_);
@@ -99,9 +99,9 @@ int NvidiaVideoDecoder::Decode(
decoder->GetWidth() * decoder->GetHeight() * 3 / 2,
decoder->GetWidth(), decoder->GetHeight());
decoded_frame.SetReceivedTimestamp(
received_frame.ReceivedTimestamp());
received_frame->ReceivedTimestamp());
decoded_frame.SetCapturedTimestamp(
received_frame.CapturedTimestamp());
received_frame->CapturedTimestamp());
decoded_frame.SetDecodedTimestamp(clock_->CurrentTime());
on_receive_decoded_frame(decoded_frame);
#ifdef SAVE_DECODED_NV12_STREAM

View File

@@ -14,7 +14,7 @@ class NvidiaVideoDecoder : public VideoDecoder {
public:
int Init();
int Decode(const ReceivedFrame& received_frame,
int Decode(std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame&)> on_receive_decoded_frame);
std::string GetDecoderName() { return "NvidiaH264"; }

View File

@@ -104,14 +104,18 @@ int OpenH264Decoder::Init() {
}
int OpenH264Decoder::Decode(
const ReceivedFrame &received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame) {
if (!openh264_decoder_) {
return -1;
}
const uint8_t *data = received_frame.Buffer();
size_t size = received_frame.Size();
const uint8_t *data = received_frame->Buffer();
size_t size = received_frame->Size();
if (data == nullptr) {
LOG_WARN("data is nullptr!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
#ifdef SAVE_RECEIVED_H264_STREAM
fwrite((unsigned char *)data, 1, size, h264_stream_);
@@ -180,8 +184,8 @@ int OpenH264Decoder::Decode(
DecodedFrame decoded_frame(nv12_frame_, nv12_frame_capacity_,
frame_width_, frame_height_);
decoded_frame.SetReceivedTimestamp(received_frame.ReceivedTimestamp());
decoded_frame.SetCapturedTimestamp(received_frame.CapturedTimestamp());
decoded_frame.SetReceivedTimestamp(received_frame->ReceivedTimestamp());
decoded_frame.SetCapturedTimestamp(received_frame->CapturedTimestamp());
decoded_frame.SetDecodedTimestamp(clock_->CurrentTime());
#ifdef SAVE_DECODED_NV12_STREAM

View File

@@ -24,7 +24,7 @@ class OpenH264Decoder : public VideoDecoder {
public:
int Init();
int Decode(const ReceivedFrame& received_frame,
int Decode(std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame&)> on_receive_decoded_frame);
std::string GetDecoderName() { return "OpenH264"; }
@@ -47,7 +47,7 @@ class OpenH264Decoder : public VideoDecoder {
int yuv420p_frame_capacity_ = 0;
int yuv420p_frame_size_ = 0;
int nv12_frame_capacity_ = 0;
int nv12_frame_capacity_ = 0;
int nv12_frame_size_ = 0;
};

View File

@@ -22,7 +22,7 @@ class VideoDecoder {
virtual int Init() = 0;
virtual int Decode(
const ReceivedFrame& received_frame,
std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame&)> on_receive_decoded_frame) = 0;
virtual std::string GetDecoderName() = 0;

View File

@@ -733,7 +733,11 @@ bool RtpVideoReceiver::Process() {
while (it != pending_frames_.end()) {
if (it->second.is_complete) {
if (on_receive_complete_frame_) {
on_receive_complete_frame_(*(it->second.frame));
auto data = it->second.frame->Buffer();
if (data == nullptr) {
LOG_WARN("data is nullptr!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
on_receive_complete_frame_(std::move(it->second.frame));
}
it = pending_frames_.erase(it);
} else {

View File

@@ -44,7 +44,8 @@ class RtpVideoReceiver : public ThreadBase,
void SetSendDataFunc(std::function<int(const char*, size_t)> data_send_func);
void SetOnReceiveCompleteFrame(
std::function<void(const ReceivedFrame&)> on_receive_complete_frame) {
std::function<void(std::unique_ptr<ReceivedFrame>)>
on_receive_complete_frame) {
on_receive_complete_frame_ = on_receive_complete_frame;
}
uint32_t GetSsrc() { return ssrc_; }
@@ -99,8 +100,8 @@ class RtpVideoReceiver : public ThreadBase,
std::map<uint16_t, RtpPacketAv1> incomplete_av1_frame_list_;
std::map<uint16_t, RtpPacket> incomplete_frame_list_;
uint8_t* nv12_data_ = nullptr;
std::function<void(const ReceivedFrame&)> on_receive_complete_frame_ =
nullptr;
std::function<void(std::unique_ptr<ReceivedFrame>)>
on_receive_complete_frame_ = nullptr;
uint32_t last_complete_frame_ts_ = 0;
RingBuffer<ReceivedFrame> compelete_video_frame_queue_;

View File

@@ -7,7 +7,8 @@ VideoChannelReceive::VideoChannelReceive() {}
VideoChannelReceive::VideoChannelReceive(
std::shared_ptr<SystemClock> clock, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const ReceivedFrame &)> on_receive_complete_frame)
std::function<void(std::unique_ptr<ReceivedFrame>)>
on_receive_complete_frame)
: ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics),
on_receive_complete_frame_(on_receive_complete_frame),
@@ -19,8 +20,8 @@ void VideoChannelReceive::Initialize(rtp::PAYLOAD_TYPE payload_type) {
rtp_video_receiver_ =
std::make_unique<RtpVideoReceiver>(clock_, ice_io_statistics_);
rtp_video_receiver_->SetOnReceiveCompleteFrame(
[this](const ReceivedFrame &received_frame) -> void {
on_receive_complete_frame_(received_frame);
[this](std::unique_ptr<ReceivedFrame> received_frame) -> void {
on_receive_complete_frame_(std::move(received_frame));
});
rtp_video_receiver_->SetSendDataFunc([this](const char *data,

View File

@@ -14,10 +14,11 @@
class VideoChannelReceive {
public:
VideoChannelReceive();
VideoChannelReceive(
std::shared_ptr<SystemClock> clock, std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const ReceivedFrame &)> on_receive_complete_frame);
VideoChannelReceive(std::shared_ptr<SystemClock> clock,
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(std::unique_ptr<ReceivedFrame>)>
on_receive_complete_frame);
~VideoChannelReceive();
@@ -51,8 +52,8 @@ class VideoChannelReceive {
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpVideoReceiver> rtp_video_receiver_ = nullptr;
std::function<void(const ReceivedFrame &)> on_receive_complete_frame_ =
nullptr;
std::function<void(std::unique_ptr<ReceivedFrame>)>
on_receive_complete_frame_ = nullptr;
private:
std::shared_ptr<SystemClock> clock_;

View File

@@ -132,9 +132,9 @@ void IceTransportController::Create(
std::weak_ptr<IceTransportController> weak_self = shared_from_this();
video_channel_receive_ = std::make_unique<VideoChannelReceive>(
clock_, ice_agent, ice_io_statistics,
[this, weak_self](const ReceivedFrame& received_frame) {
[this, weak_self](std::unique_ptr<ReceivedFrame> received_frame) {
if (auto self = weak_self.lock()) {
OnReceiveCompleteFrame(received_frame);
OnReceiveCompleteFrame(std::move(received_frame));
}
});
@@ -314,29 +314,33 @@ int IceTransportController::OnReceiveDataRtpPacket(const char* data,
}
void IceTransportController::OnReceiveCompleteFrame(
const ReceivedFrame& received_frame) {
task_queue_decode_->PostTask([this, received_frame]() mutable {
if (video_decoder_) {
int num_frame_returned = video_decoder_->Decode(
received_frame, [this](DecodedFrame decoded_frame) {
if (on_receive_video_) {
XVideoFrame x_video_frame;
x_video_frame.data = (const char*)decoded_frame.Buffer();
x_video_frame.width = decoded_frame.Width();
x_video_frame.height = decoded_frame.Height();
x_video_frame.size = decoded_frame.Size();
x_video_frame.captured_timestamp =
decoded_frame.CapturedTimestamp();
x_video_frame.received_timestamp =
decoded_frame.ReceivedTimestamp();
x_video_frame.decoded_timestamp =
decoded_frame.DecodedTimestamp();
on_receive_video_(&x_video_frame, remote_user_id_.data(),
remote_user_id_.size(), user_data_);
}
});
}
});
std::unique_ptr<ReceivedFrame> received_frame) {
task_queue_decode_->PostTask(
[this, received_frame = std::move(received_frame)]() mutable {
if (video_decoder_) {
int num_frame_returned = video_decoder_->Decode(
std::move(received_frame), [this](DecodedFrame decoded_frame) {
if (on_receive_video_) {
XVideoFrame x_video_frame;
x_video_frame.data = (const char*)decoded_frame.Buffer();
x_video_frame.width = decoded_frame.Width();
x_video_frame.height = decoded_frame.Height();
x_video_frame.size = decoded_frame.Size();
x_video_frame.captured_timestamp =
decoded_frame.CapturedTimestamp();
x_video_frame.received_timestamp =
decoded_frame.ReceivedTimestamp();
x_video_frame.decoded_timestamp =
decoded_frame.DecodedTimestamp();
if (on_receive_video_) {
on_receive_video_(&x_video_frame, remote_user_id_.data(),
remote_user_id_.size(), user_data_);
}
}
});
}
});
}
void IceTransportController::OnReceiveCompleteAudio(const char* data,

View File

@@ -65,7 +65,7 @@ class IceTransportController
int OnReceiveAudioRtpPacket(const char *data, size_t size);
int OnReceiveDataRtpPacket(const char *data, size_t size);
void OnReceiveCompleteFrame(const ReceivedFrame &received_frame);
void OnReceiveCompleteFrame(std::unique_ptr<ReceivedFrame> received_frame);
void OnReceiveCompleteAudio(const char *data, size_t size);
void OnReceiveCompleteData(const char *data, size_t size);