blob: 673c8fd8179e659eb7ca200194070d7cd0681a5a [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000011#include <errno.h>
henrik.lundine8a77e32016-06-22 06:34:03 -070012#include <inttypes.h>
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000013#include <limits.h> // For ULONG_MAX returned by strtoul.
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <stdio.h>
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000015#include <stdlib.h> // For strtoul.
Yves Gerey665174f2018-06-19 15:03:05 +020016#include <iostream>
17#include <memory>
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018#include <string>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "modules/audio_coding/neteq/include/neteq.h"
21#include "modules/audio_coding/neteq/tools/fake_decode_from_file.h"
22#include "modules/audio_coding/neteq/tools/input_audio_file.h"
23#include "modules/audio_coding/neteq/tools/neteq_delay_analyzer.h"
24#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
25#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
Yves Gerey665174f2018-06-19 15:03:05 +020026#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_coding/neteq/tools/neteq_test.h"
28#include "modules/audio_coding/neteq/tools/output_audio_file.h"
29#include "modules/audio_coding/neteq/tools/output_wav_file.h"
30#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/checks.h"
32#include "rtc_base/flags.h"
33#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020034#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000035
henrik.lundince5570e2016-05-24 06:14:57 -070036namespace webrtc {
37namespace test {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000038namespace {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +000039
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000040// Parses the input string for a valid SSRC (at the start of the string). If a
41// valid SSRC is found, it is written to the output variable |ssrc|, and true is
42// returned. Otherwise, false is returned.
43bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
44 if (str.empty())
henrik.lundin@webrtc.org91039532014-10-07 07:18:36 +000045 return true;
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000046 int base = 10;
47 // Look for "0x" or "0X" at the start and change base to 16 if found.
48 if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
49 base = 16;
50 errno = 0;
51 char* end_ptr;
52 unsigned long value = strtoul(str.c_str(), &end_ptr, base);
53 if (value == ULONG_MAX && errno == ERANGE)
54 return false; // Value out of range for unsigned long.
55 if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)
56 return false; // Value out of range for uint32_t.
57 if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
58 return false; // Part of the string was not parsed.
59 *ssrc = static_cast<uint32_t>(value);
60 return true;
61}
62
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000063// Flag validators.
oprypin6e09d872017-08-31 03:21:39 -070064bool ValidatePayloadType(int value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000065 if (value >= 0 && value <= 127) // Value is ok.
66 return true;
oprypin6e09d872017-08-31 03:21:39 -070067 printf("Payload type must be between 0 and 127, not %d\n",
68 static_cast<int>(value));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 return false;
70}
71
oprypin6e09d872017-08-31 03:21:39 -070072bool ValidateSsrcValue(const std::string& str) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000073 uint32_t dummy_ssrc;
Yves Gerey665174f2018-06-19 15:03:05 +020074 if (ParseSsrc(str, &dummy_ssrc)) // Value is ok.
oprypin6e09d872017-08-31 03:21:39 -070075 return true;
76 printf("Invalid SSRC: %s\n", str.c_str());
77 return false;
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000078}
79
oprypin6e09d872017-08-31 03:21:39 -070080static bool ValidateExtensionId(int value) {
henrik.lundin8a6a6002016-08-25 00:46:36 -070081 if (value > 0 && value <= 255) // Value is ok.
82 return true;
oprypin6e09d872017-08-31 03:21:39 -070083 printf("Extension ID must be between 1 and 255, not %d\n",
84 static_cast<int>(value));
henrik.lundin8a6a6002016-08-25 00:46:36 -070085 return false;
86}
87
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088// Define command line flags.
oprypin6e09d872017-08-31 03:21:39 -070089DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
90DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
91DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
92DEFINE_int(isac, 103, "RTP payload type for iSAC");
93DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
94DEFINE_int(opus, 111, "RTP payload type for Opus");
95DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
96DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
97DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
98DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
99DEFINE_int(g722, 9, "RTP payload type for G.722");
100DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
101DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
102DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
103DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
104DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
105DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
106DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
107DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
108DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
Yves Gerey665174f2018-06-19 15:03:05 +0200109DEFINE_bool(codec_map,
110 false,
111 "Prints the mapping between RTP payload type and "
112 "codec");
113DEFINE_string(replacement_audio_file,
114 "",
115 "A PCM file that will be used to populate "
116 "dummy"
117 " RTP packets");
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000118DEFINE_string(ssrc,
119 "",
120 "Only use packets with this SSRC (decimal or hex, the latter "
121 "starting with 0x)");
oprypin6e09d872017-08-31 03:21:39 -0700122DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
123DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
124DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200125DEFINE_int(video_content_type, 7, "Extension ID for video content type");
126DEFINE_int(video_timing, 8, "Extension ID for video timing");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200127DEFINE_bool(matlabplot,
128 false,
129 "Generates a matlab script for plotting the delay profile");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100130DEFINE_bool(pythonplot,
131 false,
132 "Generates a python script for plotting the delay profile");
oprypin6e09d872017-08-31 03:21:39 -0700133DEFINE_bool(help, false, "Prints this message");
Alex Narest7ff6ca52018-02-07 18:46:33 +0100134DEFINE_bool(concealment_events, false, "Prints concealment events");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000135
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000136// Maps a codec type to a printable name string.
henrik.lundince5570e2016-05-24 06:14:57 -0700137std::string CodecName(NetEqDecoder codec) {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000138 switch (codec) {
henrik.lundince5570e2016-05-24 06:14:57 -0700139 case NetEqDecoder::kDecoderPCMu:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000140 return "PCM-u";
henrik.lundince5570e2016-05-24 06:14:57 -0700141 case NetEqDecoder::kDecoderPCMa:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000142 return "PCM-a";
henrik.lundince5570e2016-05-24 06:14:57 -0700143 case NetEqDecoder::kDecoderILBC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000144 return "iLBC";
henrik.lundince5570e2016-05-24 06:14:57 -0700145 case NetEqDecoder::kDecoderISAC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000146 return "iSAC";
henrik.lundince5570e2016-05-24 06:14:57 -0700147 case NetEqDecoder::kDecoderISACswb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000148 return "iSAC-swb (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700149 case NetEqDecoder::kDecoderOpus:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000150 return "Opus";
henrik.lundince5570e2016-05-24 06:14:57 -0700151 case NetEqDecoder::kDecoderPCM16B:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000152 return "PCM16b-nb (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700153 case NetEqDecoder::kDecoderPCM16Bwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000154 return "PCM16b-wb (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700155 case NetEqDecoder::kDecoderPCM16Bswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000156 return "PCM16b-swb32 (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700157 case NetEqDecoder::kDecoderPCM16Bswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000158 return "PCM16b-swb48 (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700159 case NetEqDecoder::kDecoderG722:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000160 return "G.722";
henrik.lundince5570e2016-05-24 06:14:57 -0700161 case NetEqDecoder::kDecoderRED:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000162 return "redundant audio (RED)";
henrik.lundince5570e2016-05-24 06:14:57 -0700163 case NetEqDecoder::kDecoderAVT:
solenberg2779bab2016-11-17 04:45:19 -0800164 return "AVT/DTMF (8 kHz)";
165 case NetEqDecoder::kDecoderAVT16kHz:
166 return "AVT/DTMF (16 kHz)";
167 case NetEqDecoder::kDecoderAVT32kHz:
168 return "AVT/DTMF (32 kHz)";
169 case NetEqDecoder::kDecoderAVT48kHz:
170 return "AVT/DTMF (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700171 case NetEqDecoder::kDecoderCNGnb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000172 return "comfort noise (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700173 case NetEqDecoder::kDecoderCNGwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000174 return "comfort noise (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700175 case NetEqDecoder::kDecoderCNGswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000176 return "comfort noise (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700177 case NetEqDecoder::kDecoderCNGswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000178 return "comfort noise (48 kHz)";
179 default:
henrik.lundine8a77e32016-06-22 06:34:03 -0700180 FATAL();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000181 return "undefined";
182 }
183}
184
oprypin6e09d872017-08-31 03:21:39 -0700185void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000186 std::cout << CodecName(codec) << ": " << flag << std::endl;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000187}
188
189void PrintCodecMapping() {
oprypin6e09d872017-08-31 03:21:39 -0700190 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
191 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
192 PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
193 PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
194 PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
195 PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
196 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
197 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
henrik.lundince5570e2016-05-24 06:14:57 -0700198 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
oprypin6e09d872017-08-31 03:21:39 -0700199 FLAG_pcm16b_swb32);
henrik.lundince5570e2016-05-24 06:14:57 -0700200 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
oprypin6e09d872017-08-31 03:21:39 -0700201 FLAG_pcm16b_swb48);
202 PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
203 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
204 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
205 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
206 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
207 PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
208 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
209 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
210 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
211 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000212}
213
Danil Chapovalovb6021232018-06-19 13:26:36 +0200214absl::optional<int> CodecSampleRate(uint8_t payload_type) {
oprypin6e09d872017-08-31 03:21:39 -0700215 if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
216 payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
217 payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100218 return 8000;
oprypin6e09d872017-08-31 03:21:39 -0700219 if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
220 payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
221 payload_type == FLAG_avt_16)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100222 return 16000;
oprypin6e09d872017-08-31 03:21:39 -0700223 if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
224 payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100225 return 32000;
oprypin6e09d872017-08-31 03:21:39 -0700226 if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
227 payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100228 return 48000;
oprypin6e09d872017-08-31 03:21:39 -0700229 if (payload_type == FLAG_red)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100230 return 0;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200231 return absl::nullopt;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000232}
233
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200234// A callback class which prints whenver the inserted packet stream changes
235// the SSRC.
236class SsrcSwitchDetector : public NetEqPostInsertPacket {
237 public:
238 // Takes a pointer to another callback object, which will be invoked after
239 // this object finishes. This does not transfer ownership, and null is a
240 // valid value.
Henrik Lundina2af0002017-06-20 16:54:39 +0200241 explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200242 : other_callback_(other_callback) {}
243
Henrik Lundina2af0002017-06-20 16:54:39 +0200244 void AfterInsertPacket(const NetEqInput::PacketData& packet,
245 NetEq* neteq) override {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200246 if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
247 std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
Yves Gerey665174f2018-06-19 15:03:05 +0200248 << " to 0x" << std::hex << packet.header.ssrc << std::dec
249 << " (payload type "
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200250 << static_cast<int>(packet.header.payloadType) << ")"
251 << std::endl;
252 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100253 last_ssrc_ = packet.header.ssrc;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200254 if (other_callback_) {
255 other_callback_->AfterInsertPacket(packet, neteq);
256 }
257 }
258
259 private:
260 NetEqPostInsertPacket* other_callback_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200261 absl::optional<uint32_t> last_ssrc_;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200262};
263
henrik.lundin303d3e12016-05-26 05:56:03 -0700264int RunTest(int argc, char* argv[]) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000265 std::string program_name = argv[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200266 std::string usage =
267 "Tool for decoding an RTP dump file using NetEq.\n"
268 "Run " +
269 program_name +
270 " --help for usage.\n"
271 "Example usage:\n" +
272 program_name + " input.rtp output.{pcm, wav}\n";
oprypin6e09d872017-08-31 03:21:39 -0700273 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
274 return 1;
275 }
276 if (FLAG_help) {
277 std::cout << usage;
278 rtc::FlagList::Print(nullptr, false);
279 return 0;
280 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000281
oprypin6e09d872017-08-31 03:21:39 -0700282 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000283 PrintCodecMapping();
284 }
285
286 if (argc != 3) {
oprypin6e09d872017-08-31 03:21:39 -0700287 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288 // We have already printed the codec map. Just end the program.
289 return 0;
290 }
291 // Print usage information.
oprypin6e09d872017-08-31 03:21:39 -0700292 std::cout << usage;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000293 return 0;
294 }
oprypin6e09d872017-08-31 03:21:39 -0700295 RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
296 RTC_CHECK(ValidatePayloadType(FLAG_pcma));
297 RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
298 RTC_CHECK(ValidatePayloadType(FLAG_isac));
299 RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
300 RTC_CHECK(ValidatePayloadType(FLAG_opus));
301 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
302 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
303 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
304 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
305 RTC_CHECK(ValidatePayloadType(FLAG_g722));
306 RTC_CHECK(ValidatePayloadType(FLAG_avt));
307 RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
308 RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
309 RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
310 RTC_CHECK(ValidatePayloadType(FLAG_red));
311 RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
312 RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
313 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
314 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
315 RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
316 RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
317 RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
318 RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200319 RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
320 RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321
henrik.lundin8a6a6002016-08-25 00:46:36 -0700322 // Gather RTP header extensions in a map.
323 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
oprypin6e09d872017-08-31 03:21:39 -0700324 {FLAG_audio_level, kRtpExtensionAudioLevel},
325 {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200326 {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
327 {FLAG_video_content_type, kRtpExtensionVideoContentType},
328 {FLAG_video_timing, kRtpExtensionVideoTiming}};
henrik.lundin8a6a6002016-08-25 00:46:36 -0700329
henrik.lundine8a77e32016-06-22 06:34:03 -0700330 const std::string input_file_name = argv[1];
331 std::unique_ptr<NetEqInput> input;
332 if (RtpFileSource::ValidRtpDump(input_file_name) ||
333 RtpFileSource::ValidPcap(input_file_name)) {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700334 input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700335 } else {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700336 input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700337 }
338
henrik.lundine8a77e32016-06-22 06:34:03 -0700339 std::cout << "Input file: " << input_file_name << std::endl;
340 RTC_CHECK(input) << "Cannot open input file";
341 RTC_CHECK(!input->ended()) << "Input file is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000342
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000343 // Check if an SSRC value was provided.
oprypin6e09d872017-08-31 03:21:39 -0700344 if (strlen(FLAG_ssrc) > 0) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000345 uint32_t ssrc;
oprypin6e09d872017-08-31 03:21:39 -0700346 RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
Minyue Lib563f3d2018-05-11 16:49:38 +0200347 static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
ivoccaa5f4b2015-09-08 03:28:46 -0700348 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000349
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000350 // Check the sample rate.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200351 absl::optional<int> sample_rate_hz;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200352 std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200353 while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200354 RTC_DCHECK(first_rtp_header);
355 sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
356 if (sample_rate_hz) {
357 std::cout << "Found valid packet with payload type "
358 << static_cast<int>(first_rtp_header->payloadType)
359 << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
360 << std::dec << std::endl;
361 break;
362 }
363 // Discard this packet and move to the next. Keep track of discarded payload
364 // types and SSRCs.
365 discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
366 first_rtp_header->ssrc);
367 input->PopPacket();
368 }
369 if (!discarded_pt_and_ssrc.empty()) {
370 std::cout << "Discarded initial packets with the following payload types "
371 "and SSRCs:"
372 << std::endl;
373 for (const auto& d : discarded_pt_and_ssrc) {
374 std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
375 << static_cast<int>(d.second) << std::dec << std::endl;
376 }
377 }
378 if (!sample_rate_hz) {
379 std::cout << "Cannot find any packets with known payload types"
380 << std::endl;
381 RTC_NOTREACHED();
382 }
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000383
384 // Open the output file now that we know the sample rate. (Rate is only needed
385 // for wav files.)
henrik.lundine8a77e32016-06-22 06:34:03 -0700386 const std::string output_file_name = argv[2];
henrik.lundince5570e2016-05-24 06:14:57 -0700387 std::unique_ptr<AudioSink> output;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000388 if (output_file_name.size() >= 4 &&
389 output_file_name.substr(output_file_name.size() - 4) == ".wav") {
390 // Open a wav file.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200391 output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000392 } else {
393 // Open a pcm file.
henrik.lundince5570e2016-05-24 06:14:57 -0700394 output.reset(new OutputAudioFile(output_file_name));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000395 }
396
henrik.lundine8a77e32016-06-22 06:34:03 -0700397 std::cout << "Output file: " << output_file_name << std::endl;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000398
henrik.lundine8a77e32016-06-22 06:34:03 -0700399 NetEqTest::DecoderMap codecs = {
oprypin6e09d872017-08-31 03:21:39 -0700400 {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
401 {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
402 {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
403 {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
404 {FLAG_isac_swb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700405 std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
oprypin6e09d872017-08-31 03:21:39 -0700406 {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
407 {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
408 {FLAG_pcm16b_wb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700409 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
oprypin6e09d872017-08-31 03:21:39 -0700410 {FLAG_pcm16b_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700411 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700412 {FLAG_pcm16b_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700413 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
oprypin6e09d872017-08-31 03:21:39 -0700414 {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
415 {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
416 {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
Yves Gerey665174f2018-06-19 15:03:05 +0200417 {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
418 {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
oprypin6e09d872017-08-31 03:21:39 -0700419 {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
420 {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
421 {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
422 {FLAG_cn_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700423 std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700424 {FLAG_cn_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700425 std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}};
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000426
henrik.lundine8a77e32016-06-22 06:34:03 -0700427 // Check if a replacement audio file was provided.
428 std::unique_ptr<AudioDecoder> replacement_decoder;
429 NetEqTest::ExtDecoderMap ext_codecs;
oprypin6e09d872017-08-31 03:21:39 -0700430 if (strlen(FLAG_replacement_audio_file) > 0) {
henrik.lundine8a77e32016-06-22 06:34:03 -0700431 // Find largest unused payload type.
432 int replacement_pt = 127;
433 while (!(codecs.find(replacement_pt) == codecs.end() &&
434 ext_codecs.find(replacement_pt) == ext_codecs.end())) {
435 --replacement_pt;
436 RTC_CHECK_GE(replacement_pt, 0);
437 }
438
439 auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
440 std::set<uint8_t> b;
441 for (auto& x : a) {
442 b.insert(static_cast<uint8_t>(x));
443 }
444 return b;
445 };
446
447 std::set<uint8_t> cn_types = std_set_int32_to_uint8(
oprypin6e09d872017-08-31 03:21:39 -0700448 {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
Yves Gerey665174f2018-06-19 15:03:05 +0200449 std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
450 {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700451 input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
452 cn_types, forbidden_types));
453
454 replacement_decoder.reset(new FakeDecodeFromFile(
455 std::unique_ptr<InputAudioFile>(
oprypin6e09d872017-08-31 03:21:39 -0700456 new InputAudioFile(FLAG_replacement_audio_file)),
henrik.lundine8a77e32016-06-22 06:34:03 -0700457 48000, false));
458 NetEqTest::ExternalDecoderInfo ext_dec_info = {
459 replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
460 "replacement codec"};
461 ext_codecs[replacement_pt] = ext_dec_info;
462 }
463
henrik.lundin02739d92017-05-04 06:09:06 -0700464 NetEqTest::Callbacks callbacks;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200465 std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100466 if (FLAG_matlabplot || FLAG_pythonplot) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200467 delay_analyzer.reset(new NetEqDelayAnalyzer);
468 }
469
470 SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
471 callbacks.post_insert_packet = &ssrc_switch_detector;
Minyue Li2b415da2018-04-16 14:33:53 +0200472 NetEqStatsGetter stats_getter(std::move(delay_analyzer));
Henrik Lundina2af0002017-06-20 16:54:39 +0200473 callbacks.get_audio_callback = &stats_getter;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000474 NetEq::Config config;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200475 config.sample_rate_hz = *sample_rate_hz;
henrik.lundine8a77e32016-06-22 06:34:03 -0700476 NetEqTest test(config, codecs, ext_codecs, std::move(input),
henrik.lundin02739d92017-05-04 06:09:06 -0700477 std::move(output), callbacks);
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000478
henrik.lundine8a77e32016-06-22 06:34:03 -0700479 int64_t test_duration_ms = test.Run();
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000480
oprypin6e09d872017-08-31 03:21:39 -0700481 if (FLAG_matlabplot) {
henrik.lundinf09c9042017-08-29 09:14:08 -0700482 auto matlab_script_name = output_file_name;
483 std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
484 '_');
485 std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200486 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200487 stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
488 ".m");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200489 }
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100490 if (FLAG_pythonplot) {
491 auto python_script_name = output_file_name;
492 std::replace(python_script_name.begin(), python_script_name.end(), '.',
493 '_');
494 std::cout << "Creating Python plot script " << python_script_name + ".py"
495 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200496 stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
497 ".py");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100498 }
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200499
henrik.lundine8a77e32016-06-22 06:34:03 -0700500 printf("Simulation statistics:\n");
501 printf(" output duration: %" PRId64 " ms\n", test_duration_ms);
Henrik Lundina2af0002017-06-20 16:54:39 +0200502 auto stats = stats_getter.AverageStats();
503 printf(" packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200504 printf(" expand_rate: %f %%\n", 100.0 * stats.expand_rate);
505 printf(" speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
506 printf(" preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
507 printf(" accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
henrik.lundine8a77e32016-06-22 06:34:03 -0700508 printf(" secondary_decoded_rate: %f %%\n",
Henrik Lundina2af0002017-06-20 16:54:39 +0200509 100.0 * stats.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200510 printf(" secondary_discarded_rate: %f %%\n",
511 100.0 * stats.secondary_discarded_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200512 printf(" clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
513 printf(" mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
514 printf(" median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
515 printf(" min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
516 printf(" max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
Henrik Lundin156af4a2017-11-17 16:46:18 +0100517 printf(" current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
518 printf(" preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
Alex Narest7ff6ca52018-02-07 18:46:33 +0100519 if (FLAG_concealment_events) {
Minyue Li2b415da2018-04-16 14:33:53 +0200520 std::cout << " concealment_events_ms:" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100521 for (auto concealment_event : stats_getter.concealment_events())
Minyue Li2b415da2018-04-16 14:33:53 +0200522 std::cout << concealment_event.ToString() << std::endl;
523 std::cout << " end of concealment_events_ms" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100524 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000525 return 0;
526}
henrik.lundince5570e2016-05-24 06:14:57 -0700527
henrik.lundin303d3e12016-05-26 05:56:03 -0700528} // namespace
henrik.lundince5570e2016-05-24 06:14:57 -0700529} // namespace test
530} // namespace webrtc
henrik.lundin303d3e12016-05-26 05:56:03 -0700531
532int main(int argc, char* argv[]) {
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500533 return webrtc::test::RunTest(argc, argv);
henrik.lundin303d3e12016-05-26 05:56:03 -0700534}