blob: c80b6387a73c28f625e2be11a0d9fd71de31b136 [file] [log] [blame]
henrik.lundin@webrtc.org83317142014-12-01 11:25:041/*
2 * Copyright (c) 2014 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "test/rtp_file_writer.h"
henrik.lundin@webrtc.org83317142014-12-01 11:25:0412
Yves Gerey3e707812018-11-28 15:47:4913#include <stdint.h>
henrik.lundin@webrtc.org83317142014-12-01 11:25:0414#include <stdio.h>
Jonas Olssona4d87372019-07-05 17:08:3315
henrik.lundin@webrtc.org83317142014-12-01 11:25:0416#include <string>
17
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "rtc_base/checks.h"
henrik.lundin@webrtc.org83317142014-12-01 11:25:0419
20namespace webrtc {
21namespace test {
22
23static const uint16_t kPacketHeaderSize = 8;
24static const char kFirstLine[] = "#!rtpplay1.0 0.0.0.0/0\n";
25
26// Write RTP packets to file in rtpdump format, as documented at:
27// http://www.cs.columbia.edu/irt/software/rtptools/
28class RtpDumpWriter : public RtpFileWriter {
29 public:
30 explicit RtpDumpWriter(FILE* file) : file_(file) {
henrikg91d6ede2015-09-17 07:24:3431 RTC_CHECK(file_ != NULL);
henrik.lundin@webrtc.org83317142014-12-01 11:25:0432 Init();
33 }
Mirko Bonadeife055c12019-01-29 21:53:2834 ~RtpDumpWriter() override {
henrik.lundin@webrtc.org83317142014-12-01 11:25:0435 if (file_ != NULL) {
36 fclose(file_);
37 file_ = NULL;
38 }
39 }
40
Artem Titov6cae2d52022-01-26 15:01:1041 RtpDumpWriter(const RtpDumpWriter&) = delete;
42 RtpDumpWriter& operator=(const RtpDumpWriter&) = delete;
43
kjellander@webrtc.org14665ff2015-03-04 12:58:3544 bool WritePacket(const RtpPacket* packet) override {
henrik.lundin@webrtc.org83317142014-12-01 11:25:0445 uint16_t len = static_cast<uint16_t>(packet->length + kPacketHeaderSize);
henrik.lundin@webrtc.org83317142014-12-01 11:25:0446 uint16_t plen = static_cast<uint16_t>(packet->original_length);
47 uint32_t offset = packet->time_ms;
henrikg91d6ede2015-09-17 07:24:3448 RTC_CHECK(WriteUint16(len));
49 RTC_CHECK(WriteUint16(plen));
50 RTC_CHECK(WriteUint32(offset));
henrik.lundin@webrtc.org83317142014-12-01 11:25:0451 return fwrite(packet->data, sizeof(uint8_t), packet->length, file_) ==
52 packet->length;
53 }
54
55 private:
56 bool Init() {
57 fprintf(file_, "%s", kFirstLine);
58
henrikg91d6ede2015-09-17 07:24:3459 RTC_CHECK(WriteUint32(0));
60 RTC_CHECK(WriteUint32(0));
61 RTC_CHECK(WriteUint32(0));
62 RTC_CHECK(WriteUint16(0));
63 RTC_CHECK(WriteUint16(0));
henrik.lundin@webrtc.org83317142014-12-01 11:25:0464
65 return true;
66 }
67
68 bool WriteUint32(uint32_t in) {
69 // Loop through shifts = {24, 16, 8, 0}.
70 for (int shifts = 24; shifts >= 0; shifts -= 8) {
71 uint8_t tmp = static_cast<uint8_t>((in >> shifts) & 0xFF);
72 if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
73 return false;
74 }
75 return true;
76 }
77
78 bool WriteUint16(uint16_t in) {
79 // Write 8 MSBs.
80 uint8_t tmp = static_cast<uint8_t>((in >> 8) & 0xFF);
81 if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
82 return false;
83 // Write 8 LSBs.
84 tmp = static_cast<uint8_t>(in & 0xFF);
85 if (fwrite(&tmp, sizeof(uint8_t), 1, file_) != 1)
86 return false;
87 return true;
88 }
89
90 FILE* file_;
henrik.lundin@webrtc.org83317142014-12-01 11:25:0491};
92
93RtpFileWriter* RtpFileWriter::Create(FileFormat format,
94 const std::string& filename) {
95 FILE* file = fopen(filename.c_str(), "wb");
96 if (file == NULL) {
97 printf("ERROR: Can't open file: %s\n", filename.c_str());
98 return NULL;
99 }
100 switch (format) {
101 case kRtpDump:
102 return new RtpDumpWriter(file);
103 }
104 fclose(file);
105 return NULL;
106}
107
108} // namespace test
109} // namespace webrtc