blob: b1a3e9811d7ad6f82a74dd85506df380c1818f47 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/audio_codecs/audio_encoder.h"
Karl Wiberg5817d3d2018-04-06 10:06:42 +020018#include "api/audio_codecs/builtin_audio_decoder_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/include/audio_coding_module.h"
20#include "modules/audio_coding/neteq/tools/input_audio_file.h"
21#include "modules/audio_coding/neteq/tools/packet.h"
22#include "rtc_base/checks.h"
23#include "test/gtest.h"
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000024
25namespace webrtc {
26namespace test {
27
28AcmSendTestOldApi::AcmSendTestOldApi(InputAudioFile* audio_source,
29 int source_rate_hz,
30 int test_duration_ms)
31 : clock_(0),
Karl Wiberg5817d3d2018-04-06 10:06:42 +020032 acm_(webrtc::AudioCodingModule::Create([this] {
33 AudioCodingModule::Config config;
34 config.clock = &clock_;
35 config.decoder_factory = CreateBuiltinAudioDecoderFactory();
36 return config;
37 }())),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000038 audio_source_(audio_source),
39 source_rate_hz_(source_rate_hz),
Peter Kastingdce40cf2015-08-24 14:52:23 -070040 input_block_size_samples_(
41 static_cast<size_t>(source_rate_hz_ * kBlockSizeMs / 1000)),
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000042 codec_registered_(false),
43 test_duration_ms_(test_duration_ms),
44 frame_type_(kAudioFrameSpeech),
45 payload_type_(0),
46 timestamp_(0),
47 sequence_number_(0) {
48 input_frame_.sample_rate_hz_ = source_rate_hz_;
49 input_frame_.num_channels_ = 1;
50 input_frame_.samples_per_channel_ = input_block_size_samples_;
51 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
52 AudioFrame::kMaxDataSizeSamples);
53 acm_->RegisterTransportCallback(this);
54}
55
kwiberg65fc8b92016-08-29 10:05:24 -070056AcmSendTestOldApi::~AcmSendTestOldApi() = default;
57
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000058bool AcmSendTestOldApi::RegisterCodec(const char* payload_name,
59 int sampling_freq_hz,
60 int channels,
61 int payload_type,
62 int frame_size_samples) {
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020063 CodecInst codec;
henrikg91d6ede2015-09-17 00:24:34 -070064 RTC_CHECK_EQ(0, AudioCodingModule::Codec(payload_name, &codec,
65 sampling_freq_hz, channels));
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020066 codec.pltype = payload_type;
67 codec.pacsize = frame_size_samples;
68 codec_registered_ = (acm_->RegisterSendCodec(codec) == 0);
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000069 input_frame_.num_channels_ = channels;
70 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
71 AudioFrame::kMaxDataSizeSamples);
72 return codec_registered_;
73}
74
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020075bool AcmSendTestOldApi::RegisterExternalCodec(
kwiberg12cfc9b2015-09-08 05:57:53 -070076 AudioEncoder* external_speech_encoder) {
Karl Wiberg7e0c7d42015-05-18 14:52:29 +020077 acm_->RegisterExternalSendCodec(external_speech_encoder);
78 input_frame_.num_channels_ = external_speech_encoder->NumChannels();
79 assert(input_block_size_samples_ * input_frame_.num_channels_ <=
80 AudioFrame::kMaxDataSizeSamples);
81 return codec_registered_ = true;
82}
83
henrik.lundin46ba49c2016-05-24 22:50:47 -070084std::unique_ptr<Packet> AcmSendTestOldApi::NextPacket() {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000085 assert(codec_registered_);
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +000086 if (filter_.test(static_cast<size_t>(payload_type_))) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000087 // This payload type should be filtered out. Since the payload type is the
88 // same throughout the whole test run, no packet at all will be delivered.
89 // We can just as well signal that the test is over by returning NULL.
henrik.lundin46ba49c2016-05-24 22:50:47 -070090 return nullptr;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000091 }
92 // Insert audio and process until one packet is produced.
93 while (clock_.TimeInMilliseconds() < test_duration_ms_) {
94 clock_.AdvanceTimeMilliseconds(kBlockSizeMs);
yujo36b1a5f2017-06-12 12:45:32 -070095 RTC_CHECK(audio_source_->Read(input_block_size_samples_,
96 input_frame_.mutable_data()));
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +000097 if (input_frame_.num_channels_ > 1) {
Yves Gerey665174f2018-06-19 15:03:05 +020098 InputAudioFile::DuplicateInterleaved(
99 input_frame_.data(), input_block_size_samples_,
100 input_frame_.num_channels_, input_frame_.mutable_data());
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000101 }
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000102 data_to_send_ = false;
henrikg91d6ede2015-09-17 00:24:34 -0700103 RTC_CHECK_GE(acm_->Add10MsData(input_frame_), 0);
Peter Kastingb7e50542015-06-11 12:55:50 -0700104 input_frame_.timestamp_ += static_cast<uint32_t>(input_block_size_samples_);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000105 if (data_to_send_) {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000106 // Encoded packet received.
107 return CreatePacket();
108 }
109 }
110 // Test ended.
henrik.lundin46ba49c2016-05-24 22:50:47 -0700111 return nullptr;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000112}
113
114// This method receives the callback from ACM when a new packet is produced.
115int32_t AcmSendTestOldApi::SendData(
116 FrameType frame_type,
117 uint8_t payload_type,
118 uint32_t timestamp,
119 const uint8_t* payload_data,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000120 size_t payload_len_bytes,
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000121 const RTPFragmentationHeader* fragmentation) {
122 // Store the packet locally.
123 frame_type_ = frame_type;
124 payload_type_ = payload_type;
125 timestamp_ = timestamp;
126 last_payload_vec_.assign(payload_data, payload_data + payload_len_bytes);
127 assert(last_payload_vec_.size() == payload_len_bytes);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +0000128 data_to_send_ = true;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000129 return 0;
130}
131
henrik.lundin46ba49c2016-05-24 22:50:47 -0700132std::unique_ptr<Packet> AcmSendTestOldApi::CreatePacket() {
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000133 const size_t kRtpHeaderSize = 12;
134 size_t allocated_bytes = last_payload_vec_.size() + kRtpHeaderSize;
135 uint8_t* packet_memory = new uint8_t[allocated_bytes];
136 // Populate the header bytes.
137 packet_memory[0] = 0x80;
pkasting@chromium.org0e81fdf2015-02-02 23:54:03 +0000138 packet_memory[1] = static_cast<uint8_t>(payload_type_);
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000139 packet_memory[2] = (sequence_number_ >> 8) & 0xFF;
Yves Gerey665174f2018-06-19 15:03:05 +0200140 packet_memory[3] = (sequence_number_)&0xFF;
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000141 packet_memory[4] = (timestamp_ >> 24) & 0xFF;
142 packet_memory[5] = (timestamp_ >> 16) & 0xFF;
143 packet_memory[6] = (timestamp_ >> 8) & 0xFF;
144 packet_memory[7] = timestamp_ & 0xFF;
145 // Set SSRC to 0x12345678.
146 packet_memory[8] = 0x12;
147 packet_memory[9] = 0x34;
148 packet_memory[10] = 0x56;
149 packet_memory[11] = 0x78;
150
151 ++sequence_number_;
152
153 // Copy the payload data.
Yves Gerey665174f2018-06-19 15:03:05 +0200154 memcpy(packet_memory + kRtpHeaderSize, &last_payload_vec_[0],
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000155 last_payload_vec_.size());
henrik.lundin46ba49c2016-05-24 22:50:47 -0700156 std::unique_ptr<Packet> packet(
157 new Packet(packet_memory, allocated_bytes, clock_.TimeInMilliseconds()));
158 RTC_DCHECK(packet);
159 RTC_DCHECK(packet->valid_header());
henrik.lundin@webrtc.org0e6e4d22014-09-23 12:05:34 +0000160 return packet;
161}
162
163} // namespace test
164} // namespace webrtc