[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( int AomAv1Decoder::Decode(
const ReceivedFrame &received_frame, std::unique_ptr<ReceivedFrame> received_frame,
std::function<void(const DecodedFrame &)> on_receive_decoded_frame) { std::function<void(const DecodedFrame &)> on_receive_decoded_frame) {
const uint8_t *data = received_frame.Buffer(); const uint8_t *data = received_frame->Buffer();
size_t size = received_frame.Size(); size_t size = received_frame->Size();
#ifdef SAVE_RECEIVED_AV1_STREAM #ifdef SAVE_RECEIVED_AV1_STREAM
fwrite((unsigned char *)data, 1, size, file_av1_); 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, DecodedFrame decode_frame(nv12_data.data(), nv12_size, img_->d_w,
img_->d_h); img_->d_h);
decode_frame.SetReceivedTimestamp(received_frame.ReceivedTimestamp()); decode_frame.SetReceivedTimestamp(received_frame->ReceivedTimestamp());
decode_frame.SetCapturedTimestamp(received_frame.CapturedTimestamp()); decode_frame.SetCapturedTimestamp(received_frame->CapturedTimestamp());
decode_frame.SetDecodedTimestamp(clock_->CurrentTime()); decode_frame.SetDecodedTimestamp(clock_->CurrentTime());
#ifdef SAVE_DECODED_NV12_STREAM #ifdef SAVE_DECODED_NV12_STREAM

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -24,7 +24,7 @@ class OpenH264Decoder : public VideoDecoder {
public: public:
int Init(); 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::function<void(const DecodedFrame&)> on_receive_decoded_frame);
std::string GetDecoderName() { return "OpenH264"; } std::string GetDecoderName() { return "OpenH264"; }

View File

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

View File

@@ -733,7 +733,11 @@ bool RtpVideoReceiver::Process() {
while (it != pending_frames_.end()) { while (it != pending_frames_.end()) {
if (it->second.is_complete) { if (it->second.is_complete) {
if (on_receive_complete_frame_) { 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); it = pending_frames_.erase(it);
} else { } else {

View File

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

View File

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

View File

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

View File

@@ -132,9 +132,9 @@ void IceTransportController::Create(
std::weak_ptr<IceTransportController> weak_self = shared_from_this(); std::weak_ptr<IceTransportController> weak_self = shared_from_this();
video_channel_receive_ = std::make_unique<VideoChannelReceive>( video_channel_receive_ = std::make_unique<VideoChannelReceive>(
clock_, ice_agent, ice_io_statistics, 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()) { if (auto self = weak_self.lock()) {
OnReceiveCompleteFrame(received_frame); OnReceiveCompleteFrame(std::move(received_frame));
} }
}); });
@@ -314,11 +314,12 @@ int IceTransportController::OnReceiveDataRtpPacket(const char* data,
} }
void IceTransportController::OnReceiveCompleteFrame( void IceTransportController::OnReceiveCompleteFrame(
const ReceivedFrame& received_frame) { std::unique_ptr<ReceivedFrame> received_frame) {
task_queue_decode_->PostTask([this, received_frame]() mutable { task_queue_decode_->PostTask(
[this, received_frame = std::move(received_frame)]() mutable {
if (video_decoder_) { if (video_decoder_) {
int num_frame_returned = video_decoder_->Decode( int num_frame_returned = video_decoder_->Decode(
received_frame, [this](DecodedFrame decoded_frame) { std::move(received_frame), [this](DecodedFrame decoded_frame) {
if (on_receive_video_) { if (on_receive_video_) {
XVideoFrame x_video_frame; XVideoFrame x_video_frame;
x_video_frame.data = (const char*)decoded_frame.Buffer(); x_video_frame.data = (const char*)decoded_frame.Buffer();
@@ -331,9 +332,12 @@ void IceTransportController::OnReceiveCompleteFrame(
decoded_frame.ReceivedTimestamp(); decoded_frame.ReceivedTimestamp();
x_video_frame.decoded_timestamp = x_video_frame.decoded_timestamp =
decoded_frame.DecodedTimestamp(); decoded_frame.DecodedTimestamp();
if (on_receive_video_) {
on_receive_video_(&x_video_frame, remote_user_id_.data(), on_receive_video_(&x_video_frame, remote_user_id_.data(),
remote_user_id_.size(), user_data_); remote_user_id_.size(), user_data_);
} }
}
}); });
} }
}); });

View File

@@ -65,7 +65,7 @@ class IceTransportController
int OnReceiveAudioRtpPacket(const char *data, size_t size); int OnReceiveAudioRtpPacket(const char *data, size_t size);
int OnReceiveDataRtpPacket(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 OnReceiveCompleteAudio(const char *data, size_t size);
void OnReceiveCompleteData(const char *data, size_t size); void OnReceiveCompleteData(const char *data, size_t size);