blob: bf782bf3f6cf3440b4f035e93777a70674d172b2 [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"
Ivo Creusen80006b92018-08-10 11:19:21 +020034#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "test/testsupport/fileutils.h"
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");
Ivo Creusen80006b92018-08-10 11:19:21 +0200136DEFINE_string(
137 force_fieldtrials,
138 "",
139 "Field trials control experimental feature code which can be forced. "
140 "E.g. running with --force_fieldtrials=WebRTC-FooFeature/Enable/"
141 " will assign the group Enable to field trial WebRTC-FooFeature.");
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000142
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000143// Maps a codec type to a printable name string.
henrik.lundince5570e2016-05-24 06:14:57 -0700144std::string CodecName(NetEqDecoder codec) {
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000145 switch (codec) {
henrik.lundince5570e2016-05-24 06:14:57 -0700146 case NetEqDecoder::kDecoderPCMu:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000147 return "PCM-u";
henrik.lundince5570e2016-05-24 06:14:57 -0700148 case NetEqDecoder::kDecoderPCMa:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000149 return "PCM-a";
henrik.lundince5570e2016-05-24 06:14:57 -0700150 case NetEqDecoder::kDecoderILBC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000151 return "iLBC";
henrik.lundince5570e2016-05-24 06:14:57 -0700152 case NetEqDecoder::kDecoderISAC:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000153 return "iSAC";
henrik.lundince5570e2016-05-24 06:14:57 -0700154 case NetEqDecoder::kDecoderISACswb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000155 return "iSAC-swb (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700156 case NetEqDecoder::kDecoderOpus:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000157 return "Opus";
henrik.lundince5570e2016-05-24 06:14:57 -0700158 case NetEqDecoder::kDecoderPCM16B:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000159 return "PCM16b-nb (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700160 case NetEqDecoder::kDecoderPCM16Bwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000161 return "PCM16b-wb (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700162 case NetEqDecoder::kDecoderPCM16Bswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000163 return "PCM16b-swb32 (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700164 case NetEqDecoder::kDecoderPCM16Bswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000165 return "PCM16b-swb48 (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700166 case NetEqDecoder::kDecoderG722:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000167 return "G.722";
henrik.lundince5570e2016-05-24 06:14:57 -0700168 case NetEqDecoder::kDecoderRED:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000169 return "redundant audio (RED)";
henrik.lundince5570e2016-05-24 06:14:57 -0700170 case NetEqDecoder::kDecoderAVT:
solenberg2779bab2016-11-17 04:45:19 -0800171 return "AVT/DTMF (8 kHz)";
172 case NetEqDecoder::kDecoderAVT16kHz:
173 return "AVT/DTMF (16 kHz)";
174 case NetEqDecoder::kDecoderAVT32kHz:
175 return "AVT/DTMF (32 kHz)";
176 case NetEqDecoder::kDecoderAVT48kHz:
177 return "AVT/DTMF (48 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700178 case NetEqDecoder::kDecoderCNGnb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000179 return "comfort noise (8 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700180 case NetEqDecoder::kDecoderCNGwb:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000181 return "comfort noise (16 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700182 case NetEqDecoder::kDecoderCNGswb32kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000183 return "comfort noise (32 kHz)";
henrik.lundince5570e2016-05-24 06:14:57 -0700184 case NetEqDecoder::kDecoderCNGswb48kHz:
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000185 return "comfort noise (48 kHz)";
186 default:
henrik.lundine8a77e32016-06-22 06:34:03 -0700187 FATAL();
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000188 return "undefined";
189 }
190}
191
oprypin6e09d872017-08-31 03:21:39 -0700192void PrintCodecMappingEntry(NetEqDecoder codec, int flag) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000193 std::cout << CodecName(codec) << ": " << flag << std::endl;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000194}
195
196void PrintCodecMapping() {
oprypin6e09d872017-08-31 03:21:39 -0700197 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMu, FLAG_pcmu);
198 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCMa, FLAG_pcma);
199 PrintCodecMappingEntry(NetEqDecoder::kDecoderILBC, FLAG_ilbc);
200 PrintCodecMappingEntry(NetEqDecoder::kDecoderISAC, FLAG_isac);
201 PrintCodecMappingEntry(NetEqDecoder::kDecoderISACswb, FLAG_isac_swb);
202 PrintCodecMappingEntry(NetEqDecoder::kDecoderOpus, FLAG_opus);
203 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16B, FLAG_pcm16b);
204 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bwb, FLAG_pcm16b_wb);
henrik.lundince5570e2016-05-24 06:14:57 -0700205 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb32kHz,
oprypin6e09d872017-08-31 03:21:39 -0700206 FLAG_pcm16b_swb32);
henrik.lundince5570e2016-05-24 06:14:57 -0700207 PrintCodecMappingEntry(NetEqDecoder::kDecoderPCM16Bswb48kHz,
oprypin6e09d872017-08-31 03:21:39 -0700208 FLAG_pcm16b_swb48);
209 PrintCodecMappingEntry(NetEqDecoder::kDecoderG722, FLAG_g722);
210 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT, FLAG_avt);
211 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT16kHz, FLAG_avt_16);
212 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT32kHz, FLAG_avt_32);
213 PrintCodecMappingEntry(NetEqDecoder::kDecoderAVT48kHz, FLAG_avt_48);
214 PrintCodecMappingEntry(NetEqDecoder::kDecoderRED, FLAG_red);
215 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGnb, FLAG_cn_nb);
216 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGwb, FLAG_cn_wb);
217 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb32kHz, FLAG_cn_swb32);
218 PrintCodecMappingEntry(NetEqDecoder::kDecoderCNGswb48kHz, FLAG_cn_swb48);
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000219}
220
Danil Chapovalovb6021232018-06-19 13:26:36 +0200221absl::optional<int> CodecSampleRate(uint8_t payload_type) {
oprypin6e09d872017-08-31 03:21:39 -0700222 if (payload_type == FLAG_pcmu || payload_type == FLAG_pcma ||
223 payload_type == FLAG_ilbc || payload_type == FLAG_pcm16b ||
224 payload_type == FLAG_cn_nb || payload_type == FLAG_avt)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100225 return 8000;
oprypin6e09d872017-08-31 03:21:39 -0700226 if (payload_type == FLAG_isac || payload_type == FLAG_pcm16b_wb ||
227 payload_type == FLAG_g722 || payload_type == FLAG_cn_wb ||
228 payload_type == FLAG_avt_16)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100229 return 16000;
oprypin6e09d872017-08-31 03:21:39 -0700230 if (payload_type == FLAG_isac_swb || payload_type == FLAG_pcm16b_swb32 ||
231 payload_type == FLAG_cn_swb32 || payload_type == FLAG_avt_32)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100232 return 32000;
oprypin6e09d872017-08-31 03:21:39 -0700233 if (payload_type == FLAG_opus || payload_type == FLAG_pcm16b_swb48 ||
234 payload_type == FLAG_cn_swb48 || payload_type == FLAG_avt_48)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100235 return 48000;
oprypin6e09d872017-08-31 03:21:39 -0700236 if (payload_type == FLAG_red)
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100237 return 0;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200238 return absl::nullopt;
pkasting@chromium.org4dba2e92015-01-26 19:59:32 +0000239}
240
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200241// A callback class which prints whenver the inserted packet stream changes
242// the SSRC.
243class SsrcSwitchDetector : public NetEqPostInsertPacket {
244 public:
245 // Takes a pointer to another callback object, which will be invoked after
246 // this object finishes. This does not transfer ownership, and null is a
247 // valid value.
Henrik Lundina2af0002017-06-20 16:54:39 +0200248 explicit SsrcSwitchDetector(NetEqPostInsertPacket* other_callback)
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200249 : other_callback_(other_callback) {}
250
Henrik Lundina2af0002017-06-20 16:54:39 +0200251 void AfterInsertPacket(const NetEqInput::PacketData& packet,
252 NetEq* neteq) override {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200253 if (last_ssrc_ && packet.header.ssrc != *last_ssrc_) {
254 std::cout << "Changing streams from 0x" << std::hex << *last_ssrc_
Yves Gerey665174f2018-06-19 15:03:05 +0200255 << " to 0x" << std::hex << packet.header.ssrc << std::dec
256 << " (payload type "
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200257 << static_cast<int>(packet.header.payloadType) << ")"
258 << std::endl;
259 }
Oskar Sundbom12ab00b2017-11-16 15:31:38 +0100260 last_ssrc_ = packet.header.ssrc;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200261 if (other_callback_) {
262 other_callback_->AfterInsertPacket(packet, neteq);
263 }
264 }
265
266 private:
267 NetEqPostInsertPacket* other_callback_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200268 absl::optional<uint32_t> last_ssrc_;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200269};
270
henrik.lundin303d3e12016-05-26 05:56:03 -0700271int RunTest(int argc, char* argv[]) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000272 std::string program_name = argv[0];
Yves Gerey665174f2018-06-19 15:03:05 +0200273 std::string usage =
274 "Tool for decoding an RTP dump file using NetEq.\n"
275 "Run " +
276 program_name +
277 " --help for usage.\n"
278 "Example usage:\n" +
279 program_name + " input.rtp output.{pcm, wav}\n";
oprypin6e09d872017-08-31 03:21:39 -0700280 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true)) {
281 return 1;
282 }
283 if (FLAG_help) {
284 std::cout << usage;
285 rtc::FlagList::Print(nullptr, false);
286 return 0;
287 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000288
oprypin6e09d872017-08-31 03:21:39 -0700289 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000290 PrintCodecMapping();
291 }
292
293 if (argc != 3) {
oprypin6e09d872017-08-31 03:21:39 -0700294 if (FLAG_codec_map) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000295 // We have already printed the codec map. Just end the program.
296 return 0;
297 }
298 // Print usage information.
oprypin6e09d872017-08-31 03:21:39 -0700299 std::cout << usage;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000300 return 0;
301 }
Ivo Creusen80006b92018-08-10 11:19:21 +0200302
303 ValidateFieldTrialsStringOrDie(FLAG_force_fieldtrials);
304 ScopedFieldTrials field_trials(FLAG_force_fieldtrials);
305
oprypin6e09d872017-08-31 03:21:39 -0700306 RTC_CHECK(ValidatePayloadType(FLAG_pcmu));
307 RTC_CHECK(ValidatePayloadType(FLAG_pcma));
308 RTC_CHECK(ValidatePayloadType(FLAG_ilbc));
309 RTC_CHECK(ValidatePayloadType(FLAG_isac));
310 RTC_CHECK(ValidatePayloadType(FLAG_isac_swb));
311 RTC_CHECK(ValidatePayloadType(FLAG_opus));
312 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b));
313 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_wb));
314 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb32));
315 RTC_CHECK(ValidatePayloadType(FLAG_pcm16b_swb48));
316 RTC_CHECK(ValidatePayloadType(FLAG_g722));
317 RTC_CHECK(ValidatePayloadType(FLAG_avt));
318 RTC_CHECK(ValidatePayloadType(FLAG_avt_16));
319 RTC_CHECK(ValidatePayloadType(FLAG_avt_32));
320 RTC_CHECK(ValidatePayloadType(FLAG_avt_48));
321 RTC_CHECK(ValidatePayloadType(FLAG_red));
322 RTC_CHECK(ValidatePayloadType(FLAG_cn_nb));
323 RTC_CHECK(ValidatePayloadType(FLAG_cn_wb));
324 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb32));
325 RTC_CHECK(ValidatePayloadType(FLAG_cn_swb48));
326 RTC_CHECK(ValidateSsrcValue(FLAG_ssrc));
327 RTC_CHECK(ValidateExtensionId(FLAG_audio_level));
328 RTC_CHECK(ValidateExtensionId(FLAG_abs_send_time));
329 RTC_CHECK(ValidateExtensionId(FLAG_transport_seq_no));
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200330 RTC_CHECK(ValidateExtensionId(FLAG_video_content_type));
331 RTC_CHECK(ValidateExtensionId(FLAG_video_timing));
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000332
henrik.lundin8a6a6002016-08-25 00:46:36 -0700333 // Gather RTP header extensions in a map.
334 NetEqPacketSourceInput::RtpHeaderExtensionMap rtp_ext_map = {
oprypin6e09d872017-08-31 03:21:39 -0700335 {FLAG_audio_level, kRtpExtensionAudioLevel},
336 {FLAG_abs_send_time, kRtpExtensionAbsoluteSendTime},
Henrik Lundin4e268ed2018-05-08 16:36:33 +0200337 {FLAG_transport_seq_no, kRtpExtensionTransportSequenceNumber},
338 {FLAG_video_content_type, kRtpExtensionVideoContentType},
339 {FLAG_video_timing, kRtpExtensionVideoTiming}};
henrik.lundin8a6a6002016-08-25 00:46:36 -0700340
henrik.lundine8a77e32016-06-22 06:34:03 -0700341 const std::string input_file_name = argv[1];
342 std::unique_ptr<NetEqInput> input;
343 if (RtpFileSource::ValidRtpDump(input_file_name) ||
344 RtpFileSource::ValidPcap(input_file_name)) {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700345 input.reset(new NetEqRtpDumpInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700346 } else {
henrik.lundin8a6a6002016-08-25 00:46:36 -0700347 input.reset(new NetEqEventLogInput(input_file_name, rtp_ext_map));
ivoccaa5f4b2015-09-08 03:28:46 -0700348 }
349
henrik.lundine8a77e32016-06-22 06:34:03 -0700350 std::cout << "Input file: " << input_file_name << std::endl;
351 RTC_CHECK(input) << "Cannot open input file";
352 RTC_CHECK(!input->ended()) << "Input file is empty";
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000353
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000354 // Check if an SSRC value was provided.
oprypin6e09d872017-08-31 03:21:39 -0700355 if (strlen(FLAG_ssrc) > 0) {
henrik.lundin@webrtc.org8b65d512014-10-07 05:30:04 +0000356 uint32_t ssrc;
oprypin6e09d872017-08-31 03:21:39 -0700357 RTC_CHECK(ParseSsrc(FLAG_ssrc, &ssrc)) << "Flag verification has failed.";
Minyue Lib563f3d2018-05-11 16:49:38 +0200358 static_cast<NetEqPacketSourceInput*>(input.get())->SelectSsrc(ssrc);
ivoccaa5f4b2015-09-08 03:28:46 -0700359 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000360
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000361 // Check the sample rate.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200362 absl::optional<int> sample_rate_hz;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200363 std::set<std::pair<int, uint32_t>> discarded_pt_and_ssrc;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200364 while (absl::optional<RTPHeader> first_rtp_header = input->NextHeader()) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200365 RTC_DCHECK(first_rtp_header);
366 sample_rate_hz = CodecSampleRate(first_rtp_header->payloadType);
367 if (sample_rate_hz) {
368 std::cout << "Found valid packet with payload type "
369 << static_cast<int>(first_rtp_header->payloadType)
370 << " and SSRC 0x" << std::hex << first_rtp_header->ssrc
371 << std::dec << std::endl;
372 break;
373 }
374 // Discard this packet and move to the next. Keep track of discarded payload
375 // types and SSRCs.
376 discarded_pt_and_ssrc.emplace(first_rtp_header->payloadType,
377 first_rtp_header->ssrc);
378 input->PopPacket();
379 }
380 if (!discarded_pt_and_ssrc.empty()) {
381 std::cout << "Discarded initial packets with the following payload types "
382 "and SSRCs:"
383 << std::endl;
384 for (const auto& d : discarded_pt_and_ssrc) {
385 std::cout << "PT " << d.first << "; SSRC 0x" << std::hex
386 << static_cast<int>(d.second) << std::dec << std::endl;
387 }
388 }
389 if (!sample_rate_hz) {
390 std::cout << "Cannot find any packets with known payload types"
391 << std::endl;
392 RTC_NOTREACHED();
393 }
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000394
395 // Open the output file now that we know the sample rate. (Rate is only needed
396 // for wav files.)
henrik.lundine8a77e32016-06-22 06:34:03 -0700397 const std::string output_file_name = argv[2];
henrik.lundince5570e2016-05-24 06:14:57 -0700398 std::unique_ptr<AudioSink> output;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000399 if (output_file_name.size() >= 4 &&
400 output_file_name.substr(output_file_name.size() - 4) == ".wav") {
401 // Open a wav file.
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200402 output.reset(new OutputWavFile(output_file_name, *sample_rate_hz));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000403 } else {
404 // Open a pcm file.
henrik.lundince5570e2016-05-24 06:14:57 -0700405 output.reset(new OutputAudioFile(output_file_name));
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000406 }
407
henrik.lundine8a77e32016-06-22 06:34:03 -0700408 std::cout << "Output file: " << output_file_name << std::endl;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000409
henrik.lundine8a77e32016-06-22 06:34:03 -0700410 NetEqTest::DecoderMap codecs = {
Ivo Creusen80006b92018-08-10 11:19:21 +0200411 {FLAG_pcmu, std::make_pair(NetEqDecoder::kDecoderPCMu, "pcmu")},
412 {FLAG_pcma, std::make_pair(NetEqDecoder::kDecoderPCMa, "pcma")},
413#ifdef WEBRTC_CODEC_ILBC
414 {FLAG_ilbc, std::make_pair(NetEqDecoder::kDecoderILBC, "ilbc")},
415#endif
416 {FLAG_isac, std::make_pair(NetEqDecoder::kDecoderISAC, "isac")},
417#if !defined(WEBRTC_ANDROID)
418 {FLAG_isac_swb, std::make_pair(NetEqDecoder::kDecoderISACswb, "isac-swb")},
419#endif
420#ifdef WEBRTC_CODEC_OPUS
421 {FLAG_opus, std::make_pair(NetEqDecoder::kDecoderOpus, "opus")},
422#endif
423 {FLAG_pcm16b, std::make_pair(NetEqDecoder::kDecoderPCM16B, "pcm16-nb")},
424 {FLAG_pcm16b_wb,
425 std::make_pair(NetEqDecoder::kDecoderPCM16Bwb, "pcm16-wb")},
426 {FLAG_pcm16b_swb32,
427 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb32kHz, "pcm16-swb32")},
428 {FLAG_pcm16b_swb48,
429 std::make_pair(NetEqDecoder::kDecoderPCM16Bswb48kHz, "pcm16-swb48")},
430 {FLAG_g722, std::make_pair(NetEqDecoder::kDecoderG722, "g722")},
431 {FLAG_avt, std::make_pair(NetEqDecoder::kDecoderAVT, "avt")},
432 {FLAG_avt_16, std::make_pair(NetEqDecoder::kDecoderAVT16kHz, "avt-16")},
433 {FLAG_avt_32, std::make_pair(NetEqDecoder::kDecoderAVT32kHz, "avt-32")},
434 {FLAG_avt_48, std::make_pair(NetEqDecoder::kDecoderAVT48kHz, "avt-48")},
435 {FLAG_red, std::make_pair(NetEqDecoder::kDecoderRED, "red")},
436 {FLAG_cn_nb, std::make_pair(NetEqDecoder::kDecoderCNGnb, "cng-nb")},
437 {FLAG_cn_wb, std::make_pair(NetEqDecoder::kDecoderCNGwb, "cng-wb")},
438 {FLAG_cn_swb32,
439 std::make_pair(NetEqDecoder::kDecoderCNGswb32kHz, "cng-swb32")},
440 {FLAG_cn_swb48,
441 std::make_pair(NetEqDecoder::kDecoderCNGswb48kHz, "cng-swb48")}
442 };
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000443
henrik.lundine8a77e32016-06-22 06:34:03 -0700444 // Check if a replacement audio file was provided.
445 std::unique_ptr<AudioDecoder> replacement_decoder;
446 NetEqTest::ExtDecoderMap ext_codecs;
oprypin6e09d872017-08-31 03:21:39 -0700447 if (strlen(FLAG_replacement_audio_file) > 0) {
henrik.lundine8a77e32016-06-22 06:34:03 -0700448 // Find largest unused payload type.
449 int replacement_pt = 127;
450 while (!(codecs.find(replacement_pt) == codecs.end() &&
451 ext_codecs.find(replacement_pt) == ext_codecs.end())) {
452 --replacement_pt;
453 RTC_CHECK_GE(replacement_pt, 0);
454 }
455
456 auto std_set_int32_to_uint8 = [](const std::set<int32_t>& a) {
457 std::set<uint8_t> b;
458 for (auto& x : a) {
459 b.insert(static_cast<uint8_t>(x));
460 }
461 return b;
462 };
463
464 std::set<uint8_t> cn_types = std_set_int32_to_uint8(
oprypin6e09d872017-08-31 03:21:39 -0700465 {FLAG_cn_nb, FLAG_cn_wb, FLAG_cn_swb32, FLAG_cn_swb48});
Yves Gerey665174f2018-06-19 15:03:05 +0200466 std::set<uint8_t> forbidden_types = std_set_int32_to_uint8(
467 {FLAG_g722, FLAG_red, FLAG_avt, FLAG_avt_16, FLAG_avt_32, FLAG_avt_48});
henrik.lundine8a77e32016-06-22 06:34:03 -0700468 input.reset(new NetEqReplacementInput(std::move(input), replacement_pt,
469 cn_types, forbidden_types));
470
471 replacement_decoder.reset(new FakeDecodeFromFile(
472 std::unique_ptr<InputAudioFile>(
oprypin6e09d872017-08-31 03:21:39 -0700473 new InputAudioFile(FLAG_replacement_audio_file)),
henrik.lundine8a77e32016-06-22 06:34:03 -0700474 48000, false));
475 NetEqTest::ExternalDecoderInfo ext_dec_info = {
476 replacement_decoder.get(), NetEqDecoder::kDecoderArbitrary,
477 "replacement codec"};
478 ext_codecs[replacement_pt] = ext_dec_info;
479 }
480
henrik.lundin02739d92017-05-04 06:09:06 -0700481 NetEqTest::Callbacks callbacks;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200482 std::unique_ptr<NetEqDelayAnalyzer> delay_analyzer;
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100483 if (FLAG_matlabplot || FLAG_pythonplot) {
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200484 delay_analyzer.reset(new NetEqDelayAnalyzer);
485 }
486
487 SsrcSwitchDetector ssrc_switch_detector(delay_analyzer.get());
488 callbacks.post_insert_packet = &ssrc_switch_detector;
Minyue Li2b415da2018-04-16 14:33:53 +0200489 NetEqStatsGetter stats_getter(std::move(delay_analyzer));
Henrik Lundina2af0002017-06-20 16:54:39 +0200490 callbacks.get_audio_callback = &stats_getter;
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000491 NetEq::Config config;
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200492 config.sample_rate_hz = *sample_rate_hz;
henrik.lundine8a77e32016-06-22 06:34:03 -0700493 NetEqTest test(config, codecs, ext_codecs, std::move(input),
henrik.lundin02739d92017-05-04 06:09:06 -0700494 std::move(output), callbacks);
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000495
henrik.lundine8a77e32016-06-22 06:34:03 -0700496 int64_t test_duration_ms = test.Run();
henrik.lundin@webrtc.org03499a02014-11-24 14:50:53 +0000497
oprypin6e09d872017-08-31 03:21:39 -0700498 if (FLAG_matlabplot) {
henrik.lundinf09c9042017-08-29 09:14:08 -0700499 auto matlab_script_name = output_file_name;
500 std::replace(matlab_script_name.begin(), matlab_script_name.end(), '.',
501 '_');
502 std::cout << "Creating Matlab plot script " << matlab_script_name + ".m"
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200503 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200504 stats_getter.delay_analyzer()->CreateMatlabScript(matlab_script_name +
505 ".m");
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200506 }
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100507 if (FLAG_pythonplot) {
508 auto python_script_name = output_file_name;
509 std::replace(python_script_name.begin(), python_script_name.end(), '.',
510 '_');
511 std::cout << "Creating Python plot script " << python_script_name + ".py"
512 << std::endl;
Minyue Li5ebb4162018-05-08 22:42:00 +0200513 stats_getter.delay_analyzer()->CreatePythonScript(python_script_name +
514 ".py");
Ivo Creusend1d8dfb2017-12-06 10:48:10 +0100515 }
Henrik Lundin0bc0ccd2017-06-20 14:48:50 +0200516
henrik.lundine8a77e32016-06-22 06:34:03 -0700517 printf("Simulation statistics:\n");
518 printf(" output duration: %" PRId64 " ms\n", test_duration_ms);
Henrik Lundina2af0002017-06-20 16:54:39 +0200519 auto stats = stats_getter.AverageStats();
520 printf(" packet_loss_rate: %f %%\n", 100.0 * stats.packet_loss_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200521 printf(" expand_rate: %f %%\n", 100.0 * stats.expand_rate);
522 printf(" speech_expand_rate: %f %%\n", 100.0 * stats.speech_expand_rate);
523 printf(" preemptive_rate: %f %%\n", 100.0 * stats.preemptive_rate);
524 printf(" accelerate_rate: %f %%\n", 100.0 * stats.accelerate_rate);
henrik.lundine8a77e32016-06-22 06:34:03 -0700525 printf(" secondary_decoded_rate: %f %%\n",
Henrik Lundina2af0002017-06-20 16:54:39 +0200526 100.0 * stats.secondary_decoded_rate);
minyue-webrtc0c3ca752017-08-23 15:59:38 +0200527 printf(" secondary_discarded_rate: %f %%\n",
528 100.0 * stats.secondary_discarded_rate);
Henrik Lundina2af0002017-06-20 16:54:39 +0200529 printf(" clockdrift_ppm: %f ppm\n", stats.clockdrift_ppm);
530 printf(" mean_waiting_time_ms: %f ms\n", stats.mean_waiting_time_ms);
531 printf(" median_waiting_time_ms: %f ms\n", stats.median_waiting_time_ms);
532 printf(" min_waiting_time_ms: %f ms\n", stats.min_waiting_time_ms);
533 printf(" max_waiting_time_ms: %f ms\n", stats.max_waiting_time_ms);
Henrik Lundin156af4a2017-11-17 16:46:18 +0100534 printf(" current_buffer_size_ms: %f ms\n", stats.current_buffer_size_ms);
535 printf(" preferred_buffer_size_ms: %f ms\n", stats.preferred_buffer_size_ms);
Alex Narest7ff6ca52018-02-07 18:46:33 +0100536 if (FLAG_concealment_events) {
Minyue Li2b415da2018-04-16 14:33:53 +0200537 std::cout << " concealment_events_ms:" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100538 for (auto concealment_event : stats_getter.concealment_events())
Minyue Li2b415da2018-04-16 14:33:53 +0200539 std::cout << concealment_event.ToString() << std::endl;
540 std::cout << " end of concealment_events_ms" << std::endl;
Alex Narest7ff6ca52018-02-07 18:46:33 +0100541 }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000542 return 0;
543}
henrik.lundince5570e2016-05-24 06:14:57 -0700544
henrik.lundin303d3e12016-05-26 05:56:03 -0700545} // namespace
henrik.lundince5570e2016-05-24 06:14:57 -0700546} // namespace test
547} // namespace webrtc
henrik.lundin303d3e12016-05-26 05:56:03 -0700548
549int main(int argc, char* argv[]) {
Robin Raymond1c62ffa2017-12-03 16:45:56 -0500550 return webrtc::test::RunTest(argc, argv);
henrik.lundin303d3e12016-05-26 05:56:03 -0700551}