xref: /aosp_15_r20/external/webrtc/test/rtp_file_writer.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
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 
11 #include "test/rtp_file_writer.h"
12 
13 #include <stdint.h>
14 #include <stdio.h>
15 
16 #include <string>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 namespace test {
22 
23 static const uint16_t kPacketHeaderSize = 8;
24 static 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/
28 class RtpDumpWriter : public RtpFileWriter {
29  public:
RtpDumpWriter(FILE * file)30   explicit RtpDumpWriter(FILE* file) : file_(file) {
31     RTC_CHECK(file_ != NULL);
32     Init();
33   }
~RtpDumpWriter()34   ~RtpDumpWriter() override {
35     if (file_ != NULL) {
36       fclose(file_);
37       file_ = NULL;
38     }
39   }
40 
41   RtpDumpWriter(const RtpDumpWriter&) = delete;
42   RtpDumpWriter& operator=(const RtpDumpWriter&) = delete;
43 
WritePacket(const RtpPacket * packet)44   bool WritePacket(const RtpPacket* packet) override {
45     uint16_t len = static_cast<uint16_t>(packet->length + kPacketHeaderSize);
46     uint16_t plen = static_cast<uint16_t>(packet->original_length);
47     uint32_t offset = packet->time_ms;
48     RTC_CHECK(WriteUint16(len));
49     RTC_CHECK(WriteUint16(plen));
50     RTC_CHECK(WriteUint32(offset));
51     return fwrite(packet->data, sizeof(uint8_t), packet->length, file_) ==
52            packet->length;
53   }
54 
55  private:
Init()56   bool Init() {
57     fprintf(file_, "%s", kFirstLine);
58 
59     RTC_CHECK(WriteUint32(0));
60     RTC_CHECK(WriteUint32(0));
61     RTC_CHECK(WriteUint32(0));
62     RTC_CHECK(WriteUint16(0));
63     RTC_CHECK(WriteUint16(0));
64 
65     return true;
66   }
67 
WriteUint32(uint32_t in)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 
WriteUint16(uint16_t in)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_;
91 };
92 
Create(FileFormat format,const std::string & filename)93 RtpFileWriter* 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
110