blob: 7c071dba6f7db7bf894b07fd9134e8931cd07e8f [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>
Minyue Li2b415da2018-04-16 14:33:53 +020013#include <iostream>
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000014#include <limits.h> // For ULONG_MAX returned by strtoul.
Minyue Li2b415da2018-04-16 14:33:53 +020015#include <memory>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000016#include <stdio.h>
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +000017#include <stdlib.h> // For strtoul.
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"
Minyue Li2b415da2018-04-16 14:33:53 +020024#include "modules/audio_coding/neteq/tools/neteq_stats_getter.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"
27#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;
oprypin6e09d872017-08-31 03:21:39 -070074 if (ParseSsrc(str, &dummy_ssrc)) // Value is ok.
75 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)");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000109DEFINE_bool(codec_map, false, "Prints the mapping between RTP payload type and "
110 "codec");
henrik.lundin@webrtc.org75642fc2014-02-05 08:49:13 +0000111DEFINE_string(replacement_audio_file, "",
112 "A PCM file that will be used to populate ""dummy"" RTP packets");
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000113DEFINE_string(ssrc,
114 "",
115 "Only use packets with this SSRC (decimal or hex, the latter "
116 "starting with 0x)");
oprypin6e09d872017-08-31 03:21:39 -0700117DEFINE_int(audio_level, 1, "Extension ID for audio level (RFC 6464)");
118DEFINE_int(abs_send_time, 3, "Extension ID for absolute sender time");
119DEFINE_int(transport_seq_no, 5, "Extension ID for transport sequence number");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200120DEFINE_bool(matlabplot,
121 false,
122 "Generates a matlab script for plotting the delay profile");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100123DEFINE_bool(pythonplot,
124 false,
125 "Generates a python script for plotting the delay profile");
oprypin6e09d872017-08-31 03:21:39 -0700126DEFINE_bool(help, false, "Prints this message");
Alex Narest7ff6ca52018-02-07 18:46:33 +0100127DEFINE_bool(concealment_events, false, "Prints concealment events");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000128
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000129// Maps a codec type to a printable name string.
henrik.lundince5570e2016-05-24 06:14:57 -0700130std::string CodecName(NetEqDecoder codec) {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000131 switch (codec) {
henrik.lundince5570e2016-05-24 06:14:57 -0700132 case NetEqDecoder::kDecoderPCMu:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000133 return "PCM-u";
henrik.lundince5570e2016-05-24 06:14:57 -0700134 case NetEqDecoder::kDecoderPCMa:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000135 return "PCM-a";
henrik.lundince5570e2016-05-24 06:14:57 -0700136 case NetEqDecoder::kDecoderILBC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000137 return "iLBC";
henrik.lundince5570e2016-05-24 06:14:57 -0700138 case NetEqDecoder::kDecoderISAC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000139 return "iSAC";
henrik.lundince5570e2016-05-24 06:14:57 -0700140 case NetEqDecoder::kDecoderISACswb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000141 return "iSAC-swb (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700142 case NetEqDecoder::kDecoderOpus:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000143 return "Opus";
henrik.lundince5570e2016-05-24 06:14:57 -0700144 case NetEqDecoder::kDecoderPCM16B:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000145 return "PCM16b-nb (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700146 case NetEqDecoder::kDecoderPCM16Bwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000147 return "PCM16b-wb (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700148 case NetEqDecoder::kDecoderPCM16Bswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000149 return "PCM16b-swb32 (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700150 case NetEqDecoder::kDecoderPCM16Bswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000151 return "PCM16b-swb48 (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700152 case NetEqDecoder::kDecoderG722:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000153 return "G.722";
henrik.lundince5570e2016-05-24 06:14:57 -0700154 case NetEqDecoder::kDecoderRED:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000155 return "redundant audio (RED)";
henrik.lundince5570e2016-05-24 06:14:57 -0700156 case NetEqDecoder::kDecoderAVT:
solenberg2779bab2016-11-17 04:45:19 -0800157 return "AVT/DTMF (8 kHz)";
158 case NetEqDecoder::kDecoderAVT16kHz:
159 return "AVT/DTMF (16 kHz)";
160 case NetEqDecoder::kDecoderAVT32kHz:
161 return "AVT/DTMF (32 kHz)";
162 case NetEqDecoder::kDecoderAVT48kHz:
163 return "AVT/DTMF (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700164 case NetEqDecoder::kDecoderCNGnb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000165 return "comfort noise (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700166 case NetEqDecoder::kDecoderCNGwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000167 return "comfort noise (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700168 case NetEqDecoder::kDecoderCNGswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000169 return "comfort noise (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700170 case NetEqDecoder::kDecoderCNGswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000171 return "comfort noise (48 kHz)";
172 default:
henrik.lundine8a77e32016-06-22 06:34:03 -0700173 FATAL();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000174 return "undefined";
175 }
176}
177
oprypin6e09d872017-08-31 03:21:39 -0700178void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000179 std::cout << CodecName(codec) << ": " << flag << std::endl;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000180}
181
182void PrintCodecMapping() {
oprypin6e09d872017-08-31 03:21:39 -0700183 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
184 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
185 PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
186 PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
187 PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
188 PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
189 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
190 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
henrik.lundince5570e2016-05-24 06:14:57 -0700191 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
oprypin6e09d872017-08-31 03:21:39 -0700192 FLAG_pcm16b_swb32);
henrik.lundince5570e2016-05-24 06:14:57 -0700193 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
oprypin6e09d872017-08-31 03:21:39 -0700194 FLAG_pcm16b_swb48);
195 PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
196 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
197 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
198 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
199 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
200 PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
201 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
202 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
203 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
204 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000205}
206
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200207rtc::Optional<int> CodecSampleRate(uint8_t payload_type) {
oprypin6e09d872017-08-31 03:21:39 -0700208 if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
209 payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
210 payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100211 return 8000;
oprypin6e09d872017-08-31 03:21:39 -0700212 if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
213 payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
214 payload_type == FLAG_avt_16)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100215 return 16000;
oprypin6e09d872017-08-31 03:21:39 -0700216 if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
217 payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100218 return 32000;
oprypin6e09d872017-08-31 03:21:39 -0700219 if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
220 payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100221 return 48000;
oprypin6e09d872017-08-31 03:21:39 -0700222 if (payload_type == FLAG_red)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100223 return 0;
224 return rtc::nullopt;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000225}
226
henrik.lundine8a77e32016-06-22 06:34:03 -0700227// Class to let through only the packets with a given SSRC. Should be used as an
228// outer layer on another NetEqInput object.
229class FilterSsrcInput : public NetEqInput {
230 public:
231 FilterSsrcInput(std::unique_ptr<NetEqInput> source, uint32_t ssrc)
232 : source_(std::move(source)), ssrc_(ssrc) {
233 FindNextWithCorrectSsrc();
henrik.lundind4ec9702016-09-06 01:22:45 -0700234 RTC_CHECK(source_->NextHeader()) << "Found no packet with SSRC = 0x"
235 << std::hex << ssrc_;
henrik.lundine8a77e32016-06-22 06:34:03 -0700236 }
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000237
henrik.lundine8a77e32016-06-22 06:34:03 -0700238 // All methods but PopPacket() simply relay to the |source_| object.
239 rtc::Optional<int64_t> NextPacketTime() const override {
240 return source_->NextPacketTime();
241 }
242 rtc::Optional<int64_t> NextOutputEventTime() const override {
243 return source_->NextOutputEventTime();
244 }
245
246 // Returns the next packet, and throws away upcoming packets that do not match
247 // the desired SSRC.
248 std::unique_ptr<PacketData> PopPacket() override {
249 std::unique_ptr<PacketData> packet_to_return = source_->PopPacket();
henrik.lundin246ef3e2017-04-24 09:14:32 -0700250 RTC_DCHECK(!packet_to_return || packet_to_return->header.ssrc == ssrc_);
henrik.lundine8a77e32016-06-22 06:34:03 -0700251 // Pre-fetch the next packet with correct SSRC. Hence, |source_| will always
252 // be have a valid packet (or empty if no more packets are available) when
253 // this method returns.
254 FindNextWithCorrectSsrc();
255 return packet_to_return;
256 }
257
258 void AdvanceOutputEvent() override { source_->AdvanceOutputEvent(); }
259
260 bool ended() const override { return source_->ended(); }
261
262 rtc::Optional<RTPHeader> NextHeader() const override {
263 return source_->NextHeader();
264 }
265
266 private:
267 void FindNextWithCorrectSsrc() {
268 while (source_->NextHeader() && source_->NextHeader()->ssrc != ssrc_) {
269 source_->PopPacket();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000270 }
271 }
henrik.lundine8a77e32016-06-22 06:34:03 -0700272
273 std::unique_ptr<NetEqInput> source_;
274 uint32_t ssrc_;
275};
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000276
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200277// A callback class which prints whenver the inserted packet stream changes
278// the SSRC.
279class SsrcSwitchDetector : public NetEqPostInsertPacket {
280 public:
281 // Takes a pointer to another callback object, which will be invoked after
282 // this object finishes. This does not transfer ownership, and null is a
283 // valid value.
Henrik Lundina2af0002017-06-20 16:54:39 +0200284 explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200285 : other_callback_(other_callback) {}
286
Henrik Lundina2af0002017-06-20 16:54:39 +0200287 void AfterInsertPacket(const NetEqInput::PacketData& packet,
288 NetEq* neteq) override {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200289 if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
290 std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
291 << " to 0x" << std::hex << packet.header.ssrc
292 << std::dec << " (payload type "
293 << static_cast<int>(packet.header.payloadType) << ")"
294 << std::endl;
295 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100296 last_ssrc_ = packet.header.ssrc;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200297 if (other_callback_) {
298 other_callback_->AfterInsertPacket(packet, neteq);
299 }
300 }
301
302 private:
303 NetEqPostInsertPacket* other_callback_;
304 rtc::Optional<uint32_t> last_ssrc_;
305};
306
henrik.lundin303d3e12016-05-26 05:56:03 -0700307int RunTest(int argc, char* argv[]) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000308 std::string program_name = argv[0];
309 std::string usage = "Tool for decoding an RTP dump file using NetEq.\n"
oprypin6e09d872017-08-31 03:21:39 -0700310 "Run " + program_name + " --help for usage.\n"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000311 "Example usage:\n" + program_name +
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000312 " input.rtp output.{pcm, wav}\n";
oprypin6e09d872017-08-31 03:21:39 -0700313 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
314 return 1;
315 }
316 if (FLAG_help) {
317 std::cout << usage;
318 rtc::FlagList::Print(nullptr, false);
319 return 0;
320 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000321
oprypin6e09d872017-08-31 03:21:39 -0700322 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000323 PrintCodecMapping();
324 }
325
326 if (argc != 3) {
oprypin6e09d872017-08-31 03:21:39 -0700327 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000328 // We have already printed the codec map. Just end the program.
329 return 0;
330 }
331 // Print usage information.
oprypin6e09d872017-08-31 03:21:39 -0700332 std::cout << usage;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000333 return 0;
334 }
oprypin6e09d872017-08-31 03:21:39 -0700335 RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
336 RTC_CHECK(ValidatePayloadType(FLAG_pcma));
337 RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
338 RTC_CHECK(ValidatePayloadType(FLAG_isac));
339 RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
340 RTC_CHECK(ValidatePayloadType(FLAG_opus));
341 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
342 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
343 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
344 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
345 RTC_CHECK(ValidatePayloadType(FLAG_g722));
346 RTC_CHECK(ValidatePayloadType(FLAG_avt));
347 RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
348 RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
349 RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
350 RTC_CHECK(ValidatePayloadType(FLAG_red));
351 RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
352 RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
353 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
354 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
355 RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
356 RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
357 RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
358 RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000359
henrik.lundin8a6a6002016-08-25 00:46:36 -0700360 // Gather RTP header extensions in a map.
361 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
oprypin6e09d872017-08-31 03:21:39 -0700362 {FLAG_audio_level, kRtpExtensionAudioLevel},
363 {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
364 {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber}};
henrik.lundin8a6a6002016-08-25 00:46:36 -0700365
henrik.lundine8a77e32016-06-22 06:34:03 -0700366 const std::string input_file_name = argv[1];
367 std::unique_ptr<NetEqInput> input;
368 if (RtpFileSource::ValidRtpDump(input_file_name) ||
369 RtpFileSource::ValidPcap(input_file_name)) {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700370 input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700371 } else {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700372 input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700373 }
374
henrik.lundine8a77e32016-06-22 06:34:03 -0700375 std::cout << "Input file: " << input_file_name << std::endl;
376 RTC_CHECK(input) << "Cannot open input file";
377 RTC_CHECK(!input->ended()) << "Input file is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000378
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000379 // Check if an SSRC value was provided.
oprypin6e09d872017-08-31 03:21:39 -0700380 if (strlen(FLAG_ssrc) > 0) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000381 uint32_t ssrc;
oprypin6e09d872017-08-31 03:21:39 -0700382 RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
henrik.lundine8a77e32016-06-22 06:34:03 -0700383 input.reset(new FilterSsrcInput(std::move(input), ssrc));
ivoccaa5f4b2015-09-08 03:28:46 -0700384 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000385
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000386 // Check the sample rate.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200387 rtc::Optional<int> sample_rate_hz;
388 std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
389 while (input->NextHeader()) {
390 rtc::Optional<RTPHeader> first_rtp_header = input->NextHeader();
391 RTC_DCHECK(first_rtp_header);
392 sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
393 if (sample_rate_hz) {
394 std::cout << "Found valid packet with payload type "
395 << static_cast<int>(first_rtp_header->payloadType)
396 << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
397 << std::dec << std::endl;
398 break;
399 }
400 // Discard this packet and move to the next. Keep track of discarded payload
401 // types and SSRCs.
402 discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
403 first_rtp_header->ssrc);
404 input->PopPacket();
405 }
406 if (!discarded_pt_and_ssrc.empty()) {
407 std::cout << "Discarded initial packets with the following payload types "
408 "and SSRCs:"
409 << std::endl;
410 for (const auto& d : discarded_pt_and_ssrc) {
411 std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
412 << static_cast<int>(d.second) << std::dec << std::endl;
413 }
414 }
415 if (!sample_rate_hz) {
416 std::cout << "Cannot find any packets with known payload types"
417 << std::endl;
418 RTC_NOTREACHED();
419 }
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000420
421 // Open the output file now that we know the sample rate. (Rate is only needed
422 // for wav files.)
henrik.lundine8a77e32016-06-22 06:34:03 -0700423 const std::string output_file_name = argv[2];
henrik.lundince5570e2016-05-24 06:14:57 -0700424 std::unique_ptr<AudioSink> output;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000425 if (output_file_name.size() >= 4 &&
426 output_file_name.substr(output_file_name.size() - 4) == ".wav") {
427 // Open a wav file.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200428 output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000429 } else {
430 // Open a pcm file.
henrik.lundince5570e2016-05-24 06:14:57 -0700431 output.reset(new OutputAudioFile(output_file_name));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000432 }
433
henrik.lundine8a77e32016-06-22 06:34:03 -0700434 std::cout << "Output file: " << output_file_name << std::endl;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000435
henrik.lundine8a77e32016-06-22 06:34:03 -0700436 NetEqTest::DecoderMap codecs = {
oprypin6e09d872017-08-31 03:21:39 -0700437 {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
438 {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
439 {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
440 {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
441 {FLAG_isac_swb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700442 std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
oprypin6e09d872017-08-31 03:21:39 -0700443 {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
444 {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
445 {FLAG_pcm16b_wb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700446 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
oprypin6e09d872017-08-31 03:21:39 -0700447 {FLAG_pcm16b_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700448 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700449 {FLAG_pcm16b_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700450 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
oprypin6e09d872017-08-31 03:21:39 -0700451 {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
452 {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
453 {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
454 {FLAG_avt_32,
solenberg2779bab2016-11-17 04:45:19 -0800455 std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
oprypin6e09d872017-08-31 03:21:39 -0700456 {FLAG_avt_48,
solenberg2779bab2016-11-17 04:45:19 -0800457 std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
oprypin6e09d872017-08-31 03:21:39 -0700458 {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
459 {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
460 {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
461 {FLAG_cn_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700462 std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700463 {FLAG_cn_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700464 std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}};
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000465
henrik.lundine8a77e32016-06-22 06:34:03 -0700466 // Check if a replacement audio file was provided.
467 std::unique_ptr<AudioDecoder> replacement_decoder;
468 NetEqTest::ExtDecoderMap ext_codecs;
oprypin6e09d872017-08-31 03:21:39 -0700469 if (strlen(FLAG_replacement_audio_file) > 0) {
henrik.lundine8a77e32016-06-22 06:34:03 -0700470 // Find largest unused payload type.
471 int replacement_pt = 127;
472 while (!(codecs.find(replacement_pt) == codecs.end() &&
473 ext_codecs.find(replacement_pt) == ext_codecs.end())) {
474 --replacement_pt;
475 RTC_CHECK_GE(replacement_pt, 0);
476 }
477
478 auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
479 std::set<uint8_t> b;
480 for (auto& x : a) {
481 b.insert(static_cast<uint8_t>(x));
482 }
483 return b;
484 };
485
486 std::set<uint8_t> cn_types = std_set_int32_to_uint8(
oprypin6e09d872017-08-31 03:21:39 -0700487 {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700488 std::set<uint8_t> forbidden_types =
oprypin6e09d872017-08-31 03:21:39 -0700489 std_set_int32_to_uint8({FLAG_g722, FLAG_red, FLAG_avt,
490 FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700491 input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
492 cn_types, forbidden_types));
493
494 replacement_decoder.reset(new FakeDecodeFromFile(
495 std::unique_ptr<InputAudioFile>(
oprypin6e09d872017-08-31 03:21:39 -0700496 new InputAudioFile(FLAG_replacement_audio_file)),
henrik.lundine8a77e32016-06-22 06:34:03 -0700497 48000, false));
498 NetEqTest::ExternalDecoderInfo ext_dec_info = {
499 replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
500 "replacement codec"};
501 ext_codecs[replacement_pt] = ext_dec_info;
502 }
503
henrik.lundin02739d92017-05-04 06:09:06 -0700504 NetEqTest::Callbacks callbacks;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200505 std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100506 if (FLAG_matlabplot || FLAG_pythonplot) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200507 delay_analyzer.reset(new NetEqDelayAnalyzer);
508 }
509
510 SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
511 callbacks.post_insert_packet = &ssrc_switch_detector;
Minyue Li2b415da2018-04-16 14:33:53 +0200512 NetEqStatsGetter stats_getter(std::move(delay_analyzer));
Henrik Lundina2af0002017-06-20 16:54:39 +0200513 callbacks.get_audio_callback = &stats_getter;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000514 NetEq::Config config;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200515 config.sample_rate_hz = *sample_rate_hz;
henrik.lundine8a77e32016-06-22 06:34:03 -0700516 NetEqTest test(config, codecs, ext_codecs, std::move(input),
henrik.lundin02739d92017-05-04 06:09:06 -0700517 std::move(output), callbacks);
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000518
henrik.lundine8a77e32016-06-22 06:34:03 -0700519 int64_t test_duration_ms = test.Run();
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000520
oprypin6e09d872017-08-31 03:21:39 -0700521 if (FLAG_matlabplot) {
henrik.lundinf09c9042017-08-29 09:14:08 -0700522 auto matlab_script_name = output_file_name;
523 std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
524 '_');
525 std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200526 << std::endl;
henrik.lundinf09c9042017-08-29 09:14:08 -0700527 delay_analyzer->CreateMatlabScript(matlab_script_name + ".m");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200528 }
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100529 if (FLAG_pythonplot) {
530 auto python_script_name = output_file_name;
531 std::replace(python_script_name.begin(), python_script_name.end(), '.',
532 '_');
533 std::cout << "Creating Python plot script " << python_script_name + ".py"
534 << std::endl;
535 delay_analyzer->CreatePythonScript(python_script_name + ".py");
536 }
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200537
henrik.lundine8a77e32016-06-22 06:34:03 -0700538 printf("Simulation statistics:\n");
539 printf(" output duration: %" PRId64 " ms\n", test_duration_ms);
Henrik Lundina2af0002017-06-20 16:54:39 +0200540 auto stats = stats_getter.AverageStats();
541 printf(" packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200542 printf(" expand_rate: %f %%\n", 100.0 * stats.expand_rate);
543 printf(" speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
544 printf(" preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
545 printf(" accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
henrik.lundine8a77e32016-06-22 06:34:03 -0700546 printf(" secondary_decoded_rate: %f %%\n",
Henrik Lundina2af0002017-06-20 16:54:39 +0200547 100.0 * stats.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200548 printf(" secondary_discarded_rate: %f %%\n",
549 100.0 * stats.secondary_discarded_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200550 printf(" clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
551 printf(" mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
552 printf(" median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
553 printf(" min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
554 printf(" max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
Henrik Lundin156af4a2017-11-17 16:46:18 +0100555 printf(" current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
556 printf(" preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
Alex Narest7ff6ca52018-02-07 18:46:33 +0100557 if (FLAG_concealment_events) {
Minyue Li2b415da2018-04-16 14:33:53 +0200558 std::cout << " concealment_events_ms:" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100559 for (auto concealment_event : stats_getter.concealment_events())
Minyue Li2b415da2018-04-16 14:33:53 +0200560 std::cout << concealment_event.ToString() << std::endl;
561 std::cout << " end of concealment_events_ms" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100562 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000563 return 0;
564}
henrik.lundince5570e2016-05-24 06:14:57 -0700565
henrik.lundin303d3e12016-05-26 05:56:03 -0700566} // namespace
henrik.lundince5570e2016-05-24 06:14:57 -0700567} // namespace test
568} // namespace webrtc
henrik.lundin303d3e12016-05-26 05:56:03 -0700569
570int main(int argc, char* argv[]) {
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500571 return webrtc::test::RunTest(argc, argv);
henrik.lundin303d3e12016-05-26 05:56:03 -0700572}