Skip to content

Commit fb3230d

Browse files
refactor: improve audio handling
1 parent 7248864 commit fb3230d

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

src/client/client_context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ struct ClientContext {
2020
std::shared_ptr<VirtualUSBHubService> virtual_usb_hub_service;
2121
std::shared_ptr<WebRTCService> webrtc_service;
2222
std::shared_ptr<audivis_widget> root_widget;
23+
24+
std::optional<std::thread> audio_thread;
2325
};
2426

2527
} // namespace client

src/client/main.cc

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,6 @@ ClientContext::ClientContext() {
649649

650650
void ClientContext::init_webrtc_service() {
651651
webrtc_service = std::make_shared<WebRTCService>(
652-
[&](const std::vector<uint8_t> &data) {
653-
if (virtual_usb_hub_service && virtual_usb_hub_service->device_) {
654-
return virtual_usb_hub_service->device_->submit_audio_data(data);
655-
}
656-
657-
return false;
658-
},
659652
[&](const WebRTCService::WebRTCStatus &status) {
660653
root_widget->owner_rt->post_loop_thread_task([this, status]() {
661654
if (status.state == WebRTCService::ConnectionState::Gathering) {
@@ -674,6 +667,34 @@ void ClientContext::init_webrtc_service() {
674667
}
675668
});
676669
});
670+
671+
if (!audio_thread) {
672+
audio_thread.emplace([&]() {
673+
while (true) {
674+
std::this_thread::yield();
675+
std::unique_lock<std::mutex> lock(webrtc_service->audioBufferMutex_);
676+
while (webrtc_service->audioBuffer_.size() >= 960) {
677+
std::vector<uint8_t> buffer(webrtc_service->audioBuffer_.begin(),
678+
webrtc_service->audioBuffer_.begin() +
679+
960);
680+
if (virtual_usb_hub_service && virtual_usb_hub_service->device_) {
681+
try {
682+
if (virtual_usb_hub_service->device_->submit_audio_data(buffer)) {
683+
webrtc_service->audioBuffer_.erase(
684+
webrtc_service->audioBuffer_.begin(),
685+
webrtc_service->audioBuffer_.begin() + 960);
686+
}
687+
} catch (const std::exception &e) {
688+
std::cerr << "Error submitting audio data: " << e.what()
689+
<< std::endl;
690+
lock.unlock();
691+
break;
692+
}
693+
}
694+
}
695+
}
696+
});
697+
}
677698
}
678699

679700
} // namespace client

src/client/webrtc_service.cc

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ using json = nlohmann::json;
66

77
namespace client {
88

9-
WebRTCService::WebRTCService(AudioDataCallback audio_data_callback,
10-
StatusCallback status_callback)
9+
WebRTCService::WebRTCService(StatusCallback status_callback)
1110
: cli_("http://audivis-signaling-server.microblock.cc"),
12-
audio_data_callback_(std::move(audio_data_callback)),
1311
status_callback_(std::move(status_callback)) {
1412
cli_.set_connection_timeout(0, 300000); // 5 minutes
1513
setup_peer_connection();
@@ -109,18 +107,8 @@ void WebRTCService::setup_data_channel() {
109107
for (const auto &byte : *binary) {
110108
data.push_back(static_cast<uint8_t>(byte));
111109
}
110+
std::lock_guard<std::mutex> lock(audioBufferMutex_);
112111
audioBuffer_.insert(audioBuffer_.end(), data.begin(), data.end());
113-
114-
std::println("Received audio data, buffer size: {}", audioBuffer_.size());
115-
while (audioBuffer_.size() >= 960) {
116-
std::vector<uint8_t> buffer(audioBuffer_.begin(),
117-
audioBuffer_.begin() + 960);
118-
if (audio_data_callback_ && audio_data_callback_(buffer)) {
119-
audioBuffer_.erase(audioBuffer_.begin(), audioBuffer_.begin() + 960);
120-
}
121-
}
122-
std::println("Processed audio data, remaining buffer size: {}",
123-
audioBuffer_.size());
124112
}
125113
});
126114
}

src/client/webrtc_service.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ struct WebRTCService {
2727
std::string session_id;
2828
};
2929

30-
using AudioDataCallback = std::function<bool(const std::vector<uint8_t>&)>;
3130
using StatusCallback = std::function<void(const WebRTCStatus&)>;
3231

33-
WebRTCService(AudioDataCallback audio_data_callback, StatusCallback status_callback);
32+
WebRTCService(StatusCallback status_callback);
3433
~WebRTCService();
3534

3635
void start_signaling();
@@ -40,7 +39,7 @@ struct WebRTCService {
4039
std::shared_ptr<rtc::PeerConnection> pc_;
4140
std::shared_ptr<rtc::DataChannel> dc_;
4241
std::deque<uint8_t> audioBuffer_;
43-
AudioDataCallback audio_data_callback_;
42+
std::mutex audioBufferMutex_;
4443
StatusCallback status_callback_;
4544
std::string session_id_;
4645

0 commit comments

Comments
 (0)