content/media/gtest/TestTrackEncoder.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

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

mercurial