Skip to content

Commit 25536de

Browse files
committed
Python 側のエラーを出力する
1 parent 70f8f6d commit 25536de

File tree

5 files changed

+49
-14
lines changed

5 files changed

+49
-14
lines changed

src/sora_audio_sink.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <api/audio/channel_layout.h>
77
#include <modules/audio_mixer/audio_frame_manipulator.h>
88

9+
#include "sora_call.h"
10+
911
SoraAudioSinkImpl::SoraAudioSinkImpl(SoraTrackInterface* track,
1012
int output_sample_rate,
1113
size_t output_channels)
@@ -111,7 +113,7 @@ void SoraAudioSinkImpl::AppendData(const int16_t* audio_data,
111113
sample_rate_ = sample_rate;
112114
number_of_channels_ = number_of_channels;
113115
if (on_format_) {
114-
on_format_(sample_rate_, number_of_channels_);
116+
call_python(on_format_, sample_rate_, number_of_channels_);
115117
}
116118
}
117119

@@ -129,7 +131,7 @@ void SoraAudioSinkImpl::AppendData(const int16_t* audio_data,
129131
auto data = nb::ndarray<nb::numpy, int16_t, nb::shape<-1, -1>>(
130132
(void*)audio_data, 2, shape, nb::handle());
131133
/* まだ使ったことながない。現状 Python 側で on_frame と同じ感覚でコールバックの外に値を持ち出すと落ちるはず。 */
132-
on_data_(data);
134+
call_python(on_data_, data);
133135
}
134136
}
135137

src/sora_audio_stream_sink.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <modules/audio_processing/agc2/rnn_vad/common.h>
1111
#include <modules/audio_processing/include/audio_frame_view.h>
1212

13+
#include "sora_call.h"
14+
1315
SoraAudioFrameDefaultImpl::SoraAudioFrameDefaultImpl(
1416
std::unique_ptr<webrtc::AudioFrame> audio_frame)
1517
: audio_frame_(std::move(audio_frame)) {}
@@ -208,5 +210,6 @@ void SoraAudioStreamSinkImpl::OnData(
208210
webrtc::RemixFrame(output_channels_, tuned_frame.get());
209211
}
210212

211-
on_frame_(std::make_shared<SoraAudioFrame>(std::move(tuned_frame)));
213+
call_python(on_frame_,
214+
std::make_shared<SoraAudioFrame>(std::move(tuned_frame)));
212215
}

src/sora_call.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef SORA_CALL_H_
2+
#define SORA_CALL_H_
3+
4+
#include <type_traits>
5+
6+
// nanobind
7+
#include <nanobind/nanobind.h>
8+
9+
// WebRTC
10+
#include <rtc_base/logging.h>
11+
12+
template <class F, class... Args>
13+
auto call_python(F f, Args&&... args) -> std::invoke_result_t<F, Args...> {
14+
try {
15+
return f(args...);
16+
} catch (nanobind::python_error& e) {
17+
RTC_LOG(LS_ERROR) << "Failed to call python function: " << e.what();
18+
throw;
19+
} catch (std::exception& e) {
20+
RTC_LOG(LS_ERROR) << "Failed to call python function: " << e.what();
21+
throw;
22+
} catch (...) {
23+
RTC_LOG(LS_ERROR) << "Failed to call python function: Unknown exception";
24+
throw;
25+
}
26+
}
27+
28+
#endif

src/sora_connection.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <nanobind/nanobind.h>
1717

1818
#include "gil.h"
19+
#include "sora_call.h"
1920

2021
namespace nb = nanobind;
2122

@@ -180,7 +181,7 @@ void SoraConnection::OnSetOffer(std::string offer) {
180181
}
181182
}
182183
if (on_set_offer_) {
183-
on_set_offer_(offer);
184+
call_python(on_set_offer_, offer);
184185
}
185186
}
186187

@@ -189,34 +190,34 @@ void SoraConnection::OnDisconnect(sora::SoraSignalingErrorCode ec,
189190
gil_scoped_acquire acq;
190191
ioc_->stop();
191192
if (on_disconnect_) {
192-
on_disconnect_(ec, message);
193+
call_python(on_disconnect_, ec, message);
193194
}
194195
}
195196

196197
void SoraConnection::OnNotify(std::string text) {
197198
gil_scoped_acquire acq;
198199
if (on_notify_) {
199-
on_notify_(text);
200+
call_python(on_notify_, text);
200201
}
201202
}
202203

203204
void SoraConnection::OnPush(std::string text) {
204205
if (on_push_) {
205-
on_push_(text);
206+
call_python(on_push_, text);
206207
}
207208
}
208209

209210
void SoraConnection::OnMessage(std::string label, std::string data) {
210211
gil_scoped_acquire acq;
211212
if (on_message_) {
212-
on_message_(label, nb::bytes(data.c_str(), data.size()));
213+
call_python(on_message_, label, nb::bytes(data.c_str(), data.size()));
213214
}
214215
}
215216

216217
void SoraConnection::OnSwitched(std::string text) {
217218
gil_scoped_acquire acq;
218219
if (on_switched_) {
219-
on_switched_(text);
220+
call_python(on_switched_, text);
220221
}
221222
}
222223

@@ -225,14 +226,14 @@ void SoraConnection::OnSignalingMessage(sora::SoraSignalingType type,
225226
std::string message) {
226227
gil_scoped_acquire acq;
227228
if (on_signaling_message_) {
228-
on_signaling_message_(type, direction, message);
229+
call_python(on_signaling_message_, type, direction, message);
229230
}
230231
}
231232

232233
void SoraConnection::OnWsClose(uint16_t code, std::string message) {
233234
gil_scoped_acquire acq;
234235
if (on_ws_close_) {
235-
on_ws_close_(code, message);
236+
call_python(on_ws_close_, code, message);
236237
}
237238
}
238239

@@ -244,7 +245,7 @@ void SoraConnection::OnTrack(
244245
// shared_ptr になってないとリークする
245246
auto track = std::make_shared<SoraMediaTrack>(this, receiver);
246247
AddSubscriber(track.get());
247-
on_track_(track);
248+
call_python(on_track_, track);
248249
}
249250
}
250251

@@ -257,6 +258,6 @@ void SoraConnection::OnRemoveTrack(
257258
void SoraConnection::OnDataChannel(std::string label) {
258259
gil_scoped_acquire acq;
259260
if (on_data_channel_) {
260-
on_data_channel_(label);
261+
call_python(on_data_channel_, label);
261262
}
262263
}

src/sora_video_sink.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <third_party/libyuv/include/libyuv.h>
88

99
#include "gil.h"
10+
#include "sora_call.h"
1011

1112
SoraVideoFrame::SoraVideoFrame(
1213
rtc::scoped_refptr<webrtc::I420BufferInterface> i420_data)
@@ -108,7 +109,7 @@ void SoraVideoSinkImpl::OnFrame(const webrtc::VideoFrame& frame) {
108109
*/
109110
rtc::scoped_refptr<webrtc::I420BufferInterface> i420_data =
110111
frame.video_frame_buffer()->ToI420();
111-
on_frame_(std::make_shared<SoraVideoFrame>(i420_data));
112+
call_python(on_frame_, std::make_shared<SoraVideoFrame>(i420_data));
112113
}
113114
});
114115
}

0 commit comments

Comments
 (0)