Bug 936981 - Setup GTest for content/media and add a test case for OpusTrackEncoder. r=rillian

This commit is contained in:
Shelly Lin 2014-01-07 11:04:51 +08:00
parent 12edeb2bec
commit b56675514e
6 changed files with 124 additions and 7 deletions

View File

@ -17,6 +17,11 @@
namespace mozilla {
// The Opus format supports up to 8 channels, and supports multitrack audio up
// to 255 channels, but the current implementation supports only mono and
// stereo, and downmixes any more than that.
static const int MAX_SUPPORTED_AUDIO_CHANNELS = 8;
// http://www.opus-codec.org/docs/html_api-1.0.2/group__opus__encoder.html
// In section "opus_encoder_init", channels must be 1 or 2 of input signal.
static const int MAX_CHANNELS = 2;
@ -143,15 +148,15 @@ OpusTrackEncoder::Init(int aChannels, int aSamplingRate)
// This monitor is used to wake up other methods that are waiting for encoder
// to be completely initialized.
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
NS_ENSURE_TRUE((aChannels <= MAX_SUPPORTED_AUDIO_CHANNELS) && (aChannels > 0),
NS_ERROR_FAILURE);
// This version of encoder API only support 1 or 2 channels,
// So set the mChannels less or equal 2 and
// let InterleaveTrackData downmix pcm data.
mChannels = aChannels > MAX_CHANNELS ? MAX_CHANNELS : aChannels;
if (aChannels <= 0) {
return NS_ERROR_FAILURE;
}
// According to www.opus-codec.org, creating an opus encoder requires the
// sampling rate of source signal be one of 8000, 12000, 16000, 24000, or
// 48000. If this constraint is not satisfied, we resample the input to 48kHz.
@ -171,6 +176,7 @@ OpusTrackEncoder::Init(int aChannels, int aSamplingRate)
}
}
mSamplingRate = aSamplingRate;
NS_ENSURE_TRUE(mSamplingRate > 0, NS_ERROR_FAILURE);
int error = 0;
mEncoder = opus_encoder_create(GetOutputSampleRate(), mChannels,

View File

@ -41,13 +41,13 @@ protected:
nsresult Init(int aChannels, int aSamplingRate) MOZ_OVERRIDE;
private:
/**
* Get the samplerate of the data to be fed to the Opus encoder. This might be
* different from the intput samplerate if resampling occurs.
* different from the input samplerate if resampling occurs.
*/
int GetOutputSampleRate();
private:
/**
* The Opus encoder from libopus.
*/

View File

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

View File

@ -0,0 +1,22 @@
# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
LIBRARY_NAME = 'media_gtest'
UNIFIED_SOURCES += [
'TestTrackEncoder.cpp',
]
LIBXUL_LIBRARY = True
EXPORT_LIBRARY = True
include('/ipc/chromium/chromium-config.mozbuild')
LOCAL_INCLUDES += [
'/content/media/encoder',
]

View File

@ -49,7 +49,10 @@ if CONFIG['MOZ_OMX_DECODER']:
PARALLEL_DIRS += ['webspeech']
TEST_DIRS += ['test']
TEST_DIRS += [
'test',
'gtest',
]
EXPORTS += [
'AbstractMediaDecoder.h',

View File

@ -448,6 +448,7 @@ COMPONENT_LIBS += \
gfxtest \
ssltest \
xpcom_glue_gtest \
media_gtest \
$(NULL)
endif