blob: 4d3bd0f145a8b11d45867d8dab5fbcb6b6f316d6 [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 Lundin4e268ed2018-05-08 16:36:33 +0200120DEFINE_int(video_content_type, 7, "Extension ID for video content type");
121DEFINE_int(video_timing, 8, "Extension ID for video timing");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200122DEFINE_bool(matlabplot,
123 false,
124 "Generates a matlab script for plotting the delay profile");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100125DEFINE_bool(pythonplot,
126 false,
127 "Generates a python script for plotting the delay profile");
oprypin6e09d872017-08-31 03:21:39 -0700128DEFINE_bool(help, false, "Prints this message");
Alex Narest7ff6ca52018-02-07 18:46:33 +0100129DEFINE_bool(concealment_events, false, "Prints concealment events");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000130
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000131// Maps a codec type to a printable name string.
henrik.lundince5570e2016-05-24 06:14:57 -0700132std::string CodecName(NetEqDecoder codec) {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000133 switch (codec) {
henrik.lundince5570e2016-05-24 06:14:57 -0700134 case NetEqDecoder::kDecoderPCMu:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000135 return "PCM-u";
henrik.lundince5570e2016-05-24 06:14:57 -0700136 case NetEqDecoder::kDecoderPCMa:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000137 return "PCM-a";
henrik.lundince5570e2016-05-24 06:14:57 -0700138 case NetEqDecoder::kDecoderILBC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000139 return "iLBC";
henrik.lundince5570e2016-05-24 06:14:57 -0700140 case NetEqDecoder::kDecoderISAC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000141 return "iSAC";
henrik.lundince5570e2016-05-24 06:14:57 -0700142 case NetEqDecoder::kDecoderISACswb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000143 return "iSAC-swb (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700144 case NetEqDecoder::kDecoderOpus:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000145 return "Opus";
henrik.lundince5570e2016-05-24 06:14:57 -0700146 case NetEqDecoder::kDecoderPCM16B:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000147 return "PCM16b-nb (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700148 case NetEqDecoder::kDecoderPCM16Bwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000149 return "PCM16b-wb (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700150 case NetEqDecoder::kDecoderPCM16Bswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000151 return "PCM16b-swb32 (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700152 case NetEqDecoder::kDecoderPCM16Bswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000153 return "PCM16b-swb48 (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700154 case NetEqDecoder::kDecoderG722:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000155 return "G.722";
henrik.lundince5570e2016-05-24 06:14:57 -0700156 case NetEqDecoder::kDecoderRED:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000157 return "redundant audio (RED)";
henrik.lundince5570e2016-05-24 06:14:57 -0700158 case NetEqDecoder::kDecoderAVT:
solenberg2779bab2016-11-17 04:45:19 -0800159 return "AVT/DTMF (8 kHz)";
160 case NetEqDecoder::kDecoderAVT16kHz:
161 return "AVT/DTMF (16 kHz)";
162 case NetEqDecoder::kDecoderAVT32kHz:
163 return "AVT/DTMF (32 kHz)";
164 case NetEqDecoder::kDecoderAVT48kHz:
165 return "AVT/DTMF (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700166 case NetEqDecoder::kDecoderCNGnb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000167 return "comfort noise (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700168 case NetEqDecoder::kDecoderCNGwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000169 return "comfort noise (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700170 case NetEqDecoder::kDecoderCNGswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000171 return "comfort noise (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700172 case NetEqDecoder::kDecoderCNGswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000173 return "comfort noise (48 kHz)";
174 default:
henrik.lundine8a77e32016-06-22 06:34:03 -0700175 FATAL();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000176 return "undefined";
177 }
178}
179
oprypin6e09d872017-08-31 03:21:39 -0700180void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000181 std::cout << CodecName(codec) << ": " << flag << std::endl;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000182}
183
184void PrintCodecMapping() {
oprypin6e09d872017-08-31 03:21:39 -0700185 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
186 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
187 PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
188 PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
189 PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
190 PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
191 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
192 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
henrik.lundince5570e2016-05-24 06:14:57 -0700193 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
oprypin6e09d872017-08-31 03:21:39 -0700194 FLAG_pcm16b_swb32);
henrik.lundince5570e2016-05-24 06:14:57 -0700195 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
oprypin6e09d872017-08-31 03:21:39 -0700196 FLAG_pcm16b_swb48);
197 PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
198 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
199 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
200 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
201 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
202 PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
203 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
204 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
205 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
206 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000207}
208
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200209rtc::Optional<int> CodecSampleRate(uint8_t payload_type) {
oprypin6e09d872017-08-31 03:21:39 -0700210 if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
211 payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
212 payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100213 return 8000;
oprypin6e09d872017-08-31 03:21:39 -0700214 if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
215 payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
216 payload_type == FLAG_avt_16)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100217 return 16000;
oprypin6e09d872017-08-31 03:21:39 -0700218 if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
219 payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100220 return 32000;
oprypin6e09d872017-08-31 03:21:39 -0700221 if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
222 payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100223 return 48000;
oprypin6e09d872017-08-31 03:21:39 -0700224 if (payload_type == FLAG_red)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100225 return 0;
226 return rtc::nullopt;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000227}
228
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200229// A callback class which prints whenver the inserted packet stream changes
230// the SSRC.
231class SsrcSwitchDetector : public NetEqPostInsertPacket {
232 public:
233 // Takes a pointer to another callback object, which will be invoked after
234 // this object finishes. This does not transfer ownership, and null is a
235 // valid value.
Henrik Lundina2af0002017-06-20 16:54:39 +0200236 explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200237 : other_callback_(other_callback) {}
238
Henrik Lundina2af0002017-06-20 16:54:39 +0200239 void AfterInsertPacket(const NetEqInput::PacketData& packet,
240 NetEq* neteq) override {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200241 if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
242 std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
243 << " to 0x" << std::hex << packet.header.ssrc
244 << std::dec << " (payload type "
245 << static_cast<int>(packet.header.payloadType) << ")"
246 << std::endl;
247 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100248 last_ssrc_ = packet.header.ssrc;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200249 if (other_callback_) {
250 other_callback_->AfterInsertPacket(packet, neteq);
251 }
252 }
253
254 private:
255 NetEqPostInsertPacket* other_callback_;
256 rtc::Optional<uint32_t> last_ssrc_;
257};
258
henrik.lundin303d3e12016-05-26 05:56:03 -0700259int RunTest(int argc, char* argv[]) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000260 std::string program_name = argv[0];
261 std::string usage = "Tool for decoding an RTP dump file using NetEq.\n"
oprypin6e09d872017-08-31 03:21:39 -0700262 "Run " + program_name + " --help for usage.\n"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000263 "Example usage:\n" + program_name +
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000264 " input.rtp output.{pcm, wav}\n";
oprypin6e09d872017-08-31 03:21:39 -0700265 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
266 return 1;
267 }
268 if (FLAG_help) {
269 std::cout << usage;
270 rtc::FlagList::Print(nullptr, false);
271 return 0;
272 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000273
oprypin6e09d872017-08-31 03:21:39 -0700274 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000275 PrintCodecMapping();
276 }
277
278 if (argc != 3) {
oprypin6e09d872017-08-31 03:21:39 -0700279 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000280 // We have already printed the codec map. Just end the program.
281 return 0;
282 }
283 // Print usage information.
oprypin6e09d872017-08-31 03:21:39 -0700284 std::cout << usage;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000285 return 0;
286 }
oprypin6e09d872017-08-31 03:21:39 -0700287 RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
288 RTC_CHECK(ValidatePayloadType(FLAG_pcma));
289 RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
290 RTC_CHECK(ValidatePayloadType(FLAG_isac));
291 RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
292 RTC_CHECK(ValidatePayloadType(FLAG_opus));
293 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
294 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
295 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
296 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
297 RTC_CHECK(ValidatePayloadType(FLAG_g722));
298 RTC_CHECK(ValidatePayloadType(FLAG_avt));
299 RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
300 RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
301 RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
302 RTC_CHECK(ValidatePayloadType(FLAG_red));
303 RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
304 RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
305 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
306 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
307 RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
308 RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
309 RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
310 RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200311 RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
312 RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000313
henrik.lundin8a6a6002016-08-25 00:46:36 -0700314 // Gather RTP header extensions in a map.
315 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
oprypin6e09d872017-08-31 03:21:39 -0700316 {FLAG_audio_level, kRtpExtensionAudioLevel},
317 {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200318 {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
319 {FLAG_video_content_type, kRtpExtensionVideoContentType},
320 {FLAG_video_timing, kRtpExtensionVideoTiming}};
henrik.lundin8a6a6002016-08-25 00:46:36 -0700321
henrik.lundine8a77e32016-06-22 06:34:03 -0700322 const std::string input_file_name = argv[1];
323 std::unique_ptr<NetEqInput> input;
324 if (RtpFileSource::ValidRtpDump(input_file_name) ||
325 RtpFileSource::ValidPcap(input_file_name)) {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700326 input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700327 } else {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700328 input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700329 }
330
henrik.lundine8a77e32016-06-22 06:34:03 -0700331 std::cout << "Input file: " << input_file_name << std::endl;
332 RTC_CHECK(input) << "Cannot open input file";
333 RTC_CHECK(!input->ended()) << "Input file is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000334
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000335 // Check if an SSRC value was provided.
oprypin6e09d872017-08-31 03:21:39 -0700336 if (strlen(FLAG_ssrc) > 0) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000337 uint32_t ssrc;
oprypin6e09d872017-08-31 03:21:39 -0700338 RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
Minyue Lib563f3d2018-05-11 16:49:38 +0200339 static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
ivoccaa5f4b2015-09-08 03:28:46 -0700340 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000341
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000342 // Check the sample rate.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200343 rtc::Optional<int> sample_rate_hz;
344 std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
Minyue Lib563f3d2018-05-11 16:49:38 +0200345 while (rtc::Optional<RTPHeader> first_rtp_header = input->NextHeader()) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200346 RTC_DCHECK(first_rtp_header);
347 sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
348 if (sample_rate_hz) {
349 std::cout << "Found valid packet with payload type "
350 << static_cast<int>(first_rtp_header->payloadType)
351 << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
352 << std::dec << std::endl;
353 break;
354 }
355 // Discard this packet and move to the next. Keep track of discarded payload
356 // types and SSRCs.
357 discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
358 first_rtp_header->ssrc);
359 input->PopPacket();
360 }
361 if (!discarded_pt_and_ssrc.empty()) {
362 std::cout << "Discarded initial packets with the following payload types "
363 "and SSRCs:"
364 << std::endl;
365 for (const auto& d : discarded_pt_and_ssrc) {
366 std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
367 << static_cast<int>(d.second) << std::dec << std::endl;
368 }
369 }
370 if (!sample_rate_hz) {
371 std::cout << "Cannot find any packets with known payload types"
372 << std::endl;
373 RTC_NOTREACHED();
374 }
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000375
376 // Open the output file now that we know the sample rate. (Rate is only needed
377 // for wav files.)
henrik.lundine8a77e32016-06-22 06:34:03 -0700378 const std::string output_file_name = argv[2];
henrik.lundince5570e2016-05-24 06:14:57 -0700379 std::unique_ptr<AudioSink> output;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000380 if (output_file_name.size() >= 4 &&
381 output_file_name.substr(output_file_name.size() - 4) == ".wav") {
382 // Open a wav file.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200383 output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000384 } else {
385 // Open a pcm file.
henrik.lundince5570e2016-05-24 06:14:57 -0700386 output.reset(new OutputAudioFile(output_file_name));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000387 }
388
henrik.lundine8a77e32016-06-22 06:34:03 -0700389 std::cout << "Output file: " << output_file_name << std::endl;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000390
henrik.lundine8a77e32016-06-22 06:34:03 -0700391 NetEqTest::DecoderMap codecs = {
oprypin6e09d872017-08-31 03:21:39 -0700392 {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
393 {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
394 {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
395 {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
396 {FLAG_isac_swb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700397 std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
oprypin6e09d872017-08-31 03:21:39 -0700398 {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
399 {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
400 {FLAG_pcm16b_wb,
henrik.lundine8a77e32016-06-22 06:34:03 -0700401 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
oprypin6e09d872017-08-31 03:21:39 -0700402 {FLAG_pcm16b_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700403 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700404 {FLAG_pcm16b_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700405 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
oprypin6e09d872017-08-31 03:21:39 -0700406 {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
407 {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
408 {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
409 {FLAG_avt_32,
solenberg2779bab2016-11-17 04:45:19 -0800410 std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
oprypin6e09d872017-08-31 03:21:39 -0700411 {FLAG_avt_48,
solenberg2779bab2016-11-17 04:45:19 -0800412 std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
oprypin6e09d872017-08-31 03:21:39 -0700413 {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
414 {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
415 {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
416 {FLAG_cn_swb32,
henrik.lundine8a77e32016-06-22 06:34:03 -0700417 std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
oprypin6e09d872017-08-31 03:21:39 -0700418 {FLAG_cn_swb48,
henrik.lundine8a77e32016-06-22 06:34:03 -0700419 std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}};
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000420
henrik.lundine8a77e32016-06-22 06:34:03 -0700421 // Check if a replacement audio file was provided.
422 std::unique_ptr<AudioDecoder> replacement_decoder;
423 NetEqTest::ExtDecoderMap ext_codecs;
oprypin6e09d872017-08-31 03:21:39 -0700424 if (strlen(FLAG_replacement_audio_file) > 0) {
henrik.lundine8a77e32016-06-22 06:34:03 -0700425 // Find largest unused payload type.
426 int replacement_pt = 127;
427 while (!(codecs.find(replacement_pt) == codecs.end() &&
428 ext_codecs.find(replacement_pt) == ext_codecs.end())) {
429 --replacement_pt;
430 RTC_CHECK_GE(replacement_pt, 0);
431 }
432
433 auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
434 std::set<uint8_t> b;
435 for (auto& x : a) {
436 b.insert(static_cast<uint8_t>(x));
437 }
438 return b;
439 };
440
441 std::set<uint8_t> cn_types = std_set_int32_to_uint8(
oprypin6e09d872017-08-31 03:21:39 -0700442 {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700443 std::set<uint8_t> forbidden_types =
oprypin6e09d872017-08-31 03:21:39 -0700444 std_set_int32_to_uint8({FLAG_g722, FLAG_red, FLAG_avt,
445 FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700446 input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
447 cn_types, forbidden_types));
448
449 replacement_decoder.reset(new FakeDecodeFromFile(
450 std::unique_ptr<InputAudioFile>(
oprypin6e09d872017-08-31 03:21:39 -0700451 new InputAudioFile(FLAG_replacement_audio_file)),
henrik.lundine8a77e32016-06-22 06:34:03 -0700452 48000, false));
453 NetEqTest::ExternalDecoderInfo ext_dec_info = {
454 replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
455 "replacement codec"};
456 ext_codecs[replacement_pt] = ext_dec_info;
457 }
458
henrik.lundin02739d92017-05-04 06:09:06 -0700459 NetEqTest::Callbacks callbacks;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200460 std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100461 if (FLAG_matlabplot || FLAG_pythonplot) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200462 delay_analyzer.reset(new NetEqDelayAnalyzer);
463 }
464
465 SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
466 callbacks.post_insert_packet = &ssrc_switch_detector;
Minyue Li2b415da2018-04-16 14:33:53 +0200467 NetEqStatsGetter stats_getter(std::move(delay_analyzer));
Henrik Lundina2af0002017-06-20 16:54:39 +0200468 callbacks.get_audio_callback = &stats_getter;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000469 NetEq::Config config;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200470 config.sample_rate_hz = *sample_rate_hz;
henrik.lundine8a77e32016-06-22 06:34:03 -0700471 NetEqTest test(config, codecs, ext_codecs, std::move(input),
henrik.lundin02739d92017-05-04 06:09:06 -0700472 std::move(output), callbacks);
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000473
henrik.lundine8a77e32016-06-22 06:34:03 -0700474 int64_t test_duration_ms = test.Run();
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000475
oprypin6e09d872017-08-31 03:21:39 -0700476 if (FLAG_matlabplot) {
henrik.lundinf09c9042017-08-29 09:14:08 -0700477 auto matlab_script_name = output_file_name;
478 std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
479 '_');
480 std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200481 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200482 stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
483 ".m");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200484 }
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100485 if (FLAG_pythonplot) {
486 auto python_script_name = output_file_name;
487 std::replace(python_script_name.begin(), python_script_name.end(), '.',
488 '_');
489 std::cout << "Creating Python plot script " << python_script_name + ".py"
490 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200491 stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
492 ".py");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100493 }
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200494
henrik.lundine8a77e32016-06-22 06:34:03 -0700495 printf("Simulation statistics:\n");
496 printf(" output duration: %" PRId64 " ms\n", test_duration_ms);
Henrik Lundina2af0002017-06-20 16:54:39 +0200497 auto stats = stats_getter.AverageStats();
498 printf(" packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200499 printf(" expand_rate: %f %%\n", 100.0 * stats.expand_rate);
500 printf(" speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
501 printf(" preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
502 printf(" accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
henrik.lundine8a77e32016-06-22 06:34:03 -0700503 printf(" secondary_decoded_rate: %f %%\n",
Henrik Lundina2af0002017-06-20 16:54:39 +0200504 100.0 * stats.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200505 printf(" secondary_discarded_rate: %f %%\n",
506 100.0 * stats.secondary_discarded_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200507 printf(" clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
508 printf(" mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
509 printf(" median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
510 printf(" min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
511 printf(" max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
Henrik Lundin156af4a2017-11-17 16:46:18 +0100512 printf(" current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
513 printf(" preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
Alex Narest7ff6ca52018-02-07 18:46:33 +0100514 if (FLAG_concealment_events) {
Minyue Li2b415da2018-04-16 14:33:53 +0200515 std::cout << " concealment_events_ms:" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100516 for (auto concealment_event : stats_getter.concealment_events())
Minyue Li2b415da2018-04-16 14:33:53 +0200517 std::cout << concealment_event.ToString() << std::endl;
518 std::cout << " end of concealment_events_ms" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100519 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000520 return 0;
521}
henrik.lundince5570e2016-05-24 06:14:57 -0700522
henrik.lundin303d3e12016-05-26 05:56:03 -0700523} // namespace
henrik.lundince5570e2016-05-24 06:14:57 -0700524} // namespace test
525} // namespace webrtc
henrik.lundin303d3e12016-05-26 05:56:03 -0700526
527int main(int argc, char* argv[]) {
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500528 return webrtc::test::RunTest(argc, argv);
henrik.lundin303d3e12016-05-26 05:56:03 -0700529}