blob: dad3750940b2b2dc830d75bd85883fbaff89d74e [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
11#include <assert.h>
12#include <stdio.h>
kwiberg2d0c3322016-02-14 09:28:33 -080013
14#include <memory>
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000015#include <vector>
16
Mirko Bonadei14be7992019-06-27 15:59:09 +020017#include "absl/flags/flag.h"
18#include "absl/flags/parse.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_coding/neteq/tools/packet.h"
20#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000021
Mirko Bonadei14be7992019-06-27 15:59:09 +020022ABSL_FLAG(int, red, 117, "RTP payload type for RED");
23ABSL_FLAG(int,
24 audio_level,
25 -1,
26 "Extension ID for audio level (RFC 6464); "
27 "-1 not to print audio level");
28ABSL_FLAG(int,
29 abs_send_time,
30 -1,
31 "Extension ID for absolute sender time; "
32 "-1 not to print absolute send time");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000033
34int main(int argc, char* argv[]) {
Mirko Bonadei14be7992019-06-27 15:59:09 +020035 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000036 std::string usage =
37 "Tool for parsing an RTP dump file to text output.\n"
Mirko Bonadei14be7992019-06-27 15:59:09 +020038 "Example usage:\n"
39 "./rtp_analyze input.rtp output.txt\n\n"
40 "Output is sent to stdout if no output file is given. "
oprypin6e09d872017-08-31 03:21:39 -070041 "Note that this tool can read files with or without payloads.\n";
Mirko Bonadei14be7992019-06-27 15:59:09 +020042 if (args.size() != 2 && args.size() != 3) {
oprypin6e09d872017-08-31 03:21:39 -070043 printf("%s", usage.c_str());
oprypin6e09d872017-08-31 03:21:39 -070044 return 1;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000045 }
46
Mirko Bonadei14be7992019-06-27 15:59:09 +020047 RTC_CHECK(absl::GetFlag(FLAGS_red) >= 0 &&
48 absl::GetFlag(FLAGS_red) <= 127); // Payload type
49 RTC_CHECK(absl::GetFlag(FLAGS_audio_level) == -1 || // Default
50 (absl::GetFlag(FLAGS_audio_level) > 0 &&
51 absl::GetFlag(FLAGS_audio_level) <= 255)); // Extension ID
52 RTC_CHECK(absl::GetFlag(FLAGS_abs_send_time) == -1 || // Default
53 (absl::GetFlag(FLAGS_abs_send_time) > 0 &&
54 absl::GetFlag(FLAGS_abs_send_time) <= 255)); // Extension ID
oprypin6e09d872017-08-31 03:21:39 -070055
Mirko Bonadei14be7992019-06-27 15:59:09 +020056 printf("Input file: %s\n", args[1]);
kwiberg2d0c3322016-02-14 09:28:33 -080057 std::unique_ptr<webrtc::test::RtpFileSource> file_source(
Mirko Bonadei14be7992019-06-27 15:59:09 +020058 webrtc::test::RtpFileSource::Create(args[1]));
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000059 assert(file_source.get());
Henrik Lundinb0b54252015-04-17 11:47:07 +020060 // Set RTP extension IDs.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000061 bool print_audio_level = false;
Mirko Bonadei14be7992019-06-27 15:59:09 +020062 if (absl::GetFlag(FLAGS_audio_level) != -1) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000063 print_audio_level = true;
64 file_source->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel,
Mirko Bonadei14be7992019-06-27 15:59:09 +020065 absl::GetFlag(FLAGS_audio_level));
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000066 }
Henrik Lundinb0b54252015-04-17 11:47:07 +020067 bool print_abs_send_time = false;
Mirko Bonadei14be7992019-06-27 15:59:09 +020068 if (absl::GetFlag(FLAGS_abs_send_time) != -1) {
Henrik Lundinb0b54252015-04-17 11:47:07 +020069 print_abs_send_time = true;
70 file_source->RegisterRtpHeaderExtension(
Mirko Bonadei14be7992019-06-27 15:59:09 +020071 webrtc::kRtpExtensionAbsoluteSendTime,
72 absl::GetFlag(FLAGS_abs_send_time));
Henrik Lundinb0b54252015-04-17 11:47:07 +020073 }
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000074
75 FILE* out_file;
Mirko Bonadei14be7992019-06-27 15:59:09 +020076 if (args.size() == 3) {
77 out_file = fopen(args[2], "wt");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000078 if (!out_file) {
Mirko Bonadei14be7992019-06-27 15:59:09 +020079 printf("Cannot open output file %s\n", args[2]);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000080 return -1;
81 }
Mirko Bonadei14be7992019-06-27 15:59:09 +020082 printf("Output file: %s\n\n", args[2]);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000083 } else {
84 out_file = stdout;
85 }
86
87 // Print file header.
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000088 fprintf(out_file, "SeqNo TimeStamp SendTime Size PT M SSRC");
89 if (print_audio_level) {
90 fprintf(out_file, " AuLvl (V)");
91 }
Henrik Lundinb0b54252015-04-17 11:47:07 +020092 if (print_abs_send_time) {
93 fprintf(out_file, " AbsSendTime");
94 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000095 fprintf(out_file, "\n");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000096
Henrik Lundinb0b54252015-04-17 11:47:07 +020097 uint32_t max_abs_send_time = 0;
98 int cycles = -1;
kwiberg2d0c3322016-02-14 09:28:33 -080099 std::unique_ptr<webrtc::test::Packet> packet;
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:38 +0000100 while (true) {
henrik.lundin46ba49c2016-05-24 22:50:47 -0700101 packet = file_source->NextPacket();
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000102 if (!packet.get()) {
henrik.lundin@webrtc.org4b133da2014-10-02 08:19:38 +0000103 // End of file reached.
104 break;
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000105 }
Henrik Lundinb0b54252015-04-17 11:47:07 +0200106 // Write packet data to file. Use virtual_packet_length_bytes so that the
107 // correct packet sizes are printed also for RTP header-only dumps.
Yves Gerey665174f2018-06-19 15:03:05 +0200108 fprintf(out_file, "%5u %10u %10u %5i %5i %2i %#08X",
109 packet->header().sequenceNumber, packet->header().timestamp,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000110 static_cast<unsigned int>(packet->time_ms()),
Henrik Lundinb0b54252015-04-17 11:47:07 +0200111 static_cast<int>(packet->virtual_packet_length_bytes()),
Yves Gerey665174f2018-06-19 15:03:05 +0200112 packet->header().payloadType, packet->header().markerBit,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000113 packet->header().ssrc);
114 if (print_audio_level && packet->header().extension.hasAudioLevel) {
Yves Gerey665174f2018-06-19 15:03:05 +0200115 fprintf(out_file, " %5u (%1i)", packet->header().extension.audioLevel,
Minyue4cee4192015-08-10 15:08:36 +0200116 packet->header().extension.voiceActivity);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000117 }
Henrik Lundinb0b54252015-04-17 11:47:07 +0200118 if (print_abs_send_time && packet->header().extension.hasAbsoluteSendTime) {
119 if (cycles == -1) {
120 // Initialize.
121 max_abs_send_time = packet->header().extension.absoluteSendTime;
122 cycles = 0;
123 }
124 // Abs sender time is 24 bit 6.18 fixed point. Shift by 8 to normalize to
125 // 32 bits (unsigned). Calculate the difference between this packet's
126 // send time and the maximum observed. Cast to signed 32-bit to get the
127 // desired wrap-around behavior.
128 if (static_cast<int32_t>(
129 (packet->header().extension.absoluteSendTime << 8) -
130 (max_abs_send_time << 8)) >= 0) {
131 // The difference is non-negative, meaning that this packet is newer
132 // than the previously observed maximum absolute send time.
133 if (packet->header().extension.absoluteSendTime < max_abs_send_time) {
134 // Wrap detected.
135 cycles++;
136 }
137 max_abs_send_time = packet->header().extension.absoluteSendTime;
138 }
139 // Abs sender time is 24 bit 6.18 fixed point. Divide by 2^18 to convert
140 // to floating point representation.
141 double send_time_seconds =
142 static_cast<double>(packet->header().extension.absoluteSendTime) /
143 262144 +
144 64.0 * cycles;
145 fprintf(out_file, " %11f", send_time_seconds);
146 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000147 fprintf(out_file, "\n");
148
Mirko Bonadei14be7992019-06-27 15:59:09 +0200149 if (packet->header().payloadType == absl::GetFlag(FLAGS_red)) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000150 std::list<webrtc::RTPHeader*> red_headers;
151 packet->ExtractRedHeaders(&red_headers);
152 while (!red_headers.empty()) {
153 webrtc::RTPHeader* red = red_headers.front();
154 assert(red);
Yves Gerey665174f2018-06-19 15:03:05 +0200155 fprintf(out_file, "* %5u %10u %10u %5i\n", red->sequenceNumber,
156 red->timestamp, static_cast<unsigned int>(packet->time_ms()),
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000157 red->payloadType);
158 red_headers.pop_front();
159 delete red;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000160 }
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000161 }
162 }
163
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000164 fclose(out_file);
165
166 return 0;
167}