Skip to content

Commit

Permalink
Merge pull request #89 from shiguredo/feature/add-on-ws-close
Browse files Browse the repository at this point in the history
on_ws_close コールバックを追加する
  • Loading branch information
voluntas authored Sep 28, 2024
2 parents a863185 + 082e4c8 commit 53f3574
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 26 deletions.
9 changes: 8 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@

## develop

- [CHANGE] client_cert と client_key の指定にはパスではなく中身の文字列を指定するようにする
- C++ SDK 側の仕様変更に追従する
- @voluntas
- [ADD] サーバー証明書チェック用の CA 証明書を指定できるようにする
- `Sora.create_connection()` の引数に `ca_cert` を追加する
- @voluntas
- [ADD] `on_ws_close` コールバックを追加する
- @tnoho
- [ADD] `on_signaling_message` コールバックを追加する
- @tnoho
- [ADD] Ubuntu 24.04 armv8 に対応する
- @melpon
- [ADD] `on_ws_close` コールバックを追加する
- @tnoho
- [UPDATE] Sora C++ SDK のバージョンを `2024.8.0` に上げる
- WEBRTC_BUILD_VERSION を `m128.6613.2.0` に上げる
- WEBRTC_BUILD_VERSION を `m129.6668.1.0` に上げる
- libwebrtc のモジュール分割に追従するため rtc::CreateRandomString のヘッダを追加
- CMAKE_VERSION を `3.30.3` に上げる
- BOOST_VERSION を `1.86.0` に上げる
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ PyPI 経由ではインストールできません。
- macOS Ventura 14 arm64
- macOS Sonoma 13 arm64
- Ubuntu 24.04 LTS x86_64
- Ubuntu 24.04 LTS arm64
- Ubuntu 22.04 LTS x86_64
- Ubuntu 22.04 LTS arm64 (NVIDIA Jetson JetPack SDK 6)
- PyPI からではなくパッケージファイルを利用してください
Expand Down
4 changes: 2 additions & 2 deletions VERSION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SORA_CPP_SDK_VERSION=2024.8.0-canary.8
WEBRTC_BUILD_VERSION=m128.6613.3.1
SORA_CPP_SDK_VERSION=2024.8.0-canary.10
WEBRTC_BUILD_VERSION=m129.6668.1.0
BOOST_VERSION=1.86.0
CMAKE_VERSION=3.30.3
OPENH264_VERSION=v2.4.1
6 changes: 6 additions & 0 deletions src/sora_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ void SoraConnection::OnSignalingMessage(sora::SoraSignalingType type,
}
}

void SoraConnection::OnWsClose(uint16_t code, std::string message) {
if (on_ws_close_) {
on_ws_close_(code, message);
}
}

