blob: 8cc5bd9f995e909d5d6fbd391cd598464d346994 [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>
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000014#include <limits>
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16#ifdef WIN32
17# include <Winsock2.h>
18#else
19# include <arpa/inet.h>
20#endif
21
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020022#include "modules/include/module_common_types.h"
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"
Mirko Bonadei71207422017-09-15 13:58:09 +020025#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000026
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000027namespace webrtc {
28
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000029void RTPStream::ParseRTPHeader(WebRtcRTPHeader* rtpInfo,
30 const uint8_t* rtpHeader) {
31 rtpInfo->header.payloadType = rtpHeader[1];
32 rtpInfo->header.sequenceNumber = (static_cast<uint16_t>(rtpHeader[2]) << 8) |
33 rtpHeader[3];
34 rtpInfo->header.timestamp = (static_cast<uint32_t>(rtpHeader[4]) << 24) |
35 (static_cast<uint32_t>(rtpHeader[5]) << 16) |
36 (static_cast<uint32_t>(rtpHeader[6]) << 8) | rtpHeader[7];
37 rtpInfo->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) | rtpHeader[11];
niklase@google.com470e71d2011-07-07 08:21:25 +000040}
41
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000042void RTPStream::MakeRTPheader(uint8_t* rtpHeader, uint8_t payloadType,
43 int16_t seqNo, uint32_t timeStamp,
44 uint32_t ssrc) {
pkasting@chromium.orgd3245462015-02-23 21:28:22 +000045 rtpHeader[0] = 0x80;
46 rtpHeader[1] = payloadType;
47 rtpHeader[2] = (seqNo >> 8) & 0xFF;
48 rtpHeader[3] = seqNo & 0xFF;
49 rtpHeader[4] = timeStamp >> 24;
50 rtpHeader[5] = (timeStamp >> 16) & 0xFF;
51 rtpHeader[6] = (timeStamp >> 8) & 0xFF;
52 rtpHeader[7] = timeStamp & 0xFF;
53 rtpHeader[8] = ssrc >> 24;
54 rtpHeader[9] = (ssrc >> 16) & 0xFF;
55 rtpHeader[10] = (ssrc >> 8) & 0xFF;
56 rtpHeader[11] = ssrc & 0xFF;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000059RTPPacket::RTPPacket(uint8_t payloadType, uint32_t timeStamp, int16_t seqNo,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000060 const uint8_t* payloadData, size_t payloadSize,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000061 uint32_t frequency)
62 : payloadType(payloadType),
63 timeStamp(timeStamp),
64 seqNo(seqNo),
65 payloadSize(payloadSize),
66 frequency(frequency) {
67 if (payloadSize > 0) {
68 this->payloadData = new uint8_t[payloadSize];
69 memcpy(this->payloadData, payloadData, payloadSize);
70 }
niklase@google.com470e71d2011-07-07 08:21:25 +000071}
72
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000073RTPPacket::~RTPPacket() {
74 delete[] payloadData;
niklase@google.com470e71d2011-07-07 08:21:25 +000075}
76
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000077RTPBuffer::RTPBuffer() {
78 _queueRWLock = RWLockWrapper::CreateRWLock();
niklase@google.com470e71d2011-07-07 08:21:25 +000079}
80
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000081RTPBuffer::~RTPBuffer() {
82 delete _queueRWLock;
niklase@google.com470e71d2011-07-07 08:21:25 +000083}
84
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000085void RTPBuffer::Write(const uint8_t payloadType, const uint32_t timeStamp,
86 const int16_t seqNo, const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000087 const size_t payloadSize, uint32_t frequency) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000088 RTPPacket *packet = new RTPPacket(payloadType, timeStamp, seqNo, payloadData,
89 payloadSize, frequency);
90 _queueRWLock->AcquireLockExclusive();
91 _rtpQueue.push(packet);
92 _queueRWLock->ReleaseLockExclusive();
niklase@google.com470e71d2011-07-07 08:21:25 +000093}
94
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000095size_t RTPBuffer::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData,
96 size_t payloadSize, uint32_t* offset) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000097 _queueRWLock->AcquireLockShared();
98 RTPPacket *packet = _rtpQueue.front();
99 _rtpQueue.pop();
100 _queueRWLock->ReleaseLockShared();
101 rtpInfo->header.markerBit = 1;
102 rtpInfo->header.payloadType = packet->payloadType;
103 rtpInfo->header.sequenceNumber = packet->seqNo;
104 rtpInfo->header.ssrc = 0;
105 rtpInfo->header.timestamp = packet->timeStamp;
106 if (packet->payloadSize > 0 && payloadSize >= packet->payloadSize) {
107 memcpy(payloadData, packet->payloadData, packet->payloadSize);
108 } else {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000109 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000110 }
111 *offset = (packet->timeStamp / (packet->frequency / 1000));
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000112
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000113 return packet->payloadSize;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114}
115
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000116bool RTPBuffer::EndOfFile() const {
117 _queueRWLock->AcquireLockShared();
118 bool eof = _rtpQueue.empty();
119 _queueRWLock->ReleaseLockShared();
120 return eof;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121}
122
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000123void RTPFile::Open(const char *filename, const char *mode) {
124 if ((_rtpFile = fopen(filename, mode)) == NULL) {
125 printf("Cannot write file %s.\n", filename);
126 ADD_FAILURE() << "Unable to write file";
127 exit(1);
128 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000131void RTPFile::Close() {
132 if (_rtpFile != NULL) {
133 fclose(_rtpFile);
134 _rtpFile = NULL;
135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000138void RTPFile::WriteHeader() {
139 // Write data in a format that NetEQ and RTP Play can parse
140 fprintf(_rtpFile, "#!RTPencode%s\n", "1.0");
141 uint32_t dummy_variable = 0;
142 // should be converted to network endian format, but does not matter when 0
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000143 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
144 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
145 EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile));
146 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
147 EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000148 fflush(_rtpFile);
niklase@google.com470e71d2011-07-07 08:21:25 +0000149}
150
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000151void RTPFile::ReadHeader() {
152 uint32_t start_sec, start_usec, source;
153 uint16_t port, padding;
154 char fileHeader[40];
155 EXPECT_TRUE(fgets(fileHeader, 40, _rtpFile) != 0);
156 EXPECT_EQ(1u, fread(&start_sec, 4, 1, _rtpFile));
157 start_sec = ntohl(start_sec);
158 EXPECT_EQ(1u, fread(&start_usec, 4, 1, _rtpFile));
159 start_usec = ntohl(start_usec);
160 EXPECT_EQ(1u, fread(&source, 4, 1, _rtpFile));
161 source = ntohl(source);
162 EXPECT_EQ(1u, fread(&port, 2, 1, _rtpFile));
163 port = ntohs(port);
164 EXPECT_EQ(1u, fread(&padding, 2, 1, _rtpFile));
165 padding = ntohs(padding);
niklase@google.com470e71d2011-07-07 08:21:25 +0000166}
167
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000168void RTPFile::Write(const uint8_t payloadType, const uint32_t timeStamp,
169 const int16_t seqNo, const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000170 const size_t payloadSize, uint32_t frequency) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000171 /* write RTP packet to file */
172 uint8_t rtpHeader[12];
173 MakeRTPheader(rtpHeader, payloadType, seqNo, timeStamp, 0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000174 ASSERT_LE(12 + payloadSize + 8, std::numeric_limits<u_short>::max());
175 uint16_t lengthBytes = htons(static_cast<u_short>(12 + payloadSize + 8));
176 uint16_t plen = htons(static_cast<u_short>(12 + payloadSize));
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000177 uint32_t offsetMs;
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000178
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000179 offsetMs = (timeStamp / (frequency / 1000));
180 offsetMs = htonl(offsetMs);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000181 EXPECT_EQ(1u, fwrite(&lengthBytes, 2, 1, _rtpFile));
182 EXPECT_EQ(1u, fwrite(&plen, 2, 1, _rtpFile));
183 EXPECT_EQ(1u, fwrite(&offsetMs, 4, 1, _rtpFile));
184 EXPECT_EQ(1u, fwrite(&rtpHeader, 12, 1, _rtpFile));
185 EXPECT_EQ(payloadSize, fwrite(payloadData, 1, payloadSize, _rtpFile));
niklase@google.com470e71d2011-07-07 08:21:25 +0000186}
187
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000188size_t RTPFile::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData,
189 size_t payloadSize, uint32_t* offset) {
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000190 uint16_t lengthBytes;
191 uint16_t plen;
192 uint8_t rtpHeader[12];
193 size_t read_len = fread(&lengthBytes, 2, 1, _rtpFile);
194 /* Check if we have reached end of file. */
195 if ((read_len == 0) && feof(_rtpFile)) {
196 _rtpEOF = true;
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000197 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000198 }
199 EXPECT_EQ(1u, fread(&plen, 2, 1, _rtpFile));
200 EXPECT_EQ(1u, fread(offset, 4, 1, _rtpFile));
201 lengthBytes = ntohs(lengthBytes);
202 plen = ntohs(plen);
203 *offset = ntohl(*offset);
204 EXPECT_GT(plen, 11);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000205
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000206 EXPECT_EQ(1u, fread(rtpHeader, 12, 1, _rtpFile));
207 ParseRTPHeader(rtpInfo, rtpHeader);
208 rtpInfo->type.Audio.isCNG = false;
209 rtpInfo->type.Audio.channel = 1;
210 EXPECT_EQ(lengthBytes, plen + 8);
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +0000211
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000212 if (plen == 0) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000213 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000214 }
215 if (lengthBytes < 20) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000216 return 0;
217 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000218 if (payloadSize < static_cast<size_t>((lengthBytes - 20))) {
henrik.lundin@webrtc.org741711a2014-09-25 07:38:14 +0000219 return 0;
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000220 }
221 lengthBytes -= 20;
222 EXPECT_EQ(lengthBytes, fread(payloadData, 1, lengthBytes, _rtpFile));
223 return lengthBytes;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224}
225
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +0000226} // namespace webrtc