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