blob: 0c2ab3c443b4b85ea00e1e749fb8375fdadde72e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 "RTPFile.h"
kjellander@webrtc.org543c3ea2011-11-23 12:20:35 +000012
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include <stdlib.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000015#include <limits>
niklase@google.com470e71d2011-07-07 08:21:25 +000016
Ali Tofigh714e3cb2022-07-20 12:53:07 +020017#include "absl/strings/string_view.h"
18
niklase@google.com470e71d2011-07-07 08:21:25 +000019#ifdef WIN32
Yves Gerey665174f2018-06-19 15:03:05 +020020#include <Winsock2.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000021#else
Yves Gerey665174f2018-06-19 15:03:05 +020022#include <arpa/inet.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000023#endif
24
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000025// TODO(tlegrand): Consider removing usage of gtest.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "test/gtest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000028namespace webrtc {
29
Niels Möllerbf474952019-02-18 12:00:06 +010030void RTPStream::ParseRTPHeader(RTPHeader* rtp_header,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000031 const uint8_t* rtpHeader) {
Niels Möllerbf474952019-02-18 12:00:06 +010032 rtp_header->payloadType = rtpHeader[1];
33 rtp_header->sequenceNumber =
Yves Gerey665174f2018-06-19 15:03:05 +020034 (static_cast<uint16_t>(rtpHeader[2]) << 8) | rtpHeader[3];
Niels Möllerbf474952019-02-18 12:00:06 +010035 rtp_header->timestamp = (static_cast<uint32_t>(rtpHeader[4]) << 24) |
36 (static_cast<uint32_t>(rtpHeader[5]) << 16) |
37 (static_cast<uint32_t>(rtpHeader[6]) << 8) |
38 rtpHeader[7];
39 rtp_header->ssrc = (static_cast<uint32_t>(rtpHeader[8]) << 24) |
40 (static_cast<uint32_t>(rtpHeader[9]) << 16) |
41 (static_cast<uint32_t>(rtpHeader[10]) << 8) |
42 rtpHeader[11];
niklase@google.com470e71d2011-07-07 08:21:25 +000043}
44
Yves Gerey665174f2018-06-19 15:03:05 +020045void RTPStream::MakeRTPheader(uint8_t* rtpHeader,
46 uint8_t payloadType,
47 int16_t seqNo,
48 uint32_t timeStamp,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000049 uint32_t ssrc) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000050 rtpHeader[0] = 0x80;
51 rtpHeader[1] = payloadType;
52 rtpHeader[2] = (seqNo >> 8) & 0xFF;
53 rtpHeader[3] = seqNo & 0xFF;
54 rtpHeader[4] = timeStamp >> 24;
55 rtpHeader[5] = (timeStamp >> 16) & 0xFF;
56 rtpHeader[6] = (timeStamp >> 8) & 0xFF;
57 rtpHeader[7] = timeStamp & 0xFF;
58 rtpHeader[8] = ssrc >> 24;
59 rtpHeader[9] = (ssrc >> 16) & 0xFF;
60 rtpHeader[10] = (ssrc >> 8) & 0xFF;
61 rtpHeader[11] = ssrc & 0xFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000062}
63
Yves Gerey665174f2018-06-19 15:03:05 +020064RTPPacket::RTPPacket(uint8_t payloadType,
65 uint32_t timeStamp,
66 int16_t seqNo,
67 const uint8_t* payloadData,
68 size_t payloadSize,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000069 uint32_t frequency)
70 : payloadType(payloadType),
71 timeStamp(timeStamp),
72 seqNo(seqNo),
73 payloadSize(payloadSize),
74 frequency(frequency) {
75 if (payloadSize > 0) {
76 this->payloadData = new uint8_t[payloadSize];
77 memcpy(this->payloadData, payloadData, payloadSize);
78 }
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081RTPPacket::~RTPPacket() {
82 delete[] payloadData;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
Yves Gerey665174f2018-06-19 15:03:05 +020085void RTPBuffer::Write(const uint8_t payloadType,
86 const uint32_t timeStamp,
87 const int16_t seqNo,
88 const uint8_t* payloadData,
89 const size_t payloadSize,
90 uint32_t frequency) {
91 RTPPacket* packet = new RTPPacket(payloadType, timeStamp, seqNo, payloadData,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000092 payloadSize, frequency);
Niels Möller84d36072020-10-28 14:05:07 +010093 MutexLock lock(&mutex_);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000094 _rtpQueue.push(packet);
niklase@google.com470e71d2011-07-07 08:21:25 +000095}
96
Niels Möllerbf474952019-02-18 12:00:06 +010097size_t RTPBuffer::Read(RTPHeader* rtp_header,
Yves Gerey665174f2018-06-19 15:03:05 +020098 uint8_t* payloadData,
99 size_t payloadSize,
100 uint32_t* offset) {
Niels Möller84d36072020-10-28 14:05:07 +0100101 RTPPacket* packet;
102 {
103 MutexLock lock(&mutex_);
104 packet = _rtpQueue.front();
105 _rtpQueue.pop();
106 }
Niels Möllerbf474952019-02-18 12:00:06 +0100107 rtp_header->markerBit = 1;
108 rtp_header->payloadType = packet->payloadType;
109 rtp_header->sequenceNumber = packet->seqNo;
110 rtp_header->ssrc = 0;
111 rtp_header->timestamp = packet->timeStamp;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000112 if (packet->payloadSize > 0 && payloadSize >= packet->payloadSize) {
113 memcpy(payloadData, packet->payloadData, packet->payloadSize);
114 } else {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000115 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116 }
117 *offset = (packet->timeStamp / (packet->frequency / 1000));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000118
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000119 return packet->payloadSize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120}
121
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000122bool RTPBuffer::EndOfFile() const {
Niels Möller84d36072020-10-28 14:05:07 +0100123 MutexLock lock(&mutex_);
124 return _rtpQueue.empty();
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
Ali Tofigh714e3cb2022-07-20 12:53:07 +0200127void RTPFile::Open(absl::string_view filename, absl::string_view mode) {
128 std::string filename_str = std::string(filename);
129 if ((_rtpFile = fopen(filename_str.c_str(), std::string(mode).c_str())) ==
130 NULL) {
131 printf("Cannot write file %s.\n", filename_str.c_str());
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000132 ADD_FAILURE() << "Unable to write file";
133 exit(1);
134 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000135}
136
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000137void RTPFile::Close() {
138 if (_rtpFile != NULL) {
139 fclose(_rtpFile);
140 _rtpFile = NULL;
141 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000142}
143
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000144void RTPFile::WriteHeader() {
145 // Write data in a format that NetEQ and RTP Play can parse
146 fprintf(_rtpFile, "#!RTPencode%s\n", "1.0");
147 uint32_t dummy_variable = 0;
148 // should be converted to network endian format, but does not matter when 0
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000149 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
150 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
151 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
152 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
153 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000154 fflush(_rtpFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000155}
156
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000157void RTPFile::ReadHeader() {
158 uint32_t start_sec, start_usec, source;
159 uint16_t port, padding;
160 char fileHeader[40];
161 EXPECT_TRUE(fgets(fileHeader, 40, _rtpFile) != 0);
162 EXPECT_EQ(1u, fread(&start_sec, 4, 1, _rtpFile));
163 start_sec = ntohl(start_sec);
164 EXPECT_EQ(1u, fread(&start_usec, 4, 1, _rtpFile));
165 start_usec = ntohl(start_usec);
166 EXPECT_EQ(1u, fread(&source, 4, 1, _rtpFile));
167 source = ntohl(source);
168 EXPECT_EQ(1u, fread(&port, 2, 1, _rtpFile));
169 port = ntohs(port);
170 EXPECT_EQ(1u, fread(&padding, 2, 1, _rtpFile));
171 padding = ntohs(padding);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172}
173
Yves Gerey665174f2018-06-19 15:03:05 +0200174void RTPFile::Write(const uint8_t payloadType,
175 const uint32_t timeStamp,
176 const int16_t seqNo,
177 const uint8_t* payloadData,
178 const size_t payloadSize,
179 uint32_t frequency) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000180 /* write RTP packet to file */
181 uint8_t rtpHeader[12];
182 MakeRTPheader(rtpHeader, payloadType, seqNo, timeStamp, 0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000183 ASSERT_LE(12 + payloadSize + 8, std::numeric_limits<u_short>::max());
184 uint16_t lengthBytes = htons(static_cast<u_short>(12 + payloadSize + 8));
185 uint16_t plen = htons(static_cast<u_short>(12 + payloadSize));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000186 uint32_t offsetMs;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000187
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000188 offsetMs = (timeStamp / (frequency / 1000));
189 offsetMs = htonl(offsetMs);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000190 EXPECT_EQ(1u, fwrite(&lengthBytes, 2, 1, _rtpFile));
191 EXPECT_EQ(1u, fwrite(&plen, 2, 1, _rtpFile));
192 EXPECT_EQ(1u, fwrite(&offsetMs, 4, 1, _rtpFile));
193 EXPECT_EQ(1u, fwrite(&rtpHeader, 12, 1, _rtpFile));
194 EXPECT_EQ(payloadSize, fwrite(payloadData, 1, payloadSize, _rtpFile));
niklase@google.com470e71d2011-07-07 08:21:25 +0000195}
196
Niels Möllerbf474952019-02-18 12:00:06 +0100197size_t RTPFile::Read(RTPHeader* rtp_header,
Yves Gerey665174f2018-06-19 15:03:05 +0200198 uint8_t* payloadData,
199 size_t payloadSize,
200 uint32_t* offset) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000201 uint16_t lengthBytes;
202 uint16_t plen;
203 uint8_t rtpHeader[12];
204 size_t read_len = fread(&lengthBytes, 2, 1, _rtpFile);
205 /* Check if we have reached end of file. */
206 if ((read_len == 0) && feof(_rtpFile)) {
207 _rtpEOF = true;
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000208 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000209 }
210 EXPECT_EQ(1u, fread(&plen, 2, 1, _rtpFile));
211 EXPECT_EQ(1u, fread(offset, 4, 1, _rtpFile));
212 lengthBytes = ntohs(lengthBytes);
213 plen = ntohs(plen);
214 *offset = ntohl(*offset);
215 EXPECT_GT(plen, 11);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000216
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000217 EXPECT_EQ(1u, fread(rtpHeader, 12, 1, _rtpFile));
Niels Möllerbf474952019-02-18 12:00:06 +0100218 ParseRTPHeader(rtp_header, rtpHeader);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000219 EXPECT_EQ(lengthBytes, plen + 8);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000220
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000221 if (plen == 0) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000222 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000223 }
224 if (lengthBytes < 20) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000225 return 0;
226 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000227 if (payloadSize < static_cast<size_t>((lengthBytes - 20))) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000228 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000229 }
230 lengthBytes -= 20;
231 EXPECT_EQ(lengthBytes, fread(payloadData, 1, lengthBytes, _rtpFile));
232 return lengthBytes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000233}
234
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000235} // namespace webrtc