|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "gtest/gtest.h" |
|
7 #include "OpusTrackEncoder.h" |
|
8 |
|
9 using namespace mozilla; |
|
10 |
|
11 class TestOpusTrackEncoder : public OpusTrackEncoder |
|
12 { |
|
13 public: |
|
14 // Return true if it has successfully initialized the Opus encoder. |
|
15 bool TestOpusCreation(int aChannels, int aSamplingRate) |
|
16 { |
|
17 if (Init(aChannels, aSamplingRate) == NS_OK) { |
|
18 if (GetPacketDuration()) { |
|
19 return true; |
|
20 } |
|
21 } |
|
22 return false; |
|
23 } |
|
24 |
|
25 // Return the sample rate of data to be fed to the Opus encoder, could be |
|
26 // re-sampled if it was not one of the Opus supported sampling rates. |
|
27 // Init() is expected to be called first. |
|
28 int TestGetOutputSampleRate() |
|
29 { |
|
30 return mInitialized ? GetOutputSampleRate() : 0; |
|
31 } |
|
32 }; |
|
33 |
|
34 static bool |
|
35 TestOpusInit(int aChannels, int aSamplingRate) |
|
36 { |
|
37 TestOpusTrackEncoder encoder; |
|
38 return encoder.TestOpusCreation(aChannels, aSamplingRate); |
|
39 } |
|
40 |
|
41 static int |
|
42 TestOpusResampler(int aChannels, int aSamplingRate) |
|
43 { |
|
44 TestOpusTrackEncoder encoder; |
|
45 EXPECT_TRUE(encoder.TestOpusCreation(aChannels, aSamplingRate)); |
|
46 return encoder.TestGetOutputSampleRate(); |
|
47 } |
|
48 |
|
49 TEST(Media, OpusEncoder_Init) |
|
50 { |
|
51 // Expect false with 0 or negative channels of input signal. |
|
52 EXPECT_FALSE(TestOpusInit(0, 16000)); |
|
53 EXPECT_FALSE(TestOpusInit(-1, 16000)); |
|
54 |
|
55 // The Opus format supports up to 8 channels, and supports multitrack audio up |
|
56 // to 255 channels, but the current implementation supports only mono and |
|
57 // stereo, and downmixes any more than that. |
|
58 // Expect false with channels of input signal exceed the max supported number. |
|
59 EXPECT_FALSE(TestOpusInit(8 + 1, 16000)); |
|
60 |
|
61 // Should accept channels within valid range. |
|
62 for (int i = 1; i <= 8; i++) { |
|
63 EXPECT_TRUE(TestOpusInit(i, 16000)); |
|
64 } |
|
65 |
|
66 // Expect false with 0 or negative sampling rate of input signal. |
|
67 EXPECT_FALSE(TestOpusInit(1, 0)); |
|
68 EXPECT_FALSE(TestOpusInit(1, -1)); |
|
69 } |
|
70 |
|
71 TEST(Media, OpusEncoder_Resample) |
|
72 { |
|
73 // Sampling rates of data to be fed to Opus encoder, should remain unchanged |
|
74 // if it is one of Opus supported rates (8000, 12000, 16000, 24000 and 48000 |
|
75 // (kHz)) at initialization. |
|
76 EXPECT_TRUE(TestOpusResampler(1, 8000) == 8000); |
|
77 EXPECT_TRUE(TestOpusResampler(1, 12000) == 12000); |
|
78 EXPECT_TRUE(TestOpusResampler(1, 16000) == 16000); |
|
79 EXPECT_TRUE(TestOpusResampler(1, 24000) == 24000); |
|
80 EXPECT_TRUE(TestOpusResampler(1, 48000) == 48000); |
|
81 |
|
82 // Otherwise, it should be resampled to 48kHz by resampler. |
|
83 EXPECT_FALSE(TestOpusResampler(1, 9600) == 9600); |
|
84 EXPECT_FALSE(TestOpusResampler(1, 44100) == 44100); |
|
85 } |