2015-03-30 19:00:44 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2015 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2015-11-26 04:44:54 -08:00
|
|
|
#include "webrtc/modules/audio_coding/acm2/codec_manager.h"
|
2015-03-30 19:00:44 +02:00
|
|
|
|
|
|
|
|
#include "webrtc/base/checks.h"
|
2015-05-18 12:18:54 +02:00
|
|
|
#include "webrtc/engine_configurations.h"
|
2015-11-26 04:44:54 -08:00
|
|
|
#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
|
2015-10-28 18:17:40 +01:00
|
|
|
#include "webrtc/system_wrappers/include/trace.h"
|
2015-03-30 19:00:44 +02:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace acm2 {
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// Check if the given codec is a valid to be registered as send codec.
|
2015-11-18 08:27:51 -08:00
|
|
|
int IsValidSendCodec(const CodecInst& send_codec) {
|
2015-03-30 19:00:44 +02:00
|
|
|
int dummy_id = 0;
|
|
|
|
|
if ((send_codec.channels != 1) && (send_codec.channels != 2)) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
|
|
|
|
"Wrong number of channels (%d, only mono and stereo are "
|
2015-11-18 08:27:51 -08:00
|
|
|
"supported)",
|
|
|
|
|
send_codec.channels);
|
2015-03-30 19:00:44 +02:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 14:28:00 -08:00
|
|
|
auto maybe_codec_id = RentACodec::CodecIdByInst(send_codec);
|
|
|
|
|
if (!maybe_codec_id) {
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
|
|
|
|
"Invalid codec setting for the send codec.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Telephone-event cannot be a send codec.
|
|
|
|
|
if (!STR_CASE_CMP(send_codec.plname, "telephone-event")) {
|
|
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
|
|
|
|
"telephone-event cannot be a send codec");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-06 14:28:00 -08:00
|
|
|
if (!RentACodec::IsSupportedNumChannels(*maybe_codec_id, send_codec.channels)
|
|
|
|
|
.value_or(false)) {
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
|
|
|
|
"%d number of channels not supportedn for %s.",
|
|
|
|
|
send_codec.channels, send_codec.plname);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2015-11-06 14:28:00 -08:00
|
|
|
return RentACodec::CodecIndexFromId(*maybe_codec_id).value_or(-1);
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-05-07 15:49:23 +02:00
|
|
|
bool IsOpus(const CodecInst& codec) {
|
|
|
|
|
return
|
|
|
|
|
#ifdef WEBRTC_CODEC_OPUS
|
|
|
|
|
!STR_CASE_CMP(codec.plname, "opus") ||
|
|
|
|
|
#endif
|
|
|
|
|
false;
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 19:00:44 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
CodecManager::CodecManager() {
|
2015-03-30 19:00:44 +02:00
|
|
|
thread_checker_.DetachFromThread();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-13 09:31:16 +02:00
|
|
|
CodecManager::~CodecManager() = default;
|
2015-03-30 19:00:44 +02:00
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
bool CodecManager::RegisterEncoder(const CodecInst& send_codec) {
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(thread_checker_.CalledOnValidThread());
|
2015-11-18 08:27:51 -08:00
|
|
|
int codec_id = IsValidSendCodec(send_codec);
|
2015-03-30 19:00:44 +02:00
|
|
|
|
|
|
|
|
// Check for reported errors from function IsValidSendCodec().
|
|
|
|
|
if (codec_id < 0) {
|
2015-12-16 04:19:08 -08:00
|
|
|
return false;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int dummy_id = 0;
|
2015-11-23 04:30:52 -08:00
|
|
|
switch (RentACodec::RegisterRedPayloadType(
|
|
|
|
|
&codec_stack_params_.red_payload_types, send_codec)) {
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kOk:
|
2015-12-16 04:19:08 -08:00
|
|
|
return true;
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kBadFreq:
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
2015-11-18 07:32:49 -08:00
|
|
|
"RegisterSendCodec() failed, invalid frequency for RED"
|
|
|
|
|
" registration");
|
2015-12-16 04:19:08 -08:00
|
|
|
return false;
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kSkip:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-11-23 04:30:52 -08:00
|
|
|
switch (RentACodec::RegisterCngPayloadType(
|
|
|
|
|
&codec_stack_params_.cng_payload_types, send_codec)) {
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kOk:
|
2015-12-16 04:19:08 -08:00
|
|
|
return true;
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kBadFreq:
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, dummy_id,
|
2015-11-18 07:32:49 -08:00
|
|
|
"RegisterSendCodec() failed, invalid frequency for CNG"
|
|
|
|
|
" registration");
|
2015-12-16 04:19:08 -08:00
|
|
|
return false;
|
2015-11-18 07:32:49 -08:00
|
|
|
case RentACodec::RegistrationResult::kSkip:
|
|
|
|
|
break;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
if (IsOpus(send_codec)) {
|
2015-12-01 04:45:04 -08:00
|
|
|
// VAD/DTX not supported.
|
|
|
|
|
codec_stack_params_.use_cng = false;
|
2015-05-07 15:49:23 +02:00
|
|
|
}
|
2015-03-30 19:00:44 +02:00
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
send_codec_inst_ = rtc::Optional<CodecInst>(send_codec);
|
|
|
|
|
codec_stack_params_.speech_encoder = nullptr; // Caller must recreate it.
|
|
|
|
|
return true;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
CodecInst CodecManager::ForgeCodecInst(
|
|
|
|
|
const AudioEncoder* external_speech_encoder) {
|
|
|
|
|
CodecInst ci;
|
|
|
|
|
ci.channels = external_speech_encoder->NumChannels();
|
|
|
|
|
ci.plfreq = external_speech_encoder->SampleRateHz();
|
|
|
|
|
ci.pacsize = rtc::CheckedDivExact(
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static_cast<int>(external_speech_encoder->Max10MsFramesInAPacket() *
|
2015-12-16 04:19:08 -08:00
|
|
|
ci.plfreq),
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
100);
|
2015-12-16 04:19:08 -08:00
|
|
|
ci.pltype = -1; // Not valid.
|
|
|
|
|
ci.rate = -1; // Not valid.
|
2015-05-18 14:52:29 +02:00
|
|
|
static const char kName[] = "external";
|
2015-12-16 04:19:08 -08:00
|
|
|
memcpy(ci.plname, kName, sizeof(kName));
|
|
|
|
|
return ci;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CodecManager::SetCopyRed(bool enable) {
|
2015-11-23 04:30:52 -08:00
|
|
|
if (enable && codec_stack_params_.use_codec_fec) {
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, 0,
|
|
|
|
|
"Codec internal FEC and RED cannot be co-enabled.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-16 04:19:08 -08:00
|
|
|
if (enable && send_codec_inst_ &&
|
|
|
|
|
codec_stack_params_.red_payload_types.count(send_codec_inst_->plfreq) <
|
2015-11-23 04:30:52 -08:00
|
|
|
1) {
|
2015-05-07 15:49:23 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, 0,
|
2015-12-16 04:19:08 -08:00
|
|
|
"Cannot enable RED at %i Hz.", send_codec_inst_->plfreq);
|
2015-03-30 19:00:44 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2015-12-16 04:19:08 -08:00
|
|
|
codec_stack_params_.use_red = enable;
|
2015-03-30 19:00:44 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
bool CodecManager::SetVAD(bool enable, ACMVADMode mode) {
|
2015-03-30 19:00:44 +02:00
|
|
|
// Sanity check of the mode.
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(mode == VADNormal || mode == VADLowBitrate || mode == VADAggr ||
|
|
|
|
|
mode == VADVeryAggr);
|
2015-03-30 19:00:44 +02:00
|
|
|
|
|
|
|
|
// Check that the send codec is mono. We don't support VAD/DTX for stereo
|
|
|
|
|
// sending.
|
2015-12-15 14:21:33 -08:00
|
|
|
const bool stereo_send =
|
|
|
|
|
codec_stack_params_.speech_encoder
|
|
|
|
|
? (codec_stack_params_.speech_encoder->NumChannels() != 1)
|
|
|
|
|
: false;
|
2015-11-10 09:47:36 -08:00
|
|
|
if (enable && stereo_send) {
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceAudioCoding, 0,
|
|
|
|
|
"VAD/DTX not supported for stereo sending");
|
2015-12-16 04:19:08 -08:00
|
|
|
return false;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 06:24:05 -08:00
|
|
|
// TODO(kwiberg): This doesn't protect Opus when injected as an external
|
|
|
|
|
// encoder.
|
|
|
|
|
if (send_codec_inst_ && IsOpus(*send_codec_inst_)) {
|
2015-12-16 04:19:08 -08:00
|
|
|
// VAD/DTX not supported, but don't fail.
|
|
|
|
|
enable = false;
|
2015-05-07 15:49:23 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
codec_stack_params_.use_cng = enable;
|
|
|
|
|
codec_stack_params_.vad_mode = mode;
|
|
|
|
|
return true;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
bool CodecManager::SetCodecFEC(bool enable_codec_fec) {
|
2015-11-23 04:30:52 -08:00
|
|
|
if (enable_codec_fec && codec_stack_params_.use_red) {
|
2015-03-30 19:00:44 +02:00
|
|
|
WEBRTC_TRACE(webrtc::kTraceWarning, webrtc::kTraceAudioCoding, 0,
|
|
|
|
|
"Codec internal FEC and RED cannot be co-enabled.");
|
2015-12-16 04:19:08 -08:00
|
|
|
return false;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2015-12-16 04:19:08 -08:00
|
|
|
codec_stack_params_.use_codec_fec = enable_codec_fec;
|
|
|
|
|
return true;
|
2015-03-30 19:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace acm2
|
|
|
|
|
} // namespace webrtc
|