blob: 7f1263cbc6671d15ecb9a1cbfc92bd4b5e792e6d [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"
Henrik Lundin9f2e6242018-07-02 14:05:37 +020024#include "modules/audio_coding/neteq/tools/neteq_event_log_input.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "modules/audio_coding/neteq/tools/neteq_packet_source_input.h"
26#include "modules/audio_coding/neteq/tools/neteq_replacement_input.h"
Yves Gerey665174f2018-06-19 15:03:05 +020027#include "modules/audio_coding/neteq/tools/neteq_stats_getter.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "modules/audio_coding/neteq/tools/neteq_test.h"
29#include "modules/audio_coding/neteq/tools/output_audio_file.h"
30#include "modules/audio_coding/neteq/tools/output_wav_file.h"
31#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/checks.h"
33#include "rtc_base/flags.h"
34#include "test/testsupport/fileutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020035#include "typedefs.h" // NOLINT(build/include)
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000036
henrik.lundince5570e2016-05-24 06:14:57 -070037namespace webrtc {
38namespace test {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000039namespace {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +000040
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000041// Parses the input string for a valid SSRC (at the start of the string). If a
42// valid SSRC is found, it is written to the output variable |ssrc|, and true is
43// returned. Otherwise, false is returned.
44bool ParseSsrc(const std::string& str, uint32_t* ssrc) {
45 if (str.empty())
henrik.lundin@webrtc.org91039532014-10-07 07:18:36 +000046 return true;
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000047 int base = 10;
48 // Look for "0x" or "0X" at the start and change base to 16 if found.
49 if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0))
50 base = 16;
51 errno = 0;
52 char* end_ptr;
53 unsigned long value = strtoul(str.c_str(), &end_ptr, base);
54 if (value == ULONG_MAX && errno == ERANGE)
55 return false; // Value out of range for unsigned long.
56 if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF)
57 return false; // Value out of range for uint32_t.
58 if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length()))
59 return false; // Part of the string was not parsed.
60 *ssrc = static_cast<uint32_t>(value);
61 return true;
62}
63
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000064// Flag validators.
oprypin6e09d872017-08-31 03:21:39 -070065bool ValidatePayloadType(int value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066 if (value >= 0 && value <= 127) // Value is ok.
67 return true;
oprypin6e09d872017-08-31 03:21:39 -070068 printf("Payload type must be between 0 and 127, not %d\n",
69 static_cast<int>(value));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070 return false;
71}
72
oprypin6e09d872017-08-31 03:21:39 -070073bool ValidateSsrcValue(const std::string& str) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000074 uint32_t dummy_ssrc;
Yves Gerey665174f2018-06-19 15:03:05 +020075 if (ParseSsrc(str, &dummy_ssrc)) // Value is ok.
oprypin6e09d872017-08-31 03:21:39 -070076 return true;
77 printf("Invalid SSRC: %s\n", str.c_str());
78 return false;
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000079}
80
oprypin6e09d872017-08-31 03:21:39 -070081static bool ValidateExtensionId(int value) {
henrik.lundin8a6a6002016-08-25 00:46:36 -070082 if (value > 0 && value <= 255) // Value is ok.
83 return true;
oprypin6e09d872017-08-31 03:21:39 -070084 printf("Extension ID must be between 1 and 255, not %d\n",
85 static_cast<int>(value));
henrik.lundin8a6a6002016-08-25 00:46:36 -070086 return false;
87}
88
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089// Define command line flags.
oprypin6e09d872017-08-31 03:21:39 -070090DEFINE_int(pcmu, 0, "RTP payload type for PCM-u");
91DEFINE_int(pcma, 8, "RTP payload type for PCM-a");
92DEFINE_int(ilbc, 102, "RTP payload type for iLBC");
93DEFINE_int(isac, 103, "RTP payload type for iSAC");
94DEFINE_int(isac_swb, 104, "RTP payload type for iSAC-swb (32 kHz)");
95DEFINE_int(opus, 111, "RTP payload type for Opus");
96DEFINE_int(pcm16b, 93, "RTP payload type for PCM16b-nb (8 kHz)");
97DEFINE_int(pcm16b_wb, 94, "RTP payload type for PCM16b-wb (16 kHz)");
98DEFINE_int(pcm16b_swb32, 95, "RTP payload type for PCM16b-swb32 (32 kHz)");
99DEFINE_int(pcm16b_swb48, 96, "RTP payload type for PCM16b-swb48 (48 kHz)");
100DEFINE_int(g722, 9, "RTP payload type for G.722");
101DEFINE_int(avt, 106, "RTP payload type for AVT/DTMF (8 kHz)");
102DEFINE_int(avt_16, 114, "RTP payload type for AVT/DTMF (16 kHz)");
103DEFINE_int(avt_32, 115, "RTP payload type for AVT/DTMF (32 kHz)");
104DEFINE_int(avt_48, 116, "RTP payload type for AVT/DTMF (48 kHz)");
105DEFINE_int(red, 117, "RTP payload type for redundant audio (RED)");
106DEFINE_int(cn_nb, 13, "RTP payload type for comfort noise (8 kHz)");
107DEFINE_int(cn_wb, 98, "RTP payload type for comfort noise (16 kHz)");
108DEFINE_int(cn_swb32, 99, "RTP payload type for comfort noise (32 kHz)");
109DEFINE_int(cn_swb48, 100, "RTP payload type for comfort noise (48 kHz)");
Yves Gerey665174f2018-06-19 15:03:05 +0200110DEFINE_bool(codec_map,
111 false,
112 "Prints the mapping between RTP payload type and "
113 "codec");
114DEFINE_string(replacement_audio_file,
115 "",
116 "A PCM file that will be used to populate "
117 "dummy"
118 " RTP packets");
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000119DEFINE_string(ssrc,
120 "",
121 "Only use packets with this SSRC (decimal or hex, the latter "
122 "starting with 0x)");
oprypin6e09d872017-08-31 03:21:39 -0700123DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
124DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
125DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200126DEFINE_int(video_content_type, 7, "Extension ID for video content type");
127DEFINE_int(video_timing, 8, "Extension ID for video timing");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200128DEFINE_bool(matlabplot,
129 false,
130 "Generates a matlab script for plotting the delay profile");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100131DEFINE_bool(pythonplot,
132 false,
133 "Generates a python script for plotting the delay profile");
oprypin6e09d872017-08-31 03:21:39 -0700134DEFINE_bool(help, false, "Prints this message");
Alex Narest7ff6ca52018-02-07 18:46:33 +0100135DEFINE_bool(concealment_events, false, "Prints concealment events");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000136
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000137// Maps a codec type to a printable name string.
henrik.lundince5570e2016-05-24 06:14:57 -0700138std::string CodecName(NetEqDecoder codec) {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000139 switch (codec) {
henrik.lundince5570e2016-05-24 06:14:57 -0700140 case NetEqDecoder::kDecoderPCMu:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000141 return "PCM-u";
henrik.lundince5570e2016-05-24 06:14:57 -0700142 case NetEqDecoder::kDecoderPCMa:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000143 return "PCM-a";
henrik.lundince5570e2016-05-24 06:14:57 -0700144 case NetEqDecoder::kDecoderILBC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000145 return "iLBC";
henrik.lundince5570e2016-05-24 06:14:57 -0700146 case NetEqDecoder::kDecoderISAC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000147 return "iSAC";
henrik.lundince5570e2016-05-24 06:14:57 -0700148 case NetEqDecoder::kDecoderISACswb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000149 return "iSAC-swb (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700150 case NetEqDecoder::kDecoderOpus:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000151 return "Opus";
henrik.lundince5570e2016-05-24 06:14:57 -0700152 case NetEqDecoder::kDecoderPCM16B:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000153 return "PCM16b-nb (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700154 case NetEqDecoder::kDecoderPCM16Bwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000155 return "PCM16b-wb (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700156 case NetEqDecoder::kDecoderPCM16Bswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000157 return "PCM16b-swb32 (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700158 case NetEqDecoder::kDecoderPCM16Bswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000159 return "PCM16b-swb48 (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700160 case NetEqDecoder::kDecoderG722:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000161 return "G.722";
henrik.lundince5570e2016-05-24 06:14:57 -0700162 case NetEqDecoder::kDecoderRED:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000163 return "redundant audio (RED)";
henrik.lundince5570e2016-05-24 06:14:57 -0700164 case NetEqDecoder::kDecoderAVT:
solenberg2779bab2016-11-17 04:45:19 -0800165 return "AVT/DTMF (8 kHz)";
166 case NetEqDecoder::kDecoderAVT16kHz:
167 return "AVT/DTMF (16 kHz)";
168 case NetEqDecoder::kDecoderAVT32kHz:
169 return "AVT/DTMF (32 kHz)";
170 case NetEqDecoder::kDecoderAVT48kHz:
171 return "AVT/DTMF (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700172 case NetEqDecoder::kDecoderCNGnb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000173 return "comfort noise (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700174 case NetEqDecoder::kDecoderCNGwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000175 return "comfort noise (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700176 case NetEqDecoder::kDecoderCNGswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000177 return "comfort noise (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700178 case NetEqDecoder::kDecoderCNGswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000179 return "comfort noise (48 kHz)";
180 default:
henrik.lundine8a77e32016-06-22 06:34:03 -0700181 FATAL();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000182 return "undefined";
183 }
184}
185
oprypin6e09d872017-08-31 03:21:39 -0700186void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000187 std::cout << CodecName(codec) << ": " << flag << std::endl;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000188}
189
190void PrintCodecMapping() {
oprypin6e09d872017-08-31 03:21:39 -0700191 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
192 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
193 PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
194 PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
195 PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
196 PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
197 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
198 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
henrik.lundince5570e2016-05-24 06:14:57 -0700199 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
oprypin6e09d872017-08-31 03:21:39 -0700200 FLAG_pcm16b_swb32);
henrik.lundince5570e2016-05-24 06:14:57 -0700201 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
oprypin6e09d872017-08-31 03:21:39 -0700202 FLAG_pcm16b_swb48);
203 PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
204 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
205 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
206 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
207 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
208 PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
209 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
210 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
211 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
212 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000213}
214
Danil Chapovalovb6021232018-06-19 13:26:36 +0200215absl::optional<int> CodecSampleRate(uint8_t payload_type) {
oprypin6e09d872017-08-31 03:21:39 -0700216 if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
217 payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
218 payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100219 return 8000;
oprypin6e09d872017-08-31 03:21:39 -0700220 if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
221 payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
222 payload_type == FLAG_avt_16)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100223 return 16000;
oprypin6e09d872017-08-31 03:21:39 -0700224 if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
225 payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100226 return 32000;
oprypin6e09d872017-08-31 03:21:39 -0700227 if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
228 payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100229 return 48000;
oprypin6e09d872017-08-31 03:21:39 -0700230 if (payload_type == FLAG_red)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100231 return 0;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200232 return absl::nullopt;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000233}
234
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200235// A callback class which prints whenver the inserted packet stream changes
236// the SSRC.
237class SsrcSwitchDetector : public NetEqPostInsertPacket {
238 public:
239 // Takes a pointer to another callback object, which will be invoked after
240 // this object finishes. This does not transfer ownership, and null is a
241 // valid value.
Henrik Lundina2af0002017-06-20 16:54:39 +0200242 explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200243 : other_callback_(other_callback) {}
244
Henrik Lundina2af0002017-06-20 16:54:39 +0200245 void AfterInsertPacket(const NetEqInput::PacketData& packet,
246 NetEq* neteq) override {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200247 if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
248 std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
Yves Gerey665174f2018-06-19 15:03:05 +0200249 << " to 0x" << std::hex << packet.header.ssrc << std::dec
250 << " (payload type "
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200251 << static_cast<int>(packet.header.payloadType) << ")"
252 << std::endl;
253 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100254 last_ssrc_ = packet.header.ssrc;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200255 if (other_callback_) {
256 other_callback_->AfterInsertPacket(packet, neteq);
257 }
258 }
259
260 private:
261 NetEqPostInsertPacket* other_callback_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200262 absl::optional<uint32_t> last_ssrc_;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200263};
264
henrik.lundin303d3e12016-05-26 05:56:03 -0700265int RunTest(int argc, char* argv[]) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000266 std::string program_name = argv[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200267 std::string usage =
268 "Tool for decoding an RTP dump file using NetEq.\n"
269 "Run " +
270 program_name +
271 " --help for usage.\n"
272 "Example usage:\n" +
273 program_name + " input.rtp output.{pcm, wav}\n";
oprypin6e09d872017-08-31 03:21:39 -0700274 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
275 return 1;
276 }
277 if (FLAG_help) {
278 std::cout << usage;
279 rtc::FlagList::Print(nullptr, false);
280 return 0;
281 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000282
oprypin6e09d872017-08-31 03:21:39 -0700283 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000284 PrintCodecMapping();
285 }
286
287 if (argc != 3) {
oprypin6e09d872017-08-31 03:21:39 -0700288 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000289 // We have already printed the codec map. Just end the program.
290 return 0;
291 }
292 // Print usage information.
oprypin6e09d872017-08-31 03:21:39 -0700293 std::cout << usage;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000294 return 0;
295 }
oprypin6e09d872017-08-31 03:21:39 -0700296 RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
297 RTC_CHECK(ValidatePayloadType(FLAG_pcma));
298 RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
299 RTC_CHECK(ValidatePayloadType(FLAG_isac));
300 RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
301 RTC_CHECK(ValidatePayloadType(FLAG_opus));
302 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
303 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
304 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
305 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
306 RTC_CHECK(ValidatePayloadType(FLAG_g722));
307 RTC_CHECK(ValidatePayloadType(FLAG_avt));
308 RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
309 RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
310 RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
311 RTC_CHECK(ValidatePayloadType(FLAG_red));
312 RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
313 RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
314 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
315 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
316 RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
317 RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
318 RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
319 RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200320 RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
321 RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000322
henrik.lundin8a6a6002016-08-25 00:46:36 -0700323 // Gather RTP header extensions in a map.
324 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
oprypin6e09d872017-08-31 03:21:39 -0700325 {FLAG_audio_level, kRtpExtensionAudioLevel},
326 {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200327 {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
328 {FLAG_video_content_type, kRtpExtensionVideoContentType},
329 {FLAG_video_timing, kRtpExtensionVideoTiming}};
henrik.lundin8a6a6002016-08-25 00:46:36 -0700330
henrik.lundine8a77e32016-06-22 06:34:03 -0700331 const std::string input_file_name = argv[1];
332 std::unique_ptr<NetEqInput> input;
333 if (RtpFileSource::ValidRtpDump(input_file_name) ||
334 RtpFileSource::ValidPcap(input_file_name)) {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700335 input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700336 } else {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700337 input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700338 }
339
henrik.lundine8a77e32016-06-22 06:34:03 -0700340 std::cout << "Input file: " << input_file_name << std::endl;
341 RTC_CHECK(input) << "Cannot open input file";
342 RTC_CHECK(!input->ended()) << "Input file is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000343
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000344 // Check if an SSRC value was provided.
oprypin6e09d872017-08-31 03:21:39 -0700345 if (strlen(FLAG_ssrc) > 0) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000346 uint32_t ssrc;
oprypin6e09d872017-08-31 03:21:39 -0700347 RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
Minyue Lib563f3d2018-05-11 16:49:38 +0200348 static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
ivoccaa5f4b2015-09-08 03:28:46 -0700349 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000350
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000351 // Check the sample rate.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200352 absl::optional<int> sample_rate_hz;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200353 std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200354 while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200355 RTC_DCHECK(first_rtp_header);
356 sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
357 if (sample_rate_hz) {
358 std::cout << "Found valid packet with payload type "
359 << static_cast<int>(first_rtp_header->payloadType)
360 << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
361 << std::dec << std::endl;
362 break;
363 }
364 // Discard this packet and move to the next. Keep track of discarded payload
365 // types and SSRCs.
366 discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
367 first_rtp_header->ssrc);
368 input->PopPacket();
369 }
370 if (!discarded_pt_and_ssrc.empty()) {
371 std::cout << "Discarded initial packets with the following payload types "
372 "and SSRCs:"
373 << std::endl;
374 for (const auto& d : discarded_pt_and_ssrc) {
375 std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
376 << static_cast<int>(d.second) << std::dec << std::endl;
377 }
378 }
379 if (!sample_rate_hz) {
380 std::cout << "Cannot find any packets with known payload types"
381 << std::endl;
382 RTC_NOTREACHED();
383 }
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000384
385 // Open the output file now that we know the sample rate. (Rate is only needed
386 // for wav files.)
henrik.lundine8a77e32016-06-22 06:34:03 -0700387 const std::string output_file_name = argv[2];
henrik.lundince5570e2016-05-24 06:14:57 -0700388 std::unique_ptr<AudioSink> output;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000389 if (output_file_name.size() >= 4 &&
390 output_file_name.substr(output_file_name.size() - 4) == ".wav") {
391 // Open a wav file.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200392 output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000393 } else {
394 // Open a pcm file.
henrik.lundince5570e2016-05-24 06:14:57 -0700395 output.reset(new OutputAudioFile(output_file_name));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000396 }
397
henrik.lundine8a77e32016-06-22 06:34:03 -0700398 std::cout << "Output file: " << output_file_name << std::endl;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000399
henrik.lundine8a77e32016-06-22 06:34:03 -0700400 NetEqTest::DecoderMap codecs = {
oprypin6e09d872017-08-31 03:21:39 -0700401 {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
402 {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
403 {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
404 {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
405 {FLAG_isac_swb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700406 std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
oprypin6e09d872017-08-31 03:21:39 -0700407 {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
408 {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
409 {FLAG_pcm16b_wb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700410 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
oprypin6e09d872017-08-31 03:21:39 -0700411 {FLAG_pcm16b_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700412 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700413 {FLAG_pcm16b_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700414 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
oprypin6e09d872017-08-31 03:21:39 -0700415 {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
416 {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
417 {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
Yves Gerey665174f2018-06-19 15:03:05 +0200418 {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
419 {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
oprypin6e09d872017-08-31 03:21:39 -0700420 {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
421 {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
422 {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
423 {FLAG_cn_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700424 std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700425 {FLAG_cn_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700426 std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}};
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000427
henrik.lundine8a77e32016-06-22 06:34:03 -0700428 // Check if a replacement audio file was provided.
429 std::unique_ptr<AudioDecoder> replacement_decoder;
430 NetEqTest::ExtDecoderMap ext_codecs;
oprypin6e09d872017-08-31 03:21:39 -0700431 if (strlen(FLAG_replacement_audio_file) > 0) {
henrik.lundine8a77e32016-06-22 06:34:03 -0700432 // Find largest unused payload type.
433 int replacement_pt = 127;
434 while (!(codecs.find(replacement_pt) == codecs.end() &&
435 ext_codecs.find(replacement_pt) == ext_codecs.end())) {
436 --replacement_pt;
437 RTC_CHECK_GE(replacement_pt, 0);
438 }
439
440 auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
441 std::set<uint8_t> b;
442 for (auto& x : a) {
443 b.insert(static_cast<uint8_t>(x));
444 }
445 return b;
446 };
447
448 std::set<uint8_t> cn_types = std_set_int32_to_uint8(
oprypin6e09d872017-08-31 03:21:39 -0700449 {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
Yves Gerey665174f2018-06-19 15:03:05 +0200450 std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
451 {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700452 input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
453 cn_types, forbidden_types));
454
455 replacement_decoder.reset(new FakeDecodeFromFile(
456 std::unique_ptr<InputAudioFile>(
oprypin6e09d872017-08-31 03:21:39 -0700457 new InputAudioFile(FLAG_replacement_audio_file)),
henrik.lundine8a77e32016-06-22 06:34:03 -0700458 48000, false));
459 NetEqTest::ExternalDecoderInfo ext_dec_info = {
460 replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
461 "replacement codec"};
462 ext_codecs[replacement_pt] = ext_dec_info;
463 }
464
henrik.lundin02739d92017-05-04 06:09:06 -0700465 NetEqTest::Callbacks callbacks;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200466 std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100467 if (FLAG_matlabplot || FLAG_pythonplot) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200468 delay_analyzer.reset(new NetEqDelayAnalyzer);
469 }
470
471 SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
472 callbacks.post_insert_packet = &ssrc_switch_detector;
Minyue Li2b415da2018-04-16 14:33:53 +0200473 NetEqStatsGetter stats_getter(std::move(delay_analyzer));
Henrik Lundina2af0002017-06-20 16:54:39 +0200474 callbacks.get_audio_callback = &stats_getter;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000475 NetEq::Config config;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200476 config.sample_rate_hz = *sample_rate_hz;
henrik.lundine8a77e32016-06-22 06:34:03 -0700477 NetEqTest test(config, codecs, ext_codecs, std::move(input),
henrik.lundin02739d92017-05-04 06:09:06 -0700478 std::move(output), callbacks);
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000479
henrik.lundine8a77e32016-06-22 06:34:03 -0700480 int64_t test_duration_ms = test.Run();
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000481
oprypin6e09d872017-08-31 03:21:39 -0700482 if (FLAG_matlabplot) {
henrik.lundinf09c9042017-08-29 09:14:08 -0700483 auto matlab_script_name = output_file_name;
484 std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
485 '_');
486 std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200487 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200488 stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
489 ".m");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200490 }
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100491 if (FLAG_pythonplot) {
492 auto python_script_name = output_file_name;
493 std::replace(python_script_name.begin(), python_script_name.end(), '.',
494 '_');
495 std::cout << "Creating Python plot script " << python_script_name + ".py"
496 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200497 stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
498 ".py");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100499 }
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200500
henrik.lundine8a77e32016-06-22 06:34:03 -0700501 printf("Simulation statistics:\n");
502 printf(" output duration: %" PRId64 " ms\n", test_duration_ms);
Henrik Lundina2af0002017-06-20 16:54:39 +0200503 auto stats = stats_getter.AverageStats();
504 printf(" packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200505 printf(" expand_rate: %f %%\n", 100.0 * stats.expand_rate);
506 printf(" speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
507 printf(" preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
508 printf(" accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
henrik.lundine8a77e32016-06-22 06:34:03 -0700509 printf(" secondary_decoded_rate: %f %%\n",
Henrik Lundina2af0002017-06-20 16:54:39 +0200510 100.0 * stats.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200511 printf(" secondary_discarded_rate: %f %%\n",
512 100.0 * stats.secondary_discarded_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200513 printf(" clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
514 printf(" mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
515 printf(" median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
516 printf(" min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
517 printf(" max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
Henrik Lundin156af4a2017-11-17 16:46:18 +0100518 printf(" current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
519 printf(" preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
Alex Narest7ff6ca52018-02-07 18:46:33 +0100520 if (FLAG_concealment_events) {
Minyue Li2b415da2018-04-16 14:33:53 +0200521 std::cout << " concealment_events_ms:" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100522 for (auto concealment_event : stats_getter.concealment_events())
Minyue Li2b415da2018-04-16 14:33:53 +0200523 std::cout << concealment_event.ToString() << std::endl;
524 std::cout << " end of concealment_events_ms" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100525 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000526 return 0;
527}
henrik.lundince5570e2016-05-24 06:14:57 -0700528
henrik.lundin303d3e12016-05-26 05:56:03 -0700529} // namespace
henrik.lundince5570e2016-05-24 06:14:57 -0700530} // namespace test
531} // namespace webrtc
henrik.lundin303d3e12016-05-26 05:56:03 -0700532
533int main(int argc, char* argv[]) {
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500534 return webrtc::test::RunTest(argc, argv);
henrik.lundin303d3e12016-05-26 05:56:03 -0700535}