summaryrefslogtreecommitdiff
path: root/test/test_opus.cc
blob: 1d55d5a8725b785a3bd020b040d2944eff74d5d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- 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 <cppunit/extensions/HelperMacros.h>

#include <lrtp.h>

#include <stdio.h>

#include <string>
#include <vector>

#include <math.h>
#include <opus/opus.h>
#include <limits.h>
#include <ao/ao.h>

#define KEY "123456789012345678901234567890123456789012345678901234567890"
#define SSRC 1234567890

#define FS 48000

#define F1 440
#define AF1 0.3

#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;
};

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 = 0;//(short)(sin(2 * M_PI / FS * x * F2) * amp2);
}

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<std::string> packets;
    unsigned int csrc = 42;
  
    size_t ts = 0;

    int sent = 0;

    { // 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);
      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]);
        }

        // 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,
                                (unsigned char*)frame, framesize);

        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;
      }

      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);

      int err;
      OpusDecoder *opus = opus_decoder_create(FS, channels, &err);
      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;
  
      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;
        }

        i++;
      }

      CPPUNIT_ASSERT(sent); // Fail if no data was sent.
      CPPUNIT_ASSERT_EQUAL(sent, (int)sin_x);

      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);