blob: 92a7a8d2150afb44e17fcc6d3e14905f67884a93 [file] [log] [blame]
Henrik Lundin81af4142017-11-24 13:39:39 +01001/*
2 * Copyright (c) 2017 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 <stdio.h>
12#include <algorithm>
13#include <fstream>
14#include <iostream>
15#include <vector>
16
17#include "api/array_view.h"
18#include "modules/rtp_rtcp/source/byte_io.h"
19#include "rtc_base/buffer.h"
20#include "rtc_base/flags.h"
Henrik Lundin81af4142017-11-24 13:39:39 +010021
22namespace webrtc {
23namespace test {
24namespace {
25
Mirko Bonadei2dfa9982018-10-18 11:35:32 +020026WEBRTC_DEFINE_bool(help, false, "Print help message");
Henrik Lundin81af4142017-11-24 13:39:39 +010027
28constexpr size_t kRtpDumpHeaderLength = 8;
29
30// Returns the next packet or an empty buffer if end of file was encountered.
31rtc::Buffer ReadNextPacket(FILE* file) {
32 // Read the rtpdump header for the next packet.
33 rtc::Buffer buffer;
34 buffer.SetData(kRtpDumpHeaderLength, [&](rtc::ArrayView<uint8_t> x) {
35 return fread(x.data(), 1, x.size(), file);
36 });
37 if (buffer.size() != kRtpDumpHeaderLength) {
38 return rtc::Buffer();
39 }
40
41 // Get length field. This is the total length for this packet written to file,
42 // including the kRtpDumpHeaderLength bytes already read.
43 const uint16_t len = ByteReader<uint16_t>::ReadBigEndian(buffer.data());
44 RTC_CHECK_GE(len, kRtpDumpHeaderLength);
45
46 // Read remaining data from file directly into buffer.
47 buffer.AppendData(len - kRtpDumpHeaderLength, [&](rtc::ArrayView<uint8_t> x) {
48 return fread(x.data(), 1, x.size(), file);
49 });
50 if (buffer.size() != len) {
51 buffer.Clear();
52 }
53 return buffer;
54}
55
56struct PacketAndTime {
57 rtc::Buffer packet;
58 int time;
59};
60
61void WritePacket(const PacketAndTime& packet, FILE* file) {
62 // Write the first 4 bytes from the original packet.
63 const auto* payload_ptr = packet.packet.data();
64 RTC_CHECK_EQ(fwrite(payload_ptr, 4, 1, file), 1);
65 payload_ptr += 4;
66
67 // Convert the new time offset to network endian, and write to file.
68 uint8_t time[sizeof(uint32_t)];
69 ByteWriter<uint32_t, sizeof(uint32_t)>::WriteBigEndian(time, packet.time);
70 RTC_CHECK_EQ(fwrite(time, sizeof(uint32_t), 1, file), 1);
71 payload_ptr += 4; // Skip the old time in the original payload.
72
73 // Write the remaining part of the payload.
74 RTC_DCHECK_EQ(payload_ptr - packet.packet.data(), kRtpDumpHeaderLength);
75 RTC_CHECK_EQ(
76 fwrite(payload_ptr, packet.packet.size() - kRtpDumpHeaderLength, 1, file),
77 1);
78}
79
80int RunRtpJitter(int argc, char* argv[]) {
81 const std::string program_name = argv[0];
82 const std::string usage =
83 "Tool for alternating the arrival times in an RTP dump file.\n"
84 "Example usage:\n" +
85 program_name + " input.rtp arrival_times_ms.txt output.rtp\n\n";
86 if (rtc::FlagList::SetFlagsFromCommandLine(&argc, argv, true) || FLAG_help ||
87 argc != 4) {
88 printf("%s", usage.c_str());
89 return FLAG_help ? 0 : 1;
90 }
91
92 printf("Input RTP file: %s\n", argv[1]);
93 FILE* in_file = fopen(argv[1], "rb");
94 RTC_CHECK(in_file) << "Could not open file " << argv[1] << " for reading";
95 printf("Timing file: %s\n", argv[2]);
96 std::ifstream timing_file(argv[2]);
97 printf("Output file: %s\n", argv[3]);
98 FILE* out_file = fopen(argv[3], "wb");
99 RTC_CHECK(out_file) << "Could not open file " << argv[2] << " for writing";
100
101 // Copy the RTP file header to the output file.
102 char header_string[30];
103 RTC_CHECK(fgets(header_string, 30, in_file));
104 fprintf(out_file, "%s", header_string);
105 uint8_t file_header[16];
106 RTC_CHECK_EQ(fread(file_header, sizeof(file_header), 1, in_file), 1);
107 RTC_CHECK_EQ(fwrite(file_header, sizeof(file_header), 1, out_file), 1);
108
109 // Read all time values from the timing file. Store in a vector.
110 std::vector<int> new_arrival_times;
111 int new_time;
112 while (timing_file >> new_time) {
113 new_arrival_times.push_back(new_time);
114 }
115
116 // Read all packets from the input RTP file, but no more than the number of
117 // new time values. Store RTP packets together with new time values.
118 auto time_it = new_arrival_times.begin();
119 std::vector<PacketAndTime> packets;
120 while (1) {
121 auto packet = ReadNextPacket(in_file);
122 if (packet.empty() || time_it == new_arrival_times.end()) {
123 break;
124 }
125 packets.push_back({std::move(packet), *time_it});
126 ++time_it;
127 }
128
129 // Sort on new time values.
130 std::sort(packets.begin(), packets.end(),
131 [](const PacketAndTime& a, const PacketAndTime& b) {
132 return a.time < b.time;
133 });
134
135 // Write packets to output file.
136 for (const auto& p : packets) {
137 WritePacket(p, out_file);
138 }
139
140 fclose(in_file);
141 fclose(out_file);
142 return 0;
143}
144
145} // namespace
146} // namespace test
147} // namespace webrtc
148
149int main(int argc, char* argv[]) {
150 return webrtc::test::RunRtpJitter(argc, argv);
151}