blob: 7ecf925ebba00b6bdfa8f8fed30f26ec69c76198 [file] [log] [blame]
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +00001/*
2 * Copyright (c) 2012 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.org184b9132014-04-02 20:56:17 +000011#include <stdio.h>
kwiberg2d0c3322016-02-14 09:28:33 -080012
13#include <memory>
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000014#include <vector>
15
Mirko Bonadei14be7992019-06-27 15:59:09 +020016#include "absl/flags/flag.h"
17#include "absl/flags/parse.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_coding/neteq/tools/packet.h"
19#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000020
Mirko Bonadei14be7992019-06-27 15:59:09 +020021ABSL_FLAG(int, red, 117, "RTP payload type for RED");
22ABSL_FLAG(int,
23 audio_level,
24 -1,
25 "Extension ID for audio level (RFC 6464); "
26 "-1 not to print audio level");
27ABSL_FLAG(int,
28 abs_send_time,
29 -1,
30 "Extension ID for absolute sender time; "
31 "-1 not to print absolute send time");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000032
33int main(int argc, char* argv[]) {
Mirko Bonadei14be7992019-06-27 15:59:09 +020034 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000035 std::string usage =
36 "Tool for parsing an RTP dump file to text output.\n"
Mirko Bonadei14be7992019-06-27 15:59:09 +020037 "Example usage:\n"
38 "./rtp_analyze input.rtp output.txt\n\n"
39 "Output is sent to stdout if no output file is given. "
oprypin6e09d872017-08-31 03:21:39 -070040 "Note that this tool can read files with or without payloads.\n";
Mirko Bonadei14be7992019-06-27 15:59:09 +020041 if (args.size() != 2 && args.size() != 3) {
oprypin6e09d872017-08-31 03:21:39 -070042 printf("%s", usage.c_str());
oprypin6e09d872017-08-31 03:21:39 -070043 return 1;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000044 }
45
Mirko Bonadei14be7992019-06-27 15:59:09 +020046 RTC_CHECK(absl::GetFlag(FLAGS_red) >= 0 &&
47 absl::GetFlag(FLAGS_red) <= 127); // Payload type
48 RTC_CHECK(absl::GetFlag(FLAGS_audio_level) == -1 || // Default
49 (absl::GetFlag(FLAGS_audio_level) > 0 &&
50 absl::GetFlag(FLAGS_audio_level) <= 255)); // Extension ID
51 RTC_CHECK(absl::GetFlag(FLAGS_abs_send_time) == -1 || // Default
52 (absl::GetFlag(FLAGS_abs_send_time) > 0 &&
53 absl::GetFlag(FLAGS_abs_send_time) <= 255)); // Extension ID
oprypin6e09d872017-08-31 03:21:39 -070054
Mirko Bonadei14be7992019-06-27 15:59:09 +020055 printf("Input file: %s\n", args[1]);
kwiberg2d0c3322016-02-14 09:28:33 -080056 std::unique_ptr<webrtc::test::RtpFileSource> file_source(
Mirko Bonadei14be7992019-06-27 15:59:09 +020057 webrtc::test::RtpFileSource::Create(args[1]));
Mirko Bonadei25ab3222021-07-08 20:08:20 +020058 RTC_DCHECK(file_source.get());
Henrik Lundinb0b54252015-04-17 11:47:07 +020059 // Set RTP extension IDs.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000060 bool print_audio_level = false;
Mirko Bonadei14be7992019-06-27 15:59:09 +020061 if (absl::GetFlag(FLAGS_audio_level) != -1) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000062 print_audio_level = true;
63 file_source->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel,
Mirko Bonadei14be7992019-06-27 15:59:09 +020064 absl::GetFlag(FLAGS_audio_level));
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000065 }
Henrik Lundinb0b54252015-04-17 11:47:07 +020066 bool print_abs_send_time = false;
Mirko Bonadei14be7992019-06-27 15:59:09 +020067 if (absl::GetFlag(FLAGS_abs_send_time) != -1) {
Henrik Lundinb0b54252015-04-17 11:47:07 +020068 print_abs_send_time = true;
69 file_source->RegisterRtpHeaderExtension(
Mirko Bonadei14be7992019-06-27 15:59:09 +020070 webrtc::kRtpExtensionAbsoluteSendTime,
71 absl::GetFlag(FLAGS_abs_send_time));
Henrik Lundinb0b54252015-04-17 11:47:07 +020072 }
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000073
74 FILE* out_file;
Mirko Bonadei14be7992019-06-27 15:59:09 +020075 if (args.size() == 3) {
76 out_file = fopen(args[2], "wt");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000077 if (!out_file) {
Mirko Bonadei14be7992019-06-27 15:59:09 +020078 printf("Cannot open output file %s\n", args[2]);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000079 return -1;
80 }
Mirko Bonadei14be7992019-06-27 15:59:09 +020081 printf("Output file: %s\n\n", args[2]);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000082 } else {
83 out_file = stdout;
84 }
85
86 // Print file header.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000087 fprintf(out_file, "SeqNo TimeStamp SendTime Size PT M SSRC");
88 if (print_audio_level) {
89 fprintf(out_file, " AuLvl (V)");
90 }
Henrik Lundinb0b54252015-04-17 11:47:07 +020091 if (print_abs_send_time) {
92 fprintf(out_file, " AbsSendTime");
93 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000094 fprintf(out_file, "\n");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000095
Henrik Lundinb0b54252015-04-17 11:47:07 +020096 uint32_t max_abs_send_time = 0;
97 int cycles = -1;
kwiberg2d0c3322016-02-14 09:28:33 -080098 std::unique_ptr<webrtc::test::Packet> packet;
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:38 +000099 while (true) {
henrik.lundin46ba49c2016-05-24 22:50:47 -0700100 packet = file_source->NextPacket();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000101 if (!packet.get()) {
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:38 +0000102 // End of file reached.
103 break;
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000104 }
Henrik Lundinb0b54252015-04-17 11:47:07 +0200105 // Write packet data to file. Use virtual_packet_length_bytes so that the
106 // correct packet sizes are printed also for RTP header-only dumps.
Yves Gerey665174f2018-06-19 15:03:05 +0200107 fprintf(out_file, "%5u %10u %10u %5i %5i %2i %#08X",
108 packet->header().sequenceNumber, packet->header().timestamp,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000109 static_cast<unsigned int>(packet->time_ms()),
Henrik Lundinb0b54252015-04-17 11:47:07 +0200110 static_cast<int>(packet->virtual_packet_length_bytes()),
Yves Gerey665174f2018-06-19 15:03:05 +0200111 packet->header().payloadType, packet->header().markerBit,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000112 packet->header().ssrc);
113 if (print_audio_level && packet->header().extension.hasAudioLevel) {
Yves Gerey665174f2018-06-19 15:03:05 +0200114 fprintf(out_file, " %5u (%1i)", packet->header().extension.audioLevel,
Minyue4cee4192015-08-10 15:08:36 +0200115 packet->header().extension.voiceActivity);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000116 }
Henrik Lundinb0b54252015-04-17 11:47:07 +0200117 if (print_abs_send_time && packet->header().extension.hasAbsoluteSendTime) {
118 if (cycles == -1) {
119 // Initialize.
120 max_abs_send_time = packet->header().extension.absoluteSendTime;
121 cycles = 0;
122 }
123 // Abs sender time is 24 bit 6.18 fixed point. Shift by 8 to normalize to
124 // 32 bits (unsigned). Calculate the difference between this packet's
125 // send time and the maximum observed. Cast to signed 32-bit to get the
126 // desired wrap-around behavior.
127 if (static_cast<int32_t>(
128 (packet->header().extension.absoluteSendTime << 8) -
129 (max_abs_send_time << 8)) >= 0) {
130 // The difference is non-negative, meaning that this packet is newer
131 // than the previously observed maximum absolute send time.
132 if (packet->header().extension.absoluteSendTime < max_abs_send_time) {
133 // Wrap detected.
134 cycles++;
135 }
136 max_abs_send_time = packet->header().extension.absoluteSendTime;
137 }
138 // Abs sender time is 24 bit 6.18 fixed point. Divide by 2^18 to convert
139 // to floating point representation.
140 double send_time_seconds =
141 static_cast<double>(packet->header().extension.absoluteSendTime) /
142 262144 +
143 64.0 * cycles;
144 fprintf(out_file, " %11f", send_time_seconds);
145 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000146 fprintf(out_file, "\n");
147
Mirko Bonadei14be7992019-06-27 15:59:09 +0200148 if (packet->header().payloadType == absl::GetFlag(FLAGS_red)) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000149 std::list<webrtc::RTPHeader*> red_headers;
150 packet->ExtractRedHeaders(&red_headers);
151 while (!red_headers.empty()) {
152 webrtc::RTPHeader* red = red_headers.front();
Mirko Bonadei25ab3222021-07-08 20:08:20 +0200153 RTC_DCHECK(red);
Yves Gerey665174f2018-06-19 15:03:05 +0200154 fprintf(out_file, "* %5u %10u %10u %5i\n", red->sequenceNumber,
155 red->timestamp, static_cast<unsigned int>(packet->time_ms()),
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000156 red->payloadType);
157 red_headers.pop_front();
158 delete red;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000159 }
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000160 }
161 }
162
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000163 fclose(out_file);
164
165 return 0;
166}