2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
16 # include <Winsock2.h>
18 # include <arpa/inet.h>
21 #include "audio_coding_module.h"
22 #include "engine_configurations.h"
23 #include "gtest/gtest.h" // TODO (tlegrand): Consider removing usage of gtest.
24 #include "rw_lock_wrapper.h"
28 void RTPStream::ParseRTPHeader(WebRtcRTPHeader* rtpInfo,
29 const uint8_t* rtpHeader) {
30 rtpInfo->header.payloadType = rtpHeader[1];
31 rtpInfo->header.sequenceNumber = (static_cast<uint16_t>(rtpHeader[2]) << 8) |
33 rtpInfo->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) | rtpHeader[7];
36 rtpInfo->header.ssrc = (static_cast<uint32_t>(rtpHeader[8]) << 24) |
37 (static_cast<uint32_t>(rtpHeader[9]) << 16) |
38 (static_cast<uint32_t>(rtpHeader[10]) << 8) | rtpHeader[11];
41 void RTPStream::MakeRTPheader(uint8_t* rtpHeader, uint8_t payloadType,
42 int16_t seqNo, uint32_t timeStamp,
44 rtpHeader[0] = (unsigned char) 0x80;
45 rtpHeader[1] = (unsigned char) (payloadType & 0xFF);
46 rtpHeader[2] = (unsigned char) ((seqNo >> 8) & 0xFF);
47 rtpHeader[3] = (unsigned char) ((seqNo) & 0xFF);
48 rtpHeader[4] = (unsigned char) ((timeStamp >> 24) & 0xFF);
49 rtpHeader[5] = (unsigned char) ((timeStamp >> 16) & 0xFF);
51 rtpHeader[6] = (unsigned char) ((timeStamp >> 8) & 0xFF);
52 rtpHeader[7] = (unsigned char) (timeStamp & 0xFF);
54 rtpHeader[8] = (unsigned char) ((ssrc >> 24) & 0xFF);
55 rtpHeader[9] = (unsigned char) ((ssrc >> 16) & 0xFF);
57 rtpHeader[10] = (unsigned char) ((ssrc >> 8) & 0xFF);
58 rtpHeader[11] = (unsigned char) (ssrc & 0xFF);
61 RTPPacket::RTPPacket(uint8_t payloadType, uint32_t timeStamp, int16_t seqNo,
62 const uint8_t* payloadData, uint16_t payloadSize,
64 : payloadType(payloadType),
67 payloadSize(payloadSize),
68 frequency(frequency) {
69 if (payloadSize > 0) {
70 this->payloadData = new uint8_t[payloadSize];
71 memcpy(this->payloadData, payloadData, payloadSize);
75 RTPPacket::~RTPPacket() {
79 RTPBuffer::RTPBuffer() {
80 _queueRWLock = RWLockWrapper::CreateRWLock();
83 RTPBuffer::~RTPBuffer() {
87 void RTPBuffer::Write(const uint8_t payloadType, const uint32_t timeStamp,
88 const int16_t seqNo, const uint8_t* payloadData,
89 const uint16_t payloadSize, uint32_t frequency) {
90 RTPPacket *packet = new RTPPacket(payloadType, timeStamp, seqNo, payloadData,
91 payloadSize, frequency);
92 _queueRWLock->AcquireLockExclusive();
93 _rtpQueue.push(packet);
94 _queueRWLock->ReleaseLockExclusive();
97 uint16_t RTPBuffer::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData,
98 uint16_t payloadSize, uint32_t* offset) {
99 _queueRWLock->AcquireLockShared();
100 RTPPacket *packet = _rtpQueue.front();
102 _queueRWLock->ReleaseLockShared();
103 rtpInfo->header.markerBit = 1;
104 rtpInfo->header.payloadType = packet->payloadType;
105 rtpInfo->header.sequenceNumber = packet->seqNo;
106 rtpInfo->header.ssrc = 0;
107 rtpInfo->header.timestamp = packet->timeStamp;
108 if (packet->payloadSize > 0 && payloadSize >= packet->payloadSize) {
109 memcpy(payloadData, packet->payloadData, packet->payloadSize);
113 *offset = (packet->timeStamp / (packet->frequency / 1000));
115 return packet->payloadSize;
118 bool RTPBuffer::EndOfFile() const {
119 _queueRWLock->AcquireLockShared();
120 bool eof = _rtpQueue.empty();
121 _queueRWLock->ReleaseLockShared();
125 void RTPFile::Open(const char *filename, const char *mode) {
126 if ((_rtpFile = fopen(filename, mode)) == NULL) {
127 printf("Cannot write file %s.\n", filename);
128 ADD_FAILURE() << "Unable to write file";
133 void RTPFile::Close() {
134 if (_rtpFile != NULL) {
140 void 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
145 if (fwrite(&dummy_variable, 4, 1, _rtpFile) != 1) {
148 if (fwrite(&dummy_variable, 4, 1, _rtpFile) != 1) {
151 if (fwrite(&dummy_variable, 4, 1, _rtpFile) != 1) {
154 if (fwrite(&dummy_variable, 2, 1, _rtpFile) != 1) {
157 if (fwrite(&dummy_variable, 2, 1, _rtpFile) != 1) {
163 void RTPFile::ReadHeader() {
164 uint32_t start_sec, start_usec, source;
165 uint16_t port, padding;
167 EXPECT_TRUE(fgets(fileHeader, 40, _rtpFile) != 0);
168 EXPECT_EQ(1u, fread(&start_sec, 4, 1, _rtpFile));
169 start_sec = ntohl(start_sec);
170 EXPECT_EQ(1u, fread(&start_usec, 4, 1, _rtpFile));
171 start_usec = ntohl(start_usec);
172 EXPECT_EQ(1u, fread(&source, 4, 1, _rtpFile));
173 source = ntohl(source);
174 EXPECT_EQ(1u, fread(&port, 2, 1, _rtpFile));
176 EXPECT_EQ(1u, fread(&padding, 2, 1, _rtpFile));
177 padding = ntohs(padding);
180 void RTPFile::Write(const uint8_t payloadType, const uint32_t timeStamp,
181 const int16_t seqNo, const uint8_t* payloadData,
182 const uint16_t payloadSize, uint32_t frequency) {
183 /* write RTP packet to file */
184 uint8_t rtpHeader[12];
185 MakeRTPheader(rtpHeader, payloadType, seqNo, timeStamp, 0);
186 uint16_t lengthBytes = htons(12 + payloadSize + 8);
187 uint16_t plen = htons(12 + payloadSize);
190 offsetMs = (timeStamp / (frequency / 1000));
191 offsetMs = htonl(offsetMs);
192 if (fwrite(&lengthBytes, 2, 1, _rtpFile) != 1) {
195 if (fwrite(&plen, 2, 1, _rtpFile) != 1) {
198 if (fwrite(&offsetMs, 4, 1, _rtpFile) != 1) {
201 if (fwrite(rtpHeader, 12, 1, _rtpFile) != 1) {
204 if (fwrite(payloadData, 1, payloadSize, _rtpFile) != payloadSize) {
209 uint16_t RTPFile::Read(WebRtcRTPHeader* rtpInfo, uint8_t* payloadData,
210 uint16_t payloadSize, uint32_t* offset) {
211 uint16_t lengthBytes;
213 uint8_t rtpHeader[12];
214 size_t read_len = fread(&lengthBytes, 2, 1, _rtpFile);
215 /* Check if we have reached end of file. */
216 if ((read_len == 0) && feof(_rtpFile)) {
220 EXPECT_EQ(1u, fread(&plen, 2, 1, _rtpFile));
221 EXPECT_EQ(1u, fread(offset, 4, 1, _rtpFile));
222 lengthBytes = ntohs(lengthBytes);
224 *offset = ntohl(*offset);
227 EXPECT_EQ(1u, fread(rtpHeader, 12, 1, _rtpFile));
228 ParseRTPHeader(rtpInfo, rtpHeader);
229 rtpInfo->type.Audio.isCNG = false;
230 rtpInfo->type.Audio.channel = 1;
231 EXPECT_EQ(lengthBytes, plen + 8);
236 if (payloadSize < (lengthBytes - 20)) {
239 if (lengthBytes < 20) {
243 EXPECT_EQ(lengthBytes, fread(payloadData, 1, lengthBytes, _rtpFile));
247 } // namespace webrtc