2014-09-23 12:05:34 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2014 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 "modules/audio_coding/acm2/acm_send_test.h"
|
2014-09-23 12:05:34 +00:00
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
Remove CodecInst pt.2
The following APIs on AudioCodingModule are deprecated with this CL:
static int NumberOfCodecs();
static int Codec(int, CodecInst*);
static int Codec(const char*, CodecInst*, int, size_t);
static int Codec(const char*, int, size_t);
absl::optional<CodecInst> SendCodec() const;
bool RegisterReceiveCodec(int, const SdpAudioFormat&);
int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
int UnregisterReceiveCodec(uint8_t);
int32_t ReceiveCodec(CodecInst*);
absl::optional<SdpAudioFormat> ReceiveFormat();
As well as this method on RtpRtcp module:
int32_t RegisterSendPayload(const CodecInst&);
Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
2018-12-11 12:22:10 +01:00
|
|
|
#include "absl/strings/match.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "api/audio_codecs/audio_encoder.h"
|
2018-04-06 10:06:42 +02:00
|
|
|
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
|
2018-08-16 15:01:12 +02:00
|
|
|
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/include/audio_coding_module.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/tools/input_audio_file.h"
|
|
|
|
|
#include "modules/audio_coding/neteq/tools/packet.h"
|
|
|
|
|
#include "rtc_base/checks.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/string_encode.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "test/gtest.h"
|
2014-09-23 12:05:34 +00:00
|
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
namespace test {
|
|
|
|
|
|
|
|
|
|
AcmSendTestOldApi::AcmSendTestOldApi(InputAudioFile* audio_source,
|
|
|
|
|
int source_rate_hz,
|
|
|
|
|
int test_duration_ms)
|
|
|
|
|
: clock_(0),
|
2018-04-06 10:06:42 +02:00
|
|
|
acm_(webrtc::AudioCodingModule::Create([this] {
|
|
|
|
|
AudioCodingModule::Config config;
|
|
|
|
|
config.clock = &clock_;
|
|
|
|
|
config.decoder_factory = CreateBuiltinAudioDecoderFactory();
|
|
|
|
|
return config;
|
|
|
|
|
}())),
|
2014-09-23 12:05:34 +00:00
|
|
|
audio_source_(audio_source),
|
|
|
|
|
source_rate_hz_(source_rate_hz),
|
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
|
|
|
input_block_size_samples_(
|
|
|
|
|
static_cast<size_t>(source_rate_hz_ * kBlockSizeMs / 1000)),
|
2014-09-23 12:05:34 +00:00
|
|
|
codec_registered_(false),
|
|
|
|
|
test_duration_ms_(test_duration_ms),
|
2019-03-19 14:10:16 +01:00
|
|
|
frame_type_(AudioFrameType::kAudioFrameSpeech),
|
2014-09-23 12:05:34 +00:00
|
|
|
payload_type_(0),
|
|
|
|
|
timestamp_(0),
|
|
|
|
|
sequence_number_(0) {
|
|
|
|
|
input_frame_.sample_rate_hz_ = source_rate_hz_;
|
|
|
|
|
input_frame_.num_channels_ = 1;
|
|
|
|
|
input_frame_.samples_per_channel_ = input_block_size_samples_;
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK_LE(input_block_size_samples_ * input_frame_.num_channels_,
|
|
|
|
|
AudioFrame::kMaxDataSizeSamples);
|
2014-09-23 12:05:34 +00:00
|
|
|
acm_->RegisterTransportCallback(this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-29 10:05:24 -07:00
|
|
|
AcmSendTestOldApi::~AcmSendTestOldApi() = default;
|
|
|
|
|
|
2014-09-23 12:05:34 +00:00
|
|
|
bool AcmSendTestOldApi::RegisterCodec(const char* payload_name,
|
Remove CodecInst pt.2
The following APIs on AudioCodingModule are deprecated with this CL:
static int NumberOfCodecs();
static int Codec(int, CodecInst*);
static int Codec(const char*, CodecInst*, int, size_t);
static int Codec(const char*, int, size_t);
absl::optional<CodecInst> SendCodec() const;
bool RegisterReceiveCodec(int, const SdpAudioFormat&);
int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
int UnregisterReceiveCodec(uint8_t);
int32_t ReceiveCodec(CodecInst*);
absl::optional<SdpAudioFormat> ReceiveFormat();
As well as this method on RtpRtcp module:
int32_t RegisterSendPayload(const CodecInst&);
Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
2018-12-11 12:22:10 +01:00
|
|
|
int clockrate_hz,
|
|
|
|
|
int num_channels,
|
2014-09-23 12:05:34 +00:00
|
|
|
int payload_type,
|
|
|
|
|
int frame_size_samples) {
|
Remove CodecInst pt.2
The following APIs on AudioCodingModule are deprecated with this CL:
static int NumberOfCodecs();
static int Codec(int, CodecInst*);
static int Codec(const char*, CodecInst*, int, size_t);
static int Codec(const char*, int, size_t);
absl::optional<CodecInst> SendCodec() const;
bool RegisterReceiveCodec(int, const SdpAudioFormat&);
int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
int UnregisterReceiveCodec(uint8_t);
int32_t ReceiveCodec(CodecInst*);
absl::optional<SdpAudioFormat> ReceiveFormat();
As well as this method on RtpRtcp module:
int32_t RegisterSendPayload(const CodecInst&);
Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
2018-12-11 12:22:10 +01:00
|
|
|
SdpAudioFormat format(payload_name, clockrate_hz, num_channels);
|
|
|
|
|
if (absl::EqualsIgnoreCase(payload_name, "g722")) {
|
|
|
|
|
RTC_CHECK_EQ(16000, clockrate_hz);
|
|
|
|
|
format.clockrate_hz = 8000;
|
|
|
|
|
} else if (absl::EqualsIgnoreCase(payload_name, "opus")) {
|
|
|
|
|
RTC_CHECK(num_channels == 1 || num_channels == 2);
|
|
|
|
|
if (num_channels == 2) {
|
|
|
|
|
format.parameters["stereo"] = "1";
|
|
|
|
|
}
|
|
|
|
|
format.num_channels = 2;
|
|
|
|
|
}
|
2018-08-16 15:01:12 +02:00
|
|
|
format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
|
Remove CodecInst pt.2
The following APIs on AudioCodingModule are deprecated with this CL:
static int NumberOfCodecs();
static int Codec(int, CodecInst*);
static int Codec(const char*, CodecInst*, int, size_t);
static int Codec(const char*, int, size_t);
absl::optional<CodecInst> SendCodec() const;
bool RegisterReceiveCodec(int, const SdpAudioFormat&);
int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
int UnregisterReceiveCodec(uint8_t);
int32_t ReceiveCodec(CodecInst*);
absl::optional<SdpAudioFormat> ReceiveFormat();
As well as this method on RtpRtcp module:
int32_t RegisterSendPayload(const CodecInst&);
Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
2018-12-11 12:22:10 +01:00
|
|
|
frame_size_samples, rtc::CheckedDivExact(clockrate_hz, 1000)));
|
|
|
|
|
auto factory = CreateBuiltinAudioEncoderFactory();
|
2018-08-16 15:01:12 +02:00
|
|
|
acm_->SetEncoder(
|
|
|
|
|
factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
|
|
|
|
|
codec_registered_ = true;
|
Remove CodecInst pt.2
The following APIs on AudioCodingModule are deprecated with this CL:
static int NumberOfCodecs();
static int Codec(int, CodecInst*);
static int Codec(const char*, CodecInst*, int, size_t);
static int Codec(const char*, int, size_t);
absl::optional<CodecInst> SendCodec() const;
bool RegisterReceiveCodec(int, const SdpAudioFormat&);
int RegisterExternalReceiveCodec(int, AudioDecoder*, int, int, const std::string&);
int UnregisterReceiveCodec(uint8_t);
int32_t ReceiveCodec(CodecInst*);
absl::optional<SdpAudioFormat> ReceiveFormat();
As well as this method on RtpRtcp module:
int32_t RegisterSendPayload(const CodecInst&);
Bug: webrtc:7626
Change-Id: I1230732136f1fe9048cf74afdeab767ca57ac9ce
Reviewed-on: https://webrtc-review.googlesource.com/c/113816
Commit-Queue: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26025}
2018-12-11 12:22:10 +01:00
|
|
|
input_frame_.num_channels_ = num_channels;
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK_LE(input_block_size_samples_ * input_frame_.num_channels_,
|
|
|
|
|
AudioFrame::kMaxDataSizeSamples);
|
2014-09-23 12:05:34 +00:00
|
|
|
return codec_registered_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 15:01:12 +02:00
|
|
|
void AcmSendTestOldApi::RegisterExternalCodec(
|
|
|
|
|
std::unique_ptr<AudioEncoder> external_speech_encoder) {
|
2015-05-18 14:52:29 +02:00
|
|
|
input_frame_.num_channels_ = external_speech_encoder->NumChannels();
|
2018-08-16 15:01:12 +02:00
|
|
|
acm_->SetEncoder(std::move(external_speech_encoder));
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK_LE(input_block_size_samples_ * input_frame_.num_channels_,
|
|
|
|
|
AudioFrame::kMaxDataSizeSamples);
|
2018-08-16 15:01:12 +02:00
|
|
|
codec_registered_ = true;
|
2015-05-18 14:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 22:50:47 -07:00
|
|
|
std::unique_ptr<Packet> AcmSendTestOldApi::NextPacket() {
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK(codec_registered_);
|
2015-02-02 23:54:03 +00:00
|
|
|
if (filter_.test(static_cast<size_t>(payload_type_))) {
|
2014-09-23 12:05:34 +00:00
|
|
|
// This payload type should be filtered out. Since the payload type is the
|
|
|
|
|
// same throughout the whole test run, no packet at all will be delivered.
|
|
|
|
|
// We can just as well signal that the test is over by returning NULL.
|
2016-05-24 22:50:47 -07:00
|
|
|
return nullptr;
|
2014-09-23 12:05:34 +00:00
|
|
|
}
|
|
|
|
|
// Insert audio and process until one packet is produced.
|
|
|
|
|
while (clock_.TimeInMilliseconds() < test_duration_ms_) {
|
|
|
|
|
clock_.AdvanceTimeMilliseconds(kBlockSizeMs);
|
2019-02-22 10:13:44 +01:00
|
|
|
RTC_CHECK(audio_source_->Read(
|
|
|
|
|
input_block_size_samples_ * input_frame_.num_channels_,
|
|
|
|
|
input_frame_.mutable_data()));
|
2015-03-02 12:29:30 +00:00
|
|
|
data_to_send_ = false;
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_CHECK_GE(acm_->Add10MsData(input_frame_), 0);
|
Match existing type usage better.
This makes a variety of small changes to synchronize bits of code using different types, remove useless code or casts, and add explicit casts in some places previously doing implicit ones. For example:
* Change a few type declarations to better match how the majority of code uses those objects.
* Eliminate "< 0" check for unsigned values.
* Replace "(float)sin(x)", where |x| is also a float, with "sinf(x)", and similar.
* Add casts to uint32_t in many places timestamps were used and the existing code stored signed values into the unsigned objects.
* Remove downcasts when the results would be passed to a larger type, e.g. calling "foo((int16_t)x)" with an int |x| when foo() takes an int instead of an int16_t.
* Similarly, add casts when passing a larger type to a function taking a smaller one.
* Add casts to int16_t when doing something like "int16_t = int16_t + int16_t" as the "+" operation would implicitly upconvert to int, and similar.
* Use "false" instead of "0" for setting a bool.
* Shift a few temp types when doing a multi-stage calculation involving typecasts, so as to put the most logical/semantically correct type possible into the temps. For example, when doing "int foo = int + int; size_t bar = (size_t)foo + size_t;", we might change |foo| to a size_t and move the cast if it makes more sense for |foo| to be represented as a size_t.
BUG=none
R=andrew@webrtc.org, asapersson@webrtc.org, henrika@webrtc.org, juberti@webrtc.org, kwiberg@webrtc.org
TBR=andrew, asapersson, henrika
Review URL: https://codereview.webrtc.org/1168753002
Cr-Commit-Position: refs/heads/master@{#9419}
2015-06-11 12:55:50 -07:00
|
|
|
input_frame_.timestamp_ += static_cast<uint32_t>(input_block_size_samples_);
|
2015-03-02 12:29:30 +00:00
|
|
|
if (data_to_send_) {
|
2014-09-23 12:05:34 +00:00
|
|
|
// Encoded packet received.
|
|
|
|
|
return CreatePacket();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Test ended.
|
2016-05-24 22:50:47 -07:00
|
|
|
return nullptr;
|
2014-09-23 12:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method receives the callback from ACM when a new packet is produced.
|
2019-04-25 16:31:18 +02:00
|
|
|
int32_t AcmSendTestOldApi::SendData(AudioFrameType frame_type,
|
|
|
|
|
uint8_t payload_type,
|
|
|
|
|
uint32_t timestamp,
|
|
|
|
|
const uint8_t* payload_data,
|
2020-01-23 13:45:50 +01:00
|
|
|
size_t payload_len_bytes,
|
|
|
|
|
int64_t absolute_capture_timestamp_ms) {
|
2014-09-23 12:05:34 +00:00
|
|
|
// Store the packet locally.
|
|
|
|
|
frame_type_ = frame_type;
|
|
|
|
|
payload_type_ = payload_type;
|
|
|
|
|
timestamp_ = timestamp;
|
|
|
|
|
last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes);
|
2021-07-08 20:08:20 +02:00
|
|
|
RTC_DCHECK_EQ(last_payload_vec_.size(), payload_len_bytes);
|
2015-03-02 12:29:30 +00:00
|
|
|
data_to_send_ = true;
|
2014-09-23 12:05:34 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 22:50:47 -07:00
|
|
|
std::unique_ptr<Packet> AcmSendTestOldApi::CreatePacket() {
|
2014-09-23 12:05:34 +00:00
|
|
|
const size_t kRtpHeaderSize = 12;
|
2021-06-16 14:23:22 +02:00
|
|
|
rtc::CopyOnWriteBuffer packet_buffer(last_payload_vec_.size() +
|
|
|
|
|
kRtpHeaderSize);
|
|
|
|
|
uint8_t* packet_memory = packet_buffer.MutableData();
|
2014-09-23 12:05:34 +00:00
|
|
|
// Populate the header bytes.
|
|
|
|
|
packet_memory[0] = 0x80;
|
2015-02-02 23:54:03 +00:00
|
|
|
packet_memory[1] = static_cast<uint8_t>(payload_type_);
|
2014-09-23 12:05:34 +00:00
|
|
|
packet_memory[2] = (sequence_number_ >> 8) & 0xFF;
|
|
|
|
|
packet_memory[3] = (sequence_number_)&0xFF;
|
|
|
|
|
packet_memory[4] = (timestamp_ >> 24) & 0xFF;
|
|
|
|
|
packet_memory[5] = (timestamp_ >> 16) & 0xFF;
|
|
|
|
|
packet_memory[6] = (timestamp_ >> 8) & 0xFF;
|
|
|
|
|
packet_memory[7] = timestamp_ & 0xFF;
|
|
|
|
|
// Set SSRC to 0x12345678.
|
|
|
|
|
packet_memory[8] = 0x12;
|
|
|
|
|
packet_memory[9] = 0x34;
|
|
|
|
|
packet_memory[10] = 0x56;
|
|
|
|
|
packet_memory[11] = 0x78;
|
|
|
|
|
|
|
|
|
|
++sequence_number_;
|
|
|
|
|
|
|
|
|
|
// Copy the payload data.
|
|
|
|
|
memcpy(packet_memory + kRtpHeaderSize, &last_payload_vec_[0],
|
|
|
|
|
last_payload_vec_.size());
|
2021-06-16 14:23:22 +02:00
|
|
|
auto packet = std::make_unique<Packet>(std::move(packet_buffer),
|
|
|
|
|
clock_.TimeInMilliseconds());
|
2016-05-24 22:50:47 -07:00
|
|
|
RTC_DCHECK(packet);
|
|
|
|
|
RTC_DCHECK(packet->valid_header());
|
2014-09-23 12:05:34 +00:00
|
|
|
return packet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace test
|
|
|
|
|
} // namespace webrtc
|