summaryrefslogtreecommitdiff
path: root/test/test_opus.cc
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_opus.cc')
-rw-r--r--test/test_opus.cc309
1 files changed, 157 insertions, 152 deletions
diff --git a/test/test_opus.cc b/test/test_opus.cc
index 9e41a42..1d55d5a 100644
--- a/test/test_opus.cc
+++ b/test/test_opus.cc
@@ -25,6 +25,8 @@
* License along with lrtp; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#include <cppunit/extensions/HelperMacros.h>
+
#include <lrtp.h>
#include <stdio.h>
@@ -98,186 +100,189 @@ private:
ao_device *device;
};
-int main()
+void get_samples(double x, short *r, short *l)
{
- size_t channels = 2;
- size_t ms[] = { 120, 240, 480, 960, 1920, 2880 };
-
- std::vector<std::string> packets;
- unsigned int csrc = 42;
-
- double sin_x = 0;
- size_t ts = 0;
-
- printf("========== Encode ==========\n");
-
- { // Encode
- struct lrtp_t *lrtp = lrtp_init(KEY, SSRC);
-
- struct lrtp_profile_t *profile =
- lrtp_create_profile(lrtp, PROFILE_OPUS, csrc,
- //OPTION_RAW_PKG_SIZE, pkg_size,
- OPTION_END);
-
- char packet[16*1024];
- size_t packetsize = sizeof(packet);
-
- int err;
- OpusEncoder *opus = opus_encoder_create(FS, channels,
- OPUS_APPLICATION_AUDIO, &err);
- printf("Opus create err: %d\n", err);
-
- opus_encoder_ctl(opus, OPUS_SET_BITRATE(32000));// [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));
-
- 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
- short *pcm = new short[100000/*pcmsize * channels*/];
- for(int i = 0 ; i < pcmsize; i++) {
- sin_x++;
-
- if((int)sin_x % FS == 0) {
- idx++;// = rand() % (sizeof(ms)/sizeof(size_t));
- idx = idx % (sizeof(ms)/sizeof(size_t));
- }
+ double amp1 = sin((2 * M_PI / (double)FS)* x * AF1) * SHRT_MAX;
+ double amp2 = sin((2 * M_PI / (double)FS)* x * AF2) * SHRT_MAX;
- double amp1 = sin((2*M_PI/(double)FS)*(double)sin_x * AF1) * SHRT_MAX;
- double amp2 = sin((2*M_PI/(double)FS)*(double)sin_x * AF2) * SHRT_MAX;
-
- pcm[i*2] = (short)(sin(2*M_PI/FS*(double)sin_x * F1) * amp1);
- pcm[i*2+1] = (short)(sin(2*M_PI/FS*(double)sin_x * F2) * amp2);
- }
-
- // Master timestamp is sample number in 48kHz (Opus RFC states this)
- timestamp += pcmsize * 48000 / FS;
-
- // size_t pcmsize = pcmsize * channels * sizeof(short);
+ *r = (short)(sin(2 * M_PI / FS * x * F1) * amp1);
+ *l = 0;//(short)(sin(2 * M_PI / FS * x * F2) * amp2);
+}
- char frame[pcmsize];
- int framesize = sizeof(frame);
- framesize = opus_encode(opus, pcm, pcmsize,
- (unsigned char*)frame, framesize);
+class test_opus_class : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE(test_opus_class);
+ CPPUNIT_TEST(test_opus);
+ CPPUNIT_TEST_SUITE_END();
- if(framesize < 0) {
- printf("Opus error: %s\n", opus_strerror(framesize));
- }
+public:
+ void setUp() {}
+ void tearDown() {}
- printf("Opus Packet: %d bytes compressed to %d bytes\n",
- channels * pcmsize * sizeof(short), framesize);
+ void test_opus() {
+ size_t channels = 2;
+ size_t ms[] = { 120, 240, 480, 960, 1920, 2880 };
- int ret = lrtp_enqueue_frame(profile, frame, framesize);
- while( (ret = lrtp_pack(lrtp, packet, sizeof(packet))) != 0) {
- std::string p;
- p.append(packet, ret);
- packets.push_back(p);
- }
+ std::vector<std::string> packets;
+ unsigned int csrc = 42;
+
+ size_t ts = 0;
- delete[] pcm;
- }
+ int sent = 0;
- opus_encoder_destroy(opus);
- lrtp_destroy_profile(lrtp, csrc);
- lrtp_close(lrtp);
- }
+ { // Encode
+ struct lrtp_t *lrtp = lrtp_init(KEY, SSRC);
- printf("========== Decode ==========\n");
+ struct lrtp_profile_t *profile =
+ lrtp_create_profile(lrtp, PROFILE_OPUS, csrc,
+ //OPTION_RAW_PKG_SIZE, pkg_size,
+ OPTION_END);
- { // Decode
- struct lrtp_t *lrtp = lrtp_init(KEY, SSRC);
-
- struct lrtp_profile_t *profile =
- lrtp_create_profile(lrtp, PROFILE_OPUS, csrc,
- //OPTION_RAW_PKG_SIZE, pkg_size,
- OPTION_END);
+ 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(32000));// [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));
+
+ double 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*/];
+ for(int i = 0 ; i < pcmsize; i++) {
+ sin_x++;
+
+ if((int)sin_x % FS == 0) {
+ idx++;// = rand() % (sizeof(ms)/sizeof(size_t));
+ idx = idx % (sizeof(ms)/sizeof(size_t));
+ }
+
+ get_samples(sin_x, &pcm[i*2], &pcm[i*2+1]);
+ }
- int err;
- OpusDecoder *opus = opus_decoder_create(FS, channels, &err);
- printf("Opus create err: %d\n", err);
+ // Master timestamp is sample number in 48kHz (Opus RFC states this)
+ timestamp += pcmsize * 48000 / FS;
- int idx = (sizeof(ms)/sizeof(size_t)) - 1;
- printf("idx: %d\n", idx);
+ // size_t pcmsize = pcmsize * channels * sizeof(short);
- Audio audio;
+ char frame[pcmsize];
+ int framesize = sizeof(frame);
+ framesize = opus_encode(opus, pcm, pcmsize,
+ (unsigned char*)frame, framesize);
- char frame[16*1024];
-
- int cnt = 0;
- std::vector<std::string>::iterator i = packets.begin();
- while(i != packets.end()) {
- size_t packetsize = i->size();
- 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;
- 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];
- //printf("pcmsize %d\n", pcmsize); fflush(stdout);
- int res = opus_decode(opus, (const unsigned char*)frame, ret,
- pcm, pcmsize, 0);
-
- // printf("Decompressed %d bytes\n", res);
- n += res;
- // pcmsize = res * channels * sizeof(short);
-
- audio.play((char *)pcm, res * channels * sizeof(short));
+ 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;
+ p.append(packet, ret);
+ packets.push_back(p);
+ }
delete[] pcm;
}
- printf("ratio: %d => %d\n", packetsize, n * channels);
-
- i++;
+ opus_encoder_destroy(opus);
+ lrtp_destroy_profile(lrtp, csrc);
+ lrtp_close(lrtp);
}
+ { // Decode
+ struct lrtp_t *lrtp = lrtp_init(KEY, SSRC);
- /*
+ struct lrtp_profile_t *profile =
+ lrtp_create_profile(lrtp, PROFILE_OPUS, csrc,
+ //OPTION_RAW_PKG_SIZE, pkg_size,
+ OPTION_END);
- std::vector<std::string>::iterator i = packets.begin();
- while(i != packets.end()) {
- size_t packetsize = i->size();
- printf("unpack sz: %d\n", packetsize);
- const char *packet = i->data();
- unsigned int ts;
+ int err;
+ OpusDecoder *opus = opus_decoder_create(FS, channels, &err);
+ CPPUNIT_ASSERT_EQUAL(0, err);
- framesize = sizeof(frame);
+ int idx = (sizeof(ms)/sizeof(size_t)) - 1;
+ //printf("idx: %d\n", idx);
- lrtp_unpack(lrtp, packet, packetsize, frame, &framesize, &csrc, &ts);
- printf("Got %d bytes, csrc %d, ts: %d\n", framesize, csrc, ts);
+ //Audio audio;
- 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, framesize,
- pcm, pcmsize, 0);
- framesize = sizeof(frame);
+ char frame[16*1024];
+
+ double sin_x = 0;
+
+ int cnt = 0;
+ std::vector<std::string>::iterator i = packets.begin();
+ while(i != packets.end()) {
+ size_t packetsize = i->size();
+ 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;
+ 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];
+ //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]);
+ 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;
+ }
- printf("Decompressed %d bytes\n", res);
- // pcmsize = res * channels * sizeof(short);
+ i++;
+ }
- audio.play((char *)pcm, res * channels * sizeof(short));
+ CPPUNIT_ASSERT(sent); // Fail if no data was sent.
+ CPPUNIT_ASSERT_EQUAL(sent, (int)sin_x);
- delete[] pcm;
- i++;
+ opus_decoder_destroy(opus);
+ lrtp_destroy_profile(lrtp, csrc);
+ lrtp_close(lrtp);
}
- */
- opus_decoder_destroy(opus);
- lrtp_destroy_profile(lrtp, csrc);
- lrtp_close(lrtp);
}
+};
- return 0;
-}
+// Registers the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION(test_opus_class);