[feat] update transport module and channel module

This commit is contained in:
dijunkun
2025-01-06 17:10:56 +08:00
parent eef35ff0d4
commit 0737eee95a
17 changed files with 927 additions and 605 deletions

View File

@@ -1 +1,59 @@
#include "audio_channel_receive.h"
#include "audio_channel_receive.h"
#include "log.h"
AudioChannelReceive::AudioChannelReceive() {}
AudioChannelReceive::AudioChannelReceive(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const char *, size_t)> on_receive_audio)
: ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics),
on_receive_audio_(on_receive_audio) {}
AudioChannelReceive::~AudioChannelReceive() {}
void AudioChannelReceive::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
audio_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_audio_receiver_ = std::make_unique<RtpAudioReceiver>(ice_io_statistics_);
rtp_audio_receiver_->SetOnReceiveData(
[this](const char *data, size_t size) -> void {
ice_io_statistics_->UpdateAudioInboundBytes((uint32_t)size);
if (on_receive_audio_) {
on_receive_audio_(data, size);
}
});
rtp_audio_receiver_->SetSendDataFunc(
[this](const char *data, size_t size) -> int {
if (!ice_agent_) {
LOG_ERROR("ice_agent_ is nullptr");
return -1;
}
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(ice_state));
return -2;
}
ice_io_statistics_->UpdateAudioOutboundBytes((uint32_t)size);
return ice_agent_->Send(data, size);
});
}
void AudioChannelReceive::Destroy() {}
int AudioChannelReceive::OnReceiveRtpPacket(const char *data, size_t size) {
if (rtp_audio_receiver_) {
rtp_audio_receiver_->InsertRtpPacket(RtpPacket((uint8_t *)data, size));
}
return 0;
}

View File

@@ -7,12 +7,30 @@
#ifndef _AUDIO_CHANNEL_RECEIVE_H_
#define _AUDIO_CHANNEL_RECEIVE_H_
#include "ice_agent.h"
#include "rtp_audio_receiver.h"
#include "rtp_codec.h"
class AudioChannelReceive {
public:
AudioChannelReceive();
AudioChannelReceive(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const char *, size_t)> on_receive_audio);
~AudioChannelReceive();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int OnReceiveRtpPacket(const char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> audio_rtp_codec_ = nullptr;
std::unique_ptr<RtpAudioReceiver> rtp_audio_receiver_ = nullptr;
std::function<void(const char *, size_t)> on_receive_audio_ = nullptr;
};
#endif

View File

@@ -1 +1,55 @@
#include "audio_channel_send.h"
#include "audio_channel_send.h"
#include "log.h"
AudioChannelSend::AudioChannelSend() {}
AudioChannelSend::~AudioChannelSend() {}
AudioChannelSend::AudioChannelSend(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics)
: ice_agent_(ice_agent), ice_io_statistics_(ice_io_statistics) {}
void AudioChannelSend::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
audio_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_audio_sender_ = std::make_unique<RtpAudioSender>(ice_io_statistics_);
rtp_audio_sender_->SetSendDataFunc(
[this](const char *data, size_t size) -> int {
if (!ice_agent_) {
LOG_ERROR("ice_agent_ is nullptr");
return -1;
}
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(ice_state));
return -2;
}
ice_io_statistics_->UpdateAudioOutboundBytes((uint32_t)size);
return ice_agent_->Send(data, size);
});
rtp_audio_sender_->Start();
}
void AudioChannelSend::Destroy() {
if (rtp_audio_sender_) {
rtp_audio_sender_->Stop();
}
}
int AudioChannelSend::SendAudio(char *data, size_t size) {
if (audio_rtp_codec_) {
std::vector<RtpPacket> packets;
audio_rtp_codec_->Encode((uint8_t *)data, (uint32_t)size, packets);
rtp_audio_sender_->Enqueue(packets);
}
return 0;
}

View File

