NetEq: Use a BuiltinAudioDecoderFactory to create decoders

Later steps in the refactoring will have the factory injected from the
outside rather than owned by NetEq.

BUG=webrtc:5801

Review-Url: https://codereview.webrtc.org/1928293002
Cr-Commit-Position: refs/heads/master@{#12604}
This commit is contained in:
kwiberg
2016-05-03 01:39:01 -07:00
committed by Commit bot
parent ddf165393f
commit 5178ee86ba
18 changed files with 217 additions and 128 deletions

View File

@ -106,14 +106,22 @@ TEST(AudioDecoderFactoryTest, CreateG722) {
TEST(AudioDecoderFactoryTest, CreateOpus) {
std::unique_ptr<AudioDecoderFactory> adf = CreateBuiltinAudioDecoderFactory();
ASSERT_TRUE(adf);
// Opus supports 48 kHz, 1-2 channels.
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 0)));
EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 1)));
EXPECT_TRUE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 2)));
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 48000, 3)));
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 8000, 1)));
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 16000, 1)));
EXPECT_FALSE(adf->MakeAudioDecoder(SdpAudioFormat("opus", 32000, 1)));
// Opus supports 48 kHz, 2 channels, and wants a "stereo" parameter whose
// value is either "0" or "1".
for (int hz : {8000, 16000, 32000, 48000}) {
for (int channels : {0, 1, 2, 3}) {
for (std::string stereo : {"XX", "0", "1", "2"}) {
std::map<std::string, std::string> params;
if (stereo != "XX") {
params["stereo"] = stereo;
}
bool good =
(hz == 48000 && channels == 2 && (stereo == "0" || stereo == "1"));
EXPECT_EQ(good, static_cast<bool>(adf->MakeAudioDecoder(SdpAudioFormat(
"opus", hz, channels, std::move(params)))));
}
}
}
}
} // namespace webrtc

View File

@ -21,6 +21,15 @@ SdpAudioFormat::SdpAudioFormat(const char* name,
int num_channels)
: name(name), clockrate_hz(clockrate_hz), num_channels(num_channels) {}
SdpAudioFormat::SdpAudioFormat(const char* name,
int clockrate_hz,
int num_channels,
Parameters&& param)
: name(name),
clockrate_hz(clockrate_hz),
num_channels(num_channels),
parameters(std::move(param)) {}
SdpAudioFormat::~SdpAudioFormat() = default;
SdpAudioFormat& SdpAudioFormat::operator=(const SdpAudioFormat&) = default;
SdpAudioFormat& SdpAudioFormat::operator=(SdpAudioFormat&&) = default;

View File

