Skip to content

Commit

Permalink
とりあえずリークしなくなった
Browse files Browse the repository at this point in the history
  • Loading branch information
tnoho committed Jan 2, 2025
1 parent c1cb589 commit c371a0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
26 changes: 26 additions & 0 deletions src/sora_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,29 @@ def __init__(self, track):
def __del__(self):
super().__del__()
del self.__track


class Sora(SoraImpl):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._connections = []

def __del__(self):
self._connections.clear()

def create_connection(self, *args, **kwargs):
# 新しい接続を生成してリストに追加
conn = super().create_connection(*args, **kwargs)
self._connections.append(conn)
return conn

def _remove_connection(self, conn):
# 閉じられた接続をリストから除外
if conn in self._connections:
self._connections.remove(conn)

def create_audio_source(self, *args):
return super().create_audio_source(*args)

def create_video_source(self):
return super().create_video_source()
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 @@ -561,7 +529,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, "SoraImpl")
.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

0 comments on commit c371a0f

Please sign in to comment.