/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ /*************************************************************************** * test_init.cc * * Mon Sep 2 14:02:16 CEST 2013 * Copyright 2013 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of lrtp. * * lrtp is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * lrtp 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with lrtp; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include #define KEY "123456789012345678901234567890123456789012345678901234567890" #define SSRC 1234567890 #define FS 48000 #define F1 440 #define AF1 0.3 #define F2 500 #define AF2 0.7 #define THRESHOLD 1000.0 void get_samples(double x, short *r, short *l) { double amp1 = sin((2 * M_PI / (double)FS)* x * AF1) * SHRT_MAX; double amp2 = sin((2 * M_PI / (double)FS)* x * AF2) * SHRT_MAX; *r = (short)(sin(2 * M_PI / FS * x * F1) * amp1); *l = (short)(sin(2 * M_PI / FS * x * F2) * amp2); // printf("x: %.0f l:%d r:%d\n", x, *l, *r); } class test_opus_class : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(test_opus_class); CPPUNIT_TEST(test_opus); CPPUNIT_TEST_SUITE_END(); public: void setUp() {} void tearDown() {} void test_opus() { size_t channels = 2; size_t ms[] = { 120, 240, 480, 960, 1920, 2880 }; std::vector packets; unsigned int csrc = 42; int32_t lookahead; //size_t ts = 0; int sent = 0; { // Encode struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); int x = lrtp_create_profile(lrtp, PROFILE_OPUS, csrc, OPTION_END); CPPUNIT_ASSERT_EQUAL(0, x); char packet[16*1024]; //size_t packetsize = sizeof(packet); int err; OpusEncoder *opus = opus_encoder_create(FS, channels, OPUS_APPLICATION_AUDIO, &err); CPPUNIT_ASSERT_EQUAL(0, err); opus_encoder_ctl(opus, OPUS_SET_BITRATE(256000));// [500;512000] opus_encoder_ctl(opus, OPUS_SET_COMPLEXITY(10)); // [0;10] opus_encoder_ctl(opus, OPUS_SET_SIGNAL(OPUS_AUTO)); opus_encoder_ctl(opus, OPUS_GET_LOOKAHEAD(&lookahead)); long long int sin_x = 0; //int cnt = 0; size_t timestamp = 0; size_t idx = 0; for(unsigned int ts = 0; ts < FS / 10; ts++) { size_t pcmsize = ms[idx] / (48000.0 / FS); // Number of samples pr channel sent += pcmsize; short *pcm = new short[100000/*pcmsize * channels*/]; for(size_t i = 0 ; i < pcmsize; i++) { sin_x++; if((int)sin_x % FS == 0) { idx++; idx = idx % (sizeof(ms)/sizeof(size_t)); } get_samples(sin_x, &pcm[i*2], &pcm[i*2+1]); } // Master timestamp is sample number in 48kHz (Opus RFC states this) timestamp += pcmsize * 48000 / FS; char frame[pcmsize]; int framesize = sizeof(frame); framesize = opus_encode(opus, pcm, pcmsize, (unsigned char*)frame, framesize); if(framesize < 0) { printf("Opus error: %s\n", opus_strerror(framesize)); } int ret = lrtp_enqueue_frame(lrtp, csrc, frame, framesize, timestamp); while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { std::string p; p.append(packet, ret); packets.push_back(p); } delete[] pcm; } opus_encoder_destroy(opus); lrtp_destroy_profile(lrtp, csrc); lrtp_close(lrtp); } { // Decode struct lrtp_t *lrtp = lrtp_init(KEY, SSRC); int x = lrtp_create_profile(lrtp, PROFILE_OPUS, csrc, OPTION_END); CPPUNIT_ASSERT_EQUAL(0, x); int err; OpusDecoder *opus = opus_decoder_create(FS, channels, &err); CPPUNIT_ASSERT_EQUAL(0, err); //int idx = (sizeof(ms)/sizeof(size_t)) - 1; char frame[16*1024]; long long int sin_x = -lookahead; long long int errl = 0; long long int errr = 0; //int cnt = 0; std::vector::iterator i = packets.begin(); while(i != packets.end()) { size_t packetsize = i->size(); const char *packet = i->data(); unsigned int ts; lrtp_unpack(lrtp, packet, packetsize); int n = 0; int ret; while((ret = lrtp_dequeue_frame(lrtp, frame, sizeof(frame), &csrc, &ts)) != 0) { size_t pcmsize = 16*1024;//ms[idx] / (48000 / FS); short *pcm = new short[pcmsize * channels]; int res = opus_decode(opus, (const unsigned char*)frame, ret, pcm, pcmsize, 0); n += res; for(int i = 0; i < res; i++) { short left; short right; get_samples(sin_x, &left, &right); if(sin_x > 0) { errl += abs(abs(left) - abs(pcm[2 * i])); errr += abs(abs(right) - abs(pcm[2 * i + 1])); } sin_x++; } delete[] pcm; } i++; } CPPUNIT_ASSERT(((double)errl / (double)sin_x) < THRESHOLD); CPPUNIT_ASSERT(((double)errr / (double)sin_x) < THRESHOLD); CPPUNIT_ASSERT(sent); // Fail if no data was sent. CPPUNIT_ASSERT_EQUAL(sent, (int)sin_x + lookahead); opus_decoder_destroy(opus); lrtp_destroy_profile(lrtp, csrc); lrtp_close(lrtp); } } }; // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION(test_opus_class);