Add libsndfile to record audio stream in different places for debugging purpuses.

Add Boost and unit test framework.
Fix crude soundSampleQueue.
Disable link SIP->Mumble for a while.
This commit is contained in:
Michał Słomkowski 2015-10-04 22:30:10 +02:00
parent 0d162df380
commit cdd782c4ce
9 changed files with 135 additions and 37 deletions

View File

@ -4,23 +4,47 @@ project(mumsi)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
INCLUDE(FindPkgConfig)
find_package(Boost COMPONENTS system unit_test_framework program_options filesystem REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(OPUS "opus")
pkg_check_modules(PJSIP "libpjproject")
pkg_check_modules(LOG4CPP "log4cpp")
pkg_check_modules(SNDFILE "sndfile")
include_directories(${OPUS_INCLUDE_DIRS})
include_directories(libmumble)
include_directories(${PJSIP_INCLUDE_DIRS})
include_directories(${LOG4CPP_INCLUDE_DIRS})
include_directories(${SNDFILE_INCLUDE_DIRS})
include_directories(libmumble)
link_directories(libmumble)
set(SOURCE_FILES main.cpp PjsuaCommunicator.cpp PjsuaMediaPort.cpp MumbleCommunicator.cpp SoundSampleQueue.cpp)
set(SOURCE_FILES
PjsuaCommunicator.cpp
PjsuaMediaPort.cpp
MumbleCommunicator.cpp
SoundSampleQueue.cpp
AbstractCommunicator.hpp
)
add_executable(mumsi ${SOURCE_FILES} AbstractCommunicator.hpp)
set(TEST_SOURCE_FILES
SoundSampleQueueTest.cpp
)
add_executable(mumsi ${SOURCE_FILES} main.cpp)
target_link_libraries(mumsi ${OPUS_LIBRARIES})
target_link_libraries(mumsi ${PJSIP_LIBRARIES})
target_link_libraries(mumsi ${LOG4CPP_LIBRARIES})
target_link_libraries(mumsi ${SNDFILE_LIBRARIES})
target_link_libraries(mumsi ${Boost_LIBRARIES})
target_link_libraries(mumsi mumble)
add_executable(mumsi_test ${SOURCE_FILES} ${TEST_SOURCE_FILES} test_main.cpp)
target_link_libraries(mumsi_test ${OPUS_LIBRARIES})
target_link_libraries(mumsi_test ${PJSIP_LIBRARIES})
target_link_libraries(mumsi_test ${LOG4CPP_LIBRARIES})
target_link_libraries(mumsi_test ${SNDFILE_LIBRARIES})
target_link_libraries(mumsi_test ${Boost_LIBRARIES})
target_link_libraries(mumsi_test mumble)

View File

@ -32,10 +32,13 @@ void mumble::MumbleCommunicator::receiveAudioFrameCallback(uint8_t *audio_data,
iAudioBufferSize,
0);
fileHandle.write(pcmData, 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);
outputQueue.push_back(pcmData, decodedSamples);
} else {
logger.warn("Received %d bytes of non-recognisable audio data.", audio_data_size);
}
@ -50,7 +53,7 @@ static void mumble_serversync_callback(char *welcome_text,
int32_t session,
int32_t max_bandwidth,
int64_t permissions,
void *userData) {
void *usterData) {
printf("%s\n", welcome_text);
}
@ -70,7 +73,11 @@ mumble::MumbleCommunicator::MumbleCommunicator(
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);
opus_encoder_ctl(opusEncoder, OPUS_SET_VBR(0));
fileHandle = SndfileHandle("capture_mumble.wav", SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, SAMPLE_RATE);
struct mumble_config config;
std::memset(&config, 0, sizeof(config));
@ -108,19 +115,24 @@ void mumble::MumbleCommunicator::loop() {
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 pcmLength = inputQueue.pop(pcmData, 960);
//
// logger.debug("Pop %d samples from inputQueue.", pcmLength);
//
// if (pcmLength > 0) {
// int encodedSamples = opus_encode(opusEncoder, pcmData, pcmLength, outputBuffer, sizeof(outputBuffer));
//
// if (encodedSamples < 1) {
// logger.warn("opus_encode returned %d: %s", encodedSamples, opus_strerror(encodedSamples));
// } else {
// logger.debug("Sending %d bytes of Opus audio data (seq %d).", encodedSamples,
// outgoingAudioSequenceNumber);
//
// mumble_send_audio_data(mumble, outgoingAudioSequenceNumber, outputBuffer, encodedSamples);
//
// outgoingAudioSequenceNumber += 1;
// }
// }
int status = mumble_tick(mumble);
if (status < 0) {

View File

@ -11,6 +11,7 @@ extern "C" {
#include <stdexcept>
#include <opus.h>
#include <log4cpp/Category.hh>
#include <sndfile.hh>
namespace mumble {
@ -45,6 +46,8 @@ namespace mumble {
OpusEncoder *opusEncoder;
int outgoingAudioSequenceNumber;
SndfileHandle fileHandle;
};
}

View File

@ -10,7 +10,9 @@ pj_status_t pjsua::MediaPort_getFrame(pjmedia_port *port,
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
int takenSamples = mediaPort->inputQueue.pop(samples, count);
int takenSamples = mediaPort->inputQueue.pop_front(samples, count);
mediaPort->fileHandle.write(samples, takenSamples);
mediaPort->logger.debug("Pop %d samples from inputQueue.", takenSamples);
@ -18,6 +20,8 @@ pj_status_t pjsua::MediaPort_getFrame(pjmedia_port *port,
samples[i] = 0;
}
// todo wrzucić sample do wav
frame->type = PJMEDIA_FRAME_TYPE_AUDIO;
return PJ_SUCCESS;
@ -29,13 +33,26 @@ pj_status_t pjsua::MediaPort_putFrame(pjmedia_port *port,
pj_int16_t *samples = static_cast<pj_int16_t *>(frame->buf);
pj_size_t count = frame->size / 2 / PJMEDIA_PIA_CCNT(&port->info);
mediaPort->outputQueue.push(samples, count);
mediaPort->logger.debug("Push %d samples into outputQueue.", count);
// if (count > 0) {
// mediaPort->outputQueue.push_back(samples, count);
//
// mediaPort->logger.debug("Push %d samples into outputQueue.", count);
// }
return PJ_SUCCESS;
}
PjsuaMediaPort::PjsuaMediaPort(
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue)
: inputQueue(inputQueue),
outputQueue(outputQueue),
_pjmedia_port(nullptr),
logger(log4cpp::Category::getInstance("PjsuaMediaPort")) {
fileHandle = SndfileHandle("capture_pjsua.wav", SFM_WRITE, SF_FORMAT_WAV | SF_FORMAT_PCM_16, 1, SAMPLING_RATE);
}
pjmedia_port *pjsua::PjsuaMediaPort::create_pjmedia_port() {
_pjmedia_port = new pjmedia_port();

View File

@ -5,6 +5,7 @@
#include "AbstractCommunicator.hpp"
#include <pjmedia.h>
#include <sndfile.hh>
#include <log4cpp/Category.hh>
@ -35,11 +36,7 @@ namespace pjsua {
PjsuaMediaPort(
SoundSampleQueue<SOUND_SAMPLE_TYPE> &inputQueue,
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue)
: inputQueue(inputQueue),
outputQueue(outputQueue),
_pjmedia_port(nullptr),
logger(log4cpp::Category::getInstance("PjsuaMediaPort")) { }
SoundSampleQueue<SOUND_SAMPLE_TYPE> &outputQueue);
~PjsuaMediaPort();
@ -54,6 +51,8 @@ namespace pjsua {
pjmedia_port *_pjmedia_port;
SndfileHandle fileHandle;
/**
* For PJSUA implementation reasons, these callbacks have to be functions, not methods.
* Since 'friend' usage.

View File

@ -3,3 +3,4 @@
//
#include "SoundSampleQueue.hpp"

View File

@ -9,9 +9,9 @@
template<typename SAMPLE_TYPE>
class SoundSampleQueue {
public:
SoundSampleQueue() {
start = 0;
stop = 0;
SoundSampleQueue()
: start(0),
stop(0) {
buffer = new SAMPLE_TYPE[10000000];
}
@ -19,22 +19,26 @@ public:
delete[] buffer;
}
void push(SAMPLE_TYPE *data, int length) {
void push_back(SAMPLE_TYPE *data, int length) {
std::lock_guard<std::mutex> lock(accessMutex);
std::memcpy(&buffer[stop], data, length);
for (int i = 0; i < length; ++i) {
buffer[stop + i] = data[i];
}
stop += length;
// printf("pos: %d\n", stop);
}
int pop(SAMPLE_TYPE *data, int maxLength) {
int pop_front(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
for (int i = 0; i < samplesToTake; ++i) {
data[i] = buffer[start + i];
}
start += samplesToTake;
return samplesToTake;
}

33
SoundSampleQueueTest.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "SoundSampleQueue.hpp"
#include <boost/test/unit_test.hpp>
constexpr int CHUNK = 10;
BOOST_AUTO_TEST_CASE(SoundSampleQueueTest_Run) {
SoundSampleQueue<int16_t> queue;
int16_t buffer[CHUNK];
int content = 0;
for (int j = 0; j < 10; ++j) {
for (int i = 0; i < CHUNK; ++i) {
buffer[i] = content;
content++;
}
queue.push_back(buffer, CHUNK);
}
for (int j = 0; j < 12; ++j) {
int how_many = queue.pop_front(buffer, CHUNK);
printf("%d: ", how_many);
for (int i = 0; i < CHUNK; ++i) {
printf("%d ", buffer[i]);
}
printf("\n");
}
BOOST_CHECK_EQUAL(1, 1);
}

5
test_main.cpp Normal file
View File

@ -0,0 +1,5 @@
#define BOOST_TEST_MODULE SZARKServerTest
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>