niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | df69775 | 2012-02-08 10:22:21 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 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 04:47:31 | [diff] [blame] | 11 | #include "modules/audio_coding/test/TestAllCodecs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 12 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 13 | #include <cstdio> |
| 14 | #include <limits> |
tina.legrand@webrtc.org | 5e7ca60 | 2012-06-12 07:16:24 | [diff] [blame] | 15 | #include <string> |
kjellander@webrtc.org | 5490c71 | 2011-12-21 13:34:18 | [diff] [blame] | 16 | |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Karl Wiberg | 5817d3d | 2018-04-06 08:06:42 | [diff] [blame] | 18 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
Karl Wiberg | 133cff0 | 2018-07-06 13:40:14 | [diff] [blame] | 19 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Danil Chapovalov | 1030eaa | 2024-06-18 09:20:40 | [diff] [blame] | 20 | #include "api/environment/environment_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 21 | #include "modules/audio_coding/include/audio_coding_module_typedefs.h" |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 22 | #include "modules/include/module_common_types.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 24 | #include "rtc_base/string_encode.h" |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 25 | #include "rtc_base/strings/string_builder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "test/gtest.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 27 | #include "test/testsupport/file_utils.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 28 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 29 | // Description of the test: |
| 30 | // In this test we set up a one-way communication channel from a participant |
| 31 | // called "a" to a participant called "b". |
| 32 | // a -> channel_a_to_b -> b |
| 33 | // |
| 34 | // The test loops through all available mono codecs, encode at "a" sends over |
| 35 | // the channel, and decodes at "b". |
| 36 | |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 37 | #define CHECK_ERROR(f) \ |
| 38 | do { \ |
| 39 | EXPECT_GE(f, 0) << "Error Calling API"; \ |
| 40 | } while (0) |
| 41 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 42 | namespace { |
| 43 | const size_t kVariableSize = std::numeric_limits<size_t>::max(); |
| 44 | } |
| 45 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 | [diff] [blame] | 46 | namespace webrtc { |
| 47 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 48 | // Class for simulating packet handling. |
| 49 | TestPack::TestPack() |
| 50 | : receiver_acm_(NULL), |
| 51 | sequence_number_(0), |
| 52 | timestamp_diff_(0), |
| 53 | last_in_timestamp_(0), |
| 54 | total_bytes_(0), |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 55 | payload_size_(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 56 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 57 | TestPack::~TestPack() {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 58 | |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 59 | void TestPack::RegisterReceiverACM(acm2::AcmReceiver* acm_receiver) { |
| 60 | receiver_acm_ = acm_receiver; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 61 | return; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 62 | } |
| 63 | |
Niels Möller | 87e2d78 | 2019-03-07 09:18:23 | [diff] [blame] | 64 | int32_t TestPack::SendData(AudioFrameType frame_type, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 65 | uint8_t payload_type, |
| 66 | uint32_t timestamp, |
| 67 | const uint8_t* payload_data, |
Minyue Li | ff0e4db | 2020-01-23 12:45:50 | [diff] [blame] | 68 | size_t payload_size, |
| 69 | int64_t absolute_capture_timestamp_ms) { |
Niels Möller | afb5dbb | 2019-02-15 14:21:47 | [diff] [blame] | 70 | RTPHeader rtp_header; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 71 | int32_t status; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 72 | |
Niels Möller | afb5dbb | 2019-02-15 14:21:47 | [diff] [blame] | 73 | rtp_header.markerBit = false; |
| 74 | rtp_header.ssrc = 0; |
| 75 | rtp_header.sequenceNumber = sequence_number_++; |
| 76 | rtp_header.payloadType = payload_type; |
| 77 | rtp_header.timestamp = timestamp; |
philipel | 0a5fe77 | 2018-06-19 14:18:31 | [diff] [blame] | 78 | |
Niels Möller | c936cb6 | 2019-03-19 13:10:16 | [diff] [blame] | 79 | if (frame_type == AudioFrameType::kEmptyFrame) { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 80 | // Skip this frame. |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | // Only run mono for all test cases. |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 85 | memcpy(payload_data_, payload_data, payload_size); |
| 86 | |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 87 | status = receiver_acm_->InsertPacket( |
Lionel Koenig | 5889cf5 | 2024-05-27 09:22:27 | [diff] [blame] | 88 | rtp_header, rtc::ArrayView<const uint8_t>(payload_data_, payload_size), |
| 89 | /*receive_time=*/Timestamp::MinusInfinity()); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 90 | |
| 91 | payload_size_ = payload_size; |
| 92 | timestamp_diff_ = timestamp - last_in_timestamp_; |
| 93 | last_in_timestamp_ = timestamp; |
| 94 | total_bytes_ += payload_size; |
| 95 | return status; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 96 | } |
| 97 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 98 | size_t TestPack::payload_size() { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 99 | return payload_size_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 100 | } |
| 101 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 102 | uint32_t TestPack::timestamp_diff() { |
| 103 | return timestamp_diff_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 104 | } |
| 105 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 106 | void TestPack::reset_payload_size() { |
| 107 | payload_size_ = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 108 | } |
| 109 | |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 110 | TestAllCodecs::TestAllCodecs() |
Danil Chapovalov | 1030eaa | 2024-06-18 09:20:40 | [diff] [blame] | 111 | : env_(CreateEnvironment()), |
| 112 | acm_a_(AudioCodingModule::Create()), |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 113 | acm_b_(std::make_unique<acm2::AcmReceiver>( |
Danil Chapovalov | 33582ea | 2024-08-05 10:40:46 | [diff] [blame] | 114 | env_, |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 115 | acm2::AcmReceiver::Config(CreateBuiltinAudioDecoderFactory()))), |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 116 | channel_a_to_b_(NULL), |
| 117 | test_count_(0), |
| 118 | packet_size_samples_(0), |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 119 | packet_size_bytes_(0) {} |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 120 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 121 | TestAllCodecs::~TestAllCodecs() { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 122 | if (channel_a_to_b_ != NULL) { |
| 123 | delete channel_a_to_b_; |
| 124 | channel_a_to_b_ = NULL; |
| 125 | } |
| 126 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 127 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 128 | void TestAllCodecs::Perform() { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 129 | const std::string file_name = |
| 130 | webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"); |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 | [diff] [blame] | 131 | infile_a_.Open(file_name, 32000, "rb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 132 | |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 133 | acm_b_->SetCodecs({{107, {"L16", 8000, 1}}, |
| 134 | {108, {"L16", 16000, 1}}, |
| 135 | {109, {"L16", 32000, 1}}, |
| 136 | {111, {"L16", 8000, 2}}, |
| 137 | {112, {"L16", 16000, 2}}, |
| 138 | {113, {"L16", 32000, 2}}, |
| 139 | {0, {"PCMU", 8000, 1}}, |
| 140 | {110, {"PCMU", 8000, 2}}, |
| 141 | {8, {"PCMA", 8000, 1}}, |
| 142 | {118, {"PCMA", 8000, 2}}, |
| 143 | {102, {"ILBC", 8000, 1}}, |
| 144 | {9, {"G722", 8000, 1}}, |
| 145 | {119, {"G722", 8000, 2}}, |
| 146 | {120, {"OPUS", 48000, 2, {{"stereo", "1"}}}}, |
| 147 | {13, {"CN", 8000, 1}}, |
| 148 | {98, {"CN", 16000, 1}}, |
| 149 | {99, {"CN", 32000, 1}}}); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 150 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 151 | // Create and connect the channel |
| 152 | channel_a_to_b_ = new TestPack; |
| 153 | acm_a_->RegisterTransportCallback(channel_a_to_b_); |
andrew@webrtc.org | 89df092 | 2013-09-12 01:27:43 | [diff] [blame] | 154 | channel_a_to_b_->RegisterReceiverACM(acm_b_.get()); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 155 | |
| 156 | // All codecs are tested for all allowed sampling frequencies, rates and |
| 157 | // packet sizes. |
Mirko Bonadei | bd4dd67 | 2024-06-07 08:38:55 | [diff] [blame] | 158 | |
| 159 | // TODO(bugs.webrtc.org/345525069): Either fix/enable or remove G722. |
| 160 | #if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer) |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 161 | test_count_++; |
| 162 | OpenOutFile(test_count_); |
| 163 | char codec_g722[] = "G722"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 164 | RegisterSendCodec(codec_g722, 16000, 64000, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 165 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 166 | RegisterSendCodec(codec_g722, 16000, 64000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 167 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 168 | RegisterSendCodec(codec_g722, 16000, 64000, 480, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 169 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 170 | RegisterSendCodec(codec_g722, 16000, 64000, 640, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 171 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 172 | RegisterSendCodec(codec_g722, 16000, 64000, 800, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 173 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 174 | RegisterSendCodec(codec_g722, 16000, 64000, 960, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 175 | Run(channel_a_to_b_); |
| 176 | outfile_b_.Close(); |
Mirko Bonadei | bd4dd67 | 2024-06-07 08:38:55 | [diff] [blame] | 177 | #endif |
| 178 | // TODO(bugs.webrtc.org/345525069): Either fix/enable or remove iLBC. |
| 179 | #if defined(__has_feature) && !__has_feature(undefined_behavior_sanitizer) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 180 | #ifdef WEBRTC_CODEC_ILBC |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 181 | test_count_++; |
| 182 | OpenOutFile(test_count_); |
| 183 | char codec_ilbc[] = "ILBC"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 184 | RegisterSendCodec(codec_ilbc, 8000, 13300, 240, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 185 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 186 | RegisterSendCodec(codec_ilbc, 8000, 13300, 480, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 187 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 188 | RegisterSendCodec(codec_ilbc, 8000, 15200, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 189 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 190 | RegisterSendCodec(codec_ilbc, 8000, 15200, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 191 | Run(channel_a_to_b_); |
| 192 | outfile_b_.Close(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 193 | #endif |
Mirko Bonadei | bd4dd67 | 2024-06-07 08:38:55 | [diff] [blame] | 194 | #endif |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 195 | test_count_++; |
| 196 | OpenOutFile(test_count_); |
| 197 | char codec_l16[] = "L16"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 198 | RegisterSendCodec(codec_l16, 8000, 128000, 80, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 199 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 200 | RegisterSendCodec(codec_l16, 8000, 128000, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 201 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 202 | RegisterSendCodec(codec_l16, 8000, 128000, 240, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 203 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 204 | RegisterSendCodec(codec_l16, 8000, 128000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 205 | Run(channel_a_to_b_); |
| 206 | outfile_b_.Close(); |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 207 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 208 | test_count_++; |
| 209 | OpenOutFile(test_count_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 210 | RegisterSendCodec(codec_l16, 16000, 256000, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 211 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 212 | RegisterSendCodec(codec_l16, 16000, 256000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 213 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 214 | RegisterSendCodec(codec_l16, 16000, 256000, 480, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 215 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 216 | RegisterSendCodec(codec_l16, 16000, 256000, 640, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 217 | Run(channel_a_to_b_); |
| 218 | outfile_b_.Close(); |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 219 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 220 | test_count_++; |
| 221 | OpenOutFile(test_count_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 222 | RegisterSendCodec(codec_l16, 32000, 512000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 223 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 224 | RegisterSendCodec(codec_l16, 32000, 512000, 640, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 225 | Run(channel_a_to_b_); |
| 226 | outfile_b_.Close(); |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 227 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 228 | test_count_++; |
| 229 | OpenOutFile(test_count_); |
| 230 | char codec_pcma[] = "PCMA"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 231 | RegisterSendCodec(codec_pcma, 8000, 64000, 80, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 232 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 233 | RegisterSendCodec(codec_pcma, 8000, 64000, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 234 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 235 | RegisterSendCodec(codec_pcma, 8000, 64000, 240, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 236 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 237 | RegisterSendCodec(codec_pcma, 8000, 64000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 238 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 239 | RegisterSendCodec(codec_pcma, 8000, 64000, 400, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 240 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 241 | RegisterSendCodec(codec_pcma, 8000, 64000, 480, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 242 | Run(channel_a_to_b_); |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 243 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 244 | char codec_pcmu[] = "PCMU"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 245 | RegisterSendCodec(codec_pcmu, 8000, 64000, 80, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 246 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 247 | RegisterSendCodec(codec_pcmu, 8000, 64000, 160, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 248 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 249 | RegisterSendCodec(codec_pcmu, 8000, 64000, 240, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 250 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 251 | RegisterSendCodec(codec_pcmu, 8000, 64000, 320, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 252 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 253 | RegisterSendCodec(codec_pcmu, 8000, 64000, 400, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 254 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 255 | RegisterSendCodec(codec_pcmu, 8000, 64000, 480, 0); |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 256 | Run(channel_a_to_b_); |
| 257 | outfile_b_.Close(); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 258 | #ifdef WEBRTC_CODEC_OPUS |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 259 | test_count_++; |
| 260 | OpenOutFile(test_count_); |
| 261 | char codec_opus[] = "OPUS"; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 262 | RegisterSendCodec(codec_opus, 48000, 6000, 480, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 263 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 264 | RegisterSendCodec(codec_opus, 48000, 20000, 480 * 2, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 265 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 266 | RegisterSendCodec(codec_opus, 48000, 32000, 480 * 4, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 267 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 268 | RegisterSendCodec(codec_opus, 48000, 48000, 480, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 269 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 270 | RegisterSendCodec(codec_opus, 48000, 64000, 480 * 4, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 271 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 272 | RegisterSendCodec(codec_opus, 48000, 96000, 480 * 6, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 273 | Run(channel_a_to_b_); |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 274 | RegisterSendCodec(codec_opus, 48000, 500000, 480 * 2, kVariableSize); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 275 | Run(channel_a_to_b_); |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 | [diff] [blame] | 276 | outfile_b_.Close(); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 | [diff] [blame] | 277 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Register Codec to use in the test |
| 281 | // |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 282 | // Input: codec_name - name to use when register the codec |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 283 | // sampling_freq_hz - sampling frequency in Herz |
| 284 | // rate - bitrate in bytes |
| 285 | // packet_size - packet size in samples |
| 286 | // extra_byte - if extra bytes needed compared to the bitrate |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 287 | // used when registering, can be an internal header |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 288 | // set to kVariableSize if the codec is a variable |
| 289 | // rate codec |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 290 | void TestAllCodecs::RegisterSendCodec(char* codec_name, |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 291 | int32_t sampling_freq_hz, |
| 292 | int rate, |
| 293 | int packet_size, |
| 294 | size_t extra_byte) { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 295 | // Store packet-size in samples, used to validate the received packet. |
| 296 | // If G.722, store half the size to compensate for the timestamp bug in the |
| 297 | // RFC for G.722. |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 298 | int clockrate_hz = sampling_freq_hz; |
| 299 | size_t num_channels = 1; |
| 300 | if (absl::EqualsIgnoreCase(codec_name, "G722")) { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 301 | packet_size_samples_ = packet_size / 2; |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 302 | clockrate_hz = sampling_freq_hz / 2; |
Fredrik Solenberg | 657b296 | 2018-12-05 09:30:25 | [diff] [blame] | 303 | } else if (absl::EqualsIgnoreCase(codec_name, "OPUS")) { |
| 304 | packet_size_samples_ = packet_size; |
| 305 | num_channels = 2; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 306 | } else { |
| 307 | packet_size_samples_ = packet_size; |
| 308 | } |
| 309 | |
| 310 | // Store the expected packet size in bytes, used to validate the received |
henrike@webrtc.org | 6ac22e6 | 2014-08-11 21:06:30 | [diff] [blame] | 311 | // packet. If variable rate codec (extra_byte == -1), set to -1. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 312 | if (extra_byte != kVariableSize) { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 313 | // Add 0.875 to always round up to a whole byte |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 314 | packet_size_bytes_ = |
| 315 | static_cast<size_t>(static_cast<float>(packet_size * rate) / |
| 316 | static_cast<float>(sampling_freq_hz * 8) + |
| 317 | 0.875) + |
| 318 | extra_byte; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 319 | } else { |
| 320 | // Packets will have a variable size. |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 321 | packet_size_bytes_ = kVariableSize; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 322 | } |
| 323 | |
Karl Wiberg | 133cff0 | 2018-07-06 13:40:14 | [diff] [blame] | 324 | auto factory = CreateBuiltinAudioEncoderFactory(); |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 325 | SdpAudioFormat format = {codec_name, clockrate_hz, num_channels}; |
Karl Wiberg | 133cff0 | 2018-07-06 13:40:14 | [diff] [blame] | 326 | format.parameters["ptime"] = rtc::ToString(rtc::CheckedDivExact( |
| 327 | packet_size, rtc::CheckedDivExact(sampling_freq_hz, 1000))); |
Danil Chapovalov | 1030eaa | 2024-06-18 09:20:40 | [diff] [blame] | 328 | acm_a_->SetEncoder(factory->Create(env_, format, {.payload_type = 17})); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 329 | } |
| 330 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 331 | void TestAllCodecs::Run(TestPack* channel) { |
| 332 | AudioFrame audio_frame; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 333 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 334 | int32_t out_freq_hz = outfile_b_.SamplingFrequency(); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 335 | size_t receive_size; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 336 | uint32_t timestamp_diff; |
| 337 | channel->reset_payload_size(); |
| 338 | int error_count = 0; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 339 | int counter = 0; |
Henrik Lundin | 4d68208 | 2015-12-10 15:24:39 | [diff] [blame] | 340 | // Set test length to 500 ms (50 blocks of 10 ms each). |
| 341 | infile_a_.SetNum10MsBlocksToRead(50); |
| 342 | // Fast-forward 1 second (100 blocks) since the file starts with silence. |
| 343 | infile_a_.FastForward(100); |
| 344 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 345 | while (!infile_a_.EndOfFile()) { |
| 346 | // Add 10 msec to ACM. |
| 347 | infile_a_.Read10MsData(audio_frame); |
| 348 | CHECK_ERROR(acm_a_->Add10MsData(audio_frame)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 349 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 350 | // Verify that the received packet size matches the settings. |
| 351 | receive_size = channel->payload_size(); |
| 352 | if (receive_size) { |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 | [diff] [blame] | 353 | if ((receive_size != packet_size_bytes_) && |
| 354 | (packet_size_bytes_ != kVariableSize)) { |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 355 | error_count++; |
| 356 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 357 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 358 | // Verify that the timestamp is updated with expected length. The counter |
| 359 | // is used to avoid problems when switching codec or frame size in the |
| 360 | // test. |
| 361 | timestamp_diff = channel->timestamp_diff(); |
henrike@webrtc.org | 6ac22e6 | 2014-08-11 21:06:30 | [diff] [blame] | 362 | if ((counter > 10) && |
| 363 | (static_cast<int>(timestamp_diff) != packet_size_samples_) && |
| 364 | (packet_size_samples_ > -1)) |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 365 | error_count++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 366 | } |
| 367 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 368 | // Run received side of ACM. |
henrik.lundin | d4ccb00 | 2016-05-17 19:21:55 | [diff] [blame] | 369 | bool muted; |
Henrik Lundin | 84f7569 | 2023-02-01 12:07:10 | [diff] [blame] | 370 | CHECK_ERROR(acm_b_->GetAudio(out_freq_hz, &audio_frame, &muted)); |
henrik.lundin | d4ccb00 | 2016-05-17 19:21:55 | [diff] [blame] | 371 | ASSERT_FALSE(muted); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 372 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 373 | // Write output speech to file. |
yujo | 36b1a5f | 2017-06-12 19:45:32 | [diff] [blame] | 374 | outfile_b_.Write10MsData(audio_frame.data(), |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 375 | audio_frame.samples_per_channel_); |
| 376 | |
| 377 | // Update loop counter |
| 378 | counter++; |
| 379 | } |
| 380 | |
| 381 | EXPECT_EQ(0, error_count); |
| 382 | |
| 383 | if (infile_a_.EndOfFile()) { |
| 384 | infile_a_.Rewind(); |
| 385 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 386 | } |
| 387 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 388 | void TestAllCodecs::OpenOutFile(int test_number) { |
| 389 | std::string filename = webrtc::test::OutputPath(); |
Jonas Olsson | 366a50c | 2018-09-06 11:41:30 | [diff] [blame] | 390 | rtc::StringBuilder test_number_str; |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 391 | test_number_str << test_number; |
| 392 | filename += "testallcodecs_out_"; |
| 393 | filename += test_number_str.str(); |
| 394 | filename += ".pcm"; |
tina.legrand@webrtc.org | ba46804 | 2012-08-17 10:38:28 | [diff] [blame] | 395 | outfile_b_.Open(filename, 32000, "wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 | [diff] [blame] | 396 | } |
| 397 | |
tina.legrand@webrtc.org | 50d5ca5 | 2012-06-18 13:35:52 | [diff] [blame] | 398 | } // namespace webrtc |