Skip to content

Commit

Permalink
multiplex: Fix issue #165, enable ZRTP initialization with 3 or more …
Browse files Browse the repository at this point in the history
…streams in separate threads
  • Loading branch information
tampsa committed Jul 24, 2023
1 parent 72ff6b3 commit 5f4da9f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
20 changes: 19 additions & 1 deletion src/media_stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ rtp_error_t uvgrtp::media_stream::free_resources(rtp_error_t ret)
media_ = nullptr;
socket_ = nullptr;

if (zrtp_) {
zrtp_->set_zrtp_busy(false);
}

return ret;
}

Expand Down Expand Up @@ -381,7 +385,20 @@ rtp_error_t uvgrtp::media_stream::init(std::shared_ptr<uvgrtp::zrtp> zrtp)
}
}
}

/* If ZRTP is already performing an MSM negotiation, wait for it to complete before starting a new one */
if (!perform_dh) {
auto start = std::chrono::system_clock::now();
while (zrtp_->is_zrtp_busy()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - start).count() > 10)
{
UVG_LOG_ERROR("Giving up on MSM after 10 seconds");
return free_resources(RTP_TIMEOUT);
}
}
}

zrtp_->set_zrtp_busy(true);
ret = RTP_OK;
if ((ret = zrtp->init(rtp_->get_ssrc(), socket_, remote_sockaddr_, remote_sockaddr_ip6_, perform_dh, ipv6_)) != RTP_OK) {
UVG_LOG_WARN("Failed to initialize ZRTP for media stream!");
Expand All @@ -394,6 +411,7 @@ rtp_error_t uvgrtp::media_stream::init(std::shared_ptr<uvgrtp::zrtp> zrtp)
if ((ret = init_srtp_with_zrtp(rce_flags_, SRTCP, srtcp_, zrtp)) != RTP_OK)
return free_resources(ret);

zrtp_->set_zrtp_busy(false);
zrtp->dh_has_finished(); // only after the DH stream has gotten its keys, do we let non-DH stream perform ZRTP
install_packet_handlers();

Expand Down
4 changes: 2 additions & 2 deletions src/zrtp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ uvgrtp::zrtp::zrtp():
confack_(nullptr),
hello_len_(0),
commit_len_(0),
dh_len_(0)
dh_len_(0),
zrtp_busy_(false)
{
cctx_.sha256 = new uvgrtp::crypto::sha256;
cctx_.dh = new uvgrtp::crypto::dh;
Expand Down Expand Up @@ -930,7 +931,6 @@ rtp_error_t uvgrtp::zrtp::init_msm(uint32_t ssrc, std::shared_ptr<uvgrtp::socket
return ret;
}
}

return RTP_OK;
}

Expand Down
11 changes: 10 additions & 1 deletion src/zrtp.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ namespace uvgrtp {
dh_finished_ = true;
}

inline bool is_zrtp_busy() const
{
return zrtp_busy_;
}
inline void set_zrtp_busy(bool status)
{
zrtp_busy_ = status;
}

private:
/* Initialize ZRTP session between us and remote using Diffie-Hellman Mode
*
Expand Down Expand Up @@ -209,7 +218,7 @@ namespace uvgrtp {

std::mutex state_mutex_;
bool dh_finished_ = false;

bool zrtp_busy_;

};
}
Expand Down

0 comments on commit 5f4da9f

Please sign in to comment.