[feat] update rtc module

This commit is contained in:
dijunkun
2025-05-13 21:47:05 +08:00
parent 6fe46f6181
commit e6e237279c
12 changed files with 46 additions and 28 deletions

View File

@@ -11,7 +11,8 @@
class ScreenCapturer { class ScreenCapturer {
public: public:
typedef std::function<void(unsigned char*, int, int, int)> cb_desktop_data; typedef std::function<void(unsigned char*, int, int, int, int)>
cb_desktop_data;
class DisplayInfo { class DisplayInfo {
public: public:

View File

@@ -123,7 +123,7 @@ int ScreenCapturerWgc::Init(const int fps, cb_desktop_data cb) {
display.top, display.right, display.bottom); display.top, display.right, display.bottom);
sessions_.push_back( sessions_.push_back(
{std::make_unique<WgcSessionImpl>(), false, false, false}); {std::make_unique<WgcSessionImpl>(i), false, false, false});
sessions_.back().session_->RegisterObserver(this); sessions_.back().session_->RegisterObserver(this);
error = sessions_.back().session_->Initialize((HMONITOR)display.handle); error = sessions_.back().session_->Initialize((HMONITOR)display.handle);
if (error != 0) { if (error != 0) {
@@ -242,7 +242,8 @@ int ScreenCapturerWgc::SwitchTo(int monitor_index) {
return 0; return 0;
} }
void ScreenCapturerWgc::OnFrame(const WgcSession::wgc_session_frame &frame) { void ScreenCapturerWgc::OnFrame(const WgcSession::wgc_session_frame &frame,
int id) {
if (on_data_) { if (on_data_) {
if (!nv12_frame_) { if (!nv12_frame_) {
nv12_frame_ = new unsigned char[frame.width * frame.height * 3 / 2]; nv12_frame_ = new unsigned char[frame.width * frame.height * 3 / 2];
@@ -254,7 +255,7 @@ void ScreenCapturerWgc::OnFrame(const WgcSession::wgc_session_frame &frame) {
frame.width, frame.width, frame.height); frame.width, frame.width, frame.height);
on_data_(nv12_frame_, frame.width * frame.height * 3 / 2, frame.width, on_data_(nv12_frame_, frame.width * frame.height * 3 / 2, frame.width,
frame.height); frame.height, id);
} }
} }

View File

@@ -32,7 +32,7 @@ class ScreenCapturerWgc : public ScreenCapturer,
int SwitchTo(int monitor_index); int SwitchTo(int monitor_index);
void OnFrame(const WgcSession::wgc_session_frame &frame); void OnFrame(const WgcSession::wgc_session_frame &frame, int id);
protected: protected:
void CleanUp(); void CleanUp();

View File

@@ -16,7 +16,7 @@ class WgcSession {
class wgc_session_observer { class wgc_session_observer {
public: public:
virtual ~wgc_session_observer() {} virtual ~wgc_session_observer() {}
virtual void OnFrame(const wgc_session_frame &frame) = 0; virtual void OnFrame(const wgc_session_frame &frame, int id) = 0;
}; };
public: public:

View File

@@ -23,7 +23,7 @@ HRESULT __stdcall CreateDirect3D11DeviceFromDXGIDevice(
::IDXGIDevice *dxgiDevice, ::IInspectable **graphicsDevice); ::IDXGIDevice *dxgiDevice, ::IInspectable **graphicsDevice);
} }
WgcSessionImpl::WgcSessionImpl() {} WgcSessionImpl::WgcSessionImpl(int id) : id_(id) {}
WgcSessionImpl::~WgcSessionImpl() { WgcSessionImpl::~WgcSessionImpl() {
Stop(); Stop();
@@ -264,11 +264,13 @@ void WgcSessionImpl::OnFrame(
// copy data from map_result.pData // copy data from map_result.pData
if (map_result.pData && observer_) { if (map_result.pData && observer_) {
observer_->OnFrame(wgc_session_frame{ observer_->OnFrame(
static_cast<unsigned int>(frame_size.Width), wgc_session_frame{static_cast<unsigned int>(frame_size.Width),
static_cast<unsigned int>(frame_size.Height), map_result.RowPitch, static_cast<unsigned int>(frame_size.Height),
map_result.RowPitch,
const_cast<const unsigned char *>( const_cast<const unsigned char *>(
(unsigned char *)map_result.pData)}); (unsigned char *)map_result.pData)},
id_);
} }
d3d11_device_context_->Unmap(d3d11_texture_mapped_.get(), 0); d3d11_device_context_->Unmap(d3d11_texture_mapped_.get(), 0);

View File

@@ -35,7 +35,7 @@ class WgcSessionImpl : public WgcSession {
} target_{0}; } target_{0};
public: public:
WgcSessionImpl(); WgcSessionImpl(int id);
~WgcSessionImpl() override; ~WgcSessionImpl() override;
public: public:
@@ -72,6 +72,7 @@ class WgcSessionImpl : public WgcSession {
// void message_func(); // void message_func();
private: private:
int id_ = -1;
std::mutex lock_; std::mutex lock_;
bool is_initialized_ = false; bool is_initialized_ = false;
bool is_running_ = false; bool is_running_ = false;

View File

@@ -71,7 +71,7 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
remote_action.d = i; remote_action.d = i;
if (props->connection_status_ == ConnectionStatus::Connected) { if (props->connection_status_ == ConnectionStatus::Connected) {
SendDataFrame(props->peer_, (const char*)&remote_action, SendDataFrame(props->peer_, (const char*)&remote_action,
sizeof(remote_action)); sizeof(remote_action), props->data_label_.c_str());
} }
} }
} }
@@ -144,7 +144,7 @@ int Render::ControlBar(std::shared_ptr<SubStreamWindowProperties>& props) {
remote_action.type = ControlType::audio_capture; remote_action.type = ControlType::audio_capture;
remote_action.a = props->audio_capture_button_pressed_; remote_action.a = props->audio_capture_button_pressed_;
SendDataFrame(props->peer_, (const char*)&remote_action, SendDataFrame(props->peer_, (const char*)&remote_action,
sizeof(remote_action)); sizeof(remote_action), props->data_label_.c_str());
} }
} }
if (!props->audio_capture_button_pressed_) { if (!props->audio_capture_button_pressed_) {

View File

@@ -142,6 +142,8 @@ int Render::ConnectTo(const std::string &remote_id, const char *password,
memcpy(&props->params_, &params_, sizeof(Params)); memcpy(&props->params_, &params_, sizeof(Params));
props->params_.user_id = props->local_id_.c_str(); props->params_.user_id = props->local_id_.c_str();
props->peer_ = CreatePeer(&props->params_); props->peer_ = CreatePeer(&props->params_);
AddAudioStream(props->peer_, props->audio_label_.c_str());
AddDataStream(props->peer_, props->data_label_.c_str());
if (props->peer_) { if (props->peer_) {
LOG_INFO("[{}] Create peer instance successful", props->local_id_); LOG_INFO("[{}] Create peer instance successful", props->local_id_);

View File

@@ -194,7 +194,9 @@ int Render::StartScreenCapturer() {
.count(); .count();
int screen_capturer_init_ret = screen_capturer_->Init( int screen_capturer_init_ret = screen_capturer_->Init(
60, [this](unsigned char* data, int size, int width, int height) -> void { 60,
[this](unsigned char* data, int size, int width, int height,
int display_id) -> void {
auto now_time = std::chrono::duration_cast<std::chrono::milliseconds>( auto now_time = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch()) std::chrono::steady_clock::now().time_since_epoch())
.count(); .count();
@@ -206,7 +208,9 @@ int Render::StartScreenCapturer() {
frame.width = width; frame.width = width;
frame.height = height; frame.height = height;
frame.captured_timestamp = GetSystemTimeMicros(peer_); frame.captured_timestamp = GetSystemTimeMicros(peer_);
SendVideoFrame(peer_, &frame); SendVideoFrame(peer_, &frame,
display_id == 0 ? video_primary_label_.c_str()
: video_secondary_label_.c_str());
last_frame_time_ = now_time; last_frame_time_ = now_time;
} }
}); });
@@ -239,7 +243,7 @@ int Render::StartSpeakerCapturer() {
speaker_capturer_ = (SpeakerCapturer*)speaker_capturer_factory_->Create(); speaker_capturer_ = (SpeakerCapturer*)speaker_capturer_factory_->Create();
int speaker_capturer_init_ret = speaker_capturer_->Init( int speaker_capturer_init_ret = speaker_capturer_->Init(
[this](unsigned char* data, size_t size) -> void { [this](unsigned char* data, size_t size) -> void {
SendAudioFrame(peer_, (const char*)data, size); SendAudioFrame(peer_, (const char*)data, size, audio_label_.c_str());
}); });
if (0 != speaker_capturer_init_ret) { if (0 != speaker_capturer_init_ret) {
@@ -359,10 +363,10 @@ int Render::CreateConnectionPeer() {
LOG_INFO("Create peer [{}] instance failed", client_id_); LOG_INFO("Create peer [{}] instance failed", client_id_);
} }
AddVideoStream(peer_, "primrey"); AddVideoStream(peer_, video_primary_label_.c_str());
AddVideoStream(peer_, "second"); AddVideoStream(peer_, video_secondary_label_.c_str());
AddAudioStream(peer_, "audio"); AddAudioStream(peer_, audio_label_.c_str());
AddDataStream(peer_, "data"); AddDataStream(peer_, data_label_.c_str());
return 0; return 0;
} }
@@ -844,7 +848,7 @@ void Render::MainLoop() {
memcpy(&remote_action.i.host_name, host_name.data(), host_name.size()); memcpy(&remote_action.i.host_name, host_name.data(), host_name.size());
remote_action.i.host_name_size = host_name.size(); remote_action.i.host_name_size = host_name.size();
int ret = SendDataFrame(peer_, (const char*)&remote_action, int ret = SendDataFrame(peer_, (const char*)&remote_action,
sizeof(remote_action)); sizeof(remote_action), data_label_.c_str());
if (0 == ret) { if (0 == ret) {
host_info_sent_ = true; host_info_sent_ = true;
} }

View File

@@ -33,6 +33,8 @@ class Render {
struct SubStreamWindowProperties { struct SubStreamWindowProperties {
Params params_; Params params_;
PeerPtr *peer_ = nullptr; PeerPtr *peer_ = nullptr;
std::string audio_label_ = "control_audio";
std::string data_label_ = "control_data";
std::string local_id_ = ""; std::string local_id_ = "";
std::string remote_id_ = ""; std::string remote_id_ = "";
bool exit_ = false; bool exit_ = false;
@@ -373,6 +375,10 @@ class Render {
bool signal_connected_ = false; bool signal_connected_ = false;
PeerPtr *peer_ = nullptr; PeerPtr *peer_ = nullptr;
PeerPtr *peer_reserved_ = nullptr; PeerPtr *peer_reserved_ = nullptr;
std::string video_primary_label_ = "primary_display";
std::string video_secondary_label_ = "secondary_display";
std::string audio_label_ = "audio";
std::string data_label_ = "data";
Params params_; Params params_;
SDL_AudioDeviceID input_dev_; SDL_AudioDeviceID input_dev_;
SDL_AudioDeviceID output_dev_; SDL_AudioDeviceID output_dev_;

View File

@@ -29,7 +29,7 @@ int Render::SendKeyCommand(int key_code, bool is_down) {
auto props = client_properties_[controlled_remote_id_]; auto props = client_properties_[controlled_remote_id_];
if (props->connection_status_ == ConnectionStatus::Connected) { if (props->connection_status_ == ConnectionStatus::Connected) {
SendDataFrame(props->peer_, (const char *)&remote_action, SendDataFrame(props->peer_, (const char *)&remote_action,
sizeof(remote_action)); sizeof(remote_action), props->data_label_.c_str());
} }
} }
} }
@@ -95,7 +95,7 @@ int Render::ProcessMouseEvent(SDL_Event &event) {
remote_action.m.flag = MouseFlag::move; remote_action.m.flag = MouseFlag::move;
} }
SendDataFrame(props->peer_, (const char *)&remote_action, SendDataFrame(props->peer_, (const char *)&remote_action,
sizeof(remote_action)); sizeof(remote_action), props->data_label_.c_str());
} else if (SDL_MOUSEWHEEL == event.type && } else if (SDL_MOUSEWHEEL == event.type &&
last_mouse_event.button.x >= props->stream_render_rect_.x && last_mouse_event.button.x >= props->stream_render_rect_.x &&
last_mouse_event.button.x <= props->stream_render_rect_.x + last_mouse_event.button.x <= props->stream_render_rect_.x +
@@ -128,7 +128,7 @@ int Render::ProcessMouseEvent(SDL_Event &event) {
render_height; render_height;
SendDataFrame(props->peer_, (const char *)&remote_action, SendDataFrame(props->peer_, (const char *)&remote_action,
sizeof(remote_action)); sizeof(remote_action), props->data_label_.c_str());
} }
} }
@@ -145,7 +145,8 @@ void Render::SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len) {
for (auto it : render->client_properties_) { for (auto it : render->client_properties_) {
auto props = it.second; auto props = it.second;
if (props->connection_status_ == ConnectionStatus::Connected) { if (props->connection_status_ == ConnectionStatus::Connected) {
SendAudioFrame(props->peer_, (const char *)stream, len); SendAudioFrame(props->peer_, (const char *)stream, len,
render->audio_label_.c_str());
} }
} }