2015-11-03 02:13:15 +01:00
|
|
|
#pragma once
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-11-03 02:13:15 +01:00
|
|
|
#include "ICommunicator.hpp"
|
2015-09-27 23:25:22 +02:00
|
|
|
|
|
|
|
#include <pjmedia.h>
|
2015-11-05 01:15:20 +01:00
|
|
|
#include <pjsua-lib/pjsua.h>
|
|
|
|
|
|
|
|
#undef isblank
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-11-04 01:22:59 +01:00
|
|
|
#include <log4cpp/Category.hh>
|
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2015-09-27 23:25:22 +02:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
2015-10-16 22:41:37 +02:00
|
|
|
#include <mutex>
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-10-16 22:41:37 +02:00
|
|
|
namespace sip {
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-10-17 22:27:37 +02:00
|
|
|
constexpr int DEFAULT_PORT = 5060;
|
2015-10-16 22:41:37 +02:00
|
|
|
constexpr int SAMPLING_RATE = 48000;
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-10-16 22:41:37 +02:00
|
|
|
class Exception : public std::runtime_error {
|
|
|
|
public:
|
2015-11-04 00:53:52 +01:00
|
|
|
Exception(const char *title) : std::runtime_error(title) {
|
|
|
|
mesg += title;
|
|
|
|
}
|
|
|
|
|
2015-10-16 22:41:37 +02:00
|
|
|
Exception(const char *title, pj_status_t status) : std::runtime_error(title) {
|
|
|
|
char errorMsgBuffer[500];
|
|
|
|
pj_strerror(status, errorMsgBuffer, sizeof(errorMsgBuffer));
|
|
|
|
|
|
|
|
mesg += title;
|
|
|
|
mesg += ": ";
|
|
|
|
mesg += errorMsgBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char *what() const throw() {
|
|
|
|
return mesg.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string mesg;
|
|
|
|
};
|
|
|
|
|
2015-11-03 23:39:41 +01:00
|
|
|
inline pj_str_t toPjString(std::string &str) {
|
2015-10-16 22:41:37 +02:00
|
|
|
return pj_str(const_cast<char *>(str.c_str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
pj_status_t MediaPort_getFrameRawCallback(pjmedia_port *port, pjmedia_frame *frame);
|
|
|
|
|
|
|
|
pj_status_t MediaPort_putFrameRawCallback(pjmedia_port *port, pjmedia_frame *frame);
|
|
|
|
|
2015-11-05 01:15:20 +01:00
|
|
|
void PjsuaCommunicator_onCallMediaState(pjsua_call_id call_id);
|
|
|
|
|
2015-11-04 01:22:59 +01:00
|
|
|
class PjsuaCommunicator : public ICommunicator, boost::noncopyable {
|
2015-09-27 23:25:22 +02:00
|
|
|
public:
|
2015-11-02 23:41:49 +01:00
|
|
|
PjsuaCommunicator();
|
|
|
|
|
|
|
|
void connect(
|
2015-09-29 02:26:45 +02:00
|
|
|
std::string host,
|
|
|
|
std::string user,
|
2015-10-17 22:27:37 +02:00
|
|
|
std::string password,
|
|
|
|
unsigned int port = DEFAULT_PORT);
|
2015-09-27 23:25:22 +02:00
|
|
|
|
|
|
|
~PjsuaCommunicator();
|
|
|
|
|
2015-11-03 02:13:15 +01:00
|
|
|
virtual void sendPcmSamples(int16_t *samples, unsigned int length);
|
2015-09-27 23:25:22 +02:00
|
|
|
|
2015-11-04 00:53:52 +01:00
|
|
|
std::function<void(std::string)> onStateChange;
|
|
|
|
|
2015-09-27 23:25:22 +02:00
|
|
|
private:
|
2015-10-16 22:41:37 +02:00
|
|
|
log4cpp::Category &logger;
|
|
|
|
log4cpp::Category &callbackLogger;
|
|
|
|
|
|
|
|
|
2015-11-05 01:15:20 +01:00
|
|
|
pjmedia_port *mediaPort = nullptr;
|
|
|
|
|
|
|
|
pj_pool_t *pool = nullptr;
|
2015-10-16 22:41:37 +02:00
|
|
|
|
|
|
|
pjmedia_circ_buf *inputBuff;
|
|
|
|
|
|
|
|
std::mutex inBuffAccessMutex;
|
|
|
|
|
|
|
|
// todo make it completely stateless
|
|
|
|
pjmedia_port *createMediaPort();
|
|
|
|
|
|
|
|
void registerAccount(std::string host,
|
|
|
|
std::string user,
|
|
|
|
std::string password);
|
|
|
|
|
|
|
|
pj_status_t mediaPortGetFrame(pjmedia_frame *frame);
|
|
|
|
|
|
|
|
void mediaPortPutFrame(pj_int16_t *samples, pj_size_t count);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For PJMEDIA implementation reasons, these callbacks have to be functions, not methods.
|
|
|
|
* That is the reason to use 'friend'.
|
|
|
|
*/
|
|
|
|
friend pj_status_t MediaPort_getFrameRawCallback(pjmedia_port *port,
|
|
|
|
pjmedia_frame *frame);
|
|
|
|
|
|
|
|
friend pj_status_t MediaPort_putFrameRawCallback(pjmedia_port *port,
|
|
|
|
pjmedia_frame *frame);
|
2015-11-05 01:15:20 +01:00
|
|
|
|
|
|
|
friend void PjsuaCommunicator_onCallMediaState(pjsua_call_id call_id);
|
2015-09-27 23:25:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|