From a3cb98c1521c34fe83e0c09e95d372afdcfa250c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Mon, 9 Dec 2013 09:09:12 +0100 Subject: Finish unit test of opus transport. --- test/test_opus.cc | 112 +++++++++++++----------------------------------------- 1 file changed, 26 insertions(+), 86 deletions(-) (limited to 'test') diff --git a/test/test_opus.cc b/test/test_opus.cc index 1d55d5a..ea6a5b4 100644 --- a/test/test_opus.cc +++ b/test/test_opus.cc @@ -37,7 +37,6 @@ #include #include #include -#include #define KEY "123456789012345678901234567890123456789012345678901234567890" #define SSRC 1234567890 @@ -50,55 +49,7 @@ #define F2 500 #define AF2 0.7 -void dump(const char *title, const char *buf, size_t size) -{ - printf("%12s: ", title); - for(int i = 0; i < size; i++) { - if(i % 8 == 0) printf(" "); - printf("%02x ", (unsigned char)*buf++); - } - printf("\n"); -} - -class Audio { -public: - Audio() { - ao_initialize(); - - device = NULL; - ao_sample_format format; - - int default_driver = ao_default_driver_id(); - if(default_driver == -1) { - printf("Error could not default driver.\n"); - return; - } - printf("Default driver: %d\n", default_driver); - - format.bits = 16; - format.channels = 2; - format.rate = FS; - format.byte_format = AO_FMT_LITTLE; - - device = ao_open_live(default_driver, &format, NULL); - if(device == NULL) { - printf("Error opening device.\n"); - return; - } - } - - ~Audio() { - if(device) ao_close(device); - ao_shutdown(); - } - - void play(char *pcm, size_t size) { - ao_play(device, pcm, size); - } - -private: - ao_device *device; -}; +#define THRESHOLD 1000.0 void get_samples(double x, short *r, short *l) { @@ -106,7 +57,9 @@ void get_samples(double x, short *r, short *l) double amp2 = sin((2 * M_PI / (double)FS)* x * AF2) * SHRT_MAX; *r = (short)(sin(2 * M_PI / FS * x * F1) * amp1); - *l = 0;//(short)(sin(2 * M_PI / FS * x * F2) * amp2); + *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 @@ -125,6 +78,8 @@ public: std::vector packets; unsigned int csrc = 42; + + int32_t lookahead; size_t ts = 0; @@ -146,20 +101,17 @@ public: OPUS_APPLICATION_AUDIO, &err); CPPUNIT_ASSERT_EQUAL(0, err); - opus_encoder_ctl(opus, OPUS_SET_BITRATE(32000));// [500;512000] + 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_SET_MAX_BANDWIDTH(OPUS_BANDWIDTH_NARROWBAND)); + opus_encoder_ctl(opus, OPUS_GET_LOOKAHEAD(&lookahead)); - double sin_x = 0; + 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++) { - //printf("packet #%d\n", ts); - - //printf("idx: %d\n", idx); size_t pcmsize = ms[idx] / (48000.0 / FS); // Number of samples pr channel sent += pcmsize; short *pcm = new short[100000/*pcmsize * channels*/]; @@ -167,7 +119,7 @@ public: sin_x++; if((int)sin_x % FS == 0) { - idx++;// = rand() % (sizeof(ms)/sizeof(size_t)); + idx++; idx = idx % (sizeof(ms)/sizeof(size_t)); } @@ -177,8 +129,6 @@ public: // Master timestamp is sample number in 48kHz (Opus RFC states this) timestamp += pcmsize * 48000 / FS; - // size_t pcmsize = pcmsize * channels * sizeof(short); - char frame[pcmsize]; int framesize = sizeof(frame); framesize = opus_encode(opus, pcm, pcmsize, @@ -187,10 +137,7 @@ public: if(framesize < 0) { printf("Opus error: %s\n", opus_strerror(framesize)); } - /* - printf("Opus Packet: %d bytes compressed to %d bytes\n", - channels * pcmsize * sizeof(short), framesize); - */ + int ret = lrtp_enqueue_frame(profile, frame, framesize); while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) { std::string p; @@ -211,7 +158,6 @@ public: struct lrtp_profile_t *profile = lrtp_create_profile(lrtp, PROFILE_OPUS, csrc, - //OPTION_RAW_PKG_SIZE, pkg_size, OPTION_END); int err; @@ -219,13 +165,13 @@ public: CPPUNIT_ASSERT_EQUAL(0, err); int idx = (sizeof(ms)/sizeof(size_t)) - 1; - //printf("idx: %d\n", idx); - - //Audio audio; char frame[16*1024]; - double sin_x = 0; + 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(); @@ -234,8 +180,6 @@ public: const char *packet = i->data(); unsigned int ts; - // printf("unpack sz: %d - %p\n", packetsize, packet); - lrtp_unpack(lrtp, packet, packetsize); int n = 0; int ret; @@ -243,30 +187,23 @@ public: != 0) { size_t pcmsize = 16*1024;//ms[idx] / (48000 / FS); short *pcm = new short[pcmsize * channels]; - //printf("pcmsize %d\n", pcmsize); fflush(stdout); + int res = opus_decode(opus, (const unsigned char*)frame, ret, pcm, pcmsize, 0); - - //printf("Decompressed %d samples\n", res); n += res; - // pcmsize = res * channels * sizeof(short); - int errl = 0; - int errr = 0; for(int i = 0; i < res; i++) { short left; short right; + get_samples(sin_x, &left, &right); - //printf("%d\t%d\n", left, pcm[2 * i]); - //printf("%d\t%d\n", right, pcm[2 * i + 1]); - errl += abs(left - pcm[2 * i]); - errr += abs(right - pcm[2 * i + 1]); + + if(sin_x > 0) { + errl += abs(abs(left) - abs(pcm[2 * i])); + errr += abs(abs(right) - abs(pcm[2 * i + 1])); + } sin_x++; } - //printf("err: %d\t%d\n", errl, errr); - // CPPUNIT_ASSERT_EQUAL(0, errl / (res * 40000)); - // CPPUNIT_ASSERT_EQUAL(0, errr / (res * 40000)); - //audio.play((char *)pcm, res * channels * sizeof(short)); delete[] pcm; } @@ -274,8 +211,11 @@ public: 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); + CPPUNIT_ASSERT_EQUAL(sent, (int)sin_x + lookahead); opus_decoder_destroy(opus); lrtp_destroy_profile(lrtp, csrc); -- cgit v1.2.3