andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "modules/audio_coding/acm2/acm_receiver.h" |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> // std::min |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 14 | #include <memory> |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 16 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 17 | #include "modules/audio_coding/acm2/rent_a_codec.h" |
| 18 | #include "modules/audio_coding/include/audio_coding_module.h" |
| 19 | #include "modules/audio_coding/neteq/tools/rtp_generator.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/safe_conversions.h" |
| 22 | #include "system_wrappers/include/clock.h" |
| 23 | #include "test/gtest.h" |
| 24 | #include "test/testsupport/fileutils.h" |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | namespace acm2 { |
| 29 | namespace { |
| 30 | |
| 31 | bool CodecsEqual(const CodecInst& codec_a, const CodecInst& codec_b) { |
| 32 | if (strcmp(codec_a.plname, codec_b.plname) != 0 || |
| 33 | codec_a.plfreq != codec_b.plfreq || |
| 34 | codec_a.pltype != codec_b.pltype || |
| 35 | codec_b.channels != codec_a.channels) |
| 36 | return false; |
| 37 | return true; |
| 38 | } |
| 39 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 40 | struct CodecIdInst { |
| 41 | explicit CodecIdInst(RentACodec::CodecId codec_id) { |
| 42 | const auto codec_ix = RentACodec::CodecIndexFromId(codec_id); |
| 43 | EXPECT_TRUE(codec_ix); |
| 44 | id = *codec_ix; |
| 45 | const auto codec_inst = RentACodec::CodecInstById(codec_id); |
| 46 | EXPECT_TRUE(codec_inst); |
| 47 | inst = *codec_inst; |
| 48 | } |
| 49 | int id; |
| 50 | CodecInst inst; |
| 51 | }; |
| 52 | |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 53 | } // namespace |
| 54 | |
| 55 | class AcmReceiverTestOldApi : public AudioPacketizationCallback, |
| 56 | public ::testing::Test { |
| 57 | protected: |
| 58 | AcmReceiverTestOldApi() |
| 59 | : timestamp_(0), |
| 60 | packet_sent_(false), |
| 61 | last_packet_send_timestamp_(timestamp_), |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 62 | last_frame_type_(kEmptyFrame) { |
ossu | e352578 | 2016-05-25 07:37:43 -0700 | [diff] [blame] | 63 | config_.decoder_factory = CreateBuiltinAudioDecoderFactory(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | ~AcmReceiverTestOldApi() {} |
| 67 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 68 | void SetUp() override { |
kwiberg | c13ded5 | 2016-06-17 06:00:45 -0700 | [diff] [blame] | 69 | acm_.reset(AudioCodingModule::Create(config_)); |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 70 | receiver_.reset(new AcmReceiver(config_)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 71 | ASSERT_TRUE(receiver_.get() != NULL); |
| 72 | ASSERT_TRUE(acm_.get() != NULL); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 73 | codecs_ = RentACodec::Database(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 74 | |
| 75 | acm_->InitializeReceiver(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 76 | acm_->RegisterTransportCallback(this); |
| 77 | |
| 78 | rtp_header_.header.sequenceNumber = 0; |
| 79 | rtp_header_.header.timestamp = 0; |
| 80 | rtp_header_.header.markerBit = false; |
| 81 | rtp_header_.header.ssrc = 0x12345678; // Arbitrary. |
| 82 | rtp_header_.header.numCSRCs = 0; |
| 83 | rtp_header_.header.payloadType = 0; |
| 84 | rtp_header_.frameType = kAudioFrameSpeech; |
| 85 | rtp_header_.type.Audio.isCNG = false; |
| 86 | } |
| 87 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 88 | void TearDown() override {} |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 89 | |
| 90 | void InsertOnePacketOfSilence(int codec_id) { |
kwiberg | d6c0f8c | 2015-11-06 14:28:00 -0800 | [diff] [blame] | 91 | CodecInst codec = |
| 92 | *RentACodec::CodecInstById(*RentACodec::CodecIdFromIndex(codec_id)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 93 | if (timestamp_ == 0) { // This is the first time inserting audio. |
| 94 | ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); |
| 95 | } else { |
kwiberg | 1fd4a4a | 2015-11-03 11:20:50 -0800 | [diff] [blame] | 96 | auto current_codec = acm_->SendCodec(); |
| 97 | ASSERT_TRUE(current_codec); |
| 98 | if (!CodecsEqual(codec, *current_codec)) |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 99 | ASSERT_EQ(0, acm_->RegisterSendCodec(codec)); |
| 100 | } |
| 101 | AudioFrame frame; |
| 102 | // Frame setup according to the codec. |
| 103 | frame.sample_rate_hz_ = codec.plfreq; |
| 104 | frame.samples_per_channel_ = codec.plfreq / 100; // 10 ms. |
| 105 | frame.num_channels_ = codec.channels; |
yujo | 36b1a5f | 2017-06-12 12:45:32 -0700 | [diff] [blame] | 106 | frame.Mute(); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 107 | packet_sent_ = false; |
| 108 | last_packet_send_timestamp_ = timestamp_; |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 109 | while (!packet_sent_) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 110 | frame.timestamp_ = timestamp_; |
| 111 | timestamp_ += frame.samples_per_channel_; |
henrik.lundin@webrtc.org | f56c162 | 2015-03-02 12:29:30 +0000 | [diff] [blame] | 112 | ASSERT_GE(acm_->Add10MsData(frame), 0); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 113 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 114 | } |
| 115 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 116 | template <size_t N> |
| 117 | void AddSetOfCodecs(const RentACodec::CodecId(&ids)[N]) { |
| 118 | for (auto id : ids) { |
| 119 | const auto i = RentACodec::CodecIndexFromId(id); |
| 120 | ASSERT_TRUE(i); |
kwiberg | 6f0f616 | 2016-09-20 03:07:46 -0700 | [diff] [blame] | 121 | ASSERT_EQ(0, receiver_->AddCodec(*i, codecs_[*i].pltype, |
| 122 | codecs_[*i].channels, codecs_[*i].plfreq, |
| 123 | nullptr, codecs_[*i].plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
kjellander@webrtc.org | 14665ff | 2015-03-04 12:58:35 +0000 | [diff] [blame] | 127 | int SendData(FrameType frame_type, |
| 128 | uint8_t payload_type, |
| 129 | uint32_t timestamp, |
| 130 | const uint8_t* payload_data, |
| 131 | size_t payload_len_bytes, |
| 132 | const RTPFragmentationHeader* fragmentation) override { |
pbos | 22993e1 | 2015-10-19 02:39:06 -0700 | [diff] [blame] | 133 | if (frame_type == kEmptyFrame) |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | |
| 136 | rtp_header_.header.payloadType = payload_type; |
| 137 | rtp_header_.frameType = frame_type; |
| 138 | if (frame_type == kAudioFrameSpeech) |
| 139 | rtp_header_.type.Audio.isCNG = false; |
| 140 | else |
| 141 | rtp_header_.type.Audio.isCNG = true; |
| 142 | rtp_header_.header.timestamp = timestamp; |
| 143 | |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 144 | int ret_val = receiver_->InsertPacket( |
| 145 | rtp_header_, |
| 146 | rtc::ArrayView<const uint8_t>(payload_data, payload_len_bytes)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 147 | if (ret_val < 0) { |
| 148 | assert(false); |
| 149 | return -1; |
| 150 | } |
| 151 | rtp_header_.header.sequenceNumber++; |
| 152 | packet_sent_ = true; |
| 153 | last_frame_type_ = frame_type; |
| 154 | return 0; |
| 155 | } |
| 156 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 157 | AudioCodingModule::Config config_; |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 158 | std::unique_ptr<AcmReceiver> receiver_; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 159 | rtc::ArrayView<const CodecInst> codecs_; |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 160 | std::unique_ptr<AudioCodingModule> acm_; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 161 | WebRtcRTPHeader rtp_header_; |
| 162 | uint32_t timestamp_; |
| 163 | bool packet_sent_; // Set when SendData is called reset when inserting audio. |
| 164 | uint32_t last_packet_send_timestamp_; |
| 165 | FrameType last_frame_type_; |
| 166 | }; |
| 167 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 168 | #if defined(WEBRTC_ANDROID) |
| 169 | #define MAYBE_AddCodecGetCodec DISABLED_AddCodecGetCodec |
| 170 | #else |
| 171 | #define MAYBE_AddCodecGetCodec AddCodecGetCodec |
| 172 | #endif |
| 173 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecGetCodec) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 174 | // Add codec. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 175 | for (size_t n = 0; n < codecs_.size(); ++n) { |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 176 | if (n & 0x1) { // Just add codecs with odd index. |
| 177 | EXPECT_EQ( |
| 178 | 0, receiver_->AddCodec(n, codecs_[n].pltype, codecs_[n].channels, |
| 179 | codecs_[n].plfreq, NULL, codecs_[n].plname)); |
| 180 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 181 | } |
| 182 | // Get codec and compare. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 183 | for (size_t n = 0; n < codecs_.size(); ++n) { |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 184 | CodecInst my_codec; |
| 185 | if (n & 0x1) { |
| 186 | // Codecs with odd index should match the reference. |
| 187 | EXPECT_EQ(0, receiver_->DecoderByPayloadType(codecs_[n].pltype, |
| 188 | &my_codec)); |
| 189 | EXPECT_TRUE(CodecsEqual(codecs_[n], my_codec)); |
| 190 | } else { |
| 191 | // Codecs with even index are not registered. |
| 192 | EXPECT_EQ(-1, receiver_->DecoderByPayloadType(codecs_[n].pltype, |
| 193 | &my_codec)); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 198 | #if defined(WEBRTC_ANDROID) |
| 199 | #define MAYBE_AddCodecChangePayloadType DISABLED_AddCodecChangePayloadType |
| 200 | #else |
| 201 | #define MAYBE_AddCodecChangePayloadType AddCodecChangePayloadType |
| 202 | #endif |
| 203 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecChangePayloadType) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 204 | const CodecIdInst codec1(RentACodec::CodecId::kPCMA); |
| 205 | CodecInst codec2 = codec1.inst; |
| 206 | ++codec2.pltype; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 207 | CodecInst test_codec; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 208 | |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 209 | // Register the same codec with different payloads. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 210 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype, |
| 211 | codec1.inst.channels, codec1.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 212 | nullptr, codec1.inst.plname)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 213 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec2.pltype, codec2.channels, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 214 | codec2.plfreq, NULL, codec2.plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 215 | |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 216 | // Both payload types should exist. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 217 | EXPECT_EQ(0, |
| 218 | receiver_->DecoderByPayloadType(codec1.inst.pltype, &test_codec)); |
| 219 | EXPECT_EQ(true, CodecsEqual(codec1.inst, test_codec)); |
| 220 | EXPECT_EQ(0, receiver_->DecoderByPayloadType(codec2.pltype, &test_codec)); |
| 221 | EXPECT_EQ(true, CodecsEqual(codec2, test_codec)); |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 222 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 223 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 224 | #if defined(WEBRTC_ANDROID) |
| 225 | #define MAYBE_AddCodecChangeCodecId DISABLED_AddCodecChangeCodecId |
| 226 | #else |
| 227 | #define MAYBE_AddCodecChangeCodecId AddCodecChangeCodecId |
| 228 | #endif |
| 229 | TEST_F(AcmReceiverTestOldApi, AddCodecChangeCodecId) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 230 | const CodecIdInst codec1(RentACodec::CodecId::kPCMU); |
| 231 | CodecIdInst codec2(RentACodec::CodecId::kPCMA); |
| 232 | codec2.inst.pltype = codec1.inst.pltype; |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 233 | CodecInst test_codec; |
| 234 | |
| 235 | // Register the same payload type with different codec ID. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 236 | EXPECT_EQ(0, receiver_->AddCodec(codec1.id, codec1.inst.pltype, |
| 237 | codec1.inst.channels, codec1.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 238 | nullptr, codec1.inst.plname)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 239 | EXPECT_EQ(0, receiver_->AddCodec(codec2.id, codec2.inst.pltype, |
| 240 | codec2.inst.channels, codec2.inst.plfreq, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 241 | nullptr, codec2.inst.plname)); |
Jelena Marusic | a990784 | 2015-03-26 14:01:30 +0100 | [diff] [blame] | 242 | |
| 243 | // Make sure that the last codec is used. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 244 | EXPECT_EQ(0, |
| 245 | receiver_->DecoderByPayloadType(codec2.inst.pltype, &test_codec)); |
| 246 | EXPECT_EQ(true, CodecsEqual(codec2.inst, test_codec)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 249 | #if defined(WEBRTC_ANDROID) |
| 250 | #define MAYBE_AddCodecRemoveCodec DISABLED_AddCodecRemoveCodec |
| 251 | #else |
| 252 | #define MAYBE_AddCodecRemoveCodec AddCodecRemoveCodec |
| 253 | #endif |
| 254 | TEST_F(AcmReceiverTestOldApi, MAYBE_AddCodecRemoveCodec) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 255 | const CodecIdInst codec(RentACodec::CodecId::kPCMA); |
| 256 | const int payload_type = codec.inst.pltype; |
| 257 | EXPECT_EQ( |
| 258 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
kwiberg | d120192 | 2016-09-20 15:18:21 -0700 | [diff] [blame] | 259 | codec.inst.plfreq, nullptr, codec.inst.plname)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 260 | |
| 261 | // Remove non-existing codec should not fail. ACM1 legacy. |
| 262 | EXPECT_EQ(0, receiver_->RemoveCodec(payload_type + 1)); |
| 263 | |
| 264 | // Remove an existing codec. |
| 265 | EXPECT_EQ(0, receiver_->RemoveCodec(payload_type)); |
| 266 | |
| 267 | // Ask for the removed codec, must fail. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 268 | CodecInst ci; |
| 269 | EXPECT_EQ(-1, receiver_->DecoderByPayloadType(payload_type, &ci)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 272 | #if defined(WEBRTC_ANDROID) |
| 273 | #define MAYBE_SampleRate DISABLED_SampleRate |
| 274 | #else |
| 275 | #define MAYBE_SampleRate SampleRate |
| 276 | #endif |
| 277 | TEST_F(AcmReceiverTestOldApi, MAYBE_SampleRate) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 278 | const RentACodec::CodecId kCodecId[] = {RentACodec::CodecId::kISAC, |
| 279 | RentACodec::CodecId::kISACSWB}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 280 | AddSetOfCodecs(kCodecId); |
| 281 | |
| 282 | AudioFrame frame; |
| 283 | const int kOutSampleRateHz = 8000; // Different than codec sample rate. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 284 | for (const auto codec_id : kCodecId) { |
| 285 | const CodecIdInst codec(codec_id); |
| 286 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
| 287 | InsertOnePacketOfSilence(codec.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 288 | for (int k = 0; k < num_10ms_frames; ++k) { |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 289 | bool muted; |
| 290 | EXPECT_EQ(0, receiver_->GetAudio(kOutSampleRateHz, &frame, &muted)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 291 | } |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 292 | EXPECT_EQ(codec.inst.plfreq, receiver_->last_output_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 296 | class AcmReceiverTestFaxModeOldApi : public AcmReceiverTestOldApi { |
| 297 | protected: |
| 298 | AcmReceiverTestFaxModeOldApi() { |
| 299 | config_.neteq_config.playout_mode = kPlayoutFax; |
| 300 | } |
| 301 | |
| 302 | void RunVerifyAudioFrame(RentACodec::CodecId codec_id) { |
| 303 | // Make sure "fax mode" is enabled. This will avoid delay changes unless the |
| 304 | // packet-loss concealment is made. We do this in order to make the |
| 305 | // timestamp increments predictable; in normal mode, NetEq may decide to do |
| 306 | // accelerate or pre-emptive expand operations after some time, offsetting |
| 307 | // the timestamp. |
| 308 | EXPECT_EQ(kPlayoutFax, config_.neteq_config.playout_mode); |
| 309 | |
| 310 | const RentACodec::CodecId kCodecId[] = {codec_id}; |
| 311 | AddSetOfCodecs(kCodecId); |
| 312 | |
| 313 | const CodecIdInst codec(codec_id); |
| 314 | const int output_sample_rate_hz = codec.inst.plfreq; |
| 315 | const size_t output_channels = codec.inst.channels; |
| 316 | const size_t samples_per_ms = rtc::checked_cast<size_t>( |
| 317 | rtc::CheckedDivExact(output_sample_rate_hz, 1000)); |
| 318 | const int num_10ms_frames = rtc::CheckedDivExact( |
| 319 | codec.inst.pacsize, rtc::checked_cast<int>(10 * samples_per_ms)); |
| 320 | const AudioFrame::VADActivity expected_vad_activity = |
| 321 | output_sample_rate_hz > 16000 ? AudioFrame::kVadActive |
| 322 | : AudioFrame::kVadPassive; |
| 323 | |
| 324 | // Expect the first output timestamp to be 5*fs/8000 samples before the |
| 325 | // first inserted timestamp (because of NetEq's look-ahead). (This value is |
| 326 | // defined in Expand::overlap_length_.) |
| 327 | uint32_t expected_output_ts = last_packet_send_timestamp_ - |
| 328 | rtc::CheckedDivExact(5 * output_sample_rate_hz, 8000); |
| 329 | |
| 330 | AudioFrame frame; |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 331 | bool muted; |
| 332 | EXPECT_EQ(0, receiver_->GetAudio(output_sample_rate_hz, &frame, &muted)); |
henrik.lundin | 15c51e3 | 2016-04-06 08:38:56 -0700 | [diff] [blame] | 333 | // Expect timestamp = 0 before first packet is inserted. |
| 334 | EXPECT_EQ(0u, frame.timestamp_); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 335 | for (int i = 0; i < 5; ++i) { |
| 336 | InsertOnePacketOfSilence(codec.id); |
| 337 | for (int k = 0; k < num_10ms_frames; ++k) { |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 338 | EXPECT_EQ(0, |
| 339 | receiver_->GetAudio(output_sample_rate_hz, &frame, &muted)); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 340 | EXPECT_EQ(expected_output_ts, frame.timestamp_); |
| 341 | expected_output_ts += 10 * samples_per_ms; |
| 342 | EXPECT_EQ(10 * samples_per_ms, frame.samples_per_channel_); |
| 343 | EXPECT_EQ(output_sample_rate_hz, frame.sample_rate_hz_); |
| 344 | EXPECT_EQ(output_channels, frame.num_channels_); |
| 345 | EXPECT_EQ(AudioFrame::kNormalSpeech, frame.speech_type_); |
| 346 | EXPECT_EQ(expected_vad_activity, frame.vad_activity_); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 347 | EXPECT_FALSE(muted); |
henrik.lundin | 7dc6889 | 2016-04-06 01:03:02 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | }; |
| 352 | |
| 353 | #if defined(WEBRTC_ANDROID) |
| 354 | #define MAYBE_VerifyAudioFramePCMU DISABLED_VerifyAudioFramePCMU |
| 355 | #else |
| 356 | #define MAYBE_VerifyAudioFramePCMU VerifyAudioFramePCMU |
| 357 | #endif |
| 358 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFramePCMU) { |
| 359 | RunVerifyAudioFrame(RentACodec::CodecId::kPCMU); |
| 360 | } |
| 361 | |
| 362 | #if defined(WEBRTC_ANDROID) |
| 363 | #define MAYBE_VerifyAudioFrameISAC DISABLED_VerifyAudioFrameISAC |
| 364 | #else |
| 365 | #define MAYBE_VerifyAudioFrameISAC VerifyAudioFrameISAC |
| 366 | #endif |
| 367 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameISAC) { |
| 368 | RunVerifyAudioFrame(RentACodec::CodecId::kISAC); |
| 369 | } |
| 370 | |
| 371 | #if defined(WEBRTC_ANDROID) |
| 372 | #define MAYBE_VerifyAudioFrameOpus DISABLED_VerifyAudioFrameOpus |
| 373 | #else |
| 374 | #define MAYBE_VerifyAudioFrameOpus VerifyAudioFrameOpus |
| 375 | #endif |
| 376 | TEST_F(AcmReceiverTestFaxModeOldApi, MAYBE_VerifyAudioFrameOpus) { |
| 377 | RunVerifyAudioFrame(RentACodec::CodecId::kOpus); |
| 378 | } |
| 379 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 380 | #if defined(WEBRTC_ANDROID) |
| 381 | #define MAYBE_PostdecodingVad DISABLED_PostdecodingVad |
| 382 | #else |
| 383 | #define MAYBE_PostdecodingVad PostdecodingVad |
| 384 | #endif |
| 385 | TEST_F(AcmReceiverTestOldApi, MAYBE_PostdecodingVad) { |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 386 | EXPECT_TRUE(config_.neteq_config.enable_post_decode_vad); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 387 | const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb); |
| 388 | ASSERT_EQ( |
| 389 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 390 | codec.inst.plfreq, nullptr, "")); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 391 | const int kNumPackets = 5; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 392 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 393 | AudioFrame frame; |
| 394 | for (int n = 0; n < kNumPackets; ++n) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 395 | InsertOnePacketOfSilence(codec.id); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 396 | for (int k = 0; k < num_10ms_frames; ++k) { |
| 397 | bool muted; |
| 398 | ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted)); |
| 399 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 400 | } |
| 401 | EXPECT_EQ(AudioFrame::kVadPassive, frame.vad_activity_); |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 402 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 403 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 404 | class AcmReceiverTestPostDecodeVadPassiveOldApi : public AcmReceiverTestOldApi { |
| 405 | protected: |
| 406 | AcmReceiverTestPostDecodeVadPassiveOldApi() { |
| 407 | config_.neteq_config.enable_post_decode_vad = false; |
| 408 | } |
| 409 | }; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 410 | |
henrik.lundin | 500c04b | 2016-03-08 02:36:04 -0800 | [diff] [blame] | 411 | #if defined(WEBRTC_ANDROID) |
| 412 | #define MAYBE_PostdecodingVad DISABLED_PostdecodingVad |
| 413 | #else |
| 414 | #define MAYBE_PostdecodingVad PostdecodingVad |
| 415 | #endif |
| 416 | TEST_F(AcmReceiverTestPostDecodeVadPassiveOldApi, MAYBE_PostdecodingVad) { |
| 417 | EXPECT_FALSE(config_.neteq_config.enable_post_decode_vad); |
| 418 | const CodecIdInst codec(RentACodec::CodecId::kPCM16Bwb); |
| 419 | ASSERT_EQ( |
| 420 | 0, receiver_->AddCodec(codec.id, codec.inst.pltype, codec.inst.channels, |
| 421 | codec.inst.plfreq, nullptr, "")); |
| 422 | const int kNumPackets = 5; |
| 423 | const int num_10ms_frames = codec.inst.pacsize / (codec.inst.plfreq / 100); |
| 424 | AudioFrame frame; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 425 | for (int n = 0; n < kNumPackets; ++n) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 426 | InsertOnePacketOfSilence(codec.id); |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 427 | for (int k = 0; k < num_10ms_frames; ++k) { |
| 428 | bool muted; |
| 429 | ASSERT_EQ(0, receiver_->GetAudio(codec.inst.plfreq, &frame, &muted)); |
| 430 | } |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 431 | } |
| 432 | EXPECT_EQ(AudioFrame::kVadUnknown, frame.vad_activity_); |
| 433 | } |
| 434 | |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 435 | #if defined(WEBRTC_ANDROID) |
| 436 | #define MAYBE_LastAudioCodec DISABLED_LastAudioCodec |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 437 | #else |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 438 | #define MAYBE_LastAudioCodec LastAudioCodec |
kwiberg | 98ab3a4 | 2015-09-30 21:54:21 -0700 | [diff] [blame] | 439 | #endif |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 440 | #if defined(WEBRTC_CODEC_ISAC) |
| 441 | TEST_F(AcmReceiverTestOldApi, MAYBE_LastAudioCodec) { |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 442 | const RentACodec::CodecId kCodecId[] = { |
| 443 | RentACodec::CodecId::kISAC, RentACodec::CodecId::kPCMA, |
| 444 | RentACodec::CodecId::kISACSWB, RentACodec::CodecId::kPCM16Bswb32kHz}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 445 | AddSetOfCodecs(kCodecId); |
| 446 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 447 | const RentACodec::CodecId kCngId[] = { |
| 448 | // Not including full-band. |
| 449 | RentACodec::CodecId::kCNNB, RentACodec::CodecId::kCNWB, |
| 450 | RentACodec::CodecId::kCNSWB}; |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 451 | AddSetOfCodecs(kCngId); |
| 452 | |
| 453 | // Register CNG at sender side. |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 454 | for (auto id : kCngId) |
| 455 | ASSERT_EQ(0, acm_->RegisterSendCodec(CodecIdInst(id).inst)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 456 | |
| 457 | CodecInst codec; |
| 458 | // No audio payload is received. |
| 459 | EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec)); |
| 460 | |
| 461 | // Start with sending DTX. |
| 462 | ASSERT_EQ(0, acm_->SetVAD(true, true, VADVeryAggr)); |
| 463 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 464 | InsertOnePacketOfSilence(CodecIdInst(kCodecId[0]).id); // Enough to test |
| 465 | // with one codec. |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 466 | ASSERT_TRUE(packet_sent_); |
| 467 | EXPECT_EQ(kAudioFrameCN, last_frame_type_); |
| 468 | |
| 469 | // Has received, only, DTX. Last Audio codec is undefined. |
| 470 | EXPECT_EQ(-1, receiver_->LastAudioCodec(&codec)); |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 471 | EXPECT_FALSE(receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 472 | |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 473 | for (auto id : kCodecId) { |
| 474 | const CodecIdInst c(id); |
| 475 | |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 476 | // Set DTX off to send audio payload. |
| 477 | acm_->SetVAD(false, false, VADAggr); |
| 478 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 479 | InsertOnePacketOfSilence(c.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 480 | |
| 481 | // Sanity check if Actually an audio payload received, and it should be |
| 482 | // of type "speech." |
| 483 | ASSERT_TRUE(packet_sent_); |
| 484 | ASSERT_EQ(kAudioFrameSpeech, last_frame_type_); |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 485 | EXPECT_EQ(rtc::Optional<int>(c.inst.plfreq), |
| 486 | receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 487 | |
| 488 | // Set VAD on to send DTX. Then check if the "Last Audio codec" returns |
| 489 | // the expected codec. |
| 490 | acm_->SetVAD(true, true, VADAggr); |
| 491 | |
| 492 | // Do as many encoding until a DTX is sent. |
| 493 | while (last_frame_type_ != kAudioFrameCN) { |
| 494 | packet_sent_ = false; |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 495 | InsertOnePacketOfSilence(c.id); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 496 | ASSERT_TRUE(packet_sent_); |
| 497 | } |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 498 | EXPECT_EQ(rtc::Optional<int>(c.inst.plfreq), |
| 499 | receiver_->last_packet_sample_rate_hz()); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 500 | EXPECT_EQ(0, receiver_->LastAudioCodec(&codec)); |
kwiberg | fce4a94 | 2015-10-27 11:40:24 -0700 | [diff] [blame] | 501 | EXPECT_TRUE(CodecsEqual(c.inst, codec)); |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Peter Boström | e2976c8 | 2016-01-04 22:44:05 +0100 | [diff] [blame] | 504 | #endif |
andresp@webrtc.org | 4f6f22f | 2014-09-23 11:37:57 +0000 | [diff] [blame] | 505 | |
| 506 | } // namespace acm2 |
| 507 | |
| 508 | } // namespace webrtc |