diff --git a/CMakeLists.txt b/CMakeLists.txt index 10f0745..f10d875 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/MumbleCommunicator.cpp b/MumbleCommunicator.cpp index cc036c2..2fceb36 100644 --- a/MumbleCommunicator.cpp +++ b/MumbleCommunicator.cpp @@ -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) { diff --git a/MumbleCommunicator.hpp b/MumbleCommunicator.hpp index 88f67dc..04eb686 100644 --- a/MumbleCommunicator.hpp +++ b/MumbleCommunicator.hpp @@ -11,6 +11,7 @@ extern "C" { #include #include #include +#include namespace mumble { @@ -45,6 +46,8 @@ namespace mumble { OpusEncoder *opusEncoder; int outgoingAudioSequenceNumber; + + SndfileHandle fileHandle; }; } diff --git a/PjsuaMediaPort.cpp b/PjsuaMediaPort.cpp index a591d37..6d73a39 100644 --- a/PjsuaMediaPort.cpp +++ b/PjsuaMediaPort.cpp @@ -10,7 +10,9 @@ pj_status_t pjsua::MediaPort_getFrame(pjmedia_port *port, pj_int16_t *samples = static_cast(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(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 &inputQueue, + SoundSampleQueue &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(); diff --git a/PjsuaMediaPort.hpp b/PjsuaMediaPort.hpp index c7704a7..f9a3697 100644 --- a/PjsuaMediaPort.hpp +++ b/PjsuaMediaPort.hpp @@ -5,6 +5,7 @@ #include "AbstractCommunicator.hpp" #include +#include #include @@ -35,11 +36,7 @@ namespace pjsua { PjsuaMediaPort( SoundSampleQueue &inputQueue, - SoundSampleQueue &outputQueue) - : inputQueue(inputQueue), - outputQueue(outputQueue), - _pjmedia_port(nullptr), - logger(log4cpp::Category::getInstance("PjsuaMediaPort")) { } + SoundSampleQueue &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. diff --git a/SoundSampleQueue.cpp b/SoundSampleQueue.cpp index b1a1735..83fee88 100644 --- a/SoundSampleQueue.cpp +++ b/SoundSampleQueue.cpp @@ -3,3 +3,4 @@ // #include "SoundSampleQueue.hpp" + diff --git a/SoundSampleQueue.hpp b/SoundSampleQueue.hpp index ec0abb3..7ea98da 100644 --- a/SoundSampleQueue.hpp +++ b/SoundSampleQueue.hpp @@ -9,9 +9,9 @@ template 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 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 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; } diff --git a/SoundSampleQueueTest.cpp b/SoundSampleQueueTest.cpp new file mode 100644 index 0000000..c3d7220 --- /dev/null +++ b/SoundSampleQueueTest.cpp @@ -0,0 +1,33 @@ +#include "SoundSampleQueue.hpp" + +#include + +constexpr int CHUNK = 10; + +BOOST_AUTO_TEST_CASE(SoundSampleQueueTest_Run) { + + SoundSampleQueue 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); +} \ No newline at end of file diff --git a/test_main.cpp b/test_main.cpp new file mode 100644 index 0000000..03e1944 --- /dev/null +++ b/test_main.cpp @@ -0,0 +1,5 @@ +#define BOOST_TEST_MODULE SZARKServerTest +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MAIN + +#include