1 /*
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 <stdio.h>
12
13 #include <memory>
14 #include <vector>
15
16 #include "absl/flags/flag.h"
17 #include "absl/flags/parse.h"
18 #include "modules/audio_coding/neteq/tools/packet.h"
19 #include "modules/audio_coding/neteq/tools/rtp_file_source.h"
20
21 ABSL_FLAG(int, red, 117, "RTP payload type for RED");
22 ABSL_FLAG(int,
23 audio_level,
24 -1,
25 "Extension ID for audio level (RFC 6464); "
26 "-1 not to print audio level");
27 ABSL_FLAG(int,
28 abs_send_time,
29 -1,
30 "Extension ID for absolute sender time; "
31 "-1 not to print absolute send time");
32
main(int argc,char * argv[])33 int main(int argc, char* argv[]) {
34 std::vector<char*> args = absl::ParseCommandLine(argc, argv);
35 std::string usage =
36 "Tool for parsing an RTP dump file to text output.\n"
37 "Example usage:\n"
38 "./rtp_analyze input.rtp output.txt\n\n"
39 "Output is sent to stdout if no output file is given. "
40 "Note that this tool can read files with or without payloads.\n";
41 if (args.size() != 2 && args.size() != 3) {
42 printf("%s", usage.c_str());
43 return 1;
44 }
45
46 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
54
55 printf("Input file: %s\n", args[1]);
56 std::unique_ptr<webrtc::test::RtpFileSource> file_source(
57 webrtc::test::RtpFileSource::Create(args[1]));
58 RTC_DCHECK(file_source.get());
59 // Set RTP extension IDs.
60 bool print_audio_level = false;
61 if (absl::GetFlag(FLAGS_audio_level) != -1) {
62 print_audio_level = true;
63 file_source->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel,
64 absl::GetFlag(FLAGS_audio_level));
65 }
66 bool print_abs_send_time = false;
67 if (absl::GetFlag(FLAGS_abs_send_time) != -1) {
68 print_abs_send_time = true;
69 file_source->RegisterRtpHeaderExtension(
70 webrtc::kRtpExtensionAbsoluteSendTime,
71 absl::GetFlag(FLAGS_abs_send_time));
72 }
73
74 FILE* out_file;
75 if (args.size() == 3) {
76 out_file = fopen(args[2], "wt");
77 if (!out_file) {
78 printf("Cannot open output file %s\n", args[2]);
79 return -1;
80 }
81 printf("Output file: %s\n\n", args[2]);
82 } else {
83 out_file = stdout;
84 }
85
86 // Print file header.
87 fprintf(out_file, "SeqNo TimeStamp SendTime Size PT M SSRC");
88 if (print_audio_level) {
89 fprintf(out_file, " AuLvl (V)");
90 }
91 if (print_abs_send_time) {
92 fprintf(out_file, " AbsSendTime");
93 }
94 fprintf(out_file, "\n");
95
96 uint32_t max_abs_send_time = 0;
97 int cycles = -1;
98 std::unique_ptr<webrtc::test::Packet> packet;
99 while (true) {
100 packet = file_source->NextPacket();
101 if (!packet.get()) {
102 // End of file reached.
103 break;
104 }
105 // 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.
107 fprintf(out_file, "%5u %10u %10u %5i %5i %2i %#08X",
108 packet->header().sequenceNumber, packet->header().timestamp,
109 static_cast<unsigned int>(packet->time_ms()),
110 static_cast<int>(packet->virtual_packet_length_bytes()),
111 packet->header().payloadType, packet->header().markerBit,
112 packet->header().ssrc);
113 if (print_audio_level && packet->header().extension.hasAudioLevel) {
114 fprintf(out_file, " %5u (%1i)", packet->header().extension.audioLevel,
115 packet->header().extension.voiceActivity);
116 }
117 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 }
146 fprintf(out_file, "\n");
147
148 if (packet->header().payloadType == absl::GetFlag(FLAGS_red)) {
149 std::list<webrtc::RTPHeader*> red_headers;
150 packet->ExtractRedHeaders(&red_headers);
151 while (!red_headers.empty()) {
152 webrtc::RTPHeader* red = red_headers.front();
153 RTC_DCHECK(red);
154 fprintf(out_file, "* %5u %10u %10u %5i\n", red->sequenceNumber,
155 red->timestamp, static_cast<unsigned int>(packet->time_ms()),
156 red->payloadType);
157 red_headers.pop_front();
158 delete red;
159 }
160 }
161 }
162
163 fclose(out_file);
164
165 return 0;
166 }
167