Commit f36d5b9c authored by Adrien Béraud's avatar Adrien Béraud

call: make CallState, ConnectionState enum classes

Call has two enum states: CallState and ConnectionState.
Those enums are used in a fair amount of files.
Using enum class would formally forbid the
easy confusion between them.

Refs #75549

Change-Id: I16802a56b1111303af6c10784e81424e3da5357c
parent 14895c24
......@@ -42,6 +42,7 @@
#include "map_utils.h"
#include "call_factory.h"
#include "string_utils.h"
#include "enumclass_utils.h"
namespace ring {
......@@ -90,25 +91,25 @@ bool
Call::validTransition(CallState newState)
{
switch (callState_) {
case INACTIVE:
case CallState::INACTIVE:
switch (newState) {
case INACTIVE:
case CallState::INACTIVE:
return false;
default:
return true;
}
case ACTIVE:
case CallState::ACTIVE:
switch (newState) {
case HOLD:
case CallState::HOLD:
return true;
default:
return false;
}
case HOLD:
case CallState::HOLD:
switch (newState) {
case ACTIVE:
case CallState::ACTIVE:
return true;
default:
return false;
......@@ -124,11 +125,8 @@ Call::setState(CallState state)
{
std::lock_guard<std::mutex> lock(callMutex_);
if (not validTransition(state)) {
static const char *states[] = {"INACTIVE", "ACTIVE", "HOLD", "BUSY", "ERROR"};
assert(callState_ < RING_ARRAYSIZE(states) and state < RING_ARRAYSIZE(states));
RING_ERR("Invalid call state transition from %s to %s",
states[callState_], states[state]);
assert((int)callState_ < enum_class_size<CallState>() && (int)state < enum_class_size<CallState>());
//RING_ERR("Invalid call state transition from %s to %s", states[callState_], states[state]);
return false;
}
......@@ -147,31 +145,31 @@ std::string
Call::getStateStr() const
{
switch (getState()) {
case ACTIVE:
case CallState::ACTIVE:
switch (getConnectionState()) {
case RINGING:
case ConnectionState::RINGING:
return isIncoming() ? "INCOMING" : "RINGING";
case CONNECTED:
case ConnectionState::CONNECTED:
default:
return "CURRENT";
}
case HOLD:
case CallState::HOLD:
return "HOLD";
case BUSY:
case CallState::BUSY:
return "BUSY";
case INACTIVE:
case CallState::INACTIVE:
switch (getConnectionState()) {
case RINGING:
case ConnectionState::RINGING:
return isIncoming() ? "INCOMING" : "RINGING";
case CONNECTED:
case ConnectionState::CONNECTED:
return "CURRENT";
default:
return "INACTIVE";
}
case MERROR:
case CallState::MERROR:
default:
return "FAILURE";
}
......
......@@ -77,12 +77,12 @@ class Call : public Recordable, public std::enable_shared_from_this<Call> {
* Audio should be transmitted when ConnectionState = Connected AND
* CallState = Active.
*/
enum ConnectionState {DISCONNECTED, TRYING, PROGRESSING, RINGING, CONNECTED};
enum class ConnectionState {DISCONNECTED, TRYING, PROGRESSING, RINGING, CONNECTED, COUNT__};
/**
* The Call State.
*/
enum CallState {INACTIVE, ACTIVE, HOLD, BUSY, MERROR};
enum class CallState {INACTIVE, ACTIVE, HOLD, BUSY, MERROR, COUNT__};
virtual ~Call();
......@@ -369,10 +369,10 @@ class Call : public Recordable, public std::enable_shared_from_this<Call> {
Account& account_;
/** Disconnected/Progressing/Trying/Ringing/Connected */
ConnectionState connectionState_ {Call::DISCONNECTED};
ConnectionState connectionState_ {ConnectionState::DISCONNECTED};
/** Inactive/Active/Hold/Busy/Error */
CallState callState_ {Call::INACTIVE};
CallState callState_ {CallState::INACTIVE};
/** Direct IP-to-IP or classic call */
bool isIPToIP_ {false};
......
......@@ -145,8 +145,8 @@ IAXAccount::newOutgoingCall(const std::string& toUrl)
iaxOutgoingInvite(call.get());
call->setConnectionState(Call::PROGRESSING);
call->setState(Call::ACTIVE);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
call->setState(Call::CallState::ACTIVE);
return call;
}
......
......@@ -150,8 +150,8 @@ IAXCall::answer()
iax_answer(session);
}
setState(Call::ACTIVE);
setConnectionState(Call::CONNECTED);
setState(Call::CallState::ACTIVE);
setConnectionState(Call::ConnectionState::CONNECTED);
Manager::instance().getRingBufferPool().flushAllBuffers();
}
......@@ -204,7 +204,7 @@ IAXCall::onhold()
iax_quelch_moh(session, true);
}
setState(Call::HOLD);
setState(Call::CallState::HOLD);
}
void
......@@ -217,7 +217,7 @@ IAXCall::offhold()
iax_unquelch(session);
}
setState(Call::ACTIVE);
setState(Call::CallState::ACTIVE);
Manager::instance().startAudioDriverStream();
}
......
......@@ -152,7 +152,7 @@ void
IAXVoIPLink::sendAudioFromMic()
{
for (const auto currentCall : Manager::instance().callFactory.getAllCalls<IAXCall>()) {
if (currentCall->getState() != Call::ACTIVE)
if (currentCall->getState() != Call::CallState::ACTIVE)
continue;
int codecType = currentCall->getAudioCodecPayload();
......@@ -216,8 +216,8 @@ IAXVoIPLink::sendAudioFromMic()
void
IAXVoIPLink::handleReject(IAXCall& call)
{
call.setConnectionState(Call::CONNECTED);
call.setState(Call::MERROR);
call.setConnectionState(Call::ConnectionState::CONNECTED);
call.setState(Call::CallState::MERROR);
Manager::instance().callFailure(call);
call.removeCall();
}
......@@ -232,11 +232,11 @@ IAXVoIPLink::handleAccept(iax_event* event, IAXCall& call)
void
IAXVoIPLink::handleAnswerTransfer(iax_event* event, IAXCall& call)
{
if (call.getConnectionState() == Call::CONNECTED)
if (call.getConnectionState() == Call::ConnectionState::CONNECTED)
return;
call.setConnectionState(Call::CONNECTED);
call.setState(Call::ACTIVE);
call.setConnectionState(Call::ConnectionState::CONNECTED);
call.setState(Call::CallState::ACTIVE);
if (event->ies.format)
call.format = event->ies.format;
......@@ -250,8 +250,8 @@ IAXVoIPLink::handleAnswerTransfer(iax_event* event, IAXCall& call)
void
IAXVoIPLink::handleBusy(IAXCall& call)
{
call.setConnectionState(Call::CONNECTED);
call.setState(Call::BUSY);
call.setConnectionState(Call::ConnectionState::CONNECTED);
call.setState(Call::CallState::BUSY);
Manager::instance().callBusy(call);
call.removeCall();
......@@ -269,7 +269,7 @@ IAXVoIPLink::handleMessage(iax_event* event, IAXCall& call)
void
IAXVoIPLink::handleRinging(IAXCall& call)
{
call.setConnectionState(Call::RINGING);
call.setConnectionState(Call::ConnectionState::RINGING);
Manager::instance().peerRingingCall(call);
}
......@@ -413,7 +413,7 @@ void IAXVoIPLink::iaxHandlePrecallEvent(iax_event* event)
}
call->session = event->session;
call->setConnectionState(Call::PROGRESSING);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
if (event->ies.calling_number)
call->setPeerNumber(event->ies.calling_number);
......
......@@ -1613,7 +1613,7 @@ Manager::incomingCall(Call &call, const std::string& accountId)
}
if (not hasCurrentCall()) {
call.setConnectionState(Call::RINGING);
call.setConnectionState(Call::ConnectionState::RINGING);
playRingtone(accountId);
}
......
......@@ -181,8 +181,8 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
/* First step: wait for an initialized ICE transport for SIP channel */
if (ice->isFailed() or std::chrono::steady_clock::now() >= iceInitTimeout) {
RING_DBG("ice init failed (or timeout)");
call->setConnectionState(Call::DISCONNECTED);
call->setState(Call::MERROR);
call->setConnectionState(Call::ConnectionState::DISCONNECTED);
call->setState(Call::CallState::MERROR);
Manager::instance().callFailure(*call); // signal client
call->removeCall();
return false;
......@@ -199,7 +199,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
std::weak_ptr<SIPCall> weak_call = call;
call->setConnectionState(Call::TRYING);
call->setConnectionState(Call::ConnectionState::TRYING);
shared_this->dht_.putEncrypted(
callkey, toH,
dht::Value {
......@@ -210,7 +210,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
if (!ok) {
RING_WARN("Can't put ICE descriptor on DHT");
if (auto call = weak_call.lock()) {
call->setConnectionState(Call::DISCONNECTED);
call->setConnectionState(Call::ConnectionState::DISCONNECTED);
Manager::instance().callFailure(*call); // signal client
call->removeCall();
}
......@@ -227,7 +227,7 @@ RingAccount::newOutgoingCall(const std::string& toUrl)
if (msg.id != replyvid)
return true;
RING_WARN("ICE request replied from DHT peer %s", toH.toString().c_str());
call->setConnectionState(Call::PROGRESSING);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
emitSignal<DRing::CallSignal::StateChange>(call->getCallId(), DRing::Call::StateEvent::CONNECTING, 0);
ice->start(msg.ice_data);
return false;
......@@ -374,8 +374,8 @@ RingAccount::SIPStartCall(const std::shared_ptr<SIPCall>& call, IpAddr target)
return false;
}
call->setConnectionState(Call::PROGRESSING);
call->setState(Call::ACTIVE);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
call->setState(Call::CallState::ACTIVE);
return true;
}
......@@ -593,7 +593,7 @@ RingAccount::handleEvents()
};
auto tr = link_->sipTransportBroker->getTlsIceTransport(c->ice, ICE_COMP_SIP_TRANSPORT, tlsParams);
call->setTransport(tr);
call->setConnectionState(Call::PROGRESSING);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
if (c->call_key == dht::InfoHash()) {
RING_DBG("ICE succeeded : moving incomming call to pending sip call");
auto in = c;
......@@ -609,7 +609,7 @@ RingAccount::handleEvents()
RING_WARN("ICE timeout : removing pending call");
if (c->call_key != dht::InfoHash())
dht_.cancelListen(c->call_key, c->listen_key.get());
call->setConnectionState(Call::DISCONNECTED);
call->setConnectionState(Call::ConnectionState::DISCONNECTED);
Manager::instance().callFailure(*call);
c = pendingCalls_.erase(c);
call->removeCall();
......@@ -874,7 +874,7 @@ void RingAccount::incomingCall(dht::IceCandidates&& msg)
if (!ok) {
RING_WARN("Can't put ICE descriptor on DHT");
if (auto call = weak_call.lock()) {
call->setConnectionState(Call::DISCONNECTED);
call->setConnectionState(Call::ConnectionState::DISCONNECTED);
Manager::instance().callFailure(*call);
call->removeCall();
}
......
......@@ -410,8 +410,8 @@ SIPAccount::SIPStartCall(std::shared_ptr<SIPCall>& call)
return false;
}
call->setConnectionState(Call::PROGRESSING);
call->setState(Call::ACTIVE);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
call->setState(Call::CallState::ACTIVE);
return true;
}
......
......@@ -208,9 +208,9 @@ SIPCall::setTransport(const std::shared_ptr<SipTransport>& t)
{
if (auto this_ = wthis_.lock()) {
// end the call if the SIP transport is shut down
if (not SipTransport::isAlive(t, state) and this_->getConnectionState() != DISCONNECTED) {
if (not SipTransport::isAlive(t, state) and this_->getConnectionState() != ConnectionState::DISCONNECTED) {
RING_WARN("Ending call because underlying SIP transport was closed");
this_->setConnectionState(Call::DISCONNECTED);
this_->setConnectionState(ConnectionState::DISCONNECTED);
Manager::instance().callFailure(*this_, ECONNRESET);
this_->removeCall();
}
......@@ -236,7 +236,7 @@ SIPCall::SIPSessionReinvite()
sdp_->createOffer(acc.getActiveAccountCodecInfoList(MEDIA_AUDIO),
acc.getActiveAccountCodecInfoList(acc.isVideoEnabled() ? MEDIA_VIDEO : MEDIA_NONE),
acc.getSrtpKeyExchange(),
getState() == Call::HOLD);
getState() == CallState::HOLD);
if (initIceTransport(true))
setupLocalSDPFromIce();
......@@ -331,8 +331,8 @@ void SIPCall::answer()
throw std::runtime_error("Could not send invite request answer (200 OK)");
}
setConnectionState(CONNECTED);
setState(ACTIVE);
setConnectionState(ConnectionState::CONNECTED);
setState(CallState::ACTIVE);
}
static void
......@@ -391,7 +391,7 @@ SIPCall::hangup(int reason)
void
SIPCall::refuse()
{
if (!isIncoming() or getConnectionState() == Call::CONNECTED or !inv)
if (!isIncoming() or getConnectionState() == ConnectionState::CONNECTED or !inv)
return;
stopAllMedia();
......@@ -584,12 +584,12 @@ SIPCall::attendedTransfer(const std::string& to)
void
SIPCall::onhold()
{
if (not setState(Call::HOLD))
if (not setState(CallState::HOLD))
return;
stopAllMedia();
if (getConnectionState() == Call::CONNECTED) {
if (getConnectionState() == ConnectionState::CONNECTED) {
if (SIPSessionReinvite() != PJ_SUCCESS)
RING_WARN("Reinvite failed");
}
......@@ -615,12 +615,12 @@ SIPCall::offhold()
void
SIPCall::internalOffHold(const std::function<void()>& sdp_cb)
{
if (not setState(Call::ACTIVE))
if (not setState(CallState::ACTIVE))
return;
sdp_cb();
if (getConnectionState() == Call::CONNECTED) {
if (getConnectionState() == ConnectionState::CONNECTED) {
if (SIPSessionReinvite() != PJ_SUCCESS) {
RING_WARN("Reinvite failed, resuming hold");
onhold();
......@@ -703,9 +703,9 @@ SIPCall::onClosed()
void
SIPCall::onAnswered()
{
if (getConnectionState() != Call::CONNECTED) {
setConnectionState(Call::CONNECTED);
setState(Call::ACTIVE);
if (getConnectionState() != ConnectionState::CONNECTED) {
setConnectionState(ConnectionState::CONNECTED);
setState(CallState::ACTIVE);
Manager::instance().peerAnsweredCall(*this);
}
}
......@@ -713,7 +713,7 @@ SIPCall::onAnswered()
void
SIPCall::onPeerRinging()
{
setConnectionState(Call::RINGING);
setConnectionState(ConnectionState::RINGING);
Manager::instance().peerRingingCall(*this);
}
......@@ -898,7 +898,7 @@ SIPCall::onMediaUpdate()
/* First step: wait for an ICE transport for SIP channel */
if (this_->iceTransport_->isFailed() or std::chrono::steady_clock::now() >= iceTimeout) {
RING_DBG("ice init failed (or timeout)");
this_->setConnectionState(Call::DISCONNECTED);
this_->setConnectionState(ConnectionState::DISCONNECTED);
Manager::instance().callFailure(*this_, ETIMEDOUT); // signal client
this_->removeCall();
return false;
......@@ -923,7 +923,7 @@ SIPCall::onReceiveOffer(const pjmedia_sdp_session* offer)
acc.getActiveAccountCodecInfoList(MEDIA_AUDIO),
acc.getActiveAccountCodecInfoList(acc.isVideoEnabled() ? MEDIA_VIDEO : MEDIA_NONE),
acc.getSrtpKeyExchange(),
getState() == Call::HOLD
getState() == CallState::HOLD
);
auto ice_attrs = Sdp::getIceAttributes(offer);
if (not ice_attrs.ufrag.empty() and not ice_attrs.pwd.empty()) {
......@@ -1002,7 +1002,7 @@ SIPCall::setSecure(bool sec)
{
if (srtpEnabled_)
return;
if (sec && getConnectionState() != DISCONNECTED) {
if (sec && getConnectionState() != ConnectionState::DISCONNECTED) {
throw std::runtime_error("Can't enable security since call is already connected");
}
srtpEnabled_ = sec;
......
......@@ -333,7 +333,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
/* fallback on local address */
if (not addrSdp) addrSdp = addrToUse;
call->setConnectionState(Call::PROGRESSING);
call->setConnectionState(Call::ConnectionState::PROGRESSING);
call->setPeerNumber(peerNumber);
call->setPeerDisplayName(peerDisplayName);
call->initRecFilename(peerNumber);
......@@ -433,7 +433,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
return PJ_FALSE;
}
call->setConnectionState(Call::TRYING);
call->setConnectionState(Call::ConnectionState::TRYING);
if (pjsip_inv_answer(call->inv.get(), PJSIP_SC_RINGING, NULL, NULL, &tdata) != PJ_SUCCESS) {
RING_ERR("Could not create answer RINGING");
......@@ -449,7 +449,7 @@ transaction_request_cb(pjsip_rx_data *rdata)
return PJ_FALSE;
}
call->setConnectionState(Call::RINGING);
call->setConnectionState(Call::ConnectionState::RINGING);
Manager::instance().incomingCall(*call, account_id);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment