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 /*
12  * bandwidth_estimator.h
13  *
14  * This header file contains the API for the Bandwidth Estimator
15  * designed for iSAC.
16  *
17  */
18 
19 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_BANDWIDTH_ESTIMATOR_H_
20 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_BANDWIDTH_ESTIMATOR_H_
21 
22 #include "structs.h"
23 #include "settings.h"
24 
25 
26 #define MIN_ISAC_BW     10000
27 #define MIN_ISAC_BW_LB  10000
28 #define MIN_ISAC_BW_UB  25000
29 
30 #define MAX_ISAC_BW     56000
31 #define MAX_ISAC_BW_UB  32000
32 #define MAX_ISAC_BW_LB  32000
33 
34 #define MIN_ISAC_MD     5
35 #define MAX_ISAC_MD     25
36 
37 // assumed header size, in bytes; we don't know the exact number
38 // (header compression may be used)
39 #define HEADER_SIZE        35
40 
41 // Initial Frame-Size, in ms, for Wideband & Super-Wideband Mode
42 #define INIT_FRAME_LEN_WB  60
43 #define INIT_FRAME_LEN_SWB 30
44 
45 // Initial Bottleneck Estimate, in bits/sec, for
46 // Wideband & Super-wideband mode
47 #define INIT_BN_EST_WB     20e3f
48 #define INIT_BN_EST_SWB    56e3f
49 
50 // Initial Header rate (header rate depends on frame-size),
51 // in bits/sec, for Wideband & Super-Wideband mode.
52 #define INIT_HDR_RATE_WB                                                \
53   ((float)HEADER_SIZE * 8.0f * 1000.0f / (float)INIT_FRAME_LEN_WB)
54 #define INIT_HDR_RATE_SWB                                               \
55   ((float)HEADER_SIZE * 8.0f * 1000.0f / (float)INIT_FRAME_LEN_SWB)
56 
57 // number of packets in a row for a high rate burst
58 #define BURST_LEN       3
59 
60 // ms, max time between two full bursts
61 #define BURST_INTERVAL  500
62 
63 // number of packets in a row for initial high rate burst
64 #define INIT_BURST_LEN  5
65 
66 // bits/s, rate for the first BURST_LEN packets
67 #define INIT_RATE_WB       INIT_BN_EST_WB
68 #define INIT_RATE_SWB      INIT_BN_EST_SWB
69 
70 
71 #if defined(__cplusplus)
72 extern "C" {
73 #endif
74 
75   /* This function initializes the struct                    */
76   /* to be called before using the struct for anything else  */
77   /* returns 0 if everything went fine, -1 otherwise         */
78   int32_t WebRtcIsac_InitBandwidthEstimator(
79       BwEstimatorstr*           bwest_str,
80       enum IsacSamplingRate encoderSampRate,
81       enum IsacSamplingRate decoderSampRate);
82 
83   /* This function updates the receiving estimate                                                      */
84   /* Parameters:                                                                                       */
85   /* rtp_number    - value from RTP packet, from NetEq                                                 */
86   /* frame length  - length of signal frame in ms, from iSAC decoder                                   */
87   /* send_ts       - value in RTP header giving send time in samples                                   */
88   /* arr_ts        - value given by timeGetTime() time of arrival in samples of packet from NetEq      */
89   /* pksize        - size of packet in bytes, from NetEq                                               */
90   /* Index         - integer (range 0...23) indicating bottle neck & jitter as estimated by other side */
91   /* returns 0 if everything went fine, -1 otherwise                                                   */
92   int16_t WebRtcIsac_UpdateBandwidthEstimator(
93       BwEstimatorstr* bwest_str,
94       const uint16_t rtp_number,
95       const int32_t frame_length,
96       const uint32_t send_ts,
97       const uint32_t arr_ts,
98       const size_t pksize);
99 
100   /* Update receiving estimates. Used when we only receive BWE index, no iSAC data packet. */
101   int16_t WebRtcIsac_UpdateUplinkBwImpl(
102       BwEstimatorstr*           bwest_str,
103       int16_t               Index,
104       enum IsacSamplingRate encoderSamplingFreq);
105 
106   /* Returns the bandwidth/jitter estimation code (integer 0...23) to put in the sending iSAC payload */
107   void WebRtcIsac_GetDownlinkBwJitIndexImpl(
108       BwEstimatorstr* bwest_str,
109       int16_t* bottleneckIndex,
110       int16_t* jitterInfo,
111       enum IsacSamplingRate decoderSamplingFreq);
112 
113   /* Returns the bandwidth estimation (in bps) */
114   int32_t WebRtcIsac_GetDownlinkBandwidth(
115       const BwEstimatorstr *bwest_str);
116 
117   /* Returns the max delay (in ms) */
118   int32_t WebRtcIsac_GetDownlinkMaxDelay(
119       const BwEstimatorstr *bwest_str);
120 
121   /* Returns the bandwidth that iSAC should send with in bps */
122   int32_t WebRtcIsac_GetUplinkBandwidth(const BwEstimatorstr* bwest_str);
123 
124   /* Returns the max delay value from the other side in ms */
125   int32_t WebRtcIsac_GetUplinkMaxDelay(
126       const BwEstimatorstr *bwest_str);
127 
128   /* Fills in an IsacExternalBandwidthInfo struct. */
129   void WebRtcIsacBw_GetBandwidthInfo(
130       BwEstimatorstr* bwest_str,
131       enum IsacSamplingRate decoder_sample_rate_hz,
132       IsacBandwidthInfo* bwinfo);
133 
134   /* Uses the values from an IsacExternalBandwidthInfo struct. */
135   void WebRtcIsacBw_SetBandwidthInfo(BwEstimatorstr* bwest_str,
136                                      const IsacBandwidthInfo* bwinfo);
137 
138   /*
139    * update amount of data in bottle neck buffer and burst handling
140    * returns minimum payload size (bytes)
141    */
142   int WebRtcIsac_GetMinBytes(
143       RateModel*         State,
144       int                StreamSize,    /* bytes in bitstream */
145       const int          FrameLen,      /* ms per frame */
146       const double       BottleNeck,    /* bottle neck rate; excl headers (bps) */
147       const double       DelayBuildUp,  /* max delay from bottleneck buffering (ms) */
148       enum ISACBandwidth bandwidth
149       /*,int16_t        frequentLargePackets*/);
150 
151   /*
152    * update long-term average bitrate and amount of data in buffer
153    */
154   void WebRtcIsac_UpdateRateModel(
155       RateModel*   State,
156       int          StreamSize,                /* bytes in bitstream */
157       const int    FrameSamples,        /* samples per frame */
158       const double BottleNeck);       /* bottle neck rate; excl headers (bps) */
159 
160 
161   void WebRtcIsac_InitRateModel(
162       RateModel *State);
163 
164   /* Returns the new framelength value (input argument: bottle_neck) */
165   int WebRtcIsac_GetNewFrameLength(
166       double bottle_neck,
167       int    current_framelength);
168 
169   /* Returns the new SNR value (input argument: bottle_neck) */
170   double WebRtcIsac_GetSnr(
171       double bottle_neck,
172       int    new_framelength);
173 
174 
175   int16_t WebRtcIsac_UpdateUplinkJitter(
176       BwEstimatorstr*              bwest_str,
177       int32_t                  index);
178 
179 #if defined(__cplusplus)
180 }
181 #endif
182 
183 
184 #endif /* WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_SOURCE_BANDWIDTH_ESTIMATOR_H_ */
185