2017-06-17 17:41:59 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2017 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/audio_codecs/g722/audio_encoder_g722.h"
|
2017-06-17 17:41:59 -07:00
|
|
|
|
2024-06-18 16:20:35 +03:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
|
|
#include <map>
|
2017-06-17 17:41:59 -07:00
|
|
|
#include <memory>
|
2024-08-29 13:00:40 +00:00
|
|
|
#include <optional>
|
2024-06-18 16:20:35 +03:00
|
|
|
#include <utility>
|
2017-06-17 17:41:59 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2018-10-22 09:48:08 +02:00
|
|
|
#include "absl/strings/match.h"
|
2024-06-18 16:20:35 +03:00
|
|
|
#include "api/audio_codecs/audio_codec_pair_id.h"
|
|
|
|
|
#include "api/audio_codecs/audio_encoder.h"
|
|
|
|
|
#include "api/audio_codecs/audio_format.h"
|
|
|
|
|
#include "api/audio_codecs/g722/audio_encoder_g722_config.h"
|
|
|
|
|
#include "api/field_trials_view.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/codecs/g722/audio_encoder_g722.h"
|
2024-06-18 16:20:35 +03:00
|
|
|
#include "rtc_base/checks.h"
|
2017-11-22 10:42:26 +01:00
|
|
|
#include "rtc_base/numerics/safe_conversions.h"
|
|
|
|
|
#include "rtc_base/numerics/safe_minmax.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/string_to_number.h"
|
2017-06-17 17:41:59 -07:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<AudioEncoderG722Config> AudioEncoderG722::SdpToConfig(
|
2017-06-17 17:41:59 -07:00
|
|
|
const SdpAudioFormat& format) {
|
2018-10-22 09:48:08 +02:00
|
|
|
if (!absl::EqualsIgnoreCase(format.name, "g722") ||
|
2017-08-25 22:22:42 -07:00
|
|
|
format.clockrate_hz != 8000) {
|
2024-08-29 13:00:40 +00:00
|
|
|
return std::nullopt;
|
2017-08-25 22:22:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioEncoderG722Config config;
|
|
|
|
|
config.num_channels = rtc::checked_cast<int>(format.num_channels);
|
|
|
|
|
auto ptime_iter = format.parameters.find("ptime");
|
|
|
|
|
if (ptime_iter != format.parameters.end()) {
|
2025-02-18 08:01:34 +00:00
|
|
|
auto ptime = StringToNumber<int>(ptime_iter->second);
|
2017-08-25 22:22:42 -07:00
|
|
|
if (ptime && *ptime > 0) {
|
|
|
|
|
const int whole_packets = *ptime / 10;
|
2025-02-20 09:42:51 +00:00
|
|
|
config.frame_size_ms = SafeClamp<int>(whole_packets * 10, 10, 60);
|
2017-08-25 22:22:42 -07:00
|
|
|
}
|
|
|
|
|
}
|
2021-11-24 19:29:10 +00:00
|
|
|
if (!config.IsOk()) {
|
|
|
|
|
RTC_DCHECK_NOTREACHED();
|
2024-08-29 13:00:40 +00:00
|
|
|
return std::nullopt;
|
2021-11-24 19:29:10 +00:00
|
|
|
}
|
|
|
|
|
return config;
|
2017-06-17 17:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AudioEncoderG722::AppendSupportedEncoders(
|
|
|
|
|
std::vector<AudioCodecSpec>* specs) {
|
2017-08-25 03:10:50 -07:00
|
|
|
const SdpAudioFormat fmt = {"G722", 8000, 1};
|
2017-06-17 17:41:59 -07:00
|
|
|
const AudioCodecInfo info = QueryAudioEncoder(*SdpToConfig(fmt));
|
|
|
|
|
specs->push_back({fmt, info});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AudioCodecInfo AudioEncoderG722::QueryAudioEncoder(
|
|
|
|
|
const AudioEncoderG722Config& config) {
|
|
|
|
|
RTC_DCHECK(config.IsOk());
|
|
|
|
|
return {16000, rtc::dchecked_cast<size_t>(config.num_channels),
|
|
|
|
|
64000 * config.num_channels};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<AudioEncoder> AudioEncoderG722::MakeAudioEncoder(
|
|
|
|
|
const AudioEncoderG722Config& config,
|
2018-03-01 15:13:27 +01:00
|
|
|
int payload_type,
|
2024-08-29 13:00:40 +00:00
|
|
|
std::optional<AudioCodecPairId> /*codec_pair_id*/,
|
2024-10-27 14:24:44 +02:00
|
|
|
const FieldTrialsView* /* field_trials */) {
|
2021-11-24 19:29:10 +00:00
|
|
|
if (!config.IsOk()) {
|
|
|
|
|
RTC_DCHECK_NOTREACHED();
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2019-09-17 17:06:18 +02:00
|
|
|
return std::make_unique<AudioEncoderG722Impl>(config, payload_type);
|
2017-06-17 17:41:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace webrtc
|