Skip to content

Commit

Permalink
Merge pull request #71 from shiguredo/feature/get-stats
Browse files Browse the repository at this point in the history
get_stats 関数を追加
  • Loading branch information
melpon authored Jul 14, 2024
2 parents bc1539e + 04243b0 commit 0f3818a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v4
- run: |
sudo apt-get -y install libva-dev libdrm-dev
- uses: eifinger/setup-rye@v4
- uses: eifinger/setup-rye@v3
with:
version: 'latest'
- run: echo "$HOME/.rye/shims" >> $GITHUB_PATH
Expand Down Expand Up @@ -89,7 +89,7 @@ jobs:
sudo apt-get update
sudo apt-get -y install libva-dev libdrm-dev
if: ${{ matrix.platform.os == 'ubuntu' }}
- uses: eifinger/setup-rye@v4
- uses: eifinger/setup-rye@v3
with:
version: 'latest'
- run: echo "$HOME/.rye/shims" >> $GITHUB_PATH
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
- @voluntas
- [ADD] sora_sdk に型を付ける
- @melpon
- [ADD] SoraConnection に get_stats 関数を追加
- @melpon
- [FIX] SoraAudioSink.read が timeout を無視して失敗を返すケースがあったので修正する
- @enm10k
- [FIX] SoraAudioSink.read が timeout を無視するケースがある問題を修正した結果、 read の実行タイミングによってはクラッシュするようになったので修正する
Expand Down
20 changes: 20 additions & 0 deletions src/sora_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <chrono>
#include <stdexcept>

// Sora C++ SDK
#include <sora/rtc_stats.h>

// Boost
#include <boost/asio/signal_set.hpp>

Expand Down Expand Up @@ -106,6 +109,23 @@ bool SoraConnection::SendDataChannel(const std::string& label,
return conn_->SendDataChannel(label, std::string(data.c_str(), data.size()));
}

std::string SoraConnection::GetStats() {
auto pc = conn_->GetPeerConnection();
if (pc == nullptr) {
return "[]";
}
std::promise<std::string> stats;
std::future<std::string> future = stats.get_future();
nb::gil_scoped_release release;
pc->GetStats(
sora::RTCStatsCallback::Create(
[&](const rtc::scoped_refptr<const webrtc::RTCStatsReport>& report) {
stats.set_value(report->ToJson());
})
.get());
return future.get();
}

void SoraConnection::OnSetOffer(std::string offer) {
std::string stream_id = rtc::CreateRandomString(16);
if (audio_source_) {
Expand Down
8 changes: 8 additions & 0 deletions src/sora_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class SoraConnection : public sora::SoraSignalingObserver,
*/
bool SendDataChannel(const std::string& label, nb::bytes& data);

/**
* WebRTC の統計情報を取得します。
*
* この関数は PeerConnection::GetStats() を呼んで、結果のコールバックがやってくるまでスレッドをブロックすることに注意してください。
* また、libwebrtc のシグナリングスレッドから呼ぶとデッドロックするので、必ずそれ以外のスレッドから呼ぶようにしてください。
*/
std::string GetStats();

// sora::SoraSignalingObserver に定義されているコールバック関数
void OnSetOffer(std::string offer) override;
void OnDisconnect(sora::SoraSignalingErrorCode ec,
Expand Down
1 change: 1 addition & 0 deletions src/sora_sdk_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ NB_MODULE(sora_sdk_ext, m) {
.def("disconnect", &SoraConnection::Disconnect)
.def("send_data_channel", &SoraConnection::SendDataChannel, "label"_a,
"data"_a)
.def("get_stats", &SoraConnection::GetStats)
.def_rw("on_set_offer", &SoraConnection::on_set_offer_)
.def_rw("on_disconnect", &SoraConnection::on_disconnect_)
.def_rw("on_notify", &SoraConnection::on_notify_)
Expand Down

0 comments on commit 0f3818a

Please sign in to comment.