1/*
2 *  Copyright (c) 2011 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
11#include "structs.h"
12#include "bandwidth_estimator.h"
13#include "entropy_coding.h"
14#include "codec.h"
15
16
17int
18WebRtcIsac_EstimateBandwidth(
19    BwEstimatorstr*           bwest_str,
20    Bitstr*                   streamdata,
21    int32_t               packet_size,
22    uint16_t              rtp_seq_number,
23    uint32_t              send_ts,
24    uint32_t              arr_ts,
25    enum IsacSamplingRate encoderSampRate,
26    enum IsacSamplingRate decoderSampRate)
27{
28  int16_t  index;
29  int16_t  frame_samples;
30  uint32_t sendTimestampIn16kHz;
31  uint32_t arrivalTimestampIn16kHz;
32  uint32_t diffSendTime;
33  uint32_t diffArrivalTime;
34  int err;
35
36  /* decode framelength and BW estimation */
37  err = WebRtcIsac_DecodeFrameLen(streamdata, &frame_samples);
38  if(err < 0)  // error check
39  {
40    return err;
41  }
42  err = WebRtcIsac_DecodeSendBW(streamdata, &index);
43  if(err < 0)  // error check
44  {
45    return err;
46  }
47
48  /* UPDATE ESTIMATES FROM OTHER SIDE */
49  err = WebRtcIsac_UpdateUplinkBwImpl(bwest_str, index, encoderSampRate);
50  if(err < 0)
51  {
52    return err;
53  }
54
55  // We like BWE to work at 16 kHz sampling rate,
56  // therefore, we have to change the timestamps accordingly.
57  // translate the send timestamp if required
58  diffSendTime = (uint32_t)((uint32_t)send_ts -
59                                  (uint32_t)bwest_str->senderTimestamp);
60  bwest_str->senderTimestamp = send_ts;
61
62  diffArrivalTime = (uint32_t)((uint32_t)arr_ts -
63                                     (uint32_t)bwest_str->receiverTimestamp);
64  bwest_str->receiverTimestamp = arr_ts;
65
66  if(decoderSampRate == kIsacSuperWideband)
67  {
68    diffArrivalTime = (uint32_t)diffArrivalTime >> 1;
69    diffSendTime = (uint32_t)diffSendTime >> 1;
70  }
71
72  // arrival timestamp in 16 kHz
73  arrivalTimestampIn16kHz = (uint32_t)((uint32_t)
74                                             bwest_str->prev_rec_arr_ts + (uint32_t)diffArrivalTime);
75  // send timestamp in 16 kHz
76  sendTimestampIn16kHz = (uint32_t)((uint32_t)
77                                          bwest_str->prev_rec_send_ts + (uint32_t)diffSendTime);
78
79  err = WebRtcIsac_UpdateBandwidthEstimator(bwest_str, rtp_seq_number,
80                                            (frame_samples * 1000) / FS, sendTimestampIn16kHz,
81                                            arrivalTimestampIn16kHz, packet_size);
82  // error check
83  if(err < 0)
84  {
85    return err;
86  }
87
88  return 0;
89}
90