提交 fc9b9c51 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

room.close() and peer.close() do close their Worker instances.

上级 a9262fec
...@@ -12,6 +12,9 @@ class Peer extends EventEmitter ...@@ -12,6 +12,9 @@ class Peer extends EventEmitter
super(); super();
// Room identificator
this._roomId = options.roomId;
// Peer identificator // Peer identificator
this._peerId = options.peerId; this._peerId = options.peerId;
...@@ -33,6 +36,23 @@ class Peer extends EventEmitter ...@@ -33,6 +36,23 @@ class Peer extends EventEmitter
this._closed = true; this._closed = true;
let request =
{
roomId: this._roomId,
peerId: this._peerId
};
// Send Channel request
this._channel.request('closePeer', request)
.then(() =>
{
logger.debug('"closePeer" request succeeded');
})
.catch((error) =>
{
logger.error('"closePeer" request failed [status:%s, error:"%s"]', error.status, error.message);
});
this.emit('close', error); this.emit('close', error);
} }
} }
......
...@@ -45,6 +45,22 @@ class Room extends EventEmitter ...@@ -45,6 +45,22 @@ class Room extends EventEmitter
peer.close(); peer.close();
} }
let request =
{
roomId: this._roomId
};
// Send Channel request
this._channel.request('closeRoom', request)
.then(() =>
{
logger.debug('"closeRoom" request succeeded');
})
.catch((error) =>
{
logger.error('"closeRoom" request failed [status:%s, error:"%s"]', error.status, error.message);
});
this.emit('close', error); this.emit('close', error);
} }
......
...@@ -113,21 +113,26 @@ class Worker extends EventEmitter ...@@ -113,21 +113,26 @@ class Worker extends EventEmitter
{ {
logger.debug('close() [error:%s]', error); logger.debug('close() [error:%s]', error);
// Kill child // Close every Room
if (this._child) this._rooms.forEach((room) => room.close());
{
this._child.removeAllListeners('exit');
this._child.removeAllListeners('error');
this._child.kill('SIGTERM');
}
this._child = null; // Kill mediasoup-worker process (let it some time so previous
// requests are properly sent and replied)
setTimeout(() =>
{
// Kill child
if (this._child)
{
this._child.removeAllListeners('exit');
this._child.removeAllListeners('error');
this._child.kill('SIGTERM');
}
// Close the Channel instance this._child = null;
this._channel.close();
// Close every Room // Close the Channel instance
this._rooms.forEach((room) => room.close()); this._channel.close();
}, 100);
this.emit('close', error); this.emit('close', error);
} }
......
...@@ -56,3 +56,26 @@ tap.test('room.createPeer() with same `peerId` must fail', { timeout: 1000 }, (t ...@@ -56,3 +56,26 @@ tap.test('room.createPeer() with same `peerId` must fail', { timeout: 1000 }, (t
}) })
.catch((error) => t.fail(`should not fail: ${error}`)); .catch((error) => t.fail(`should not fail: ${error}`));
}); });
tap.test('room.createPeer() with same `peerId` must succeed if previous peer is closed before', { timeout: 1000 }, (t) =>
{
let server = mediasoup.Server();
t.tearDown(() => server.close());
server.createRoom()
.then((room) =>
{
room.createPeer('alice')
.then((peer) =>
{
peer.close();
room.createPeer('alice')
.then(() => t.end())
.catch((error) => t.fail(`should not fail: ${error}`));
})
.catch((error) => t.fail(`should not fail: ${error}`));
})
.catch((error) => t.fail(`should not fail: ${error}`));
});
...@@ -18,7 +18,9 @@ namespace Channel ...@@ -18,7 +18,9 @@ namespace Channel
{ {
updateSettings = 1, updateSettings = 1,
createRoom, createRoom,
createPeer closeRoom,
createPeer,
closePeer
}; };
public: public:
......
...@@ -21,6 +21,7 @@ namespace RTC ...@@ -21,6 +21,7 @@ namespace RTC
virtual ~Room(); virtual ~Room();
void HandleCreatePeerRequest(Channel::Request* request); void HandleCreatePeerRequest(Channel::Request* request);
void HandleClosePeerRequest(Channel::Request* request);
void Close(); void Close();
private: private:
......
...@@ -15,7 +15,9 @@ namespace Channel ...@@ -15,7 +15,9 @@ namespace Channel
{ {
{ "updateSettings", Request::MethodId::updateSettings }, { "updateSettings", Request::MethodId::updateSettings },
{ "createRoom", Request::MethodId::createRoom }, { "createRoom", Request::MethodId::createRoom },
{ "createPeer", Request::MethodId::createPeer } { "closeRoom", Request::MethodId::closeRoom },
{ "createPeer", Request::MethodId::createPeer },
{ "closePeer", Request::MethodId::closePeer }
}; };
/* Class methods. */ /* Class methods. */
......
...@@ -76,6 +76,7 @@ void Loop::Close() ...@@ -76,6 +76,7 @@ void Loop::Close()
if (this->closed) if (this->closed)
{ {
MS_ERROR("already closed"); MS_ERROR("already closed");
return; return;
} }
this->closed = true; this->closed = true;
...@@ -155,7 +156,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -155,7 +156,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
request->Reject(500, error.what()); request->Reject(500, error.what());
return; return;
} }
...@@ -164,7 +164,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -164,7 +164,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_ERROR("Room already exists"); MS_ERROR("Room already exists");
request->Reject(500, "Room already exists"); request->Reject(500, "Room already exists");
return; return;
} }
...@@ -175,11 +174,48 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -175,11 +174,48 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_ERROR("failed to create Room"); MS_ERROR("failed to create Room");
request->Reject(500, "failed to create Room"); request->Reject(500, "failed to create Room");
return; return;
} }
this->rooms[roomId] = room; this->rooms[roomId] = room;
MS_DEBUG("Room created [roomId:%u]", roomId);
request->Accept();
break;
}
case Channel::Request::MethodId::closeRoom:
{
MS_DEBUG("'closeRoom' method");
RTC::Room* room;
unsigned int roomId;
try
{
room = GetRoomFromRequest(request, &roomId);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!room)
{
MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist");
return;
}
room->Close();
// TODO: Instead of this, the Room should fire an onRoomClosed() here.
this->rooms.erase(roomId);
MS_DEBUG("Room closed [roomId:%u]", roomId);
request->Accept(); request->Accept();
break; break;
...@@ -198,7 +234,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -198,7 +234,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
request->Reject(500, error.what()); request->Reject(500, error.what());
return; return;
} }
...@@ -207,7 +242,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -207,7 +242,6 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
MS_ERROR("Room does not exist"); MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist"); request->Reject(500, "Room does not exist");
return; return;
} }
...@@ -216,6 +250,35 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request ...@@ -216,6 +250,35 @@ void Loop::onChannelRequest(Channel::UnixStreamSocket* channel, Channel::Request
break; break;
} }
case Channel::Request::MethodId::closePeer:
{
MS_DEBUG("'closePeer' method");
RTC::Room* room;
try
{
room = GetRoomFromRequest(request);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!room)
{
MS_ERROR("Room does not exist");
request->Reject(500, "Room does not exist");
return;
}
room->HandleClosePeerRequest(request);
break;
}
default: default:
{ {
MS_ABORT("unexpected methodId"); MS_ABORT("unexpected methodId");
......
...@@ -88,6 +88,42 @@ namespace RTC ...@@ -88,6 +88,42 @@ namespace RTC
} }
this->peers[peerId] = peer; this->peers[peerId] = peer;
MS_DEBUG("Peer created [peerId:%s]", peerId.c_str());
request->Accept();
}
void Room::HandleClosePeerRequest(Channel::Request* request)
{
MS_TRACE();
RTC::Peer* peer;
std::string peerId;
try
{
peer = GetPeerFromRequest(request, &peerId);
}
catch (const MediaSoupError &error)
{
request->Reject(500, error.what());
return;
}
if (!peer)
{
MS_ERROR("Peer does not exist");
request->Reject(500, "Peer does not exist");
return;
}
peer->Close();
// TODO: Instead of this, the Peer should fire an onPeerClosed() here.
this->peers.erase(peerId);
MS_DEBUG("Peer closed [peerId:%s]", peerId.c_str());
request->Accept(); request->Accept();
} }
......
...@@ -186,7 +186,6 @@ void Settings::HandleUpdateRequest(Channel::Request* request) ...@@ -186,7 +186,6 @@ void Settings::HandleUpdateRequest(Channel::Request* request)
catch (const MediaSoupError &error) catch (const MediaSoupError &error)
{ {
request->Reject(500, error.what()); request->Reject(500, error.what());
return; return;
} }
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论