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.

275 lines
8.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 "modules/audio_coding/test/Channel.h"
  11. #include <assert.h>
  12. #include <iostream>
  13. #include "rtc_base/format_macros.h"
  14. #include "rtc_base/time_utils.h"
  15. namespace webrtc {
  16. int32_t Channel::SendData(AudioFrameType frameType,
  17. uint8_t payloadType,
  18. uint32_t timeStamp,
  19. const uint8_t* payloadData,
  20. size_t payloadSize) {
  21. RTPHeader rtp_header;
  22. int32_t status;
  23. size_t payloadDataSize = payloadSize;
  24. rtp_header.markerBit = false;
  25. rtp_header.ssrc = 0;
  26. rtp_header.sequenceNumber =
  27. (external_sequence_number_ < 0)
  28. ? _seqNo++
  29. : static_cast<uint16_t>(external_sequence_number_);
  30. rtp_header.payloadType = payloadType;
  31. rtp_header.timestamp = (external_send_timestamp_ < 0)
  32. ? timeStamp
  33. : static_cast<uint32_t>(external_send_timestamp_);
  34. if (frameType == AudioFrameType::kEmptyFrame) {
  35. // When frame is empty, we should not transmit it. The frame size of the
  36. // next non-empty frame will be based on the previous frame size.
  37. _useLastFrameSize = _lastFrameSizeSample > 0;
  38. return 0;
  39. }
  40. memcpy(_payloadData, payloadData, payloadDataSize);
  41. if (_isStereo) {
  42. if (_leftChannel) {
  43. _rtp_header = rtp_header;
  44. _leftChannel = false;
  45. } else {
  46. rtp_header = _rtp_header;
  47. _leftChannel = true;
  48. }
  49. }
  50. _channelCritSect.Enter();
  51. if (_saveBitStream) {
  52. // fwrite(payloadData, sizeof(uint8_t), payloadSize, _bitStreamFile);
  53. }
  54. if (!_isStereo) {
  55. CalcStatistics(rtp_header, payloadSize);
  56. }
  57. _useLastFrameSize = false;
  58. _lastInTimestamp = timeStamp;
  59. _totalBytes += payloadDataSize;
  60. _channelCritSect.Leave();
  61. if (_useFECTestWithPacketLoss) {
  62. _packetLoss += 1;
  63. if (_packetLoss == 3) {
  64. _packetLoss = 0;
  65. return 0;
  66. }
  67. }
  68. if (num_packets_to_drop_ > 0) {
  69. num_packets_to_drop_--;
  70. return 0;
  71. }
  72. status =
  73. _receiverACM->IncomingPacket(_payloadData, payloadDataSize, rtp_header);
  74. return status;
  75. }
  76. // TODO(turajs): rewite this method.
  77. void Channel::CalcStatistics(const RTPHeader& rtp_header, size_t payloadSize) {
  78. int n;
  79. if ((rtp_header.payloadType != _lastPayloadType) &&
  80. (_lastPayloadType != -1)) {
  81. // payload-type is changed.
  82. // we have to terminate the calculations on the previous payload type
  83. // we ignore the last packet in that payload type just to make things
  84. // easier.
  85. for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
  86. if (_lastPayloadType == _payloadStats[n].payloadType) {
  87. _payloadStats[n].newPacket = true;
  88. break;
  89. }
  90. }
  91. }
  92. _lastPayloadType = rtp_header.payloadType;
  93. bool newPayload = true;
  94. ACMTestPayloadStats* currentPayloadStr = NULL;
  95. for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
  96. if (rtp_header.payloadType == _payloadStats[n].payloadType) {
  97. newPayload = false;
  98. currentPayloadStr = &_payloadStats[n];
  99. break;
  100. }
  101. }
  102. if (!newPayload) {
  103. if (!currentPayloadStr->newPacket) {
  104. if (!_useLastFrameSize) {
  105. _lastFrameSizeSample =
  106. (uint32_t)((uint32_t)rtp_header.timestamp -
  107. (uint32_t)currentPayloadStr->lastTimestamp);
  108. }
  109. assert(_lastFrameSizeSample > 0);
  110. int k = 0;
  111. for (; k < MAX_NUM_FRAMESIZES; ++k) {
  112. if ((currentPayloadStr->frameSizeStats[k].frameSizeSample ==
  113. _lastFrameSizeSample) ||
  114. (currentPayloadStr->frameSizeStats[k].frameSizeSample == 0)) {
  115. break;
  116. }
  117. }
  118. if (k == MAX_NUM_FRAMESIZES) {
  119. // New frame size found but no space to count statistics on it. Skip it.
  120. printf("No memory to store statistics for payload %d : frame size %d\n",
  121. _lastPayloadType, _lastFrameSizeSample);
  122. return;
  123. }
  124. ACMTestFrameSizeStats* currentFrameSizeStats =
  125. &(currentPayloadStr->frameSizeStats[k]);
  126. currentFrameSizeStats->frameSizeSample = (int16_t)_lastFrameSizeSample;
  127. // increment the number of encoded samples.
  128. currentFrameSizeStats->totalEncodedSamples += _lastFrameSizeSample;
  129. // increment the number of recveived packets
  130. currentFrameSizeStats->numPackets++;
  131. // increment the total number of bytes (this is based on
  132. // the previous payload we don't know the frame-size of
  133. // the current payload.
  134. currentFrameSizeStats->totalPayloadLenByte +=
  135. currentPayloadStr->lastPayloadLenByte;
  136. // store the maximum payload-size (this is based on
  137. // the previous payload we don't know the frame-size of
  138. // the current payload.
  139. if (currentFrameSizeStats->maxPayloadLen <
  140. currentPayloadStr->lastPayloadLenByte) {
  141. currentFrameSizeStats->maxPayloadLen =
  142. currentPayloadStr->lastPayloadLenByte;
  143. }
  144. // store the current values for the next time
  145. currentPayloadStr->lastTimestamp = rtp_header.timestamp;
  146. currentPayloadStr->lastPayloadLenByte = payloadSize;
  147. } else {
  148. currentPayloadStr->newPacket = false;
  149. currentPayloadStr->lastPayloadLenByte = payloadSize;
  150. currentPayloadStr->lastTimestamp = rtp_header.timestamp;
  151. currentPayloadStr->payloadType = rtp_header.payloadType;
  152. memset(currentPayloadStr->frameSizeStats, 0,
  153. MAX_NUM_FRAMESIZES * sizeof(ACMTestFrameSizeStats));
  154. }
  155. } else {
  156. n = 0;
  157. while (_payloadStats[n].payloadType != -1) {
  158. n++;
  159. }
  160. // first packet
  161. _payloadStats[n].newPacket = false;
  162. _payloadStats[n].lastPayloadLenByte = payloadSize;
  163. _payloadStats[n].lastTimestamp = rtp_header.timestamp;
  164. _payloadStats[n].payloadType = rtp_header.payloadType;
  165. memset(_payloadStats[n].frameSizeStats, 0,
  166. MAX_NUM_FRAMESIZES * sizeof(ACMTestFrameSizeStats));
  167. }
  168. }
  169. Channel::Channel(int16_t chID)
  170. : _receiverACM(NULL),
  171. _seqNo(0),
  172. _bitStreamFile(NULL),
  173. _saveBitStream(false),
  174. _lastPayloadType(-1),
  175. _isStereo(false),
  176. _leftChannel(true),
  177. _lastInTimestamp(0),
  178. _useLastFrameSize(false),
  179. _lastFrameSizeSample(0),
  180. _packetLoss(0),
  181. _useFECTestWithPacketLoss(false),
  182. _beginTime(rtc::TimeMillis()),
  183. _totalBytes(0),
  184. external_send_timestamp_(-1),
  185. external_sequence_number_(-1),
  186. num_packets_to_drop_(0) {
  187. int n;
  188. int k;
  189. for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
  190. _payloadStats[n].payloadType = -1;
  191. _payloadStats[n].newPacket = true;
  192. for (k = 0; k < MAX_NUM_FRAMESIZES; k++) {
  193. _payloadStats[n].frameSizeStats[k].frameSizeSample = 0;
  194. _payloadStats[n].frameSizeStats[k].maxPayloadLen = 0;
  195. _payloadStats[n].frameSizeStats[k].numPackets = 0;
  196. _payloadStats[n].frameSizeStats[k].totalPayloadLenByte = 0;
  197. _payloadStats[n].frameSizeStats[k].totalEncodedSamples = 0;
  198. }
  199. }
  200. if (chID >= 0) {
  201. _saveBitStream = true;
  202. char bitStreamFileName[500];
  203. sprintf(bitStreamFileName, "bitStream_%d.dat", chID);
  204. _bitStreamFile = fopen(bitStreamFileName, "wb");
  205. } else {
  206. _saveBitStream = false;
  207. }
  208. }
  209. Channel::~Channel() {}
  210. void Channel::RegisterReceiverACM(AudioCodingModule* acm) {
  211. _receiverACM = acm;
  212. return;
  213. }
  214. void Channel::ResetStats() {
  215. int n;
  216. int k;
  217. _channelCritSect.Enter();
  218. _lastPayloadType = -1;
  219. for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
  220. _payloadStats[n].payloadType = -1;
  221. _payloadStats[n].newPacket = true;
  222. for (k = 0; k < MAX_NUM_FRAMESIZES; k++) {
  223. _payloadStats[n].frameSizeStats[k].frameSizeSample = 0;
  224. _payloadStats[n].frameSizeStats[k].maxPayloadLen = 0;
  225. _payloadStats[n].frameSizeStats[k].numPackets = 0;
  226. _payloadStats[n].frameSizeStats[k].totalPayloadLenByte = 0;
  227. _payloadStats[n].frameSizeStats[k].totalEncodedSamples = 0;
  228. }
  229. }
  230. _beginTime = rtc::TimeMillis();
  231. _totalBytes = 0;
  232. _channelCritSect.Leave();
  233. }
  234. uint32_t Channel::LastInTimestamp() {
  235. uint32_t timestamp;
  236. _channelCritSect.Enter();
  237. timestamp = _lastInTimestamp;
  238. _channelCritSect.Leave();
  239. return timestamp;
  240. }
  241. double Channel::BitRate() {
  242. double rate;
  243. uint64_t currTime = rtc::TimeMillis();
  244. _channelCritSect.Enter();
  245. rate = ((double)_totalBytes * 8.0) / (double)(currTime - _beginTime);
  246. _channelCritSect.Leave();
  247. return rate;
  248. }
  249. } // namespace webrtc