mumsi/PjsuaCommunicator.cpp

311 lines
9.7 KiB
C++
Raw Normal View History

#include "PjsuaCommunicator.hpp"
#include <pjlib.h>
#include <pjsua-lib/pjsua.h>
2015-11-04 01:22:59 +01:00
#include <boost/algorithm/string.hpp>
2015-11-04 00:53:52 +01:00
#include <boost/format.hpp>
using namespace std;
2015-11-07 16:32:21 +01:00
namespace sip {
using namespace log4cpp;
2015-11-07 16:32:21 +01:00
class _LogWriter : public pj::LogWriter {
public:
_LogWriter(Category &logger)
: logger(logger) { }
2015-11-03 02:13:15 +01:00
virtual void write(const pj::LogEntry &entry) override {
2015-11-07 16:32:21 +01:00
auto message = entry.msg.substr(0, entry.msg.size() - 1); // remove newline
2015-11-07 16:32:21 +01:00
logger << prioritiesMap.at(entry.level) << message;
}
2015-11-07 16:32:21 +01:00
private:
log4cpp::Category &logger;
std::map<int, Priority::Value> prioritiesMap = {
{1, Priority::ERROR},
{2, Priority::WARN},
{3, Priority::NOTICE},
{4, Priority::INFO},
{5, Priority::DEBUG},
{6, Priority::DEBUG}
};
};
2015-11-07 16:32:21 +01:00
class _MumlibAudioMedia : public pj::AudioMedia {
public:
_MumlibAudioMedia(sip::PjsuaCommunicator &comm)
: communicator(comm) {
createMediaPort();
registerMediaPort(&mediaPort);
}
2015-11-07 16:32:21 +01:00
~_MumlibAudioMedia() {
unregisterMediaPort();
}
2015-11-07 16:32:21 +01:00
private:
pjmedia_port mediaPort;
sip::PjsuaCommunicator &communicator;
2015-11-07 16:32:21 +01:00
static pj_status_t callback_getFrame(pjmedia_port *port, pjmedia_frame *frame) {
auto *communicator = static_cast<sip::PjsuaCommunicator *>(port->port_data.pdata);
return communicator->mediaPortGetFrame(port, frame);
}
2015-11-07 16:32:21 +01:00
static pj_status_t callback_putFrame(pjmedia_port *port, pjmedia_frame *frame) {
auto *communicator = static_cast<sip::PjsuaCommunicator *>(port->port_data.pdata);
return communicator->mediaPortPutFrame(port, frame);
}
2015-11-07 16:32:21 +01:00
void createMediaPort() {
2015-11-04 00:53:52 +01:00
2015-11-07 16:32:21 +01:00
auto name = pj_str((char *) "MumsiMediaPort");
2015-11-04 00:53:52 +01:00
2015-11-07 16:32:21 +01:00
pj_status_t status = pjmedia_port_info_init(&(mediaPort.info),
&name,
PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'),
SAMPLING_RATE,
1,
16,
SAMPLING_RATE * 20 /
1000); // todo recalculate to match mumble specs
2015-11-07 16:32:21 +01:00
if (status != PJ_SUCCESS) {
throw sip::Exception("error while calling pjmedia_port_info_init()", status);
}
2015-11-07 16:32:21 +01:00
mediaPort.port_data.pdata = &communicator;
2015-11-04 00:53:52 +01:00
2015-11-07 16:32:21 +01:00
mediaPort.get_frame = &callback_getFrame;
mediaPort.put_frame = &callback_putFrame;
}
};
2015-11-04 00:53:52 +01:00
2015-11-07 16:32:21 +01:00
class _Call : public pj::Call {
public:
_Call(sip::PjsuaCommunicator &comm, pj::Account &acc, int call_id = PJSUA_INVALID_ID)
: pj::Call(acc, call_id),
communicator(comm),
account(acc) { }
virtual void onCallState(pj::OnCallStateParam &prm) override;
2015-11-04 00:53:52 +01:00
virtual void onCallMediaState(pj::OnCallMediaStateParam &prm) override;
2015-11-06 02:23:48 +01:00
virtual void onDtmfDigit(pj::OnDtmfDigitParam &prm) override;
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
private:
sip::PjsuaCommunicator &communicator;
pj::Account &account;
};
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
class _Account : public pj::Account {
public:
_Account(sip::PjsuaCommunicator &comm)
: communicator(comm) { }
virtual void onRegState(pj::OnRegStateParam &prm) override;
2015-11-06 02:23:48 +01:00
virtual void onIncomingCall(pj::OnIncomingCallParam &iprm) override;
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
private:
sip::PjsuaCommunicator &communicator;
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
bool available = true;
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
friend class _Call;
};
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
void _Call::onCallState(pj::OnCallStateParam &prm) {
auto ci = getInfo();
2015-11-06 02:23:48 +01:00
2015-11-09 00:51:51 +01:00
communicator.logger.info("Call %d state=%s.", ci.id, ci.stateText.c_str());
string address = ci.remoteUri;
boost::replace_all(address, "<", "");
boost::replace_all(address, ">", "");
if (ci.state == PJSIP_INV_STATE_CONFIRMED) {
auto msgText = "Incoming call from " + address + ".";
communicator.logger.notice(msgText);
communicator.onStateChange(msgText);
} else if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
2015-11-09 01:42:04 +01:00
auto &acc = dynamic_cast<_Account &>(account);
2015-11-09 00:51:51 +01:00
2015-11-09 01:42:04 +01:00
if (not acc.available) {
auto msgText = "Call from " + address + " finished.";
2015-11-06 02:23:48 +01:00
2015-11-09 01:42:04 +01:00
communicator.logger.notice(msgText);
communicator.onStateChange(msgText);
acc.available = true;
}
2015-11-09 00:51:51 +01:00
delete this;
2015-11-07 16:32:21 +01:00
}
2015-11-09 00:51:51 +01:00
}
2015-11-07 16:32:21 +01:00
2015-11-09 00:51:51 +01:00
void _Call::onCallMediaState(pj::OnCallMediaStateParam &prm) {
auto ci = getInfo();
2015-11-02 23:41:49 +01:00
2015-11-09 00:51:51 +01:00
if (ci.media.size() != 1) {
throw sip::Exception("ci.media.size is not 1");
}
if (ci.media[0].status == PJSUA_CALL_MEDIA_ACTIVE) {
auto *aud_med = static_cast<pj::AudioMedia *>(getMedia(0));
2015-11-02 23:41:49 +01:00
2015-11-09 00:51:51 +01:00
communicator.media->startTransmit(*aud_med);
aud_med->startTransmit(*communicator.media);
} else if (ci.media[0].status == PJSUA_CALL_MEDIA_NONE) {
dynamic_cast<_Account &>(account).available = true;
2015-11-07 16:32:21 +01:00
}
2015-11-09 00:51:51 +01:00
}
2015-11-09 00:51:51 +01:00
void _Call::onDtmfDigit(pj::OnDtmfDigitParam &prm) {
communicator.logger.notice("DTMF digit '%s' (call %d).", prm.digit.c_str(), getId());
}
2015-11-03 02:13:15 +01:00
2015-11-09 00:51:51 +01:00
void _Account::onRegState(pj::OnRegStateParam &prm) {
pj::AccountInfo ai = getInfo();
communicator.logger << log4cpp::Priority::INFO
<< (ai.regIsActive ? "Register:" : "Unregister:") << " code=" << prm.code;
}
2015-11-09 00:51:51 +01:00
void _Account::onIncomingCall(pj::OnIncomingCallParam &iprm) {
auto *call = new _Call(communicator, *this, iprm.callId);
string uri = call->getInfo().remoteUri;
communicator.logger.info("Incoming call from %s.", uri.c_str());
2015-11-09 01:42:04 +01:00
pj::CallOpParam param;
2015-11-09 00:51:51 +01:00
if (communicator.uriValidator.validateUri(uri)) {
if (available) {
param.statusCode = PJSIP_SC_OK;
available = false;
} else {
param.statusCode = PJSIP_SC_BUSY_EVERYWHERE;
}
2015-11-04 00:53:52 +01:00
2015-11-07 16:32:21 +01:00
call->answer(param);
2015-11-09 00:51:51 +01:00
} else {
communicator.logger.warn("Refusing call from %s.", uri.c_str());
param.statusCode = PJSIP_SC_SERVICE_UNAVAILABLE;
2015-11-09 01:42:04 +01:00
call->hangup(param);
2015-11-07 16:32:21 +01:00
}
2015-11-09 00:51:51 +01:00
}
2015-11-07 16:32:21 +01:00
}
2015-11-09 00:51:51 +01:00
sip::PjsuaCommunicator::PjsuaCommunicator(IncomingConnectionValidator &validator)
2015-11-07 16:32:21 +01:00
: logger(log4cpp::Category::getInstance("SipCommunicator")),
2015-11-09 00:51:51 +01:00
pjsuaLogger(log4cpp::Category::getInstance("Pjsua")),
uriValidator(validator) {
2015-11-03 23:39:41 +01:00
2015-11-07 16:32:21 +01:00
logWriter.reset(new sip::_LogWriter(pjsuaLogger));
2015-11-07 16:32:21 +01:00
endpoint.libCreate();
2015-11-07 16:32:21 +01:00
pj::EpConfig endpointConfig;
endpointConfig.uaConfig.userAgent = "Mumsi Mumble-SIP gateway";
endpointConfig.uaConfig.maxCalls = 1;
2015-11-07 16:32:21 +01:00
endpointConfig.logConfig.writer = logWriter.get();
endpointConfig.logConfig.level = 5;
2015-11-07 16:32:21 +01:00
endpointConfig.medConfig.noVad = true;
endpoint.libInit(endpointConfig);
pj_caching_pool_init(&cachingPool, &pj_pool_factory_default_policy, 0);
2015-10-16 22:41:37 +02:00
mixer.reset(new mixer::AudioFramesMixer(cachingPool.factory));
2015-11-06 02:23:48 +01:00
2015-11-07 16:32:21 +01:00
media.reset(new _MumlibAudioMedia(*this));
2015-11-03 02:13:15 +01:00
}
void sip::PjsuaCommunicator::connect(
std::string host,
std::string user,
std::string password,
unsigned int port) {
2015-11-03 23:39:41 +01:00
2015-11-07 16:32:21 +01:00
pj::TransportConfig transportConfig;
2015-11-03 02:13:15 +01:00
transportConfig.port = port;
2015-11-07 16:32:21 +01:00
endpoint.transportCreate(PJSIP_TRANSPORT_UDP, transportConfig); // todo try catch
2015-10-16 22:41:37 +02:00
2015-11-07 16:32:21 +01:00
endpoint.libStart();
2015-11-06 02:23:48 +01:00
2015-11-07 16:32:21 +01:00
pj_status_t status = pjsua_set_null_snd_dev();
2015-11-06 02:23:48 +01:00
if (status != PJ_SUCCESS) {
throw sip::Exception("error in pjsua_set_null_std_dev()", status);
}
2015-10-16 22:41:37 +02:00
registerAccount(host, user, password);
}
sip::PjsuaCommunicator::~PjsuaCommunicator() {
2015-11-07 16:32:21 +01:00
endpoint.libDestroy();
2015-10-16 22:41:37 +02:00
}
void sip::PjsuaCommunicator::sendPcmSamples(int sessionId, int sequenceNumber, int16_t *samples, unsigned int length) {
mixer->addFrameToBuffer(sessionId, sequenceNumber, samples, length);
}
2015-10-16 22:41:37 +02:00
2015-11-06 02:23:48 +01:00
pj_status_t sip::PjsuaCommunicator::mediaPortGetFrame(pjmedia_port *port, pjmedia_frame *frame) {
2015-10-16 22:41:37 +02:00
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
2015-11-06 02:23:48 +01:00
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&(port->info));
2015-10-16 22:41:37 +02:00
const int readSamples = mixer->getMixedSamples(samples, count);
2015-10-16 22:41:37 +02:00
if (readSamples < count) {
pjsuaLogger.debug("Requested %d samples, available %d, filling remaining with zeros.",
count, readSamples);
2015-10-16 22:41:37 +02:00
for (int i = readSamples; i < count; ++i) {
2015-10-16 23:33:23 +02:00
samples[i] = 0;
}
2015-10-16 22:41:37 +02:00
}
return PJ_SUCCESS;
}
2015-11-06 02:23:48 +01:00
pj_status_t sip::PjsuaCommunicator::mediaPortPutFrame(pjmedia_port *port, pjmedia_frame *frame) {
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
2015-11-02 23:41:49 +01:00
if (count > 0) {
2015-11-07 16:32:21 +01:00
pjsuaLogger.debug("Calling onIncomingPcmSamples with %d samples.", count);
2015-11-03 02:13:15 +01:00
onIncomingPcmSamples(samples, count);
2015-11-02 23:41:49 +01:00
}
2015-11-06 02:23:48 +01:00
return PJ_SUCCESS;
}
2015-10-16 22:41:37 +02:00
void sip::PjsuaCommunicator::registerAccount(string host, string user, string password) {
2015-11-07 16:32:21 +01:00
string uri = "sip:" + user + "@" + host;
pj::AccountConfig accountConfig;
accountConfig.idUri = uri;
accountConfig.regConfig.registrarUri = "sip:" + host;
2015-11-03 02:13:15 +01:00
2015-11-07 16:32:21 +01:00
pj::AuthCredInfo cred("digest", "*", user, 0, password);
accountConfig.sipConfig.authCreds.push_back(cred);
2015-11-03 02:13:15 +01:00
logger.info("Registering account for URI: %s.", uri.c_str());
2015-11-07 16:32:21 +01:00
account.reset(new _Account(*this));
account->create(accountConfig);
}