[feat] add protocol to control audio capture

This commit is contained in:
dijunkun
2024-08-15 14:50:47 +08:00
parent 4704d494ec
commit 018231eee4
6 changed files with 107 additions and 86 deletions

View File

@@ -9,7 +9,7 @@
#include <stdio.h> #include <stdio.h>
typedef enum { mouse = 0, keyboard } ControlType; typedef enum { mouse = 0, keyboard, audio_capture } ControlType;
typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag; typedef enum { move = 0, left_down, left_up, right_down, right_up } MouseFlag;
typedef enum { key_down = 0, key_up } KeyFlag; typedef enum { key_down = 0, key_up } KeyFlag;
typedef struct { typedef struct {
@@ -28,6 +28,7 @@ typedef struct {
union { union {
Mouse m; Mouse m;
Key k; Key k;
bool a;
}; };
} RemoteAction; } RemoteAction;

View File

@@ -48,6 +48,12 @@ int Render::ControlBar() {
localization::audio_capture[localization_language_index_]; localization::audio_capture[localization_language_index_];
} }
audio_capture_button_pressed_ = !audio_capture_button_pressed_; audio_capture_button_pressed_ = !audio_capture_button_pressed_;
RemoteAction remote_action;
remote_action.type = ControlType::audio_capture;
remote_action.a = audio_capture_button_pressed_;
SendData(peer_, DATA_TYPE::DATA, (const char *)&remote_action,
sizeof(remote_action));
} }
ImGui::SameLine(); ImGui::SameLine();

View File

@@ -152,79 +152,16 @@ int Render::LoadSettingsFromCacheFile() {
return 0; return 0;
} }
int Render::StartScreenCapture() {
screen_capturer_ = (ScreenCapturer *)screen_capturer_factory_->Create();
ScreenCapturer::RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = screen_width_;
rect.bottom = screen_height_;
last_frame_time_ = std::chrono::steady_clock::now();
int screen_capturer_init_ret = screen_capturer_->Init(
rect, 60,
[this](unsigned char *data, int size, int width, int height) -> void {
auto now_time = std::chrono::steady_clock::now();
std::chrono::duration<double> duration = now_time - last_frame_time_;
auto tc = duration.count() * 1000;
if (tc >= 0 && connection_established_) {
SendData(peer_, DATA_TYPE::VIDEO, (const char *)data,
NV12_BUFFER_SIZE);
last_frame_time_ = now_time;
}
});
if (0 == screen_capturer_init_ret) {
screen_capturer_->Start();
} else {
screen_capturer_->Destroy();
delete screen_capturer_;
screen_capturer_ = nullptr;
}
return 0;
}
int Render::StopScreenCapture() {
if (screen_capturer_) {
LOG_INFO("Stop screen capturer")
screen_capturer_->Stop();
screen_capturer_->Destroy();
delete screen_capturer_;
screen_capturer_ = nullptr;
}
return 0;
}
int Render::StartSpeakerCapture() { int Render::StartSpeakerCapture() {
speaker_capturer_ = (SpeakerCapturer *)speaker_capturer_factory_->Create(); if (speaker_capturer_) {
int speaker_capturer_init_ret =
speaker_capturer_->Init([this](unsigned char *data, size_t size) -> void {
if (connection_established_) {
SendData(peer_, DATA_TYPE::AUDIO, (const char *)data, size);
}
});
if (0 == speaker_capturer_init_ret) {
speaker_capturer_->Start(); speaker_capturer_->Start();
} else {
speaker_capturer_->Destroy();
delete speaker_capturer_;
speaker_capturer_ = nullptr;
} }
return 0; return 0;
} }
int Render::StopSpeakerCapture() { int Render::StopSpeakerCapture() {
if (speaker_capturer_) { if (speaker_capturer_) {
LOG_INFO("Destroy speaker capturer") speaker_capturer_->Stop();
speaker_capturer_->Destroy();
delete speaker_capturer_;
speaker_capturer_ = nullptr;
} }
return 0; return 0;
@@ -419,22 +356,62 @@ int Render::Run() {
{ {
nv12_buffer_ = new char[NV12_BUFFER_SIZE]; nv12_buffer_ = new char[NV12_BUFFER_SIZE];
// Screen capture // Screen capture init
screen_capturer_factory_ = new ScreenCapturerFactory(); screen_capturer_factory_ = new ScreenCapturerFactory();
screen_capturer_ = (ScreenCapturer *)screen_capturer_factory_->Create();
ScreenCapturer::RECORD_DESKTOP_RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = screen_width_;
rect.bottom = screen_height_;
last_frame_time_ = std::chrono::steady_clock::now();
// Speaker capture int screen_capturer_init_ret = screen_capturer_->Init(
rect, 60,
[this](unsigned char *data, int size, int width, int height) -> void {
auto now_time = std::chrono::steady_clock::now();
std::chrono::duration<double> duration = now_time - last_frame_time_;
auto tc = duration.count() * 1000;
if (tc >= 0 && connection_established_) {
SendData(peer_, DATA_TYPE::VIDEO, (const char *)data,
NV12_BUFFER_SIZE);
last_frame_time_ = now_time;
}
});
if (0 == screen_capturer_init_ret) {
screen_capturer_->Start();
} else {
screen_capturer_->Destroy();
delete screen_capturer_;
screen_capturer_ = nullptr;
}
// Speaker capture init
speaker_capturer_factory_ = new SpeakerCapturerFactory(); speaker_capturer_factory_ = new SpeakerCapturerFactory();
speaker_capturer_ = (SpeakerCapturer *)speaker_capturer_factory_->Create();
int speaker_capturer_init_ret = speaker_capturer_->Init(
[this](unsigned char *data, size_t size) -> void {
if (connection_established_) {
SendData(peer_, DATA_TYPE::AUDIO, (const char *)data, size);
}
});
if (0 != speaker_capturer_init_ret) {
speaker_capturer_->Destroy();
delete speaker_capturer_;
speaker_capturer_ = nullptr;
}
// Mouse control // Mouse control
device_controller_factory_ = new DeviceControllerFactory(); device_controller_factory_ = new DeviceControllerFactory();
} }
StartSpeakerCapture();
// Main loop // Main loop
while (!exit_) { while (!exit_) {
if (SignalStatus::SignalConnected == signal_status_ && if (SignalStatus::SignalConnected == signal_status_ &&
!is_create_connection_) { !is_create_connection_ && password_inited_) {
LOG_INFO("Connected with signal server, create p2p connection"); LOG_INFO("Connected with signal server, create p2p connection");
is_create_connection_ = is_create_connection_ =
CreateConnection(peer_, client_id_, password_saved_.c_str()) ? false CreateConnection(peer_, client_id_, password_saved_.c_str()) ? false
@@ -463,13 +440,13 @@ int Render::Run() {
localization_language_index_last_ = localization_language_index_; localization_language_index_last_ = localization_language_index_;
} }
if (start_screen_capture_ && !screen_capture_is_started_) { // if (start_screen_capture_ && !screen_capture_is_started_) {
StartScreenCapture(); // StartScreenCapture();
screen_capture_is_started_ = true; // screen_capture_is_started_ = true;
} else if (!start_screen_capture_ && screen_capture_is_started_) { // } else if (!start_screen_capture_ && screen_capture_is_started_) {
StopScreenCapture(); // StopScreenCapture();
screen_capture_is_started_ = false; // screen_capture_is_started_ = false;
} // }
if (start_mouse_control_ && !mouse_control_is_started_) { if (start_mouse_control_ && !mouse_control_is_started_) {
StartMouseControl(); StartMouseControl();
@@ -621,6 +598,39 @@ int Render::Run() {
} }
// Cleanup // Cleanup
if (screen_capturer_) {
screen_capturer_->Destroy();
delete screen_capturer_;
screen_capturer_ = nullptr;
}
if (speaker_capturer_) {
speaker_capturer_->Destroy();
delete speaker_capturer_;
speaker_capturer_ = nullptr;
}
if (mouse_controller_) {
mouse_controller_->Destroy();
delete mouse_controller_;
mouse_controller_ = nullptr;
}
if (screen_capturer_factory_) {
delete screen_capturer_factory_;
screen_capturer_factory_ = nullptr;
}
if (speaker_capturer_factory_) {
delete speaker_capturer_factory_;
speaker_capturer_factory_ = nullptr;
}
if (device_controller_factory_) {
delete device_controller_factory_;
device_controller_factory_ = nullptr;
}
if (is_create_connection_) { if (is_create_connection_) {
LOG_INFO("[{}] Leave connection [{}]", client_id_, client_id_); LOG_INFO("[{}] Leave connection [{}]", client_id_, client_id_);
LeaveConnection(peer_, client_id_); LeaveConnection(peer_, client_id_);

View File

@@ -77,9 +77,6 @@ class Render {
int SaveSettingsIntoCacheFile(); int SaveSettingsIntoCacheFile();
int LoadSettingsFromCacheFile(); int LoadSettingsFromCacheFile();
int StartScreenCapture();
int StopScreenCapture();
int StartSpeakerCapture(); int StartSpeakerCapture();
int StopSpeakerCapture(); int StopSpeakerCapture();

View File

@@ -145,11 +145,15 @@ void Render::OnReceiveDataBufferCb(const char *data, size_t size,
RemoteAction remote_action; RemoteAction remote_action;
memcpy(&remote_action, data, sizeof(remote_action)); memcpy(&remote_action, data, sizeof(remote_action));
#if MOUSE_CONTROL if (ControlType::mouse == remote_action.type && render->mouse_controller_) {
if (render->mouse_controller_) {
render->mouse_controller_->SendCommand(remote_action); render->mouse_controller_->SendCommand(remote_action);
} else if (ControlType::audio_capture == remote_action.type) {
if (remote_action.a) {
render->StartSpeakerCapture();
} else {
render->StopSpeakerCapture();
}
} }
#endif
} }
void Render::OnSignalStatusCb(SignalStatus status, void *user_data) { void Render::OnSignalStatusCb(SignalStatus status, void *user_data) {

View File

@@ -88,10 +88,13 @@ int SpeakerCapturerWasapi::Start() {
} }
int SpeakerCapturerWasapi::Stop() { int SpeakerCapturerWasapi::Stop() {
ma_device_stop(&device_);
return 0;
}
int SpeakerCapturerWasapi::Destroy() {
ma_device_uninit(&device_); ma_device_uninit(&device_);
return 0; return 0;
} }
int SpeakerCapturerWasapi::Destroy() { return 0; }
int SpeakerCapturerWasapi::Pause() { return 0; } int SpeakerCapturerWasapi::Pause() { return 0; }