blob: 12721ccc96d072bea8fd4ffdeef27472b10fd4a0 [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 Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/tools/packet.h"
18#include "modules/audio_coding/neteq/tools/rtp_file_source.h"
19#include "rtc_base/flags.h"
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000020
21// Define command line flags.
oprypin6e09d872017-08-31 03:21:39 -070022DEFINE_int(red, 117, "RTP payload type for RED");
23DEFINE_int(audio_level, -1, "Extension ID for audio level (RFC 6464); "
24 "-1 not to print audio level");
25DEFINE_int(abs_send_time, -1, "Extension ID for absolute sender time; "
26 "-1 not to print absolute send time");
27DEFINE_bool(help, false, "Print this message");
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000028
29int main(int argc, char* argv[]) {
30 std::string program_name = argv[0];
31 std::string usage =
32 "Tool for parsing an RTP dump file to text output.\n"
33 "Run " +
34 program_name +
oprypin6e09d872017-08-31 03:21:39 -070035 " --help for usage.\n"
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000036 "Example usage:\n" +
37 program_name + " input.rtp output.txt\n\n" +
oprypin6e09d872017-08-31 03:21:39 -070038 "Output is sent to stdout if no output file is given. " +
39 "Note that this tool can read files with or without payloads.\n";
40 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) ||
41 FLAG_help || (argc != 2 && argc != 3)) {
42 printf("%s", usage.c_str());
43 if (FLAG_help) {
44 rtc::FlagList::Print(nullptr, false);
45 return 0;
46 }
47 return 1;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000048 }
49
oprypin6e09d872017-08-31 03:21:39 -070050 RTC_CHECK(FLAG_red >= 0 && FLAG_red <= 127); // Payload type
51 RTC_CHECK(FLAG_audio_level == -1 || // Default
52 (FLAG_audio_level > 0 && FLAG_audio_level <= 255)); // Extension ID
53 RTC_CHECK(FLAG_abs_send_time == -1 || // Default
54 (FLAG_abs_send_time > 0 && FLAG_abs_send_time <= 255)); // Extension ID
55
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +000056 printf("Input file: %s\n", argv[1]);
kwiberg2d0c3322016-02-14 09:28:33 -080057 std::unique_ptr<webrtc::test::RtpFileSource> file_source(
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000058 webrtc::test::RtpFileSource::Create(argv[1]));
59 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;
oprypin6e09d872017-08-31 03:21:39 -070062 if (FLAG_audio_level != -1) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +000063 print_audio_level = true;
64 file_source->RegisterRtpHeaderExtension(webrtc::kRtpExtensionAudioLevel,
oprypin6e09d872017-08-31 03:21:39 -070065 FLAG_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;
oprypin6e09d872017-08-31 03:21:39 -070068 if (FLAG_abs_send_time != -1) {
Henrik Lundinb0b54252015-04-17 11:47:07 +020069 print_abs_send_time = true;
70 file_source->RegisterRtpHeaderExtension(
oprypin6e09d872017-08-31 03:21:39 -070071 webrtc::kRtpExtensionAbsoluteSendTime, FLAG_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;
75 if (argc == 3) {
76 out_file = fopen(argv[2], "wt");
77 if (!out_file) {
78 printf("Cannot open output file %s\n", argv[2]);
79 return -1;
80 }
81 printf("Output file: %s\n\n", argv[2]);
82 } 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.
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000107 fprintf(out_file,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000108 "%5u %10u %10u %5i %5i %2i %#08X",
109 packet->header().sequenceNumber,
110 packet->header().timestamp,
111 static_cast<unsigned int>(packet->time_ms()),
Henrik Lundinb0b54252015-04-17 11:47:07 +0200112 static_cast<int>(packet->virtual_packet_length_bytes()),
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000113 packet->header().payloadType,
114 packet->header().markerBit,
115 packet->header().ssrc);
116 if (print_audio_level && packet->header().extension.hasAudioLevel) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000117 fprintf(out_file,
118 " %5u (%1i)",
Minyue4cee4192015-08-10 15:08:36 +0200119 packet->header().extension.audioLevel,
120 packet->header().extension.voiceActivity);
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000121 }
Henrik Lundinb0b54252015-04-17 11:47:07 +0200122 if (print_abs_send_time && packet->header().extension.hasAbsoluteSendTime) {
123 if (cycles == -1) {
124 // Initialize.
125 max_abs_send_time = packet->header().extension.absoluteSendTime;
126 cycles = 0;
127 }
128 // Abs sender time is 24 bit 6.18 fixed point. Shift by 8 to normalize to
129 // 32 bits (unsigned). Calculate the difference between this packet's
130 // send time and the maximum observed. Cast to signed 32-bit to get the
131 // desired wrap-around behavior.
132 if (static_cast<int32_t>(
133 (packet->header().extension.absoluteSendTime << 8) -
134 (max_abs_send_time << 8)) >= 0) {
135 // The difference is non-negative, meaning that this packet is newer
136 // than the previously observed maximum absolute send time.
137 if (packet->header().extension.absoluteSendTime < max_abs_send_time) {
138 // Wrap detected.
139 cycles++;
140 }
141 max_abs_send_time = packet->header().extension.absoluteSendTime;
142 }
143 // Abs sender time is 24 bit 6.18 fixed point. Divide by 2^18 to convert
144 // to floating point representation.
145 double send_time_seconds =
146 static_cast<double>(packet->header().extension.absoluteSendTime) /
147 262144 +
148 64.0 * cycles;
149 fprintf(out_file, " %11f", send_time_seconds);
150 }
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000151 fprintf(out_file, "\n");
152
oprypin6e09d872017-08-31 03:21:39 -0700153 if (packet->header().payloadType == FLAG_red) {
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000154 std::list<webrtc::RTPHeader*> red_headers;
155 packet->ExtractRedHeaders(&red_headers);
156 while (!red_headers.empty()) {
157 webrtc::RTPHeader* red = red_headers.front();
158 assert(red);
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000159 fprintf(out_file,
henrik.lundin@webrtc.org810acbc2014-04-14 18:42:23 +0000160 "* %5u %10u %10u %5i\n",
161 red->sequenceNumber,
162 red->timestamp,
163 static_cast<unsigned int>(packet->time_ms()),
164 red->payloadType);
165 red_headers.pop_front();
166 delete red;
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000167 }
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000168 }
169 }
170
henrik.lundin@webrtc.org184b9132014-04-02 20:56:17 +0000171 fclose(out_file);
172
173 return 0;
174}