@@ -7,12 +7,27 @@
#ifndef _AUDIO_CHANNEL_SEND_H_
#define _AUDIO_CHANNEL_SEND_H_
#include "ice_agent.h"
#include "rtp_audio_sender.h"
#include "rtp_codec.h"
class AudioChannelSend {
public:
AudioChannelSend();
AudioChannelSend(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics);
~AudioChannelSend();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int SendAudio(char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> audio_rtp_codec_ = nullptr;
std::unique_ptr<RtpAudioSender> rtp_audio_sender_ = nullptr;
};
#endif

View File

@@ -1 +1,60 @@
#include "data_channel_receive.h"
#include "data_channel_receive.h"
#include "log.h"
DataChannelReceive::DataChannelReceive() {}
DataChannelReceive::DataChannelReceive(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const char *, size_t)> on_receive_data)
: ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics),
on_receive_data_(on_receive_data) {}
DataChannelReceive::~DataChannelReceive() {}
void DataChannelReceive::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
data_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_data_receiver_ = std::make_unique<RtpDataReceiver>(ice_io_statistics_);
rtp_data_receiver_->SetOnReceiveData(
[this](const char *data, size_t size) -> void {
ice_io_statistics_->UpdateDataInboundBytes((uint32_t)size);
if (on_receive_data_) {
on_receive_data_(data, size);
}
});
rtp_data_receiver_->SetSendDataFunc(
[this](const char *data, size_t size) -> int {
if (!ice_agent_) {
LOG_ERROR("ice_agent_ is nullptr");
return -1;
}
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(ice_state));
return -2;
}
ice_io_statistics_->UpdateDataOutboundBytes((uint32_t)size);
return ice_agent_->Send(data, size);
});
}
void DataChannelReceive::Destroy() {}
int DataChannelReceive::OnReceiveRtpPacket(const char *data, size_t size) {
if (rtp_data_receiver_) {
rtp_data_receiver_->InsertRtpPacket(RtpPacket((uint8_t *)data, size));
}
return -1;
}

View File

@@ -7,12 +7,29 @@
#ifndef _DATA_CHANNEL_RECEIVE_H_
#define _DATA_CHANNEL_RECEIVE_H_
#include "ice_agent.h"
#include "rtp_codec.h"
#include "rtp_data_receiver.h"
class DataChannelReceive {
public:
DataChannelReceive();
DataChannelReceive(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(const char *, size_t)> on_receive_data);
~DataChannelReceive();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int OnReceiveRtpPacket(const char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> data_rtp_codec_ = nullptr;
std::unique_ptr<RtpDataReceiver> rtp_data_receiver_ = nullptr;
std::function<void(const char *, size_t)> on_receive_data_ = nullptr;
};
#endif

View File

@@ -1 +1,58 @@
#include "data_channel_send.h"
#include "data_channel_send.h"
#include "log.h"
DataChannelSend::DataChannelSend() {}
DataChannelSend::~DataChannelSend() {}
DataChannelSend::DataChannelSend(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics)
: ice_agent_(ice_agent), ice_io_statistics_(ice_io_statistics) {}
void DataChannelSend::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
data_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_data_sender_ = std::make_unique<RtpDataSender>(ice_io_statistics_);
rtp_data_sender_->SetSendDataFunc(
[this](const char *data, size_t size) -> int {
if (!ice_agent_) {
LOG_ERROR("ice_agent_ is nullptr");
return -1;
}
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(ice_state));
return -2;
}
ice_io_statistics_->UpdateDataOutboundBytes((uint32_t)size);
return ice_agent_->Send(data, size);
});
rtp_data_sender_->Start();
}
void DataChannelSend::Destroy() {
if (rtp_data_sender_) {
rtp_data_sender_->Stop();
}
}
int DataChannelSend::SendData(const char *data, size_t size) {
std::vector<RtpPacket> packets;
if (rtp_data_sender_) {
if (data_rtp_codec_) {
data_rtp_codec_->Encode((uint8_t *)data, (uint32_t)size, packets);
rtp_data_sender_->Enqueue(packets);
}
}
return 0;
}

View File

@@ -7,12 +7,27 @@
#ifndef _DATA_CHANNEL_SEND_H_
#define _DATA_CHANNEL_SEND_H_
#include "ice_agent.h"
#include "rtp_codec.h"
#include "rtp_data_sender.h"
class DataChannelSend {
public:
DataChannelSend();
DataChannelSend(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics);
~DataChannelSend();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int SendData(const char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> data_rtp_codec_ = nullptr;
std::unique_ptr<RtpDataSender> rtp_data_sender_ = nullptr;
};
#endif

View File

@@ -1 +1,59 @@
#include "video_channel_receive.h"
#include "video_channel_receive.h"
#include "log.h"
VideoChannelReceive::VideoChannelReceive() {}
VideoChannelReceive::VideoChannelReceive(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(VideoFrame &)> on_receive_complete_frame)
: ice_agent_(ice_agent),
ice_io_statistics_(ice_io_statistics),
on_receive_complete_frame_(on_receive_complete_frame) {}
VideoChannelReceive::~VideoChannelReceive() {}
void VideoChannelReceive::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
video_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_video_receiver_ = std::make_unique<RtpVideoReceiver>(ice_io_statistics_);
rtp_video_receiver_->SetOnReceiveCompleteFrame(
[this](VideoFrame &video_frame) -> void {
ice_io_statistics_->UpdateVideoInboundBytes(
(uint32_t)video_frame.Size());
on_receive_complete_frame_(video_frame);
});
rtp_video_receiver_->SetSendDataFunc(
[this](const char *data, size_t size) -> int {
if (!ice_agent_) {
LOG_ERROR("ice_agent_ is nullptr");
return -1;
}
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(ice_state));
return -2;
}
ice_io_statistics_->UpdateVideoInboundBytes((uint32_t)size);
return ice_agent_->Send(data, size);
});
rtp_video_receiver_->Start();
}
void VideoChannelReceive::Destroy() {}
int VideoChannelReceive::OnReceiveRtpPacket(const char *data, size_t size) {
if (rtp_video_receiver_) {
rtp_video_receiver_->InsertRtpPacket(RtpPacket((uint8_t *)data, size));
}
return 0;
}

