You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

240 lines
7.5 KiB

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