Skip to content

Commit

Permalink
Added new Exception while connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
Laky-64 committed Feb 3, 2024
1 parent c06e554 commit 5bc69d5
Showing 1 changed file with 67 additions and 64 deletions.
131 changes: 67 additions & 64 deletions ntgcalls/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,74 +47,77 @@ namespace ntgcalls {
stream->setAVStream(config);
}

void Client::connect(const std::string& jsonData) const
{
auto data = json::parse(jsonData);
if (!data["rtmp"].is_null()) {
throw RTMPNeeded("Needed rtmp connection");
}
if (data["transport"].is_null()) {
throw InvalidParams("Transport not found");
}
data = data["transport"];
wrtc::Conference conference;
void Client::connect(const std::string& jsonData) const {
try {
conference = {
{
data["ufrag"].get<std::string>(),
data["pwd"].get<std::string>()
},
audioSource,
sourceGroups
};
for (const auto& item : data["fingerprints"].items()) {
conference.transport.fingerprints.push_back({
item.value()["hash"],
item.value()["fingerprint"],
});
auto data = json::parse(jsonData);
if (!data["rtmp"].is_null()) {
throw RTMPNeeded("Needed rtmp connection");
}
for (const auto& item : data["candidates"].items()) {
conference.transport.candidates.push_back({
item.value()["generation"].get<std::string>(),
item.value()["component"].get<std::string>(),
item.value()["protocol"].get<std::string>(),
item.value()["port"].get<std::string>(),
item.value()["ip"].get<std::string>(),
item.value()["foundation"].get<std::string>(),
item.value()["id"].get<std::string>(),
item.value()["priority"].get<std::string>(),
item.value()["type"].get<std::string>(),
item.value()["network"].get<std::string>()
});
if (data["transport"].is_null()) {
throw InvalidParams("Transport not found");
}
} catch (...) {
throw InvalidParams("Invalid transport");
}

const auto remoteDescription = wrtc::Description(
wrtc::Description::Type::Answer,
wrtc::SdpBuilder::fromConference(conference)
);
connection->setRemoteDescription(remoteDescription);

wrtc::Sync<void> waitConnection;
connection->onIceStateChange([&waitConnection](const wrtc::IceState state) {
switch (state) {
case wrtc::IceState::Connected:
waitConnection.onSuccess();
break;
case wrtc::IceState::Disconnected:
case wrtc::IceState::Failed:
case wrtc::IceState::Closed:
waitConnection.onFailed(ConnectionError("Connection failed to Telegram WebRTC"));
break;
default:
break;
data = data["transport"];
wrtc::Conference conference;
try {
conference = {
{
data["ufrag"].get<std::string>(),
data["pwd"].get<std::string>()
},
audioSource,
sourceGroups
};
for (const auto& item : data["fingerprints"].items()) {
conference.transport.fingerprints.push_back({
item.value()["hash"],
item.value()["fingerprint"],
});
}
for (const auto& item : data["candidates"].items()) {
conference.transport.candidates.push_back({
item.value()["generation"].get<std::string>(),
item.value()["component"].get<std::string>(),
item.value()["protocol"].get<std::string>(),
item.value()["port"].get<std::string>(),
item.value()["ip"].get<std::string>(),
item.value()["foundation"].get<std::string>(),
item.value()["id"].get<std::string>(),
item.value()["priority"].get<std::string>(),
item.value()["type"].get<std::string>(),
item.value()["network"].get<std::string>()
});
}
} catch (...) {
throw InvalidParams("Invalid transport");
}
});
waitConnection.wait();
connection->onIceStateChange(nullptr);
stream->start();

const auto remoteDescription = wrtc::Description(
wrtc::Description::Type::Answer,
wrtc::SdpBuilder::fromConference(conference)
);
connection->setRemoteDescription(remoteDescription);

wrtc::Sync<void> waitConnection;
connection->onIceStateChange([&waitConnection](const wrtc::IceState state) {
switch (state) {
case wrtc::IceState::Connected:
waitConnection.onSuccess();
break;
case wrtc::IceState::Disconnected:
case wrtc::IceState::Failed:
case wrtc::IceState::Closed:
waitConnection.onFailed(ConnectionError("Connection failed to Telegram WebRTC"));
break;
default:
break;
}
});
waitConnection.wait();
connection->onIceStateChange(nullptr);
stream->start();
} catch (const std::exception &exc) {
throw wrtc::RTCException(exc.what());
}
}

bool Client::pause() const {
Expand Down

0 comments on commit 5bc69d5

Please sign in to comment.