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 <assert.h>
12 #include <stdio.h>
13 #include <vector>
14
15 #include "google/gflags.h"
16 #include "webrtc/base/scoped_ptr.h"
17 #include "webrtc/modules/audio_coding/neteq/tools/packet.h"
18 #include "webrtc/modules/audio_coding/neteq/tools/rtp_file_source.h"
19
20 // Flag validator.
ValidatePayloadType(const char * flagname,int32_t value)21 static bool ValidatePayloadType(const char* flagname, int32_t value) {
22 if (value >= 0 && value <= 127) // Value is ok.
23 return true;
24 printf("Invalid value for --%s: %d\n", flagname, static_cast<int>(value));
25 return false;
26 }
ValidateExtensionId(const char * flagname,int32_t value)27 static bool ValidateExtensionId(const char* flagname, int32_t value) {
28 if (value > 0 && value <= 255) // Value is ok.
29 return true;
30 printf("Invalid value for --%s: %d\n", flagname, static_cast<int>(value));
31 return false;
32 }
33
34 // Define command line flags.
35 DEFINE_int32(red, 117, "RTP payload type for RED");
36 static const bool red_dummy =
37 google::RegisterFlagValidator(&FLAGS_red, &ValidatePayloadType);
38 DEFINE_int32(audio_level, 1, "Extension ID for audio level (RFC 6464)");
39 static const bool audio_level_dummy =
40 google::RegisterFlagValidator(&FLAGS_audio_level, &ValidateExtensionId);
41 DEFINE_int32(abs_send_time, 3, "Extension ID for absolute sender time");
42 static const bool abs_send_time_dummy =
43 google::RegisterFlagValidator(&FLAGS_abs_send_time, &ValidateExtensionId);
44
main(int argc,char * argv[])45 int main(int argc, char* argv[]) {
46 std::string program_name = argv[0];
47 std::string usage =
48 "Tool for parsing an RTP dump file to text output.\n"
49 "Run " +
50 program_name +
51 " --helpshort for usage.\n"
52 "Example usage:\n" +
53 program_name + " input.rtp output.txt\n\n" +
54 "Output is sent to stdout if no output file is given." +
55 "Note that this tool can read files with our without payloads.";
56 google::SetUsageMessage(usage);
57 google::ParseCommandLineFlags(&argc, &argv, true);
58
59 if (argc != 2 && argc != 3) {
60 // Print usage information.
61 printf("%s", google::ProgramUsage());
62 return 0;
63 }
64
65 printf("Input file: %s\n", argv[1]);
66 rtc::scoped_ptr<webrtc::test::RtpFileSource> file_source(
67 webrtc::test::RtpFileSource::Create(argv[1]));
68 assert(file_source.get());
69 // Set RTP extension IDs.
70 bool print_audio_level = false;
71 if (!google::GetCommandLineFlagInfoOrDie("audio_level").is_default) {
72 print_audio_level = true;
73 file_source->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel,
74 FLAGS_audio_level);
75 }
76 bool print_abs_send_time = false;
77 if (!google::GetCommandLineFlagInfoOrDie("abs_send_time").is_default) {
78 print_abs_send_time = true;
79 file_source->RegisterRtpHeaderExtension(
80 webrtc::kRtpExtensionAbsoluteSendTime, FLAGS_abs_send_time);
81 }
82
83 FILE* out_file;
84 if (argc == 3) {
85 out_file = fopen(argv[2], "wt");
86 if (!out_file) {
87 printf("Cannot open output file %s\n", argv[2]);
88 return -1;
89 }
90 printf("Output file: %s\n\n", argv[2]);
91 } else {
92 out_file = stdout;
93 }
94
95 // Print file header.
96 fprintf(out_file, "SeqNo TimeStamp SendTime Size PT M SSRC");
97 if (print_audio_level) {
98 fprintf(out_file, " AuLvl (V)");
99 }
100 if (print_abs_send_time) {
101 fprintf(out_file, " AbsSendTime");
102 }
103 fprintf(out_file, "\n");
104
105 uint32_t max_abs_send_time = 0;
106 int cycles = -1;
107 rtc::scoped_ptr<webrtc::test::Packet> packet;
108 while (true) {
109 packet.reset(file_source->NextPacket());
110 if (!packet.get()) {
111 // End of file reached.
112 break;
113 }
114 // Write packet data to file. Use virtual_packet_length_bytes so that the
115 // correct packet sizes are printed also for RTP header-only dumps.
116 fprintf(out_file,
117 "%5u %10u %10u %5i %5i %2i %#08X",
118 packet->header().sequenceNumber,
119 packet->header().timestamp,
120 static_cast<unsigned int>(packet->time_ms()),
121 static_cast<int>(packet->virtual_packet_length_bytes()),
122 packet->header().payloadType,
123 packet->header().markerBit,
124 packet->header().ssrc);
125 if (print_audio_level && packet->header().extension.hasAudioLevel) {
126 fprintf(out_file,
127 " %5u (%1i)",
128 packet->header().extension.audioLevel,
129 packet->header().extension.voiceActivity);
130 }
131 if (print_abs_send_time && packet->header().extension.hasAbsoluteSendTime) {
132 if (cycles == -1) {
133 // Initialize.
134 max_abs_send_time = packet->header().extension.absoluteSendTime;
135 cycles = 0;
136 }
137 // Abs sender time is 24 bit 6.18 fixed point. Shift by 8 to normalize to
138 // 32 bits (unsigned). Calculate the difference between this packet's
139 // send time and the maximum observed. Cast to signed 32-bit to get the
140 // desired wrap-around behavior.
141 if (static_cast<int32_t>(
142 (packet->header().extension.absoluteSendTime << 8) -
143 (max_abs_send_time << 8)) >= 0) {
144 // The difference is non-negative, meaning that this packet is newer
145 // than the previously observed maximum absolute send time.
146 if (packet->header().extension.absoluteSendTime < max_abs_send_time) {
147 // Wrap detected.
148 cycles++;
149 }
150 max_abs_send_time = packet->header().extension.absoluteSendTime;
151 }
152 // Abs sender time is 24 bit 6.18 fixed point. Divide by 2^18 to convert
153 // to floating point representation.
154 double send_time_seconds =
155 static_cast<double>(packet->header().extension.absoluteSendTime) /
156 262144 +
157 64.0 * cycles;
158 fprintf(out_file, " %11f", send_time_seconds);
159 }
160 fprintf(out_file, "\n");
161
162 if (packet->header().payloadType == FLAGS_red) {
163 std::list<webrtc::RTPHeader*> red_headers;
164 packet->ExtractRedHeaders(&red_headers);
165 while (!red_headers.empty()) {
166 webrtc::RTPHeader* red = red_headers.front();
167 assert(red);
168 fprintf(out_file,
169 "* %5u %10u %10u %5i\n",
170 red->sequenceNumber,
171 red->timestamp,
172 static_cast<unsigned int>(packet->time_ms()),
173 red->payloadType);
174 red_headers.pop_front();
175 delete red;
176 }
177 }
178 }
179
180 fclose(out_file);
181
182 return 0;
183 }
184