summaryrefslogtreecommitdiff
path: root/modules/gzrtp
diff options
context:
space:
mode:
authorglenvt18 <glenvt18@gmail.com>2017-11-12 18:27:42 +0300
committerAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-11-12 16:27:42 +0100
commit5e5bc86705149d660883ad6c24bb3d1a0d77ee69 (patch)
tree31eac599b36730a8e611699db209ee1321a28bd3 /modules/gzrtp
parent0643dcad57e3064e6a45c0943aa3336386382c69 (diff)
modules/gzrtp: make send handler thread-safe (#324)
It can be called from another thread (audio/video read).
Diffstat (limited to 'modules/gzrtp')
-rw-r--r--modules/gzrtp/stream.cpp26
-rw-r--r--modules/gzrtp/stream.h1
2 files changed, 20 insertions, 7 deletions
diff --git a/modules/gzrtp/stream.cpp b/modules/gzrtp/stream.cpp
index d09ca3b..84ad5e4 100644
--- a/modules/gzrtp/stream.cpp
+++ b/modules/gzrtp/stream.cpp
@@ -190,6 +190,7 @@ Stream::Stream(int& err, const ZRTPConfig& config, Session *session,
err = pthread_mutexattr_init(&attr);
err |= pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
err |= pthread_mutex_init(&m_zrtp_mutex, &attr);
+ err |= pthread_mutex_init(&m_send_mutex, &attr);
if (err)
return;
@@ -244,6 +245,7 @@ Stream::~Stream()
mem_deref(m_rtcpsock);
pthread_mutex_destroy(&m_zrtp_mutex);
+ pthread_mutex_destroy(&m_send_mutex);
tmr_cancel(&m_zrtp_timer);
}
@@ -305,8 +307,10 @@ void Stream::stop()
m_zrtp->stopZrtp();
+ pthread_mutex_lock(&m_send_mutex);
delete m_send_srtp;
m_send_srtp = NULL;
+ pthread_mutex_unlock(&m_send_mutex);
delete m_recv_srtp;
m_recv_srtp = NULL;
@@ -347,13 +351,13 @@ bool Stream::udp_helper_send_cb(int *err, struct sa *src, struct mbuf *mb,
bool Stream::udp_helper_send(int *err, struct sa *src, struct mbuf *mb)
{
- if (!started())
- return false;
-
+ bool ret = false;
enum pkt_type ptype = get_packet_type(mb);
size_t len = mbuf_get_left(mb);
int rerr = 0;
+ pthread_mutex_lock(&m_send_mutex);
+
if (ptype == PKT_TYPE_RTCP && m_send_srtp && len > 8) {
rerr = m_send_srtp->protect_ctrl(mb);
@@ -364,7 +368,7 @@ bool Stream::udp_helper_send(int *err, struct sa *src, struct mbuf *mb)
rerr = m_send_srtp->protect(mb);
}
else
- return false;
+ goto out;
if (rerr) {
warning("zrtp: protect/protect_ctrl failed (len=%u): %m\n",
@@ -373,10 +377,13 @@ bool Stream::udp_helper_send(int *err, struct sa *src, struct mbuf *mb)
if (rerr == ENOMEM)
*err = rerr;
// drop
- return true;
+ ret = true;
}
- return false;
+ out:
+ pthread_mutex_unlock(&m_send_mutex);
+
+ return ret;
}
@@ -594,8 +601,11 @@ bool Stream::srtpSecretsReady(SrtpSecret_t* secrets, EnableSecurity part)
return false;
}
- if (part == ForSender)
+ if (part == ForSender) {
+ pthread_mutex_lock(&m_send_mutex);
m_send_srtp = s;
+ pthread_mutex_unlock(&m_send_mutex);
+ }
else if (part == ForReceiver)
m_recv_srtp = s;
else
@@ -612,8 +622,10 @@ void Stream::srtpSecretsOff(EnableSecurity part)
(part == ForSender)? "sender" : "receiver");
if (part == ForSender) {
+ pthread_mutex_lock(&m_send_mutex);
delete m_send_srtp;
m_send_srtp = NULL;
+ pthread_mutex_unlock(&m_send_mutex);
}
if (part == ForReceiver) {
diff --git a/modules/gzrtp/stream.h b/modules/gzrtp/stream.h
index 7d8736e..d25a5c1 100644
--- a/modules/gzrtp/stream.h
+++ b/modules/gzrtp/stream.h
@@ -105,6 +105,7 @@ private:
struct udp_helper *m_uh_rtcp;
StreamMediaType m_media_type;
Srtp *m_send_srtp, *m_recv_srtp;
+ pthread_mutex_t m_send_mutex;
SRTPStat m_srtp_stat, m_srtcp_stat;
std::string m_sas, m_ciphers;