1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/media/gtest/TestTrackEncoder.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "gtest/gtest.h" 1.10 +#include "OpusTrackEncoder.h" 1.11 + 1.12 +using namespace mozilla; 1.13 + 1.14 +class TestOpusTrackEncoder : public OpusTrackEncoder 1.15 +{ 1.16 +public: 1.17 + // Return true if it has successfully initialized the Opus encoder. 1.18 + bool TestOpusCreation(int aChannels, int aSamplingRate) 1.19 + { 1.20 + if (Init(aChannels, aSamplingRate) == NS_OK) { 1.21 + if (GetPacketDuration()) { 1.22 + return true; 1.23 + } 1.24 + } 1.25 + return false; 1.26 + } 1.27 + 1.28 + // Return the sample rate of data to be fed to the Opus encoder, could be 1.29 + // re-sampled if it was not one of the Opus supported sampling rates. 1.30 + // Init() is expected to be called first. 1.31 + int TestGetOutputSampleRate() 1.32 + { 1.33 + return mInitialized ? GetOutputSampleRate() : 0; 1.34 + } 1.35 +}; 1.36 + 1.37 +static bool 1.38 +TestOpusInit(int aChannels, int aSamplingRate) 1.39 +{ 1.40 + TestOpusTrackEncoder encoder; 1.41 + return encoder.TestOpusCreation(aChannels, aSamplingRate); 1.42 +} 1.43 + 1.44 +static int 1.45 +TestOpusResampler(int aChannels, int aSamplingRate) 1.46 +{ 1.47 + TestOpusTrackEncoder encoder; 1.48 + EXPECT_TRUE(encoder.TestOpusCreation(aChannels, aSamplingRate)); 1.49 + return encoder.TestGetOutputSampleRate(); 1.50 +} 1.51 + 1.52 +TEST(Media, OpusEncoder_Init) 1.53 +{ 1.54 + // Expect false with 0 or negative channels of input signal. 1.55 + EXPECT_FALSE(TestOpusInit(0, 16000)); 1.56 + EXPECT_FALSE(TestOpusInit(-1, 16000)); 1.57 + 1.58 + // The Opus format supports up to 8 channels, and supports multitrack audio up 1.59 + // to 255 channels, but the current implementation supports only mono and 1.60 + // stereo, and downmixes any more than that. 1.61 + // Expect false with channels of input signal exceed the max supported number. 1.62 + EXPECT_FALSE(TestOpusInit(8 + 1, 16000)); 1.63 + 1.64 + // Should accept channels within valid range. 1.65 + for (int i = 1; i <= 8; i++) { 1.66 + EXPECT_TRUE(TestOpusInit(i, 16000)); 1.67 + } 1.68 + 1.69 + // Expect false with 0 or negative sampling rate of input signal. 1.70 + EXPECT_FALSE(TestOpusInit(1, 0)); 1.71 + EXPECT_FALSE(TestOpusInit(1, -1)); 1.72 +} 1.73 + 1.74 +TEST(Media, OpusEncoder_Resample) 1.75 +{ 1.76 + // Sampling rates of data to be fed to Opus encoder, should remain unchanged 1.77 + // if it is one of Opus supported rates (8000, 12000, 16000, 24000 and 48000 1.78 + // (kHz)) at initialization. 1.79 + EXPECT_TRUE(TestOpusResampler(1, 8000) == 8000); 1.80 + EXPECT_TRUE(TestOpusResampler(1, 12000) == 12000); 1.81 + EXPECT_TRUE(TestOpusResampler(1, 16000) == 16000); 1.82 + EXPECT_TRUE(TestOpusResampler(1, 24000) == 24000); 1.83 + EXPECT_TRUE(TestOpusResampler(1, 48000) == 48000); 1.84 + 1.85 + // Otherwise, it should be resampled to 48kHz by resampler. 1.86 + EXPECT_FALSE(TestOpusResampler(1, 9600) == 9600); 1.87 + EXPECT_FALSE(TestOpusResampler(1, 44100) == 44100); 1.88 +}