Add working example (distorted audio).
This commit is contained in:
parent
795405d218
commit
0d162df380
25
AbstractCommunicator.hpp
Normal file
25
AbstractCommunicator.hpp
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef MUMSI_ABSTRACTCOMMUNICATOR_HPP
|
||||
#define MUMSI_ABSTRACTCOMMUNICATOR_HPP
|
||||
|
||||
#include "SoundSampleQueue.hpp"
|
||||
#include <stdint.h>
|
||||
|
||||
#define SOUND_SAMPLE_TYPE int16_t
|
||||
|
||||
class AbstractCommunicator {
|
||||
public:
|
||||
|
||||
virtual void loop() = 0;
|
||||
|
||||
protected:
|
||||
AbstractCommunicator(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue)
|
||||
: inputQueue(inputQueue),
|
||||
outputQueue(outputQueue) { }
|
||||
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue;
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue;
|
||||
};
|
||||
|
||||
#endif //MUMSI_ABSTRACTCOMMUNICATOR_HPP
|
@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.1)
|
||||
cmake_minimum_required(VERSION 3.0.0)
|
||||
project(mumsi)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
@ -7,17 +7,20 @@ INCLUDE(FindPkgConfig)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
|
||||
pkg_check_modules(OPUS "opus")
|
||||
pkg_check_modules(SNDFILE "sndfile")
|
||||
pkg_check_modules(PJSIP "libpjproject")
|
||||
pkg_check_modules(LOG4CPP "log4cpp")
|
||||
|
||||
include_directories(${OPUS_INCLUDE_DIRS})
|
||||
include_directories(libmumble)
|
||||
include_directories(${PJSIP_INCLUDE_DIRS})
|
||||
include_directories(${LOG4CPP_INCLUDE_DIRS})
|
||||
|
||||
link_directories(libmumble)
|
||||
|
||||
set(SOURCE_FILES main.cpp PjsuaCommunicator.cpp PjsuaMediaPort.cpp)
|
||||
add_executable(mumsi ${SOURCE_FILES} MumbleCommunicator.cpp MumbleCommunicator.hpp)
|
||||
set(SOURCE_FILES main.cpp PjsuaCommunicator.cpp PjsuaMediaPort.cpp MumbleCommunicator.cpp SoundSampleQueue.cpp)
|
||||
|
||||
add_executable(mumsi ${SOURCE_FILES} AbstractCommunicator.hpp)
|
||||
target_link_libraries(mumsi ${OPUS_LIBRARIES})
|
||||
target_link_libraries(mumsi ${SNDFILE_LIBRARIES})
|
||||
target_link_libraries(mumsi ${PJSIP_LIBRARIES})
|
||||
target_link_libraries(mumsi ${LOG4CPP_LIBRARIES})
|
||||
target_link_libraries(mumsi mumble)
|
||||
|
@ -3,19 +3,6 @@
|
||||
|
||||
#include "MumbleCommunicator.hpp"
|
||||
|
||||
void mumble_serversync_callback(char *welcome_text,
|
||||
int32_t session,
|
||||
int32_t max_bandwidth,
|
||||
int64_t permissions,
|
||||
void *userData) {
|
||||
printf("%s\n", welcome_text);
|
||||
}
|
||||
|
||||
static int verify_cert(uint8_t *, uint32_t) {
|
||||
// Accept every cert
|
||||
return 1;
|
||||
}
|
||||
|
||||
void mumble::MumbleCommunicator::receiveAudioFrameCallback(uint8_t *audio_data, uint32_t audio_data_size) {
|
||||
int dataPointer = 1;
|
||||
opus_int16 pcmData[1024];
|
||||
@ -38,17 +25,19 @@ void mumble::MumbleCommunicator::receiveAudioFrameCallback(uint8_t *audio_data,
|
||||
unsigned int iFrameSize = mumble::SAMPLE_RATE / 100;
|
||||
iAudioBufferSize = iFrameSize;
|
||||
iAudioBufferSize *= 12;
|
||||
int decodedSamples = opus_decode(opusState,
|
||||
int decodedSamples = opus_decode(opusDecoder,
|
||||
reinterpret_cast<const unsigned char *>(&audio_data[dataPointer]),
|
||||
opusDataLength,
|
||||
pcmData,
|
||||
iAudioBufferSize,
|
||||
0);
|
||||
|
||||
printf("\nsessionId: %ld, seqNum: %ld, opus data len: %ld, last: %d, pos: %ld, decoded: %d\n",
|
||||
sessionId, sequenceNumber, opusDataLength, lastPacket, dataPointer, decodedSamples);
|
||||
logger.debug("Received %d bytes of Opus data (seq %ld), decoded to %d bytes. Push it to outputQueue.",
|
||||
opusDataLength, sequenceNumber, decodedSamples);
|
||||
|
||||
outputQueue.push(pcmData, decodedSamples);
|
||||
} else {
|
||||
printf("I received %d bytes of audio data\n", audio_data_size);
|
||||
logger.warn("Received %d bytes of non-recognisable audio data.", audio_data_size);
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,10 +46,31 @@ static void mumble_audio_callback(uint8_t *audio_data, uint32_t audio_data_size,
|
||||
mumbleCommunicator->receiveAudioFrameCallback(audio_data, audio_data_size);
|
||||
}
|
||||
|
||||
static void mumble_serversync_callback(char *welcome_text,
|
||||
int32_t session,
|
||||
int32_t max_bandwidth,
|
||||
int64_t permissions,
|
||||
void *userData) {
|
||||
printf("%s\n", welcome_text);
|
||||
}
|
||||
|
||||
mumble::MumbleCommunicator::MumbleCommunicator(std::string user, std::string password, std::string host, int port) {
|
||||
static int verify_cert(uint8_t *, uint32_t) {
|
||||
// Accept every cert
|
||||
return 1;
|
||||
}
|
||||
|
||||
opusState = opus_decoder_create(SAMPLE_RATE, 1, nullptr); //todo grab error
|
||||
mumble::MumbleCommunicator::MumbleCommunicator(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue,
|
||||
std::string user,
|
||||
std::string password,
|
||||
std::string host,
|
||||
int port) : AbstractCommunicator(inputQueue, outputQueue),
|
||||
outgoingAudioSequenceNumber(1),
|
||||
logger(log4cpp::Category::getInstance("MumbleCommunicator")) {
|
||||
|
||||
opusDecoder = opus_decoder_create(SAMPLE_RATE, 1, nullptr); //todo grab error
|
||||
opusEncoder = opus_encoder_create(SAMPLE_RATE, 1, OPUS_APPLICATION_VOIP, nullptr);
|
||||
|
||||
struct mumble_config config;
|
||||
std::memset(&config, 0, sizeof(config));
|
||||
@ -95,6 +105,23 @@ mumble::MumbleCommunicator::~MumbleCommunicator() {
|
||||
void mumble::MumbleCommunicator::loop() {
|
||||
int quit = 0;
|
||||
while (quit == 0) {
|
||||
|
||||
opus_int16 pcmData[1024];
|
||||
unsigned char outputBuffer[1024];
|
||||
int pcmLength = inputQueue.pop(pcmData, 720);
|
||||
|
||||
logger.debug("Pop %d samples from inputQueue.", pcmLength);
|
||||
|
||||
if (pcmLength > 0) {
|
||||
int encodedSamples = opus_encode(opusEncoder, pcmData, pcmLength, outputBuffer, 1024);
|
||||
|
||||
logger.debug("Sending %d bytes of Opus audio data (seq %d).", encodedSamples, outgoingAudioSequenceNumber);
|
||||
|
||||
mumble_send_audio_data(mumble, outgoingAudioSequenceNumber, outputBuffer, encodedSamples);
|
||||
|
||||
outgoingAudioSequenceNumber += 2;
|
||||
}
|
||||
|
||||
int status = mumble_tick(mumble);
|
||||
if (status < 0) {
|
||||
throw mumble::Exception("mumble_tick status " + status);
|
||||
|
@ -1,6 +1,8 @@
|
||||
#ifndef MUMSI_MUMBLECOMMUNICATOR_HPP
|
||||
#define MUMSI_MUMBLECOMMUNICATOR_HPP
|
||||
|
||||
#include "AbstractCommunicator.hpp"
|
||||
|
||||
extern "C" {
|
||||
#include "libmumble.h"
|
||||
}
|
||||
@ -8,6 +10,7 @@ extern "C" {
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <opus.h>
|
||||
#include <log4cpp/Category.hh>
|
||||
|
||||
namespace mumble {
|
||||
|
||||
@ -18,9 +21,15 @@ namespace mumble {
|
||||
Exception(const char *message) : std::runtime_error(message) { }
|
||||
};
|
||||
|
||||
class MumbleCommunicator {
|
||||
class MumbleCommunicator : public AbstractCommunicator {
|
||||
public:
|
||||
MumbleCommunicator(std::string user, std::string password, std::string host, int port = 0);
|
||||
MumbleCommunicator(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue,
|
||||
std::string user,
|
||||
std::string password,
|
||||
std::string host,
|
||||
int port = 0);
|
||||
|
||||
~MumbleCommunicator();
|
||||
|
||||
@ -29,8 +38,13 @@ namespace mumble {
|
||||
void receiveAudioFrameCallback(uint8_t *audio_data, uint32_t audio_data_size);
|
||||
|
||||
private:
|
||||
log4cpp::Category &logger;
|
||||
|
||||
mumble_struct *mumble;
|
||||
OpusDecoder *opusState;
|
||||
OpusDecoder *opusDecoder;
|
||||
OpusEncoder *opusEncoder;
|
||||
|
||||
int outgoingAudioSequenceNumber;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -60,10 +60,14 @@ static void onCallState(pjsua_call_id call_id,
|
||||
}
|
||||
|
||||
|
||||
pjsua::PjsuaCommunicator::PjsuaCommunicator(std::string host,
|
||||
pjsua::PjsuaCommunicator::PjsuaCommunicator(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue,
|
||||
std::string host,
|
||||
std::string user,
|
||||
std::string password,
|
||||
PjsuaMediaPort &mediaPort) : mediaPort(mediaPort) {
|
||||
std::string password)
|
||||
: AbstractCommunicator(inputQueue, outputQueue),
|
||||
mediaPort(inputQueue, outputQueue) {
|
||||
|
||||
pj_status_t status;
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define MUMSI_PJSUACOMMUNICATOR_HPP
|
||||
|
||||
#include "PjsuaMediaPort.hpp"
|
||||
#include "AbstractCommunicator.hpp"
|
||||
|
||||
#include <pjmedia.h>
|
||||
|
||||
@ -12,19 +13,21 @@ namespace pjsua {
|
||||
|
||||
constexpr int SIP_PORT = 5060;
|
||||
|
||||
class PjsuaCommunicator {
|
||||
class PjsuaCommunicator : public AbstractCommunicator {
|
||||
public:
|
||||
PjsuaCommunicator(std::string host,
|
||||
PjsuaCommunicator(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue,
|
||||
std::string host,
|
||||
std::string user,
|
||||
std::string password,
|
||||
PjsuaMediaPort &mediaPort);
|
||||
std::string password);
|
||||
|
||||
~PjsuaCommunicator();
|
||||
|
||||
void loop();
|
||||
|
||||
private:
|
||||
PjsuaMediaPort &mediaPort;
|
||||
PjsuaMediaPort mediaPort;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -4,15 +4,18 @@ using namespace std;
|
||||
using namespace pjsua;
|
||||
|
||||
|
||||
static pj_status_t getFrame(pjmedia_port *port,
|
||||
pj_status_t pjsua::MediaPort_getFrame(pjmedia_port *port,
|
||||
pjmedia_frame *frame) {
|
||||
PjsuaMediaPort *mediaPort = static_cast<PjsuaMediaPort *>(port->port_data.pdata);
|
||||
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
|
||||
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
|
||||
|
||||
//todo code here
|
||||
for (int i = 0; i < count; ++i) {
|
||||
samples[i] = 10000.8 * (i % 10);
|
||||
int takenSamples = mediaPort->inputQueue.pop(samples, count);
|
||||
|
||||
mediaPort->logger.debug("Pop %d samples from inputQueue.", takenSamples);
|
||||
|
||||
for (int i = takenSamples; i < count; ++i) {
|
||||
samples[i] = 0;
|
||||
}
|
||||
|
||||
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
|
||||
@ -20,13 +23,15 @@ static pj_status_t getFrame(pjmedia_port *port,
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
static pj_status_t putFrame(pjmedia_port *port,
|
||||
pj_status_t pjsua::MediaPort_putFrame(pjmedia_port *port,
|
||||
pjmedia_frame *frame) {
|
||||
PjsuaMediaPort *mediaPort = static_cast<PjsuaMediaPort *>(port->port_data.pdata);
|
||||
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
|
||||
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
|
||||
|
||||
//todo code here
|
||||
mediaPort->outputQueue.push(samples, count);
|
||||
|
||||
mediaPort->logger.debug("Push %d samples into outputQueue.", count);
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
@ -49,8 +54,8 @@ pjmedia_port *pjsua::PjsuaMediaPort::create_pjmedia_port() {
|
||||
throw pjsua::Exception("Error while calling pjmedia_port_info_init().", status);
|
||||
}
|
||||
|
||||
_pjmedia_port->get_frame = &getFrame;
|
||||
_pjmedia_port->put_frame = &putFrame;
|
||||
_pjmedia_port->get_frame = &MediaPort_getFrame;
|
||||
_pjmedia_port->put_frame = &MediaPort_putFrame;
|
||||
|
||||
_pjmedia_port->port_data.pdata = this;
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
#ifndef MUMSI_PJSUAMEDIAPORT_HPP
|
||||
#define MUMSI_PJSUAMEDIAPORT_HPP
|
||||
|
||||
//todo wywalić i wrzucić do nagłówka z definicjami
|
||||
#include "AbstractCommunicator.hpp"
|
||||
|
||||
#include <pjmedia.h>
|
||||
|
||||
#include <log4cpp/Category.hh>
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
@ -21,18 +26,43 @@ namespace pjsua {
|
||||
}
|
||||
};
|
||||
|
||||
pj_status_t MediaPort_getFrame(pjmedia_port *port, pjmedia_frame *frame);
|
||||
|
||||
pj_status_t MediaPort_putFrame(pjmedia_port *port, pjmedia_frame *frame);
|
||||
|
||||
class PjsuaMediaPort {
|
||||
public:
|
||||
|
||||
PjsuaMediaPort() : _pjmedia_port(nullptr) { }
|
||||
PjsuaMediaPort(
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue)
|
||||
: inputQueue(inputQueue),
|
||||
outputQueue(outputQueue),
|
||||
_pjmedia_port(nullptr),
|
||||
logger(log4cpp::Category::getInstance("PjsuaMediaPort")) { }
|
||||
|
||||
~PjsuaMediaPort();
|
||||
|
||||
|
||||
pjmedia_port *create_pjmedia_port();
|
||||
|
||||
|
||||
private:
|
||||
log4cpp::Category &logger;
|
||||
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue;
|
||||
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue;
|
||||
|
||||
pjmedia_port *_pjmedia_port;
|
||||
|
||||
/**
|
||||
* For PJSUA implementation reasons, these callbacks have to be functions, not methods.
|
||||
* Since 'friend' usage.
|
||||
*/
|
||||
friend pj_status_t MediaPort_getFrame(pjmedia_port *port,
|
||||
pjmedia_frame *frame);
|
||||
|
||||
friend pj_status_t MediaPort_putFrame(pjmedia_port *port,
|
||||
pjmedia_frame *frame);
|
||||
};
|
||||
}
|
||||
|
||||
|
5
SoundSampleQueue.cpp
Normal file
5
SoundSampleQueue.cpp
Normal file
@ -0,0 +1,5 @@
|
||||
//
|
||||
// Created by michal on 28.09.15.
|
||||
//
|
||||
|
||||
#include "SoundSampleQueue.hpp"
|
51
SoundSampleQueue.hpp
Normal file
51
SoundSampleQueue.hpp
Normal file
@ -0,0 +1,51 @@
|
||||
#ifndef MUMSI_SOUNDSAMPLEQUEUE_HPP
|
||||
#define MUMSI_SOUNDSAMPLEQUEUE_HPP
|
||||
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
template<typename SAMPLE_TYPE>
|
||||
class SoundSampleQueue {
|
||||
public:
|
||||
SoundSampleQueue() {
|
||||
start = 0;
|
||||
stop = 0;
|
||||
buffer = new SAMPLE_TYPE[10000000];
|
||||
}
|
||||
|
||||
~SoundSampleQueue() {
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
void push(SAMPLE_TYPE *data, int length) {
|
||||
std::lock_guard<std::mutex> lock(accessMutex);
|
||||
|
||||
std::memcpy(&buffer[stop], data, length);
|
||||
stop += length;
|
||||
// printf("pos: %d\n", stop);
|
||||
}
|
||||
|
||||
int pop(SAMPLE_TYPE *data, int maxLength) {
|
||||
std::lock_guard<std::mutex> lock(accessMutex);
|
||||
|
||||
int samplesToTake = std::min(stop - start, maxLength);
|
||||
std::memcpy(data, &buffer[stop - samplesToTake], samplesToTake);
|
||||
stop -= samplesToTake;
|
||||
|
||||
//todo maksymalna pojemność bufora
|
||||
return samplesToTake;
|
||||
}
|
||||
|
||||
private:
|
||||
int start;
|
||||
int stop;
|
||||
|
||||
std::mutex accessMutex;
|
||||
|
||||
SAMPLE_TYPE *buffer;
|
||||
};
|
||||
|
||||
|
||||
#endif //MUMSI_SOUNDSAMPLEQUEUE_HPP
|
35
main.cpp
35
main.cpp
@ -1,8 +1,15 @@
|
||||
#include <stdio.h>
|
||||
#include "log4cpp/Category.hh"
|
||||
#include "log4cpp/Appender.hh"
|
||||
#include "log4cpp/FileAppender.hh"
|
||||
#include "log4cpp/OstreamAppender.hh"
|
||||
#include "log4cpp/Layout.hh"
|
||||
#include "log4cpp/BasicLayout.hh"
|
||||
#include "log4cpp/Priority.hh"
|
||||
|
||||
#include "PjsuaCommunicator.hpp"
|
||||
#include "PjsuaMediaPort.hpp"
|
||||
#include "MumbleCommunicator.hpp"
|
||||
|
||||
#include "SoundSampleQueue.hpp"
|
||||
|
||||
#define SIP_DOMAIN "sip.antisip.com"
|
||||
#define SIP_USER "melangtone"
|
||||
@ -14,19 +21,31 @@
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
mumble::MumbleCommunicator mumbleCommunicator(MUMBLE_USER, MUMBLE_PASSWD, MUMBLE_DOMAIN);
|
||||
log4cpp::Appender *appender1 = new log4cpp::OstreamAppender("console", &std::cout);
|
||||
appender1->setLayout(new log4cpp::BasicLayout());
|
||||
log4cpp::Category &logger = log4cpp::Category::getRoot();
|
||||
logger.setPriority(log4cpp::Priority::DEBUG);
|
||||
logger.addAppender(appender1);
|
||||
|
||||
pjsua::PjsuaMediaPort mediaPort;
|
||||
pjsua::PjsuaCommunicator pjsuaCommunicator(SIP_DOMAIN, SIP_USER, SIP_PASSWD, mediaPort);
|
||||
SoundSampleQueue<int16_t> mumbleToSipQueue;
|
||||
SoundSampleQueue<int16_t> sipToMumbleQueue;
|
||||
|
||||
mumble::MumbleCommunicator mumbleCommunicator(
|
||||
sipToMumbleQueue,
|
||||
mumbleToSipQueue,
|
||||
MUMBLE_USER, MUMBLE_PASSWD, MUMBLE_DOMAIN);
|
||||
|
||||
pjsua::PjsuaCommunicator pjsuaCommunicator(
|
||||
mumbleToSipQueue,
|
||||
sipToMumbleQueue,
|
||||
SIP_DOMAIN, SIP_USER, SIP_PASSWD);
|
||||
|
||||
logger.info("Application started.");
|
||||
|
||||
pjsuaCommunicator.loop();
|
||||
|
||||
mumbleCommunicator.loop();
|
||||
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
290
sim.c
290
sim.c
@ -1,290 +0,0 @@
|
||||
/* $Id: simple_pjsua.c 3553 2011-05-05 06:14:19Z nanang $ */
|
||||
/*
|
||||
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
|
||||
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <pjsua-lib/pjsua.h>
|
||||
|
||||
#include <pjsip.h>
|
||||
#include <pjmedia.h>
|
||||
#include <pjlib.h>
|
||||
|
||||
#include <stdlib.h> /* atoi() */
|
||||
#include <stdio.h>
|
||||
#include <math.h> /* sin() */
|
||||
|
||||
#define THIS_FILE "APP"
|
||||
|
||||
#define SIP_DOMAIN "sip.antisip.com"
|
||||
#define SIP_USER "melangtone"
|
||||
#define SIP_PASSWD "b8DU9AZXbd9tVCWg"
|
||||
|
||||
|
||||
/* Struct attached to sine generator */
|
||||
typedef struct {
|
||||
} port_data;
|
||||
|
||||
pjmedia_port *sine_port;
|
||||
int slot;
|
||||
|
||||
/* This callback is called to feed more samples */
|
||||
static pj_status_t sine_get_frame(pjmedia_port *port,
|
||||
pjmedia_frame *frame) {
|
||||
port_data *sine = port->port_data.pdata;
|
||||
pj_int16_t *samples = frame->buf;
|
||||
pj_size_t count;
|
||||
|
||||
if (PJMEDIA_PIA_CCNT(&port->info) != 1) {
|
||||
//todo throw exception
|
||||
}
|
||||
|
||||
/* Get number of samples */
|
||||
count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
samples[i] = 10000.8 * (i % 10);
|
||||
}
|
||||
|
||||
/* Must set frame->type correctly, otherwise the sound device
|
||||
* will refuse to play.
|
||||
*/
|
||||
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
static pj_status_t sine_put_frame(pjmedia_port *port,
|
||||
pjmedia_frame *frame) {
|
||||
|
||||
int sum = 0;
|
||||
pj_int16_t *samples = frame->buf;
|
||||
for (int i = 0; i < frame->size / 2; i++) {
|
||||
sum += samples[i];
|
||||
}
|
||||
printf("%d\n", sum);
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a media port to generate sine wave samples.
|
||||
*/
|
||||
static pj_status_t create_sine_port(pj_pool_t *pool,
|
||||
unsigned sampling_rate,
|
||||
unsigned channel_count,
|
||||
pjmedia_port **p_port) {
|
||||
pjmedia_port *port;
|
||||
unsigned i;
|
||||
unsigned count;
|
||||
pj_str_t name;
|
||||
port_data *sine;
|
||||
|
||||
PJ_ASSERT_RETURN(pool && channel_count > 0 && channel_count <= 2, PJ_EINVAL);
|
||||
|
||||
port = pj_pool_zalloc(pool, sizeof(pjmedia_port));
|
||||
PJ_ASSERT_RETURN(port != NULL, PJ_ENOMEM);
|
||||
|
||||
/* Fill in port info. */
|
||||
name = pj_str("sine generator");
|
||||
pjmedia_port_info_init(&port->info, &name,
|
||||
PJMEDIA_SIG_CLASS_PORT_AUD('s', 'i'),
|
||||
sampling_rate,
|
||||
1,
|
||||
16, sampling_rate * 20 / 1000 * 1);
|
||||
|
||||
/* Set the function to feed frame */
|
||||
port->get_frame = &sine_get_frame;
|
||||
port->put_frame = &sine_put_frame;
|
||||
|
||||
/* Create sine port data */
|
||||
port->port_data.pdata = sine = pj_pool_zalloc(pool, sizeof(port_data));
|
||||
|
||||
*p_port = port;
|
||||
|
||||
return PJ_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* Callback called by the library upon receiving incoming call */
|
||||
static void on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
|
||||
pjsip_rx_data *rdata) {
|
||||
pjsua_call_info ci;
|
||||
|
||||
PJ_UNUSED_ARG(acc_id);
|
||||
PJ_UNUSED_ARG(rdata);
|
||||
|
||||
pjsua_call_get_info(call_id, &ci);
|
||||
|
||||
PJ_LOG(3, (THIS_FILE, "Incoming call from %.*s!!",
|
||||
(int) ci.remote_info.slen,
|
||||
ci.remote_info.ptr));
|
||||
|
||||
/* Automatically answer incoming calls with 200/OK */
|
||||
pjsua_call_answer(call_id, 200, NULL, NULL);
|
||||
}
|
||||
|
||||
/* Callback called by the library when call's state has changed */
|
||||
static void on_call_state(pjsua_call_id call_id, pjsip_event *e) {
|
||||
pjsua_call_info ci;
|
||||
|
||||
PJ_UNUSED_ARG(e);
|
||||
|
||||
pjsua_call_get_info(call_id, &ci);
|
||||
PJ_LOG(3, (THIS_FILE, "Call %d state=%.*s", call_id,
|
||||
(int) ci.state_text.slen,
|
||||
ci.state_text.ptr));
|
||||
}
|
||||
|
||||
/* Callback called by the library when call's media state has changed */
|
||||
static void on_call_media_state(pjsua_call_id call_id) {
|
||||
pjsua_call_info ci;
|
||||
|
||||
pjsua_call_get_info(call_id, &ci);
|
||||
|
||||
if (ci.media_status == PJSUA_CALL_MEDIA_ACTIVE) {
|
||||
// When media is active, connect call to sound device.
|
||||
pjsua_conf_connect(ci.conf_slot, slot);
|
||||
pjsua_conf_connect(slot, ci.conf_slot);
|
||||
}
|
||||
}
|
||||
|
||||
/* Display error and exit application */
|
||||
static void error_exit(const char *title, pj_status_t status) {
|
||||
pjsua_perror(THIS_FILE, title, status);
|
||||
pjsua_destroy();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
* main()
|
||||
*
|
||||
* argv[1] may contain URL to call.
|
||||
*/
|
||||
int main(int argc, char *argv[]) {
|
||||
pjsua_acc_id acc_id;
|
||||
pj_status_t status;
|
||||
pj_pool_t *pool;
|
||||
|
||||
int channel_count = 1;
|
||||
|
||||
/* Create pjsua first! */
|
||||
status = pjsua_create();
|
||||
if (status != PJ_SUCCESS) error_exit("Error in pjsua_create()", status);
|
||||
|
||||
/* If argument is specified, it's got to be a valid SIP URL */
|
||||
if (argc > 1) {
|
||||
status = pjsua_verify_url(argv[1]);
|
||||
if (status != PJ_SUCCESS) error_exit("Invalid URL in argv", status);
|
||||
}
|
||||
|
||||
/* Init pjsua */
|
||||
{
|
||||
pjsua_config cfg;
|
||||
pjsua_logging_config log_cfg;
|
||||
|
||||
pjsua_config_default(&cfg);
|
||||
cfg.cb.on_incoming_call = &on_incoming_call;
|
||||
cfg.cb.on_call_media_state = &on_call_media_state;
|
||||
cfg.cb.on_call_state = &on_call_state;
|
||||
|
||||
pjsua_logging_config_default(&log_cfg);
|
||||
log_cfg.console_level = 4;
|
||||
|
||||
status = pjsua_init(&cfg, &log_cfg, NULL);
|
||||
if (status != PJ_SUCCESS) error_exit("Error in pjsua_init()", status);
|
||||
}
|
||||
|
||||
/* Add UDP transport. */
|
||||
{
|
||||
pjsua_transport_config cfg;
|
||||
|
||||
pjsua_transport_config_default(&cfg);
|
||||
cfg.port = 5060;
|
||||
status = pjsua_transport_create(PJSIP_TRANSPORT_UDP, &cfg, NULL);
|
||||
if (status != PJ_SUCCESS) error_exit("Error creating transport", status);
|
||||
}
|
||||
|
||||
pj_caching_pool cp;
|
||||
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
|
||||
|
||||
pool = pj_pool_create(&cp.factory, /* pool factory */
|
||||
"wav", /* pool name. */
|
||||
4000, /* init size */
|
||||
4000, /* increment size */
|
||||
NULL /* callback on error */
|
||||
);
|
||||
|
||||
status = create_sine_port(pool, /* memory pool */
|
||||
8000, /* sampling rate */
|
||||
channel_count,/* # of channels */
|
||||
&sine_port /* returned port */
|
||||
);
|
||||
if (status != PJ_SUCCESS) {
|
||||
error_exit("Unable to create sine port", status);
|
||||
return 1;
|
||||
}
|
||||
|
||||
pjsua_set_null_snd_dev();
|
||||
|
||||
pjsua_conf_add_port(pool, sine_port, &slot);
|
||||
|
||||
/* Initialization is done, now start pjsua */
|
||||
status = pjsua_start();
|
||||
if (status != PJ_SUCCESS) error_exit("Error starting pjsua", status);
|
||||
|
||||
/* Register to SIP server by creating SIP account. */
|
||||
{
|
||||
pjsua_acc_config cfg;
|
||||
|
||||
pjsua_acc_config_default(&cfg);
|
||||
cfg.id = pj_str("sip:" SIP_USER "@" SIP_DOMAIN);
|
||||
cfg.reg_uri = pj_str("sip:" SIP_DOMAIN);
|
||||
cfg.cred_count = 1;
|
||||
cfg.cred_info[0].realm = pj_str(SIP_DOMAIN);
|
||||
cfg.cred_info[0].scheme = pj_str("digest");
|
||||
cfg.cred_info[0].username = pj_str(SIP_USER);
|
||||
cfg.cred_info[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
|
||||
cfg.cred_info[0].data = pj_str(SIP_PASSWD);
|
||||
|
||||
status = pjsua_acc_add(&cfg, PJ_TRUE, &acc_id);
|
||||
if (status != PJ_SUCCESS) error_exit("Error adding account", status);
|
||||
}
|
||||
|
||||
|
||||
/* Wait until user press "q" to quit. */
|
||||
for (; ;) {
|
||||
char option[10];
|
||||
|
||||
puts("Press 'h' to hangup all calls, 'q' to quit");
|
||||
if (fgets(option, sizeof(option), stdin) == NULL) {
|
||||
puts("EOF while reading stdin, will quit now..");
|
||||
break;
|
||||
}
|
||||
|
||||
if (option[0] == 'q')
|
||||
break;
|
||||
|
||||
if (option[0] == 'h')
|
||||
pjsua_call_hangup_all();
|
||||
}
|
||||
|
||||
/* Destroy pjsua */
|
||||
pjsua_destroy();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user