void SoraConnection::OnTrack(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) {
if (on_track_) {
Expand Down
2 changes: 2 additions & 0 deletions src/sora_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class SoraConnection : public sora::SoraSignalingObserver,
void OnSignalingMessage(sora::SoraSignalingType type,
sora::SoraSignalingDirection direction,
std::string message) override;
void OnWsClose(uint16_t code, std::string message);
void OnTrack(
rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver) override;
void OnRemoveTrack(
Expand All @@ -116,6 +117,7 @@ class SoraConnection : public sora::SoraSignalingObserver,
void(sora::SoraSignalingType, sora::SoraSignalingDirection, std::string)>
on_signaling_message_;
std::function<void(std::string)> on_set_offer_;
std::function<void(int, std::string)> on_ws_close_;
std::function<void(sora::SoraSignalingErrorCode, std::string)> on_disconnect_;
std::function<void(std::string)> on_notify_;
std::function<void(std::string)> on_push_;
Expand Down
6 changes: 6 additions & 0 deletions src/sora_sdk_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ int connection_tp_traverse(PyObject* self, visitproc visit, void* arg) {
Py_VISIT(on_set_offer.ptr());
}

if (conn->on_ws_close_) {
nb::object on_ws_close = nb::cast(conn->on_ws_close_, nb::rv_policy::none);
Py_VISIT(on_ws_close.ptr());
}

if (conn->on_disconnect_) {
nb::object on_disconnect =
nb::cast(conn->on_disconnect_, nb::rv_policy::none);
Expand Down Expand Up @@ -301,6 +306,7 @@ NB_MODULE(sora_sdk_ext, m) {
"data"_a)
.def("get_stats", &SoraConnection::GetStats)
.def_rw("on_set_offer", &SoraConnection::on_set_offer_)
.def_rw("on_ws_close", &SoraConnection::on_ws_close_)
.def_rw("on_disconnect", &SoraConnection::on_disconnect_)
.def_rw("on_signaling_message", &SoraConnection::on_signaling_message_)
.def_rw("on_notify", &SoraConnection::on_notify_)
Expand Down
28 changes: 28 additions & 0 deletions tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(
video_codec_type: Optional[str] = None,
video_bit_rate: Optional[int] = None,
data_channel_signaling: Optional[bool] = None,
ignore_disconnect_websocket: Optional[bool] = None,
openh264_path: Optional[str] = None,
use_hwa: bool = False,
audio_channels: int = 1,
Expand Down Expand Up @@ -71,6 +72,7 @@ def __init__(
video_codec_type=video_codec_type,
video_bit_rate=video_bit_rate,
data_channel_signaling=data_channel_signaling,
ignore_disconnect_websocket=ignore_disconnect_websocket,
audio_source=self._audio_source,
video_source=self._video_source,
ca_cert=ca_cert,
Expand All @@ -79,6 +81,8 @@ def __init__(

self._connected: Event = Event()
self._switched: bool = False
self._ignore_disconnect_websocket: bool = False
self._ws_close: bool = False
self._closed: Event = Event()
self._default_connection_timeout_s: float = 10.0

Expand All @@ -98,6 +102,7 @@ def __init__(
self._connection.on_switched = self._on_switched
self._connection.on_notify = self._on_notify
self._connection.on_disconnect = self._on_disconnect
self._connection.on_ws_close = self._on_ws_close

def connect(self, fake_audio=False, fake_video=False) -> None:
self._connection.connect()
Expand Down Expand Up @@ -162,6 +167,14 @@ def connected(self) -> bool:
def switched(self) -> bool:
return self._switched

@property
def ignore_disconnect_websocket(self) -> bool:
return self._ignore_disconnect_websocket

@property
def ws_close(self) -> bool:
return self._ws_close

def _fake_audio_loop(self):
while not self._closed.is_set():
time.sleep(0.02)
Expand Down Expand Up @@ -222,6 +235,7 @@ def _on_switched(self, raw_message: str) -> None:
if message["type"] == "switched":
print(f"Switched to DataChannel Signaling: connection_id={self._connection_id}")
self._switched = True
self._ignore_disconnect_websocket = message["ignore_disconnect_websocket"]

def _on_notify(self, raw_message: str) -> None:
message: dict[str, Any] = json.loads(raw_message)
Expand All @@ -244,6 +258,10 @@ def _on_disconnect(self, error_code: SoraSignalingErrorCode, message: str) -> No
if self._fake_video_thread is not None:
self._fake_video_thread.join(timeout=10)

def _on_ws_close(self, code: int, reason: str) -> None:
print(f"WebSocket closed: code={code} reason={reason}")
self._ws_close = True


class Recvonly:
def __init__(
Expand Down Expand Up @@ -275,6 +293,7 @@ def __init__(

self._connected: Event = Event()
self._switched: bool = False
self._ws_close: bool = False
self._closed: Event = Event()
self._default_connection_timeout_s: float = 10.0

Expand All @@ -299,6 +318,7 @@ def __init__(
self._connection.on_switched = self._on_switched
self._connection.on_notify = self._on_notify
self._connection.on_disconnect = self._on_disconnect
self._connection.on_ws_close = self._on_ws_close
self._connection.on_track = self._on_track

def connect(self) -> None:
Expand Down Expand Up @@ -359,6 +379,10 @@ def connected(self) -> bool:
def switched(self) -> bool:
return self._switched

@property
def ws_close(self) -> bool:
return self._ws_close

@property
def closed(self):
return self._closed.is_set()
Expand Down Expand Up @@ -427,6 +451,10 @@ def _on_disconnect(self, error_code: SoraSignalingErrorCode, message: str) -> No
self._connected.clear()
self._closed.is_set()

def _on_ws_close(self, code: int, reason: str) -> None:
print(f"WebSocket closed: code={code} reason={reason}")
self._ws_close = True

def _on_video_frame(self, frame: SoraVideoFrame) -> None:
self._q_out.put(frame)

Expand Down
46 changes: 23 additions & 23 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53f3574

Please sign in to comment.