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    WebRtc_Word32               packet_size,
22    WebRtc_UWord16              rtp_seq_number,
23    WebRtc_UWord32              send_ts,
24    WebRtc_UWord32              arr_ts,
25    enum IsacSamplingRate encoderSampRate,
26    enum IsacSamplingRate decoderSampRate)
27{
28  WebRtc_Word16  index;
29  WebRtc_Word16  frame_samples;
30  WebRtc_UWord32 sendTimestampIn16kHz;
31  WebRtc_UWord32 arrivalTimestampIn16kHz;
32  WebRtc_UWord32 diffSendTime;
33  WebRtc_UWord32 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 = (WebRtc_UWord32)((WebRtc_UWord32)send_ts -
59                                  (WebRtc_UWord32)bwest_str->senderTimestamp);
60  bwest_str->senderTimestamp = send_ts;
61
62  diffArrivalTime = (WebRtc_UWord32)((WebRtc_UWord32)arr_ts -
63                                     (WebRtc_UWord32)bwest_str->receiverTimestamp);
64  bwest_str->receiverTimestamp = arr_ts;
65
66  if(decoderSampRate == kIsacSuperWideband)
67  {
68    diffArrivalTime = (WebRtc_UWord32)diffArrivalTime >> 1;
69    diffSendTime = (WebRtc_UWord32)diffSendTime >> 1;
70  }
71  // arrival timestamp in 16 kHz
72  arrivalTimestampIn16kHz = (WebRtc_UWord32)((WebRtc_UWord32)
73                                             bwest_str->prev_rec_arr_ts + (WebRtc_UWord32)diffArrivalTime);
74  // send timestamp in 16 kHz
75  sendTimestampIn16kHz = (WebRtc_UWord32)((WebRtc_UWord32)
76                                          bwest_str->prev_rec_send_ts + (WebRtc_UWord32)diffSendTime);
77
78  err = WebRtcIsac_UpdateBandwidthEstimator(bwest_str, rtp_seq_number,
79                                            (frame_samples * 1000) / FS, sendTimestampIn16kHz,
80                                            arrivalTimestampIn16kHz, packet_size);
81  // error check
82  if(err < 0)
83  {
84    return err;
85  }
86
87  return 0;
88}
89