Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(coreav): prevent racy access to call variable
Browse files Browse the repository at this point in the history
(cherry picked from commit c507d26)
  • Loading branch information
sudden6 authored and anthonybilinski committed Mar 20, 2020
1 parent 26fcea0 commit dfeca3e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/coreav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,16 +732,18 @@ void CoreAV::callCallback(ToxAV* toxav, uint32_t friendNum, bool audio, bool vid
state |= TOXAV_FRIEND_CALL_STATE_SENDING_V | TOXAV_FRIEND_CALL_STATE_ACCEPTING_V;
it.first->second->setState(static_cast<TOXAV_FRIEND_CALL_STATE>(state));

// Must explicitely unlock, because a deadlock can happen via ChatForm/Audio
locker.unlock();

emit reinterpret_cast<CoreAV*>(self)->avInvite(friendNum, video);
emit self->avInvite(friendNum, video);
}

void CoreAV::stateCallback(ToxAV* toxav, uint32_t friendNum, uint32_t state, void* vSelf)
{
Q_UNUSED(toxav);
CoreAV* self = static_cast<CoreAV*>(vSelf);

// we must unlock this lock before emitting any signals
QWriteLocker locker{&self->callsLock};

auto it = self->calls.find(friendNum);
Expand All @@ -766,14 +768,18 @@ void CoreAV::stateCallback(ToxAV* toxav, uint32_t friendNum, uint32_t state, voi
// If our state was null, we started the call and were still ringing
if (!call.getState() && state) {
call.setActive(true);
bool videoEnabled = call.getVideoEnabled();
call.setState(static_cast<TOXAV_FRIEND_CALL_STATE>(state));
locker.unlock();
emit self->avStart(friendNum, call.getVideoEnabled());
emit self->avStart(friendNum, videoEnabled);
} else if ((call.getState() & TOXAV_FRIEND_CALL_STATE_SENDING_V)
&& !(state & TOXAV_FRIEND_CALL_STATE_SENDING_V)) {
qDebug() << "Friend" << friendNum << "stopped sending video";
if (call.getVideoSource()) {
call.getVideoSource()->stopSource();
}

call.setState(static_cast<TOXAV_FRIEND_CALL_STATE>(state));
} else if (!(call.getState() & TOXAV_FRIEND_CALL_STATE_SENDING_V)
&& (state & TOXAV_FRIEND_CALL_STATE_SENDING_V)) {
// Workaround toxav sometimes firing callbacks for "send last frame" -> "stop sending
Expand All @@ -784,9 +790,9 @@ void CoreAV::stateCallback(ToxAV* toxav, uint32_t friendNum, uint32_t state, voi
if (call.getVideoSource()) {
call.getVideoSource()->restartSource();
}
}

call.setState(static_cast<TOXAV_FRIEND_CALL_STATE>(state));
call.setState(static_cast<TOXAV_FRIEND_CALL_STATE>(state));
}
}
}

Expand Down

0 comments on commit dfeca3e

Please sign in to comment.