niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
tina.legrand@webrtc.org | 16b6b90 | 2012-04-12 11:02:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 "RTPFile.h" |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 14 | #include <limits> |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
| 16 | #ifdef WIN32 |
| 17 | # include <Winsock2.h> |
| 18 | #else |
| 19 | # include <arpa/inet.h> |
| 20 | #endif |
| 21 | |
| 22 | #include "audio_coding_module.h" |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 23 | #include "engine_configurations.h" |
andresp@webrtc.org | 86e1e48 | 2015-01-14 09:30:52 +0000 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h" |
kjellander@webrtc.org | 3c0aae1 | 2014-09-04 09:55:40 +0000 | [diff] [blame] | 25 | // TODO(tlegrand): Consider removing usage of gtest. |
| 26 | #include "testing/gtest/include/gtest/gtest.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 28 | namespace webrtc { |
| 29 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 30 | void RTPStream::ParseRTPHeader(WebRtcRTPHeader* rtpInfo, |
| 31 | const uint8_t* rtpHeader) { |
| 32 | rtpInfo->header.payloadType = rtpHeader[1]; |
| 33 | rtpInfo->header.sequenceNumber = (static_cast<uint16_t>(rtpHeader[2]) << 8) | |
| 34 | rtpHeader[3]; |
| 35 | rtpInfo->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) | rtpHeader[7]; |
| 38 | rtpInfo->header.ssrc = (static_cast<uint32_t>(rtpHeader[8]) << 24) | |
| 39 | (static_cast<uint32_t>(rtpHeader[9]) << 16) | |
| 40 | (static_cast<uint32_t>(rtpHeader[10]) << 8) | rtpHeader[11]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | } |
| 42 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 43 | void RTPStream::MakeRTPheader(uint8_t* rtpHeader, uint8_t payloadType, |
| 44 | int16_t seqNo, uint32_t timeStamp, |
| 45 | uint32_t ssrc) { |
| 46 | rtpHeader[0] = (unsigned char) 0x80; |
| 47 | rtpHeader[1] = (unsigned char) (payloadType & 0xFF); |
| 48 | rtpHeader[2] = (unsigned char) ((seqNo >> 8) & 0xFF); |
| 49 | rtpHeader[3] = (unsigned char) ((seqNo) & 0xFF); |
| 50 | rtpHeader[4] = (unsigned char) ((timeStamp >> 24) & 0xFF); |
| 51 | rtpHeader[5] = (unsigned char) ((timeStamp >> 16) & 0xFF); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 53 | rtpHeader[6] = (unsigned char) ((timeStamp >> 8) & 0xFF); |
| 54 | rtpHeader[7] = (unsigned char) (timeStamp & 0xFF); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 56 | rtpHeader[8] = (unsigned char) ((ssrc >> 24) & 0xFF); |
| 57 | rtpHeader[9] = (unsigned char) ((ssrc >> 16) & 0xFF); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 59 | rtpHeader[10] = (unsigned char) ((ssrc >> 8) & 0xFF); |
| 60 | rtpHeader[11] = (unsigned char) (ssrc & 0xFF); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 63 | RTPPacket::RTPPacket(uint8_t payloadType, uint32_t timeStamp, int16_t seqNo, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 64 | const uint8_t* payloadData, size_t payloadSize, |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 65 | uint32_t frequency) |
| 66 | : payloadType(payloadType), |
| 67 | timeStamp(timeStamp), |
| 68 | seqNo(seqNo), |
| 69 | payloadSize(payloadSize), |
| 70 | frequency(frequency) { |
| 71 | if (payloadSize > 0) { |
| 72 | this->payloadData = new uint8_t[payloadSize]; |
| 73 | memcpy(this->payloadData, payloadData, payloadSize); |
| 74 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 77 | RTPPacket::~RTPPacket() { |
| 78 | delete[] payloadData; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 81 | RTPBuffer::RTPBuffer() { |
| 82 | _queueRWLock = RWLockWrapper::CreateRWLock(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 83 | } |
| 84 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 85 | RTPBuffer::~RTPBuffer() { |
| 86 | delete _queueRWLock; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | } |
| 88 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 89 | void RTPBuffer::Write(const uint8_t payloadType, const uint32_t timeStamp, |
| 90 | const int16_t seqNo, const uint8_t* payloadData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 91 | const size_t payloadSize, uint32_t frequency) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 92 | RTPPacket *packet = new RTPPacket(payloadType, timeStamp, seqNo, payloadData, |
| 93 | payloadSize, frequency); |
| 94 | _queueRWLock->AcquireLockExclusive(); |
| 95 | _rtpQueue.push(packet); |
| 96 | _queueRWLock->ReleaseLockExclusive(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 97 | } |
| 98 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 99 | size_t RTPBuffer::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData, |
| 100 | size_t payloadSize, uint32_t* offset) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 101 | _queueRWLock->AcquireLockShared(); |
| 102 | RTPPacket *packet = _rtpQueue.front(); |
| 103 | _rtpQueue.pop(); |
| 104 | _queueRWLock->ReleaseLockShared(); |
| 105 | rtpInfo->header.markerBit = 1; |
| 106 | rtpInfo->header.payloadType = packet->payloadType; |
| 107 | rtpInfo->header.sequenceNumber = packet->seqNo; |
| 108 | rtpInfo->header.ssrc = 0; |
| 109 | rtpInfo->header.timestamp = packet->timeStamp; |
| 110 | if (packet->payloadSize > 0 && payloadSize >= packet->payloadSize) { |
| 111 | memcpy(payloadData, packet->payloadData, packet->payloadSize); |
| 112 | } else { |
henrik.lundin@webrtc.org | 741711a | 2014-09-25 07:38:14 +0000 | [diff] [blame] | 113 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 114 | } |
| 115 | *offset = (packet->timeStamp / (packet->frequency / 1000)); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 116 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 117 | return packet->payloadSize; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | } |
| 119 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 120 | bool RTPBuffer::EndOfFile() const { |
| 121 | _queueRWLock->AcquireLockShared(); |
| 122 | bool eof = _rtpQueue.empty(); |
| 123 | _queueRWLock->ReleaseLockShared(); |
| 124 | return eof; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | } |
| 126 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 127 | void RTPFile::Open(const char *filename, const char *mode) { |
| 128 | if ((_rtpFile = fopen(filename, mode)) == NULL) { |
| 129 | printf("Cannot write file %s.\n", filename); |
| 130 | ADD_FAILURE() << "Unable to write file"; |
| 131 | exit(1); |
| 132 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 135 | void RTPFile::Close() { |
| 136 | if (_rtpFile != NULL) { |
| 137 | fclose(_rtpFile); |
| 138 | _rtpFile = NULL; |
| 139 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 140 | } |
| 141 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 142 | void RTPFile::WriteHeader() { |
| 143 | // Write data in a format that NetEQ and RTP Play can parse |
| 144 | fprintf(_rtpFile, "#!RTPencode%s\n", "1.0"); |
| 145 | uint32_t dummy_variable = 0; |
| 146 | // should be converted to network endian format, but does not matter when 0 |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 147 | EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile)); |
| 148 | EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile)); |
| 149 | EXPECT_EQ(1u, fwrite(&dummy_variable, 4, 1, _rtpFile)); |
| 150 | EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile)); |
| 151 | EXPECT_EQ(1u, fwrite(&dummy_variable, 2, 1, _rtpFile)); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 152 | fflush(_rtpFile); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 153 | } |
| 154 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 155 | void RTPFile::ReadHeader() { |
| 156 | uint32_t start_sec, start_usec, source; |
| 157 | uint16_t port, padding; |
| 158 | char fileHeader[40]; |
| 159 | EXPECT_TRUE(fgets(fileHeader, 40, _rtpFile) != 0); |
| 160 | EXPECT_EQ(1u, fread(&start_sec, 4, 1, _rtpFile)); |
| 161 | start_sec = ntohl(start_sec); |
| 162 | EXPECT_EQ(1u, fread(&start_usec, 4, 1, _rtpFile)); |
| 163 | start_usec = ntohl(start_usec); |
| 164 | EXPECT_EQ(1u, fread(&source, 4, 1, _rtpFile)); |
| 165 | source = ntohl(source); |
| 166 | EXPECT_EQ(1u, fread(&port, 2, 1, _rtpFile)); |
| 167 | port = ntohs(port); |
| 168 | EXPECT_EQ(1u, fread(&padding, 2, 1, _rtpFile)); |
| 169 | padding = ntohs(padding); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | } |
| 171 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 172 | void RTPFile::Write(const uint8_t payloadType, const uint32_t timeStamp, |
| 173 | const int16_t seqNo, const uint8_t* payloadData, |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 174 | const size_t payloadSize, uint32_t frequency) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 175 | /* write RTP packet to file */ |
| 176 | uint8_t rtpHeader[12]; |
| 177 | MakeRTPheader(rtpHeader, payloadType, seqNo, timeStamp, 0); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 178 | ASSERT_LE(12 + payloadSize + 8, std::numeric_limits<u_short>::max()); |
| 179 | uint16_t lengthBytes = htons(static_cast<u_short>(12 + payloadSize + 8)); |
| 180 | uint16_t plen = htons(static_cast<u_short>(12 + payloadSize)); |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 181 | uint32_t offsetMs; |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 182 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 183 | offsetMs = (timeStamp / (frequency / 1000)); |
| 184 | offsetMs = htonl(offsetMs); |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 185 | EXPECT_EQ(1u, fwrite(&lengthBytes, 2, 1, _rtpFile)); |
| 186 | EXPECT_EQ(1u, fwrite(&plen, 2, 1, _rtpFile)); |
| 187 | EXPECT_EQ(1u, fwrite(&offsetMs, 4, 1, _rtpFile)); |
| 188 | EXPECT_EQ(1u, fwrite(&rtpHeader, 12, 1, _rtpFile)); |
| 189 | EXPECT_EQ(payloadSize, fwrite(payloadData, 1, payloadSize, _rtpFile)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 190 | } |
| 191 | |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 192 | size_t RTPFile::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData, |
| 193 | size_t payloadSize, uint32_t* offset) { |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 194 | uint16_t lengthBytes; |
| 195 | uint16_t plen; |
| 196 | uint8_t rtpHeader[12]; |
| 197 | size_t read_len = fread(&lengthBytes, 2, 1, _rtpFile); |
| 198 | /* Check if we have reached end of file. */ |
| 199 | if ((read_len == 0) && feof(_rtpFile)) { |
| 200 | _rtpEOF = true; |
henrik.lundin@webrtc.org | 741711a | 2014-09-25 07:38:14 +0000 | [diff] [blame] | 201 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 202 | } |
| 203 | EXPECT_EQ(1u, fread(&plen, 2, 1, _rtpFile)); |
| 204 | EXPECT_EQ(1u, fread(offset, 4, 1, _rtpFile)); |
| 205 | lengthBytes = ntohs(lengthBytes); |
| 206 | plen = ntohs(plen); |
| 207 | *offset = ntohl(*offset); |
| 208 | EXPECT_GT(plen, 11); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 209 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 210 | EXPECT_EQ(1u, fread(rtpHeader, 12, 1, _rtpFile)); |
| 211 | ParseRTPHeader(rtpInfo, rtpHeader); |
| 212 | rtpInfo->type.Audio.isCNG = false; |
| 213 | rtpInfo->type.Audio.channel = 1; |
| 214 | EXPECT_EQ(lengthBytes, plen + 8); |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 215 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 216 | if (plen == 0) { |
henrik.lundin@webrtc.org | 741711a | 2014-09-25 07:38:14 +0000 | [diff] [blame] | 217 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 218 | } |
| 219 | if (lengthBytes < 20) { |
henrik.lundin@webrtc.org | 741711a | 2014-09-25 07:38:14 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } |
pkasting@chromium.org | 4591fbd | 2014-11-20 22:28:14 +0000 | [diff] [blame] | 222 | if (payloadSize < static_cast<size_t>((lengthBytes - 20))) { |
henrik.lundin@webrtc.org | 741711a | 2014-09-25 07:38:14 +0000 | [diff] [blame] | 223 | return 0; |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 224 | } |
| 225 | lengthBytes -= 20; |
| 226 | EXPECT_EQ(lengthBytes, fread(payloadData, 1, lengthBytes, _rtpFile)); |
| 227 | return lengthBytes; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
tina.legrand@webrtc.org | d5726a1 | 2013-05-03 07:34:12 +0000 | [diff] [blame] | 230 | } // namespace webrtc |