Skip to content

Instantly share code, notes, and snippets.

@wpaladins
Last active May 11, 2021 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpaladins/3534d1a799d759c520f59447267366d2 to your computer and use it in GitHub Desktop.
Save wpaladins/3534d1a799d759c520f59447267366d2 to your computer and use it in GitHub Desktop.
// This function is called when the websocket connection is established
void MediaManagerInterface::OnConnected()
{
// _signaling is the object of sending signaling through websocket
_signaling->AsyncRequest("getRouterRtpCapabilities", nlohmann::json(), [this](nlohmann::json jMsg) {
createFactory(); // Create webrtc threads, etc.
mediasoupclient::PeerConnection::Options op;
op.factory = _peerConnectionFactory;
if(!_device->IsLoaded()) {
_device->Load(jMsg["data"], &op);
}
// sctpCapabilities is added for datachannel
nlohmann::json j1 = {
{ "forceTcp", false },
{ "producing", true },
{ "consuming", false },
{ "sctpCapabilities", _device->GetSctpCapabilities() }
};
nlohmann::json r1 = _signaling->SyncRequest("createWebRtcTransport", j1);
{
nlohmann::json jData = r1["data"];
auto* sendTransportListener = new SendTransportListener(_signaling);
// This will block the current thread until completion.
mediasoupclient::PeerConnection::Options op;
op.factory = _peerConnectionFactory;
_sendTransport.reset(_device->CreateSendTransport(
sendTransportListener,
jData["id"],
jData["iceParameters"],
jData["iceCandidates"],
jData["dtlsParameters"],
jData["sctpParameters"],
&op));
}
nlohmann::json j2 = { { "forceTcp", false },
{ "producing", false },
{ "consuming", true },
{ "sctpCapabilities", _device->GetSctpCapabilities() } };
nlohmann::json r2 = _signaling->SyncRequest("createWebRtcTransport", j2);
{
nlohmann::json jData = r2["data"];
mediasoupclient::PeerConnection::Options op;
op.factory = _peerConnectionFactory;
auto* recvTransportListener = new RecvTransportListener(_signaling);
_recvTransport.reset(_device->CreateRecvTransport(
recvTransportListener,
jData["id"],
jData["iceParameters"],
jData["iceCandidates"],
jData["dtlsParameters"],
jData["sctpParameters"],
&op));
}
// join room
nlohmann::json j = {
{ "displayName", _myName },
{ "device", "windows" },
{ "rtpCapabilities", _device->GetRtpCapabilities() },
{ "sctpCapabilities", _device->GetSctpCapabilities() }
};
_signaling->AsyncRequest("join", j, [this](nlohmann::json jMsg) {
createLocalProducer(); // This method will create a DataProducer for chat
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment