1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
216b6b90a82b796460bb20762761182c7d104cd2dtina.legrand@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
113e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#include "webrtc/modules/audio_coding/test/Channel.h"
12a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org
13470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <assert.h>
14470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <iostream>
15470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
164591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org#include "webrtc/base/format_macros.h"
1798f53510b222f71fdd8b799b2f33737ceeb28c61Henrik Kjellander#include "webrtc/system_wrappers/include/tick_util.h"
1898f53510b222f71fdd8b799b2f33737ceeb28c61Henrik Kjellander#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
19470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
20554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.orgnamespace webrtc {
21470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
224591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.orgint32_t Channel::SendData(FrameType frameType,
234591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org                          uint8_t payloadType,
244591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org                          uint32_t timeStamp,
254591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org                          const uint8_t* payloadData,
264591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org                          size_t payloadSize,
27d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org                          const RTPFragmentationHeader* fragmentation) {
28d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  WebRtcRTPHeader rtpInfo;
29d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int32_t status;
304591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org  size_t payloadDataSize = payloadSize;
31d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
32d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  rtpInfo.header.markerBit = false;
33d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  rtpInfo.header.ssrc = 0;
34a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org  rtpInfo.header.sequenceNumber = (external_sequence_number_ < 0) ?
35a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      _seqNo++ : static_cast<uint16_t>(external_sequence_number_);
36d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  rtpInfo.header.payloadType = payloadType;
37a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org  rtpInfo.header.timestamp = (external_send_timestamp_ < 0) ? timeStamp :
38a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      static_cast<uint32_t>(external_send_timestamp_);
39a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org
40d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (frameType == kAudioFrameCN) {
41d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    rtpInfo.type.Audio.isCNG = true;
42d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
43d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    rtpInfo.type.Audio.isCNG = false;
44d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
4522993e1a0c114122fc1b9de0fc74d4096ec868bdpbos  if (frameType == kEmptyFrame) {
460561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org    // When frame is empty, we should not transmit it. The frame size of the
470561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org    // next non-empty frame will be based on the previous frame size.
480561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org    _useLastFrameSize = _lastFrameSizeSample > 0;
49d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    return 0;
50d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
51d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
52d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  rtpInfo.type.Audio.channel = 1;
53d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // Treat fragmentation separately
54d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (fragmentation != NULL) {
55d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // If silence for too long, send only new data.
5605db352f56b369444e1a83c5c85a4fb513bf60f0henrik.lundin@webrtc.org    if ((fragmentation->fragmentationVectorSize == 2) &&
5705db352f56b369444e1a83c5c85a4fb513bf60f0henrik.lundin@webrtc.org        (fragmentation->fragmentationTimeDiff[1] <= 0x3fff)) {
58d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // only 0x80 if we have multiple blocks
59d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadData[0] = 0x80 + fragmentation->fragmentationPlType[1];
604591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org      size_t REDheader = (fragmentation->fragmentationTimeDiff[1] << 10) +
614591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org          fragmentation->fragmentationLength[1];
62d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadData[1] = uint8_t((REDheader >> 16) & 0x000000FF);
63d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadData[2] = uint8_t((REDheader >> 8) & 0x000000FF);
64d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadData[3] = uint8_t(REDheader & 0x000000FF);
65d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
66d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadData[4] = fragmentation->fragmentationPlType[0];
67d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // copy the RED data
68d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      memcpy(_payloadData + 5,
69d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org             payloadData + fragmentation->fragmentationOffset[1],
70d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org             fragmentation->fragmentationLength[1]);
71d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // copy the normal data
72d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      memcpy(_payloadData + 5 + fragmentation->fragmentationLength[1],
73d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org             payloadData + fragmentation->fragmentationOffset[0],
74d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org             fragmentation->fragmentationLength[0]);
75d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      payloadDataSize += 5;
76d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
77d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // single block (newest one)
78d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      memcpy(_payloadData, payloadData + fragmentation->fragmentationOffset[0],
79d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org             fragmentation->fragmentationLength[0]);
804591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org      payloadDataSize = fragmentation->fragmentationLength[0];
81d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      rtpInfo.header.payloadType = fragmentation->fragmentationPlType[0];
82470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
83d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
84d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    memcpy(_payloadData, payloadData, payloadDataSize);
85d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_isStereo) {
86d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      if (_leftChannel) {
87d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        memcpy(&_rtpInfo, &rtpInfo, sizeof(WebRtcRTPHeader));
88d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        _leftChannel = false;
89d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        rtpInfo.type.Audio.channel = 1;
90d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      } else {
91d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        memcpy(&rtpInfo, &_rtpInfo, sizeof(WebRtcRTPHeader));
92d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        _leftChannel = true;
93d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        rtpInfo.type.Audio.channel = 2;
94d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
95470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
96d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
97d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
98d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
99d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_saveBitStream) {
100d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    //fwrite(payloadData, sizeof(uint8_t), payloadSize, _bitStreamFile);
101d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
102d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
103d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (!_isStereo) {
104d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    CalcStatistics(rtpInfo, payloadSize);
105d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
1060561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org  _useLastFrameSize = false;
107d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _lastInTimestamp = timeStamp;
108d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _totalBytes += payloadDataSize;
109d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
110d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
111d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_useFECTestWithPacketLoss) {
112d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _packetLoss += 1;
113d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_packetLoss == 3) {
114d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _packetLoss = 0;
115d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return 0;
116470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
117d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
118470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
119a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org  if (num_packets_to_drop_ > 0) {
120a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org    num_packets_to_drop_--;
121a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org    return 0;
122a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org  }
123a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org
124d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  status = _receiverACM->IncomingPacket(_payloadData, payloadDataSize, rtpInfo);
125470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
126d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return status;
127470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
128470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
129c2d69d3229329ebc74e9b559bb1154695246b47dturaj@webrtc.org// TODO(turajs): rewite this method.
1304591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.orgvoid Channel::CalcStatistics(WebRtcRTPHeader& rtpInfo, size_t payloadSize) {
131d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
132d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if ((rtpInfo.header.payloadType != _lastPayloadType)
133d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      && (_lastPayloadType != -1)) {
134d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // payload-type is changed.
135d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // we have to terminate the calculations on the previous payload type
136d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // we ignore the last packet in that payload type just to make things
137d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // easier.
138d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
139d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      if (_lastPayloadType == _payloadStats[n].payloadType) {
140d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        _payloadStats[n].newPacket = true;
141d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        break;
142d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
143470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
144d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
145d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _lastPayloadType = rtpInfo.header.payloadType;
146d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
147d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  bool newPayload = true;
148d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  ACMTestPayloadStats* currentPayloadStr = NULL;
149d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
150d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (rtpInfo.header.payloadType == _payloadStats[n].payloadType) {
151d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      newPayload = false;
152d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr = &_payloadStats[n];
153d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      break;
154470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
155d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
156d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
157d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (!newPayload) {
158d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (!currentPayloadStr->newPacket) {
1590561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      if (!_useLastFrameSize) {
1600561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        _lastFrameSizeSample = (uint32_t) ((uint32_t) rtpInfo.header.timestamp -
1610561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org            (uint32_t) currentPayloadStr->lastTimestamp);
1620561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      }
1630561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      assert(_lastFrameSizeSample > 0);
164d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      int k = 0;
1650561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      for (; k < MAX_NUM_FRAMESIZES; ++k) {
1660561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        if ((currentPayloadStr->frameSizeStats[k].frameSizeSample ==
1670561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org            _lastFrameSizeSample) ||
1680561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org            (currentPayloadStr->frameSizeStats[k].frameSizeSample == 0)) {
1690561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org          break;
1700561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        }
1710561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      }
1720561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      if (k == MAX_NUM_FRAMESIZES) {
1730561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        // New frame size found but no space to count statistics on it. Skip it.
1740561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        printf("No memory to store statistics for payload %d : frame size %d\n",
1750561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org               _lastPayloadType, _lastFrameSizeSample);
1760561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org        return;
177d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
178d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      ACMTestFrameSizeStats* currentFrameSizeStats = &(currentPayloadStr
179d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org          ->frameSizeStats[k]);
1800561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      currentFrameSizeStats->frameSizeSample = (int16_t) _lastFrameSizeSample;
181d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
182d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // increment the number of encoded samples.
1830561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      currentFrameSizeStats->totalEncodedSamples += _lastFrameSizeSample;
184d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // increment the number of recveived packets
185d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentFrameSizeStats->numPackets++;
186d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // increment the total number of bytes (this is based on
187d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // the previous payload we don't know the frame-size of
188d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // the current payload.
189d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentFrameSizeStats->totalPayloadLenByte += currentPayloadStr
190d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org          ->lastPayloadLenByte;
191d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // store the maximum payload-size (this is based on
192d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // the previous payload we don't know the frame-size of
193d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // the current payload.
194d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      if (currentFrameSizeStats->maxPayloadLen
195d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org          < currentPayloadStr->lastPayloadLenByte) {
196d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        currentFrameSizeStats->maxPayloadLen = currentPayloadStr
197d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org            ->lastPayloadLenByte;
198d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
199d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      // store the current values for the next time
200d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->lastTimestamp = rtpInfo.header.timestamp;
201d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->lastPayloadLenByte = payloadSize;
202d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
203d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->newPacket = false;
204d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->lastPayloadLenByte = payloadSize;
205d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->lastTimestamp = rtpInfo.header.timestamp;
206d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      currentPayloadStr->payloadType = rtpInfo.header.payloadType;
20778f0db4710c7cf00830646c75d8782ab426d514cturaj@webrtc.org      memset(currentPayloadStr->frameSizeStats, 0, MAX_NUM_FRAMESIZES *
20878f0db4710c7cf00830646c75d8782ab426d514cturaj@webrtc.org             sizeof(ACMTestFrameSizeStats));
209470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
210d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
211d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    n = 0;
212d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    while (_payloadStats[n].payloadType != -1) {
213d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      n++;
214470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
215d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // first packet
216d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].newPacket = false;
217d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].lastPayloadLenByte = payloadSize;
218d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].lastTimestamp = rtpInfo.header.timestamp;
219d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].payloadType = rtpInfo.header.payloadType;
22078f0db4710c7cf00830646c75d8782ab426d514cturaj@webrtc.org    memset(_payloadStats[n].frameSizeStats, 0, MAX_NUM_FRAMESIZES *
22178f0db4710c7cf00830646c75d8782ab426d514cturaj@webrtc.org           sizeof(ACMTestFrameSizeStats));
222d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
223470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
224470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
225d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgChannel::Channel(int16_t chID)
226d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    : _receiverACM(NULL),
227d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _seqNo(0),
228d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _channelCritSect(CriticalSectionWrapper::CreateCriticalSection()),
229d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _bitStreamFile(NULL),
230d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _saveBitStream(false),
231d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _lastPayloadType(-1),
232d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _isStereo(false),
233d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _leftChannel(true),
234d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _lastInTimestamp(0),
2350561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      _useLastFrameSize(false),
2360561716ae262461eaa3fe5291f4626c76822108aminyue@webrtc.org      _lastFrameSizeSample(0),
237d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _packetLoss(0),
238d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _useFECTestWithPacketLoss(false),
239d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _beginTime(TickTime::MillisecondTimestamp()),
240a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      _totalBytes(0),
241a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      external_send_timestamp_(-1),
242a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      external_sequence_number_(-1),
243a305e9612afb9feac6bdbced512beb3a2557c3e2turaj@webrtc.org      num_packets_to_drop_(0) {
244d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
245d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int k;
246d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
247d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].payloadType = -1;
248d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].newPacket = true;
249d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (k = 0; k < MAX_NUM_FRAMESIZES; k++) {
250d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].frameSizeSample = 0;
251d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].maxPayloadLen = 0;
252d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].numPackets = 0;
253d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].totalPayloadLenByte = 0;
254d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].totalEncodedSamples = 0;
255470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
256d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
257d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (chID >= 0) {
258d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _saveBitStream = true;
259d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    char bitStreamFileName[500];
260d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    sprintf(bitStreamFileName, "bitStream_%d.dat", chID);
261d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _bitStreamFile = fopen(bitStreamFileName, "wb");
262d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
263d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _saveBitStream = false;
264d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
265470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
266470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
267d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgChannel::~Channel() {
268d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  delete _channelCritSect;
269470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
270470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
271d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid Channel::RegisterReceiverACM(AudioCodingModule* acm) {
272d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _receiverACM = acm;
273d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
274470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
275470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
276d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid Channel::ResetStats() {
277d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
278d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int k;
279d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
280d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _lastPayloadType = -1;
281d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
282d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].payloadType = -1;
283d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _payloadStats[n].newPacket = true;
284d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (k = 0; k < MAX_NUM_FRAMESIZES; k++) {
285d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].frameSizeSample = 0;
286d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].maxPayloadLen = 0;
287d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].numPackets = 0;
288d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].totalPayloadLenByte = 0;
289d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _payloadStats[n].frameSizeStats[k].totalEncodedSamples = 0;
290470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
291d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
292d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _beginTime = TickTime::MillisecondTimestamp();
293d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _totalBytes = 0;
294d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
295470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
296470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
297d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgint16_t Channel::Stats(CodecInst& codecInst,
298d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org                       ACMTestPayloadStats& payloadStats) {
299d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
300d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
301d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  payloadStats.payloadType = -1;
302d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (n = 0; n < MAX_NUM_PAYLOADS; n++) {
303d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_payloadStats[n].payloadType == codecInst.pltype) {
304d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      memcpy(&payloadStats, &_payloadStats[n], sizeof(ACMTestPayloadStats));
305d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      break;
306470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
307d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
308d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (payloadStats.payloadType == -1) {
309d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _channelCritSect->Leave();
310d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    return -1;
311d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
312d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (n = 0; n < MAX_NUM_FRAMESIZES; n++) {
313d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (payloadStats.frameSizeStats[n].frameSizeSample == 0) {
314d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _channelCritSect->Leave();
315d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return 0;
316470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
317d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    payloadStats.frameSizeStats[n].usageLenSec = (double) payloadStats
318d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        .frameSizeStats[n].totalEncodedSamples / (double) codecInst.plfreq;
319470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
320d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    payloadStats.frameSizeStats[n].rateBitPerSec =
321d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        payloadStats.frameSizeStats[n].totalPayloadLenByte * 8
322d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org            / payloadStats.frameSizeStats[n].usageLenSec;
323470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
324d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
325d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
326d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return 0;
327470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
328470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
329d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid Channel::Stats(uint32_t* numPackets) {
330d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
331d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int k;
332d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
333d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  memset(numPackets, 0, MAX_NUM_PAYLOADS * sizeof(uint32_t));
334d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (k = 0; k < MAX_NUM_PAYLOADS; k++) {
335d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_payloadStats[k].payloadType == -1) {
336d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      break;
337470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
338d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    numPackets[k] = 0;
339d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (n = 0; n < MAX_NUM_FRAMESIZES; n++) {
340d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      if (_payloadStats[k].frameSizeStats[n].frameSizeSample == 0) {
341d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        break;
342d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
343d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      numPackets[k] += _payloadStats[k].frameSizeStats[n].numPackets;
344d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    }
345d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
346d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
347470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
348470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
349d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid Channel::Stats(uint8_t* payloadType, uint32_t* payloadLenByte) {
350d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
351470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
352d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int k;
353d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int n;
354d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  memset(payloadLenByte, 0, MAX_NUM_PAYLOADS * sizeof(uint32_t));
355d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (k = 0; k < MAX_NUM_PAYLOADS; k++) {
356d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_payloadStats[k].payloadType == -1) {
357d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      break;
358470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
359d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    payloadType[k] = (uint8_t) _payloadStats[k].payloadType;
360d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    payloadLenByte[k] = 0;
361d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (n = 0; n < MAX_NUM_FRAMESIZES; n++) {
362d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      if (_payloadStats[k].frameSizeStats[n].frameSizeSample == 0) {
363d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        break;
364d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      }
365d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      payloadLenByte[k] += (uint16_t) _payloadStats[k].frameSizeStats[n]
366d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org          .totalPayloadLenByte;
367d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    }
368d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
369d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
370d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
371d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org}
372470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
373d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid Channel::PrintStats(CodecInst& codecInst) {
374d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  ACMTestPayloadStats payloadStats;
375d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  Stats(codecInst, payloadStats);
376d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  printf("%s %d kHz\n", codecInst.plname, codecInst.plfreq / 1000);
377d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  printf("=====================================================\n");
378d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (payloadStats.payloadType == -1) {
379d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("No Packets are sent with payload-type %d (%s)\n\n",
380d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           codecInst.pltype, codecInst.plname);
381d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    return;
382d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
383d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (int k = 0; k < MAX_NUM_FRAMESIZES; k++) {
384d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (payloadStats.frameSizeStats[k].frameSizeSample == 0) {
385d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      break;
386470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
387d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("Frame-size.................... %d samples\n",
388d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           payloadStats.frameSizeStats[k].frameSizeSample);
389d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("Average Rate.................. %.0f bits/sec\n",
390d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           payloadStats.frameSizeStats[k].rateBitPerSec);
3914591fbd09f9cb6e83433c49a12dd8524c2806502pkasting@chromium.org    printf("Maximum Payload-Size.......... %" PRIuS " Bytes\n",
392d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           payloadStats.frameSizeStats[k].maxPayloadLen);
393d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf(
394d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        "Maximum Instantaneous Rate.... %.0f bits/sec\n",
395d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org        ((double) payloadStats.frameSizeStats[k].maxPayloadLen * 8.0
396d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org            * (double) codecInst.plfreq)
397d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org            / (double) payloadStats.frameSizeStats[k].frameSizeSample);
398d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("Number of Packets............. %u\n",
399d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           (unsigned int) payloadStats.frameSizeStats[k].numPackets);
400d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("Duration...................... %0.3f sec\n\n",
401d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           payloadStats.frameSizeStats[k].usageLenSec);
402d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
403d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
404470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
405470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
406470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
407d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orguint32_t Channel::LastInTimestamp() {
408d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  uint32_t timestamp;
409d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
410d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  timestamp = _lastInTimestamp;
411d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
412d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return timestamp;
413470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
414470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
415d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgdouble Channel::BitRate() {
416d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  double rate;
417d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  uint64_t currTime = TickTime::MillisecondTimestamp();
418d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Enter();
419d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  rate = ((double) _totalBytes * 8.0) / (double) (currTime - _beginTime);
420d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _channelCritSect->Leave();
421d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return rate;
422d7a71d0719251db2a31b71a9d6e9fef4f60ba778andrew@webrtc.org}
423554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.org
424d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org}  // namespace webrtc
425