OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2015 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 <iostream> | |
12 #include <memory> | |
13 #include <sstream> | |
14 #include <string> | |
15 | |
16 #include "gflags/gflags.h" | |
17 #include "webrtc/base/checks.h" | |
18 #include "webrtc/call.h" | |
19 #include "webrtc/call/rtc_event_log.h" | |
20 #include "webrtc/call/rtc_event_log_parser.h" | |
21 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
22 #include "webrtc/test/rtp_file_writer.h" | |
23 | |
24 namespace { | |
25 | |
26 DEFINE_bool(noaudio, | |
27 false, | |
28 "Excludes audio packets from the converted RTPdump file."); | |
29 DEFINE_bool(novideo, | |
30 false, | |
31 "Excludes video packets from the converted RTPdump file."); | |
32 DEFINE_bool(nodata, | |
33 false, | |
34 "Excludes data packets from the converted RTPdump file."); | |
35 DEFINE_bool(nortp, | |
36 false, | |
37 "Excludes RTP packets from the converted RTPdump file."); | |
38 DEFINE_bool(nortcp, | |
39 false, | |
40 "Excludes RTCP packets from the converted RTPdump file."); | |
41 DEFINE_string(ssrc, | |
42 "", | |
43 "Store only packets with this SSRC (decimal or hex, the latter " | |
44 "starting with 0x)."); | |
45 | |
46 // Parses the input string for a valid SSRC. If a valid SSRC is found, it is | |
47 // written to the output variable |ssrc|, and true is returned. Otherwise, | |
48 // false is returned. | |
49 // The empty string must be validated as true, because it is the default value | |
50 // of the command-line flag. In this case, no value is written to the output | |
51 // variable. | |
52 bool ParseSsrc(std::string str, uint32_t* ssrc) { | |
53 // If the input string starts with 0x or 0X it indicates a hexadecimal number. | |
54 auto read_mode = std::dec; | |
55 if (str.size() > 2 && | |
56 (str.substr(0, 2) == "0x" || str.substr(0, 2) == "0X")) { | |
57 read_mode = std::hex; | |
58 str = str.substr(2); | |
59 } | |
60 std::stringstream ss(str); | |
61 ss >> read_mode >> *ssrc; | |
62 return str.empty() || (!ss.fail() && ss.eof()); | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 // This utility will convert a stored event log to the rtpdump format. | |
68 int main(int argc, char* argv[]) { | |
69 std::string program_name = argv[0]; | |
70 std::string usage = | |
71 "Tool for converting an RtcEventLog file to an RTP dump file.\n" | |
72 "Run " + | |
73 program_name + | |
74 " --helpshort for usage.\n" | |
75 "Example usage:\n" + | |
76 program_name + " input.rel output.rtp\n"; | |
77 google::SetUsageMessage(usage); | |
78 google::ParseCommandLineFlags(&argc, &argv, true); | |
79 | |
80 if (argc != 3) { | |
81 std::cout << google::ProgramUsage(); | |
82 return 0; | |
83 } | |
84 std::string input_file = argv[1]; | |
85 std::string output_file = argv[2]; | |
86 | |
87 uint32_t ssrc_filter = 0; | |
88 if (!FLAGS_ssrc.empty()) | |
89 RTC_CHECK(ParseSsrc(FLAGS_ssrc, &ssrc_filter)) | |
90 << "Flag verification has failed."; | |
91 | |
92 webrtc::ParsedRtcEventLog parsed_stream; | |
93 if (!parsed_stream.ParseFile(input_file)) { | |
94 std::cerr << "Error while parsing input file: " << input_file << std::endl; | |
95 return -1; | |
96 } | |
97 | |
98 std::unique_ptr<webrtc::test::RtpFileWriter> rtp_writer( | |
99 webrtc::test::RtpFileWriter::Create( | |
100 webrtc::test::RtpFileWriter::FileFormat::kRtpDump, output_file)); | |
101 | |
102 if (!rtp_writer.get()) { | |
103 std::cerr << "Error while opening output file: " << output_file | |
104 << std::endl; | |
105 return -1; | |
106 } | |
107 | |
108 std::cout << "Found " << parsed_stream.GetNumberOfEvents() | |
109 << " events in the input file." << std::endl; | |
110 int rtp_counter = 0, rtcp_counter = 0; | |
111 bool header_only = false; | |
112 for (size_t i = 0; i < parsed_stream.GetNumberOfEvents(); i++) { | |
113 // The parsed_stream will assert if the protobuf event is missing | |
114 // some required fields and we attempt to access them. We could consider | |
115 // a softer failure option, but it does not seem useful to generate | |
116 // RTP dumps based on broken event logs. | |
117 if (!FLAGS_nortp && | |
118 parsed_stream.GetEventType(i) == webrtc::ParsedRtcEventLog::RTP_EVENT) { | |
119 webrtc::test::RtpPacket packet; | |
120 webrtc::PacketDirection direction; | |
121 webrtc::MediaType media_type; | |
122 parsed_stream.GetRtpHeader(i, &direction, &media_type, packet.data, | |
123 &packet.length, &packet.original_length); | |
124 if (packet.original_length > packet.length) | |
125 header_only = true; | |
126 packet.time_ms = parsed_stream.GetTimestamp(i) / 1000; | |
127 | |
128 // TODO(terelius): Maybe add a flag to dump outgoing traffic instead? | |
129 if (direction == webrtc::kOutgoingPacket) | |
130 continue; | |
131 if (FLAGS_noaudio && media_type == webrtc::MediaType::AUDIO) | |
132 continue; | |
133 if (FLAGS_novideo && media_type == webrtc::MediaType::VIDEO) | |
134 continue; | |
135 if (FLAGS_nodata && media_type == webrtc::MediaType::DATA) | |
136 continue; | |
137 if (!FLAGS_ssrc.empty()) { | |
138 const uint32_t packet_ssrc = | |
139 webrtc::ByteReader<uint32_t>::ReadBigEndian( | |
140 reinterpret_cast<const uint8_t*>(packet.data + 8)); | |
141 if (packet_ssrc != ssrc_filter) | |
142 continue; | |
143 } | |
144 | |
145 rtp_writer->WritePacket(&packet); | |
146 rtp_counter++; | |
147 } | |
148 if (!FLAGS_nortcp && | |
149 parsed_stream.GetEventType(i) == | |
150 webrtc::ParsedRtcEventLog::RTCP_EVENT) { | |
151 webrtc::test::RtpPacket packet; | |
152 webrtc::PacketDirection direction; | |
153 webrtc::MediaType media_type; | |
154 parsed_stream.GetRtcpPacket(i, &direction, &media_type, packet.data, | |
155 &packet.length); | |
156 // For RTCP packets the original_length should be set to 0 in the | |
157 // RTPdump format. | |
158 packet.original_length = 0; | |
159 packet.time_ms = parsed_stream.GetTimestamp(i) / 1000; | |
160 | |
161 // TODO(terelius): Maybe add a flag to dump outgoing traffic instead? | |
162 if (direction == webrtc::kOutgoingPacket) | |
163 continue; | |
164 if (FLAGS_noaudio && media_type == webrtc::MediaType::AUDIO) | |
165 continue; | |
166 if (FLAGS_novideo && media_type == webrtc::MediaType::VIDEO) | |
167 continue; | |
168 if (FLAGS_nodata && media_type == webrtc::MediaType::DATA) | |
169 continue; | |
170 if (!FLAGS_ssrc.empty()) { | |
171 const uint32_t packet_ssrc = | |
172 webrtc::ByteReader<uint32_t>::ReadBigEndian( | |
173 reinterpret_cast<const uint8_t*>(packet.data + 4)); | |
174 if (packet_ssrc != ssrc_filter) | |
175 continue; | |
176 } | |
177 | |
178 rtp_writer->WritePacket(&packet); | |
179 rtcp_counter++; | |
180 } | |
181 } | |
182 std::cout << "Wrote " << rtp_counter << (header_only ? " header-only" : "") | |
183 << " RTP packets and " << rtcp_counter << " RTCP packets to the " | |
184 << "output file." << std::endl; | |
185 return 0; | |
186 } | |
OLD | NEW |