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