blob: 0c44e4298c2d8d73e7e0164651497c40d27c7c83 [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
17#ifdef WIN32
Yves Gerey665174f2018-06-19 15:03:05 +020018#include <Winsock2.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000019#else
Yves Gerey665174f2018-06-19 15:03:05 +020020#include <arpa/inet.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000021#endif
22
kjellander@webrtc.org3c0aae12014-09-04 09:55:40 +000023// TODO(tlegrand): Consider removing usage of gtest.
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "test/gtest.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000026namespace webrtc {
27
Niels Möllerbf474952019-02-18 12:00:06 +010028void RTPStream::ParseRTPHeader(RTPHeader* rtp_header,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000029 const uint8_t* rtpHeader) {
Niels Möllerbf474952019-02-18 12:00:06 +010030 rtp_header->payloadType = rtpHeader[1];
31 rtp_header->sequenceNumber =
Yves Gerey665174f2018-06-19 15:03:05 +020032 (static_cast<uint16_t>(rtpHeader[2]) << 8) | rtpHeader[3];
Niels Möllerbf474952019-02-18 12:00:06 +010033 rtp_header->timestamp = (static_cast<uint32_t>(rtpHeader[4]) << 24) |
34 (static_cast<uint32_t>(rtpHeader[5]) << 16) |
35 (static_cast<uint32_t>(rtpHeader[6]) << 8) |
36 rtpHeader[7];
37 rtp_header->ssrc = (static_cast<uint32_t>(rtpHeader[8]) << 24) |
38 (static_cast<uint32_t>(rtpHeader[9]) << 16) |
39 (static_cast<uint32_t>(rtpHeader[10]) << 8) |
40 rtpHeader[11];
niklase@google.com470e71d2011-07-07 08:21:25 +000041}
42
Yves Gerey665174f2018-06-19 15:03:05 +020043void RTPStream::MakeRTPheader(uint8_t* rtpHeader,
44 uint8_t payloadType,
45 int16_t seqNo,
46 uint32_t timeStamp,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000047 uint32_t ssrc) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000048 rtpHeader[0] = 0x80;
49 rtpHeader[1] = payloadType;
50 rtpHeader[2] = (seqNo >> 8) & 0xFF;
51 rtpHeader[3] = seqNo & 0xFF;
52 rtpHeader[4] = timeStamp >> 24;
53 rtpHeader[5] = (timeStamp >> 16) & 0xFF;
54 rtpHeader[6] = (timeStamp >> 8) & 0xFF;
55 rtpHeader[7] = timeStamp & 0xFF;
56 rtpHeader[8] = ssrc >> 24;
57 rtpHeader[9] = (ssrc >> 16) & 0xFF;
58 rtpHeader[10] = (ssrc >> 8) & 0xFF;
59 rtpHeader[11] = ssrc & 0xFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000060}
61
Yves Gerey665174f2018-06-19 15:03:05 +020062RTPPacket::RTPPacket(uint8_t payloadType,
63 uint32_t timeStamp,
64 int16_t seqNo,
65 const uint8_t* payloadData,
66 size_t payloadSize,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000067 uint32_t frequency)
68 : payloadType(payloadType),
69 timeStamp(timeStamp),
70 seqNo(seqNo),
71 payloadSize(payloadSize),
72 frequency(frequency) {
73 if (payloadSize > 0) {
74 this->payloadData = new uint8_t[payloadSize];
75 memcpy(this->payloadData, payloadData, payloadSize);
76 }
niklase@google.com470e71d2011-07-07 08:21:25 +000077}
78
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000079RTPPacket::~RTPPacket() {
80 delete[] payloadData;
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
Yves Gerey665174f2018-06-19 15:03:05 +020083void RTPBuffer::Write(const uint8_t payloadType,
84 const uint32_t timeStamp,
85 const int16_t seqNo,
86 const uint8_t* payloadData,
87 const size_t payloadSize,
88 uint32_t frequency) {
89 RTPPacket* packet = new RTPPacket(payloadType, timeStamp, seqNo, payloadData,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000090 payloadSize, frequency);
Niels Möller84d36072020-10-28 14:05:07 +010091 MutexLock lock(&mutex_);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000092 _rtpQueue.push(packet);
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
Niels Möllerbf474952019-02-18 12:00:06 +010095size_t RTPBuffer::Read(RTPHeader* rtp_header,
Yves Gerey665174f2018-06-19 15:03:05 +020096 uint8_t* payloadData,
97 size_t payloadSize,
98 uint32_t* offset) {
Niels Möller84d36072020-10-28 14:05:07 +010099 RTPPacket* packet;
100 {
101 MutexLock lock(&mutex_);
102 packet = _rtpQueue.front();
103 _rtpQueue.pop();
104 }
Niels Möllerbf474952019-02-18 12:00:06 +0100105 rtp_header->markerBit = 1;
106 rtp_header->payloadType = packet->payloadType;
107 rtp_header->sequenceNumber = packet->seqNo;
108 rtp_header->ssrc = 0;
109 rtp_header->timestamp = packet->timeStamp;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110 if (packet->payloadSize > 0 && payloadSize >= packet->payloadSize) {
111 memcpy(payloadData, packet->payloadData, packet->payloadSize);
112 } else {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000113 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000114 }
115 *offset = (packet->timeStamp / (packet->frequency / 1000));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000116
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000117 return packet->payloadSize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000118}
119
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000120bool RTPBuffer::EndOfFile() const {
Niels Möller84d36072020-10-28 14:05:07 +0100121 MutexLock lock(&mutex_);
122 return _rtpQueue.empty();
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
Yves Gerey665174f2018-06-19 15:03:05 +0200125void RTPFile::Open(const char* filename, const char* mode) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000126 if ((_rtpFile = fopen(filename, mode)) == NULL) {
127 printf("Cannot write file %s.\n", filename);
128 ADD_FAILURE() << "Unable to write file";
129 exit(1);
130 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000133void RTPFile::Close() {
134 if (_rtpFile != NULL) {
135 fclose(_rtpFile);
136 _rtpFile = NULL;
137 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000138}
139
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000140void RTPFile::WriteHeader() {
141 // Write data in a format that NetEQ and RTP Play can parse
142 fprintf(_rtpFile, "#!RTPencode%s\n", "1.0");
143 uint32_t dummy_variable = 0;
144 // should be converted to network endian format, but does not matter when 0
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000145 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
146 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
147 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
148 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
149 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000150 fflush(_rtpFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151}
152
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000153void RTPFile::ReadHeader() {
154 uint32_t start_sec, start_usec, source;
155 uint16_t port, padding;
156 char fileHeader[40];
157 EXPECT_TRUE(fgets(fileHeader, 40, _rtpFile) != 0);
158 EXPECT_EQ(1u, fread(&start_sec, 4, 1, _rtpFile));
159 start_sec = ntohl(start_sec);
160 EXPECT_EQ(1u, fread(&start_usec, 4, 1, _rtpFile));
161 start_usec = ntohl(start_usec);
162 EXPECT_EQ(1u, fread(&source, 4, 1, _rtpFile));
163 source = ntohl(source);
164 EXPECT_EQ(1u, fread(&port, 2, 1, _rtpFile));
165 port = ntohs(port);
166 EXPECT_EQ(1u, fread(&padding, 2, 1, _rtpFile));
167 padding = ntohs(padding);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168}
169
Yves Gerey665174f2018-06-19 15:03:05 +0200170void RTPFile::Write(const uint8_t payloadType,
171 const uint32_t timeStamp,
172 const int16_t seqNo,
173 const uint8_t* payloadData,
174 const size_t payloadSize,
175 uint32_t frequency) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000176 /* write RTP packet to file */
177 uint8_t rtpHeader[12];
178 MakeRTPheader(rtpHeader, payloadType, seqNo, timeStamp, 0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000179 ASSERT_LE(12 + payloadSize + 8, std::numeric_limits<u_short>::max());
180 uint16_t lengthBytes = htons(static_cast<u_short>(12 + payloadSize + 8));
181 uint16_t plen = htons(static_cast<u_short>(12 + payloadSize));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000182 uint32_t offsetMs;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000183
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000184 offsetMs = (timeStamp / (frequency / 1000));
185 offsetMs = htonl(offsetMs);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000186 EXPECT_EQ(1u, fwrite(&lengthBytes, 2, 1, _rtpFile));
187 EXPECT_EQ(1u, fwrite(&plen, 2, 1, _rtpFile));
188 EXPECT_EQ(1u, fwrite(&offsetMs, 4, 1, _rtpFile));
189 EXPECT_EQ(1u, fwrite(&rtpHeader, 12, 1, _rtpFile));
190 EXPECT_EQ(payloadSize, fwrite(payloadData, 1, payloadSize, _rtpFile));
niklase@google.com470e71d2011-07-07 08:21:25 +0000191}
192
Niels Möllerbf474952019-02-18 12:00:06 +0100193size_t RTPFile::Read(RTPHeader* rtp_header,
Yves Gerey665174f2018-06-19 15:03:05 +0200194 uint8_t* payloadData,
195 size_t payloadSize,
196 uint32_t* offset) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000197 uint16_t lengthBytes;
198 uint16_t plen;
199 uint8_t rtpHeader[12];
200 size_t read_len = fread(&lengthBytes, 2, 1, _rtpFile);
201 /* Check if we have reached end of file. */
202 if ((read_len == 0) && feof(_rtpFile)) {
203 _rtpEOF = true;
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000204 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000205 }
206 EXPECT_EQ(1u, fread(&plen, 2, 1, _rtpFile));
207 EXPECT_EQ(1u, fread(offset, 4, 1, _rtpFile));
208 lengthBytes = ntohs(lengthBytes);
209 plen = ntohs(plen);
210 *offset = ntohl(*offset);
211 EXPECT_GT(plen, 11);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000212
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000213 EXPECT_EQ(1u, fread(rtpHeader, 12, 1, _rtpFile));
Niels Möllerbf474952019-02-18 12:00:06 +0100214 ParseRTPHeader(rtp_header, rtpHeader);
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000215 EXPECT_EQ(lengthBytes, plen + 8);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000216
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000217 if (plen == 0) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000218 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000219 }
220 if (lengthBytes < 20) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000221 return 0;
222 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000223 if (payloadSize < static_cast<size_t>((lengthBytes - 20))) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000224 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000225 }
226 lengthBytes -= 20;
227 EXPECT_EQ(lengthBytes, fread(payloadData, 1, lengthBytes, _rtpFile));
228 return lengthBytes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229}
230
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000231} // namespace webrtc