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 <errno.h> // for errno and ERANGE | |
12 #include <limits.h> // For ULONG_MAX returned by strtoul. | |
13 #include <stdlib.h> // For strtoul. | |
14 #include <iostream> | |
15 #include <string> | |
16 | |
17 #include "gflags/gflags.h" | |
18 #include "webrtc/base/checks.h" | |
19 #include "webrtc/base/scoped_ptr.h" | |
20 #include "webrtc/modules/rtp_rtcp/source/byte_io.h" | |
21 #include "webrtc/test/rtp_file_writer.h" | |
22 #include "webrtc/video/rtc_event_log.h" | |
23 | |
24 // Files generated at build-time by the protobuf compiler. | |
25 #ifdef WEBRTC_ANDROID_PLATFORM_BUILD | |
26 #include "external/webrtc/webrtc/video/rtc_event_log.pb.h" | |
27 #else | |
28 #include "webrtc/video/rtc_event_log.pb.h" | |
29 #endif | |
30 | |
31 namespace { | |
32 | |
33 DEFINE_bool(noaudio, | |
34 false, | |
35 "Excludes audio packets from the converted RTPdump file."); | |
36 DEFINE_bool(novideo, | |
37 false, | |
38 "Excludes video packets from the converted RTPdump file."); | |
39 DEFINE_bool(nodata, | |
40 false, | |
41 "Excludes data packets from the converted RTPdump file."); | |
42 DEFINE_bool(nortp, | |
43 false, | |
44 "Excludes RTP packets from the converted RTPdump file."); | |
45 DEFINE_bool(nortcp, | |
46 false, | |
47 "Excludes RTCP packets from the converted RTPdump file."); | |
48 DEFINE_string(ssrc, | |
49 "", | |
50 "Store only packets with this SSRC (decimal or hex, the latter " | |
51 "starting with 0x)."); | |
52 | |
53 // Parses the input string for a valid SSRC (at the start of the string). If a | |
54 // valid SSRC is found, it is written to the output variable |ssrc|, and true is | |
55 // returned. Otherwise, false is returned. | |
56 bool ParseSsrc(const std::string& str, uint32_t* ssrc) { | |
stefan-webrtc
2015/09/11 14:10:29
I wrote some code to do something similar here: ht
ivoc
2015/09/17 08:38:45
Thanks for the suggestion, I rewrote the function
| |
57 if (str.empty()) | |
58 return true; | |
59 int base = 10; | |
60 // Look for "0x" or "0X" at the start and change base to 16 if found. | |
61 if ((str.compare(0, 2, "0x") == 0) || (str.compare(0, 2, "0X") == 0)) | |
62 base = 16; | |
63 errno = 0; | |
64 char* end_ptr; | |
65 unsigned long value = strtoul(str.c_str(), &end_ptr, base); | |
66 if (value == ULONG_MAX && errno == ERANGE) | |
67 return false; // Value out of range for unsigned long. | |
68 if (sizeof(unsigned long) > sizeof(uint32_t) && value > 0xFFFFFFFF) | |
69 return false; // Value out of range for uint32_t. | |
70 if (end_ptr - str.c_str() < static_cast<ptrdiff_t>(str.length())) | |
71 return false; // Part of the string was not parsed. | |
72 *ssrc = static_cast<uint32_t>(value); | |
73 return true; | |
74 } | |
75 | |
76 } // namespace | |
77 | |
78 // This utility will convert a stored event log to the rtpdump format. | |
79 int main(int argc, char* argv[]) { | |
80 std::string program_name = argv[0]; | |
81 std::string usage = | |
82 "Tool for converting an RtcEventLog file to an RTP dump file.\n" | |
83 "Run " + | |
84 program_name + | |
85 " --helpshort for usage.\n" | |
86 "Example usage:\n" + | |
87 program_name + " input.rel output.rtp\n"; | |
88 google::SetUsageMessage(usage); | |
89 google::ParseCommandLineFlags(&argc, &argv, true); | |
90 | |
91 if (argc != 3) { | |
92 std::cout << google::ProgramUsage(); | |
93 return 0; | |
94 } | |
95 std::string input_file = argv[1]; | |
96 std::string output_file = argv[2]; | |
97 | |
98 uint32_t ssrc_filter = 0; | |
99 if (!FLAGS_ssrc.empty()) | |
100 CHECK(ParseSsrc(FLAGS_ssrc, &ssrc_filter)) | |
101 << "Flag verification has failed."; | |
102 | |
103 webrtc::rtclog::EventStream event_stream; | |
104 if (!webrtc::RtcEventLog::ParseRtcEventLog(input_file, &event_stream)) { | |
105 std::cerr << "Error while parsing input file: " << input_file << std::endl; | |
106 return -1; | |
107 } | |
108 | |
109 rtc::scoped_ptr<webrtc::test::RtpFileWriter> rtp_writer( | |
110 webrtc::test::RtpFileWriter::Create( | |
111 webrtc::test::RtpFileWriter::FileFormat::kRtpDump, output_file)); | |
112 | |
113 if (!rtp_writer.get()) { | |
114 std::cerr << "Error while opening output file: " << output_file | |
115 << std::endl; | |
116 return -1; | |
117 } | |
118 | |
119 std::cout << "Found " << event_stream.stream_size() | |
120 << " events in the input file." << std::endl; | |
121 int rtp_counter = 0, rtcp_counter = 0; | |
122 bool header_only = false; | |
123 // TODO(ivoc): This can be refactored once the packet interpretation | |
124 // functions are finished. | |
125 for (int i = 0; i < event_stream.stream_size(); i++) { | |
126 const webrtc::rtclog::Event& event = event_stream.stream(i); | |
127 if (!FLAGS_nortp && event.has_type() && event.type() == event.RTP_EVENT) { | |
128 if (event.has_timestamp_us() && event.has_rtp_packet() && | |
129 event.rtp_packet().has_header() && | |
130 event.rtp_packet().header().size() >= 12 && | |
131 event.rtp_packet().has_packet_length() && | |
132 event.rtp_packet().has_type()) { | |
133 const webrtc::rtclog::RtpPacket& rtp_packet = event.rtp_packet(); | |
134 if (FLAGS_noaudio && rtp_packet.type() == webrtc::rtclog::AUDIO) | |
135 continue; | |
136 if (FLAGS_novideo && rtp_packet.type() == webrtc::rtclog::VIDEO) | |
137 continue; | |
138 if (FLAGS_nodata && rtp_packet.type() == webrtc::rtclog::DATA) | |
139 continue; | |
140 if (!FLAGS_ssrc.empty()) { | |
141 const uint32_t packet_ssrc = | |
142 webrtc::ByteReader<uint32_t>::ReadBigEndian( | |
143 reinterpret_cast<const uint8_t*>(rtp_packet.header().data() + | |
144 8)); | |
145 if (packet_ssrc != ssrc_filter) | |
146 continue; | |
147 } | |
148 | |
149 webrtc::test::RtpPacket packet; | |
150 packet.length = rtp_packet.header().size(); | |
151 if (packet.length > packet.kMaxPacketBufferSize) { | |
152 std::cout << "Skipping packet with size " << packet.length | |
153 << ", the maximum supported size is " | |
154 << packet.kMaxPacketBufferSize << std::endl; | |
155 continue; | |
156 } | |
157 packet.original_length = rtp_packet.packet_length(); | |
158 if (packet.original_length > packet.length) | |
159 header_only = true; | |
160 packet.time_ms = event.timestamp_us() / 1000; | |
161 memcpy(packet.data, rtp_packet.header().data(), packet.length); | |
162 rtp_writer->WritePacket(&packet); | |
163 rtp_counter++; | |
164 } else { | |
165 std::cout << "Skipping malformed event." << std::endl; | |
166 } | |
167 } | |
168 if (!FLAGS_nortcp && event.has_type() && event.type() == event.RTCP_EVENT) { | |
169 if (event.has_timestamp_us() && event.has_rtcp_packet() && | |
170 event.rtcp_packet().has_type() && | |
171 event.rtcp_packet().has_packet_data() && | |
172 event.rtcp_packet().packet_data().size() > 0) { | |
173 const webrtc::rtclog::RtcpPacket& rtcp_packet = event.rtcp_packet(); | |
174 if (FLAGS_noaudio && rtcp_packet.type() == webrtc::rtclog::AUDIO) | |
175 continue; | |
176 if (FLAGS_novideo && rtcp_packet.type() == webrtc::rtclog::VIDEO) | |
177 continue; | |
178 if (FLAGS_nodata && rtcp_packet.type() == webrtc::rtclog::DATA) | |
179 continue; | |
180 if (!FLAGS_ssrc.empty()) { | |
181 const uint32_t packet_ssrc = | |
182 webrtc::ByteReader<uint32_t>::ReadBigEndian( | |
183 reinterpret_cast<const uint8_t*>( | |
184 rtcp_packet.packet_data().data() + 4)); | |
185 if (packet_ssrc != ssrc_filter) | |
186 continue; | |
187 } | |
188 | |
189 webrtc::test::RtpPacket packet; | |
190 packet.length = rtcp_packet.packet_data().size(); | |
191 if (packet.length > packet.kMaxPacketBufferSize) { | |
192 std::cout << "Skipping packet with size " << packet.length | |
193 << ", the maximum supported size is " | |
194 << packet.kMaxPacketBufferSize << std::endl; | |
195 continue; | |
196 } | |
197 // For RTCP packets the original_length should be set to 0 in the | |
198 // RTPdump format. | |
199 packet.original_length = 0; | |
200 packet.time_ms = event.timestamp_us() / 1000; | |
201 memcpy(packet.data, rtcp_packet.packet_data().data(), packet.length); | |
202 rtp_writer->WritePacket(&packet); | |
203 rtcp_counter++; | |
204 } else { | |
205 std::cout << "Skipping malformed event." << std::endl; | |
206 } | |
207 } | |
208 } | |
209 std::cout << "Wrote " << rtp_counter << (header_only ? " header-only" : "") | |
210 << " RTP packets and " << rtcp_counter << " RTCP packets to the " | |
211 << "output file." << std::endl; | |
212 return 0; | |
213 } | |
OLD | NEW |