View File

@@ -7,12 +7,32 @@
#ifndef _VIDEO_CHANNEL_RECEIVE_H_
#define _VIDEO_CHANNEL_RECEIVE_H_
#include "ice_agent.h"
#include "rtp_codec.h"
#include "rtp_video_receiver.h"
class VideoChannelReceive {
public:
VideoChannelReceive();
VideoChannelReceive(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics,
std::function<void(VideoFrame &)> on_receive_complete_frame);
~VideoChannelReceive();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int OnReceiveRtpPacket(const char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> video_rtp_codec_ = nullptr;
std::unique_ptr<RtpVideoReceiver> rtp_video_receiver_ = nullptr;
std::function<void(VideoFrame &)> on_receive_complete_frame_ = nullptr;
};
#endif

View File

@@ -1,11 +1,18 @@
#include "video_channel_send.h"
#include "log.h"
VideoChannelSend::VideoChannelSend() {}
VideoChannelSend::~VideoChannelSend() {}
void VideoChannelSend::Initialize(RtpPacket::PAYLOAD_TYPE negotiated_video_pt) {
video_rtp_codec_ = std::make_unique<RtpCodec>(negotiated_video_pt);
VideoChannelSend::VideoChannelSend(
std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics)
: ice_agent_(ice_agent), ice_io_statistics_(ice_io_statistics){};
void VideoChannelSend::Initialize(RtpPacket::PAYLOAD_TYPE payload_type) {
video_rtp_codec_ = std::make_unique<RtpCodec>(payload_type);
rtp_video_sender_ = std::make_unique<RtpVideoSender>(ice_io_statistics_);
rtp_video_sender_->SetSendDataFunc(
@@ -15,10 +22,12 @@ void VideoChannelSend::Initialize(RtpPacket::PAYLOAD_TYPE negotiated_video_pt) {
return -1;
}
if (state_ != NICE_COMPONENT_STATE_CONNECTED &&
state_ != NICE_COMPONENT_STATE_READY) {
auto ice_state = ice_agent_->GetIceState();
if (ice_state != NICE_COMPONENT_STATE_CONNECTED &&
ice_state != NICE_COMPONENT_STATE_READY) {
LOG_ERROR("Ice is not connected, state = [{}]",
nice_component_state_to_string(state_));
nice_component_state_to_string(ice_state));
return -2;
}
@@ -35,13 +44,11 @@ void VideoChannelSend::Destroy() {
}
}
int VideoChannelSend::SendVideo(char *encoded_frame, size_t size) {
int VideoChannelSend::SendVideo(char *data, size_t size) {
std::vector<RtpPacket> packets;
if (rtp_video_sender_) {
if (video_rtp_codec_) {
video_rtp_codec_->Encode(
static_cast<RtpCodec::VideoFrameType>(frame_type),
(uint8_t *)encoded_frame, (uint32_t)size, packets);
video_rtp_codec_->Encode((uint8_t *)data, (uint32_t)size, packets);
}
rtp_video_sender_->Enqueue(packets);
}

View File

@@ -7,21 +7,26 @@
#ifndef _VIDEO_CHANNEL_SEND_H_
#define _VIDEO_CHANNEL_SEND_H_
#include "ice_agent.h"
#include "rtp_codec.h"
#include "rtp_video_sender.h"
class VideoChannelSend {
public:
VideoChannelSend();
VideoChannelSend(std::shared_ptr<IceAgent> ice_agent,
std::shared_ptr<IOStatistics> ice_io_statistics);
~VideoChannelSend();
public:
void Initialize(RtpPacket::PAYLOAD_TYPE negotiated_video_pt);
void Initialize(RtpPacket::PAYLOAD_TYPE payload_type);
void Destroy();
int SendVideo(char *encoded_frame, size_t size);
int SendVideo(char *data, size_t size);
private:
std::shared_ptr<IceAgent> ice_agent_ = nullptr;
std::shared_ptr<IOStatistics> ice_io_statistics_ = nullptr;
std::unique_ptr<RtpCodec> video_rtp_codec_ = nullptr;
std::unique_ptr<RtpVideoSender> rtp_video_sender_ = nullptr;
};