michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gtest/gtest.h" michael@0: #include "OpusTrackEncoder.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: class TestOpusTrackEncoder : public OpusTrackEncoder michael@0: { michael@0: public: michael@0: // Return true if it has successfully initialized the Opus encoder. michael@0: bool TestOpusCreation(int aChannels, int aSamplingRate) michael@0: { michael@0: if (Init(aChannels, aSamplingRate) == NS_OK) { michael@0: if (GetPacketDuration()) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // Return the sample rate of data to be fed to the Opus encoder, could be michael@0: // re-sampled if it was not one of the Opus supported sampling rates. michael@0: // Init() is expected to be called first. michael@0: int TestGetOutputSampleRate() michael@0: { michael@0: return mInitialized ? GetOutputSampleRate() : 0; michael@0: } michael@0: }; michael@0: michael@0: static bool michael@0: TestOpusInit(int aChannels, int aSamplingRate) michael@0: { michael@0: TestOpusTrackEncoder encoder; michael@0: return encoder.TestOpusCreation(aChannels, aSamplingRate); michael@0: } michael@0: michael@0: static int michael@0: TestOpusResampler(int aChannels, int aSamplingRate) michael@0: { michael@0: TestOpusTrackEncoder encoder; michael@0: EXPECT_TRUE(encoder.TestOpusCreation(aChannels, aSamplingRate)); michael@0: return encoder.TestGetOutputSampleRate(); michael@0: } michael@0: michael@0: TEST(Media, OpusEncoder_Init) michael@0: { michael@0: // Expect false with 0 or negative channels of input signal. michael@0: EXPECT_FALSE(TestOpusInit(0, 16000)); michael@0: EXPECT_FALSE(TestOpusInit(-1, 16000)); michael@0: michael@0: // The Opus format supports up to 8 channels, and supports multitrack audio up michael@0: // to 255 channels, but the current implementation supports only mono and michael@0: // stereo, and downmixes any more than that. michael@0: // Expect false with channels of input signal exceed the max supported number. michael@0: EXPECT_FALSE(TestOpusInit(8 + 1, 16000)); michael@0: michael@0: // Should accept channels within valid range. michael@0: for (int i = 1; i <= 8; i++) { michael@0: EXPECT_TRUE(TestOpusInit(i, 16000)); michael@0: } michael@0: michael@0: // Expect false with 0 or negative sampling rate of input signal. michael@0: EXPECT_FALSE(TestOpusInit(1, 0)); michael@0: EXPECT_FALSE(TestOpusInit(1, -1)); michael@0: } michael@0: michael@0: TEST(Media, OpusEncoder_Resample) michael@0: { michael@0: // Sampling rates of data to be fed to Opus encoder, should remain unchanged michael@0: // if it is one of Opus supported rates (8000, 12000, 16000, 24000 and 48000 michael@0: // (kHz)) at initialization. michael@0: EXPECT_TRUE(TestOpusResampler(1, 8000) == 8000); michael@0: EXPECT_TRUE(TestOpusResampler(1, 12000) == 12000); michael@0: EXPECT_TRUE(TestOpusResampler(1, 16000) == 16000); michael@0: EXPECT_TRUE(TestOpusResampler(1, 24000) == 24000); michael@0: EXPECT_TRUE(TestOpusResampler(1, 48000) == 48000); michael@0: michael@0: // Otherwise, it should be resampled to 48kHz by resampler. michael@0: EXPECT_FALSE(TestOpusResampler(1, 9600) == 9600); michael@0: EXPECT_FALSE(TestOpusResampler(1, 44100) == 44100); michael@0: }