blob: 98d673f3a120e934fc5859af185a85967a360545 [file] [log] [blame]
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/acm2/acm_send_test.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000012
13#include <assert.h>
14#include <stdio.h>
15#include <string.h>
16
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010017#include "absl/strings/match.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/audio_codecs/audio_encoder.h"
Karl Wiberg5817d3d2018-04-06 10:06:42 +020019#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Karl Wiberg801500c2018-08-16 15:01:12 +020020#include "api/audio_codecs/builtin_audio_encoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "modules/audio_coding/include/audio_coding_module.h"
22#include "modules/audio_coding/neteq/tools/input_audio_file.h"
23#include "modules/audio_coding/neteq/tools/packet.h"
24#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/string_encode.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "test/gtest.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000027
28namespace webrtc {
29namespace test {
30
31AcmSendTestOldApi::AcmSendTestOldApi(InputAudioFile* audio_source,
32 int source_rate_hz,
33 int test_duration_ms)
34 : clock_(0),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020035 acm_(webrtc::AudioCodingModule::Create([this] {
36 AudioCodingModule::Config config;
37 config.clock = &clock_;
38 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
39 return config;
40 }())),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000041 audio_source_(audio_source),
42 source_rate_hz_(source_rate_hz),
Peter Kastingdce40cf2015-08-24 14:52:23 -070043 input_block_size_samples_(
44 static_cast<size_t>(source_rate_hz_ * kBlockSizeMs / 1000)),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000045 codec_registered_(false),
46 test_duration_ms_(test_duration_ms),
47 frame_type_(kAudioFrameSpeech),
48 payload_type_(0),
49 timestamp_(0),
50 sequence_number_(0) {
51 input_frame_.sample_rate_hz_ = source_rate_hz_;
52 input_frame_.num_channels_ = 1;
53 input_frame_.samples_per_channel_ = input_block_size_samples_;
54 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
55 AudioFrame::kMaxDataSizeSamples);
56 acm_->RegisterTransportCallback(this);
57}
58
kwiberg65fc8b92016-08-29 10:05:24 -070059AcmSendTestOldApi::~AcmSendTestOldApi() = default;
60
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000061bool AcmSendTestOldApi::RegisterCodec(const char* payload_name,
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010062 int clockrate_hz,
63 int num_channels,
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000064 int payload_type,
65 int frame_size_samples) {
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010066 SdpAudioFormat format(payload_name, clockrate_hz, num_channels);
67 if (absl::EqualsIgnoreCase(payload_name, "g722")) {
68 RTC_CHECK_EQ(16000, clockrate_hz);
69 format.clockrate_hz = 8000;
70 } else if (absl::EqualsIgnoreCase(payload_name, "opus")) {
71 RTC_CHECK(num_channels == 1 || num_channels == 2);
72 if (num_channels == 2) {
73 format.parameters["stereo"] = "1";
74 }
75 format.num_channels = 2;
76 }
Karl Wiberg801500c2018-08-16 15:01:12 +020077 format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact(
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010078 frame_size_samples, rtc::CheckedDivExact(clockrate_hz, 1000)));
79 auto factory = CreateBuiltinAudioEncoderFactory();
Karl Wiberg801500c2018-08-16 15:01:12 +020080 acm_->SetEncoder(
81 factory->MakeAudioEncoder(payload_type, format, absl::nullopt));
82 codec_registered_ = true;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010083 input_frame_.num_channels_ = num_channels;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000084 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
85 AudioFrame::kMaxDataSizeSamples);
86 return codec_registered_;
87}
88
Karl Wiberg801500c2018-08-16 15:01:12 +020089void AcmSendTestOldApi::RegisterExternalCodec(
90 std::unique_ptr<AudioEncoder> external_speech_encoder) {
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020091 input_frame_.num_channels_ = external_speech_encoder->NumChannels();
Karl Wiberg801500c2018-08-16 15:01:12 +020092 acm_->SetEncoder(std::move(external_speech_encoder));
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020093 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
94 AudioFrame::kMaxDataSizeSamples);
Karl Wiberg801500c2018-08-16 15:01:12 +020095 codec_registered_ = true;
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020096}
97
henrik.lundin46ba49c2016-05-24 22:50:47 -070098std::unique_ptr<Packet> AcmSendTestOldApi::NextPacket() {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000099 assert(codec_registered_);
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000100 if (filter_.test(static_cast<size_t>(payload_type_))) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000101 // This payload type should be filtered out. Since the payload type is the
102 // same throughout the whole test run, no packet at all will be delivered.
103 // We can just as well signal that the test is over by returning NULL.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700104 return nullptr;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000105 }
106 // Insert audio and process until one packet is produced.
107 while (clock_.TimeInMilliseconds() < test_duration_ms_) {
108 clock_.AdvanceTimeMilliseconds(kBlockSizeMs);
yujo36b1a5f2017-06-12 12:45:32 -0700109 RTC_CHECK(audio_source_->Read(input_block_size_samples_,
110 input_frame_.mutable_data()));
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000111 if (input_frame_.num_channels_ > 1) {
Yves Gerey665174f2018-06-19 15:03:05 +0200112 InputAudioFile::DuplicateInterleaved(
113 input_frame_.data(), input_block_size_samples_,
114 input_frame_.num_channels_, input_frame_.mutable_data());
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000115 }
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000116 data_to_send_ = false;
henrikg91d6ede2015-09-17 00:24:34 -0700117 RTC_CHECK_GE(acm_->Add10MsData(input_frame_), 0);
Peter Kastingb7e50542015-06-11 12:55:50 -0700118 input_frame_.timestamp_ += static_cast<uint32_t>(input_block_size_samples_);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000119 if (data_to_send_) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000120 // Encoded packet received.
121 return CreatePacket();
122 }
123 }
124 // Test ended.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700125 return nullptr;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000126}
127
128// This method receives the callback from ACM when a new packet is produced.
129int32_t AcmSendTestOldApi::SendData(
130 FrameType frame_type,
131 uint8_t payload_type,
132 uint32_t timestamp,
133 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000134 size_t payload_len_bytes,
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000135 const RTPFragmentationHeader* fragmentation) {
136 // Store the packet locally.
137 frame_type_ = frame_type;
138 payload_type_ = payload_type;
139 timestamp_ = timestamp;
140 last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes);
141 assert(last_payload_vec_.size() == payload_len_bytes);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000142 data_to_send_ = true;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000143 return 0;
144}
145
henrik.lundin46ba49c2016-05-24 22:50:47 -0700146std::unique_ptr<Packet> AcmSendTestOldApi::CreatePacket() {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000147 const size_t kRtpHeaderSize = 12;
148 size_t allocated_bytes = last_payload_vec_.size() + kRtpHeaderSize;
149 uint8_t* packet_memory = new uint8_t[allocated_bytes];
150 // Populate the header bytes.
151 packet_memory[0] = 0x80;
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000152 packet_memory[1] = static_cast<uint8_t>(payload_type_);
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000153 packet_memory[2] = (sequence_number_ >> 8) & 0xFF;
Yves Gerey665174f2018-06-19 15:03:05 +0200154 packet_memory[3] = (sequence_number_)&0xFF;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000155 packet_memory[4] = (timestamp_ >> 24) & 0xFF;
156 packet_memory[5] = (timestamp_ >> 16) & 0xFF;
157 packet_memory[6] = (timestamp_ >> 8) & 0xFF;
158 packet_memory[7] = timestamp_ & 0xFF;
159 // Set SSRC to 0x12345678.
160 packet_memory[8] = 0x12;
161 packet_memory[9] = 0x34;
162 packet_memory[10] = 0x56;
163 packet_memory[11] = 0x78;
164
165 ++sequence_number_;
166
167 // Copy the payload data.
Yves Gerey665174f2018-06-19 15:03:05 +0200168 memcpy(packet_memory + kRtpHeaderSize, &last_payload_vec_[0],
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000169 last_payload_vec_.size());
henrik.lundin46ba49c2016-05-24 22:50:47 -0700170 std::unique_ptr<Packet> packet(
171 new Packet(packet_memory, allocated_bytes, clock_.TimeInMilliseconds()));
172 RTC_DCHECK(packet);
173 RTC_DCHECK(packet->valid_header());
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000174 return packet;
175}
176
177} // namespace test
178} // namespace webrtc