Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

メモリーリークの改善 #125

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
5 changes: 5 additions & 0 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
push:
paths-ignore:
- "**.md"
branches:
- feature/fix-memoryleak3
schedule:
# UTC の 01:00 は JST だと 10:00 。
# 1-5 で 月曜日から金曜日
Expand All @@ -31,6 +33,7 @@ jobs:
contains(github.event.head_commit.message, '[e2e]') ||
contains(github.ref, 'feature/e2e-test') ||
github.event_name == 'workflow_dispatch' ||
github.event_name == 'push' ||
github.event_name == 'schedule'
}}
steps:
Expand Down Expand Up @@ -73,6 +76,7 @@ jobs:
contains(github.event.head_commit.message, '[e2e]') ||
contains(github.ref, 'feature/e2e-test') ||
github.event_name == 'workflow_dispatch' ||
github.event_name == 'push' ||
github.event_name == 'schedule'
}}
steps:
Expand Down Expand Up @@ -106,6 +110,7 @@ jobs:
contains(github.event.head_commit.message, '[e2e]') ||
contains(github.ref, 'feature/e2e-test') ||
github.event_name == 'workflow_dispatch' ||
github.event_name == 'push' ||
github.event_name == 'schedule'
}}
steps:
Expand Down
10 changes: 2 additions & 8 deletions src/sora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ std::shared_ptr<SoraConnection> Sora::CreateConnection(
std::optional<std::string> proxy_password,
std::optional<std::string> proxy_agent,
std::optional<webrtc::DegradationPreference> degradation_preference) {
std::shared_ptr<SoraConnection> conn = std::make_shared<SoraConnection>(this);
std::shared_ptr<SoraConnection> conn =
std::make_shared<SoraConnection>(this, factory_);
sora::SoraSignalingConfig config;
config.pc_factory = factory_->GetPeerConnectionFactory();
config.observer = conn;
Expand Down Expand Up @@ -213,13 +214,6 @@ std::shared_ptr<SoraConnection> Sora::CreateConnection(
conn->SetVideoSenderFrameTransformer(video_frame_transformer);
}

weak_connections_.erase(
std::remove_if(
weak_connections_.begin(), weak_connections_.end(),
[](std::weak_ptr<SoraConnection> w) { return w.expired(); }),
weak_connections_.end());
weak_connections_.push_back(conn);

return conn;
}

Expand Down
4 changes: 1 addition & 3 deletions src/sora.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@ class Sora : public DisposePublisher {
*/
SoraVideoSource* CreateVideoSource();

std::vector<std::weak_ptr<SoraConnection>> weak_connections_;

private:
/**
* Python で渡された値を boost::json::value に変換します。
Expand All @@ -186,6 +184,6 @@ class Sora : public DisposePublisher {
std::optional<sora::SoraSignalingConfig::ForwardingFilter>
ConvertForwardingFilter(const nb::handle value);

std::unique_ptr<SoraFactory> factory_;
std::shared_ptr<SoraFactory> factory_;
};
#endif
7 changes: 5 additions & 2 deletions src/sora_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

namespace nb = nanobind;

SoraConnection::SoraConnection(DisposePublisher* publisher)
: publisher_(publisher) {
SoraConnection::SoraConnection(DisposePublisher* publisher,
std::shared_ptr<SoraFactory> factory)
: publisher_(publisher), factory_(factory) {
publisher_->AddSubscriber(this);
}

Expand All @@ -30,6 +31,8 @@ SoraConnection::~SoraConnection() {
publisher_->RemoveSubscriber(this);
}
Disposed();
conn_ = nullptr;
factory_ = nullptr;
}

void SoraConnection::Disposed() {
Expand Down
5 changes: 4 additions & 1 deletion src/sora_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace nb = nanobind;

class SoraFactory;
/**
* Sora との接続ごとに生成する SoraConnection です。
*
Expand All @@ -36,7 +37,8 @@ class SoraConnection : public sora::SoraSignalingObserver,
/**
* コンストラクタではインスタンスの生成のみで実際の生成処理は Init 関数で行います。
*/
SoraConnection(DisposePublisher* publisher);
SoraConnection(DisposePublisher* publisher,
std::shared_ptr<SoraFactory> factory);
~SoraConnection();

void Disposed() override;
Expand Down Expand Up @@ -146,6 +148,7 @@ class SoraConnection : public sora::SoraSignalingObserver,
std::function<void(std::string)> on_data_channel_;

private:
std::shared_ptr<SoraFactory> factory_;
DisposePublisher* publisher_;
std::unique_ptr<boost::asio::io_context> ioc_;
std::shared_ptr<sora::SoraSignaling> conn_;
Expand Down
34 changes: 1 addition & 33 deletions src/sora_sdk_ext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,38 +288,6 @@ PyType_Slot connection_slots[] = {
{Py_tp_clear, (void*)connection_tp_clear},
{0, nullptr}};

int sora_tp_traverse(PyObject* self, visitproc visit, void* arg) {
if (!nb::inst_ready(self)) {
return 0;
}

Sora* sora = nb::inst_ptr<Sora>(self);
for (auto wc : sora->weak_connections_) {
auto conn = wc.lock();
if (conn) {
nb::object conn_obj = nb::find(conn);
Py_VISIT(conn_obj.ptr());
}
}

return 0;
}

int sora_tp_clear(PyObject* self) {
if (!nb::inst_ready(self)) {
return 0;
}

Sora* sora = nb::inst_ptr<Sora>(self);
sora->weak_connections_.clear();

return 0;
}

PyType_Slot sora_slots[] = {{Py_tp_traverse, (void*)sora_tp_traverse},
{Py_tp_clear, (void*)sora_tp_clear},
{0, nullptr}};

/**
* Python で利用するすべてのクラスと定数は以下のように定義しなければならない
*/
Expand Down Expand Up @@ -568,7 +536,7 @@ NB_MODULE(sora_sdk_ext, m) {
.def("__del__", &SoraVideoFrameTransformer::Del)
.def_rw("on_transform", &SoraVideoFrameTransformer::on_transform_);

nb::class_<Sora>(m, "Sora", nb::type_slots(sora_slots))
nb::class_<Sora>(m, "Sora")
.def(nb::init<std::optional<bool>, std::optional<std::string>>(),
"use_hardware_encoder"_a = nb::none(), "openh264"_a = nb::none())
.def("create_connection", &Sora::CreateConnection, "signaling_urls"_a,
Expand Down
4 changes: 4 additions & 0 deletions src/sora_video_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ SoraVideoSource::SoraVideoSource(
}));
}

SoraVideoSource::~SoraVideoSource() {
Disposed();
}

void SoraVideoSource::Disposed() {
if (!finished_) {
finished_ = true;
Expand Down
1 change: 1 addition & 0 deletions src/sora_video_source.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SoraVideoSource : public SoraTrackInterface {
SoraVideoSource(DisposePublisher* publisher,
rtc::scoped_refptr<sora::ScalableVideoTrackSource> source,
rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> track);
~SoraVideoSource();

void Disposed() override;
void PublisherDisposed() override;
Expand Down
Loading