@ -21,18 +21,23 @@ namespace webrtc {
// SDP specification for a single audio codec.
// NOTE: This class is still under development and may change without notice.
struct SdpAudioFormat {
using Parameters = std::map<std::string, std::string>;
// TODO(kwiberg): Get rid of the default constructor when rtc::Optional no
// longer requires it.
SdpAudioFormat();
SdpAudioFormat(const SdpAudioFormat&);
SdpAudioFormat(SdpAudioFormat&&);
SdpAudioFormat(const char* name, int clockrate_hz, int num_channels);
SdpAudioFormat(const char* name,
int clockrate_hz,
int num_channels,
Parameters&& param);
~SdpAudioFormat();
SdpAudioFormat& operator=(const SdpAudioFormat&);
SdpAudioFormat& operator=(SdpAudioFormat&&);
using Parameters = std::map<std::string, std::string>;
std::string name;
int clockrate_hz;
int num_channels;

View File

@ -13,6 +13,7 @@
#include <vector>
#include "webrtc/base/checks.h"
#include "webrtc/base/optional.h"
#include "webrtc/common_types.h"
#include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h"
#include "webrtc/modules/audio_coding/codecs/g711/audio_decoder_pcm.h"
@ -39,8 +40,7 @@ namespace {
struct NamedDecoderConstructor {
const char* name;
std::unique_ptr<AudioDecoder> (*constructor)(int clockrate_hz,
int num_channels);
std::unique_ptr<AudioDecoder> (*constructor)(const SdpAudioFormat&);
};
std::unique_ptr<AudioDecoder> Unique(AudioDecoder* d) {
@ -51,53 +51,54 @@ std::unique_ptr<AudioDecoder> Unique(AudioDecoder* d) {
// decoder.
NamedDecoderConstructor decoder_constructors[] = {
{"pcmu",
[](int clockrate_hz, int num_channels) {
return clockrate_hz == 8000 && num_channels >= 1
? Unique(new AudioDecoderPcmU(num_channels))
[](const SdpAudioFormat& format) {
return format.clockrate_hz == 8000 && format.num_channels >= 1
? Unique(new AudioDecoderPcmU(format.num_channels))
: nullptr;
}},
{"pcma",
[](int clockrate_hz, int num_channels) {
return clockrate_hz == 8000 && num_channels >= 1
? Unique(new AudioDecoderPcmA(num_channels))
[](const SdpAudioFormat& format) {
return format.clockrate_hz == 8000 && format.num_channels >= 1
? Unique(new AudioDecoderPcmA(format.num_channels))
: nullptr;
}},
#ifdef WEBRTC_CODEC_ILBC
{"ilbc",
[](int clockrate_hz, int num_channels) {
return clockrate_hz == 8000 && num_channels == 1
[](const SdpAudioFormat& format) {
return format.clockrate_hz == 8000 && format.num_channels == 1
? Unique(new AudioDecoderIlbc)
: nullptr;
}},
#endif
#if defined(WEBRTC_CODEC_ISACFX)
{"isac",
[](int clockrate_hz, int num_channels) {
return clockrate_hz == 16000 && num_channels == 1
[](const SdpAudioFormat& format) {
return format.clockrate_hz == 16000 && format.num_channels == 1
? Unique(new AudioDecoderIsacFix)
: nullptr;
}},
#elif defined(WEBRTC_CODEC_ISAC)
{"isac",
[](int clockrate_hz, int num_channels) {
return (clockrate_hz == 16000 || clockrate_hz == 32000) &&
num_channels == 1
[](const SdpAudioFormat& format) {
return (format.clockrate_hz == 16000 || format.clockrate_hz == 32000) &&
format.num_channels == 1
? Unique(new AudioDecoderIsac)
: nullptr;
}},
#endif
{"l16",
[](int clockrate_hz, int num_channels) {
return num_channels >= 1 ? Unique(new AudioDecoderPcm16B(num_channels))
: nullptr;
[](const SdpAudioFormat& format) {
return format.num_channels >= 1
? Unique(new AudioDecoderPcm16B(format.num_channels))
: nullptr;
}},
#ifdef WEBRTC_CODEC_G722
{"g722",
[](int clockrate_hz, int num_channels) {
if (clockrate_hz == 8000) {
if (num_channels == 1)
[](const SdpAudioFormat& format) {
if (format.clockrate_hz == 8000) {
if (format.num_channels == 1)
return Unique(new AudioDecoderG722);
if (num_channels == 2)
if (format.num_channels == 2)
return Unique(new AudioDecoderG722Stereo);
}
return Unique(nullptr);
@ -105,9 +106,21 @@ NamedDecoderConstructor decoder_constructors[] = {
#endif
#ifdef WEBRTC_CODEC_OPUS
{"opus",
[](int clockrate_hz, int num_channels) {
return clockrate_hz == 48000 && (num_channels == 1 || num_channels == 2)
? Unique(new AudioDecoderOpus(num_channels))
[](const SdpAudioFormat& format) {
rtc::Optional<int> num_channels = [&] {
auto stereo = format.parameters.find("stereo");
if (stereo != format.parameters.end()) {
if (stereo->second == "0") {
return rtc::Optional<int>(1);
} else if (stereo->second == "1") {
return rtc::Optional<int>(2);
}
}
return rtc::Optional<int>();
}();
return format.clockrate_hz == 48000 && format.num_channels == 2 &&
num_channels
? Unique(new AudioDecoderOpus(*num_channels))
: nullptr;
}},
#endif
@ -123,8 +136,7 @@ class BuiltinAudioDecoderFactory : public AudioDecoderFactory {
const SdpAudioFormat& format) override {
for (const auto& dc : decoder_constructors) {
if (STR_CASE_CMP(format.name.c_str(), dc.name) == 0) {
return std::unique_ptr<AudioDecoder>(
dc.constructor(format.clockrate_hz, format.num_channels));
return std::unique_ptr<AudioDecoder>(dc.constructor(format));
}
}
return nullptr;

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_MOCK_MOCK_AUDIO_DECODER_FACTORY_H_
#define WEBRTC_MODULES_AUDIO_CODING_CODECS_MOCK_MOCK_AUDIO_DECODER_FACTORY_H_
#include <vector>
#include "testing/gmock/include/gmock/gmock.h"
#include "webrtc/modules/audio_coding/codecs/audio_decoder_factory.h"
namespace webrtc {
class MockAudioDecoderFactory : public AudioDecoderFactory {
public:
MOCK_METHOD0(GetSupportedFormats, std::vector<SdpAudioFormat>());
std::unique_ptr<AudioDecoder> MakeAudioDecoder(
const SdpAudioFormat& format) {
std::unique_ptr<AudioDecoder> return_value;
MakeAudioDecoderMock(format, &return_value);
return return_value;
}
MOCK_METHOD2(MakeAudioDecoderMock,
void(const SdpAudioFormat& format,
std::unique_ptr<AudioDecoder>* return_value));
};
} // namespace webrtc
#endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_MOCK_MOCK_AUDIO_DECODER_FACTORY_H_