turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |
| 12 | #define MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
jmarusic@webrtc.org | a4bef3e | 2015-03-23 11:19:35 | [diff] [blame] | 16 | #include <map> |
kwiberg | 16c5a96 | 2016-02-15 10:27:22 | [diff] [blame] | 17 | #include <memory> |
henrik.lundin | 4cf61dd | 2015-12-09 14:20:58 | [diff] [blame] | 18 | #include <string> |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 19 | #include <utility> |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 22 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 23 | #include "api/array_view.h" |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 24 | #include "api/audio_codecs/audio_decoder.h" |
| 25 | #include "api/audio_codecs/audio_format.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 26 | #include "modules/audio_coding/acm2/acm_resampler.h" |
| 27 | #include "modules/audio_coding/acm2/call_statistics.h" |
| 28 | #include "modules/audio_coding/include/audio_coding_module.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/thread_annotations.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 34 | class Clock; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 35 | class NetEq; |
Yves Gerey | 988cc08 | 2018-10-23 10:03:01 | [diff] [blame] | 36 | struct RTPHeader; |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 | [diff] [blame] | 37 | |
| 38 | namespace acm2 { |
| 39 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 40 | class AcmReceiver { |
| 41 | public: |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 42 | // Constructor of the class |
henrik.lundin@webrtc.org | 0bc9b5a | 2014-04-29 08:09:31 | [diff] [blame] | 43 | explicit AcmReceiver(const AudioCodingModule::Config& config); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 44 | |
| 45 | // Destructor of the class. |
| 46 | ~AcmReceiver(); |
| 47 | |
| 48 | // |
| 49 | // Inserts a payload with its associated RTP-header into NetEq. |
| 50 | // |
| 51 | // Input: |
| 52 | // - rtp_header : RTP header for the incoming payload containing |
| 53 | // information about payload type, sequence number, |
| 54 | // timestamp, SSRC and marker bit. |
| 55 | // - incoming_payload : Incoming audio payload. |
| 56 | // - length_payload : Length of incoming audio payload in bytes. |
| 57 | // |
| 58 | // Return value : 0 if OK. |
| 59 | // <0 if NetEq returned an error. |
| 60 | // |
Niels Möller | afb5dbb | 2019-02-15 14:21:47 | [diff] [blame] | 61 | int InsertPacket(const RTPHeader& rtp_header, |
kwiberg | ee2bac2 | 2015-11-11 18:34:00 | [diff] [blame] | 62 | rtc::ArrayView<const uint8_t> incoming_payload); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 63 | |
| 64 | // |
| 65 | // Asks NetEq for 10 milliseconds of decoded audio. |
| 66 | // |
| 67 | // Input: |
| 68 | // -desired_freq_hz : specifies the sampling rate [Hz] of the output |
| 69 | // audio. If set -1 indicates to resampling is |
| 70 | // is required and the audio returned at the |
| 71 | // sampling rate of the decoder. |
| 72 | // |
| 73 | // Output: |
| 74 | // -audio_frame : an audio frame were output data and |
| 75 | // associated parameters are written to. |
henrik.lundin | 834a6ea | 2016-05-13 10:45:24 | [diff] [blame] | 76 | // -muted : if true, the sample data in audio_frame is not |
| 77 | // populated, and must be interpreted as all zero. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 78 | // |
| 79 | // Return value : 0 if OK. |
| 80 | // -1 if NetEq returned an error. |
| 81 | // |
henrik.lundin | 834a6ea | 2016-05-13 10:45:24 | [diff] [blame] | 82 | int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 83 | |
kwiberg | 1c07c70 | 2017-03-27 14:15:49 | [diff] [blame] | 84 | // Replace the current set of decoders with the specified set. |
| 85 | void SetCodecs(const std::map<int, SdpAudioFormat>& codecs); |
| 86 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 87 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 88 | // Sets a minimum delay for packet buffer. The given delay is maintained, |
| 89 | // unless channel condition dictates a higher delay. |
| 90 | // |
| 91 | // Input: |
| 92 | // - delay_ms : minimum delay in milliseconds. |
| 93 | // |
| 94 | // Return value : 0 if OK. |
| 95 | // <0 if NetEq returned an error. |
| 96 | // |
| 97 | int SetMinimumDelay(int delay_ms); |
| 98 | |
| 99 | // |
| 100 | // Sets a maximum delay [ms] for the packet buffer. The target delay does not |
| 101 | // exceed the given value, even if channel condition requires so. |
| 102 | // |
| 103 | // Input: |
| 104 | // - delay_ms : maximum delay in milliseconds. |
| 105 | // |
| 106 | // Return value : 0 if OK. |
| 107 | // <0 if NetEq returned an error. |
| 108 | // |
| 109 | int SetMaximumDelay(int delay_ms); |
| 110 | |
Ruslan Burakov | 9bee67c | 2019-02-05 12:49:26 | [diff] [blame] | 111 | // Sets a base minimum delay in milliseconds for the packet buffer. |
| 112 | // Base minimum delay sets lower bound minimum delay value which |
| 113 | // is set via SetMinimumDelay. |
| 114 | // |
| 115 | // Returns true if value was successfully set, false overwise. |
| 116 | bool SetBaseMinimumDelayMs(int delay_ms); |
| 117 | |
| 118 | // Returns current value of base minimum delay in milliseconds. |
| 119 | int GetBaseMinimumDelayMs() const; |
| 120 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 121 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 122 | // Resets the initial delay to zero. |
| 123 | // |
| 124 | void ResetInitialDelay(); |
| 125 | |
henrik.lundin | 057fb89 | 2015-11-23 16:19:52 | [diff] [blame] | 126 | // Returns the sample rate of the decoder associated with the last incoming |
| 127 | // packet. If no packet of a registered non-CNG codec has been received, the |
| 128 | // return value is empty. Also, if the decoder was unregistered since the last |
| 129 | // packet was inserted, the return value is empty. |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 130 | absl::optional<int> last_packet_sample_rate_hz() const; |
henrik.lundin | 057fb89 | 2015-11-23 16:19:52 | [diff] [blame] | 131 | |
henrik.lundin | d89814b | 2015-11-23 14:49:25 | [diff] [blame] | 132 | // Returns last_output_sample_rate_hz from the NetEq instance. |
| 133 | int last_output_sample_rate_hz() const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 134 | |
| 135 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 136 | // Get the current network statistics from NetEq. |
| 137 | // |
| 138 | // Output: |
| 139 | // - statistics : The current network statistics. |
| 140 | // |
Niels Möller | ed44f54 | 2019-07-30 13:15:59 | [diff] [blame] | 141 | void GetNetworkStatistics(NetworkStatistics* statistics) const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 142 | |
| 143 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 144 | // Flushes the NetEq packet and speech buffers. |
| 145 | // |
| 146 | void FlushBuffers(); |
| 147 | |
| 148 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 149 | // Remove all registered codecs. |
| 150 | // |
kwiberg | 6b19b56 | 2016-09-20 11:02:25 | [diff] [blame] | 151 | void RemoveAllCodecs(); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 152 | |
henrik.lundin | 9a410dd | 2016-04-06 08:39:22 | [diff] [blame] | 153 | // Returns the RTP timestamp for the last sample delivered by GetAudio(). |
| 154 | // The return value will be empty if no valid timestamp is available. |
Danil Chapovalov | b602123 | 2018-06-19 11:26:36 | [diff] [blame] | 155 | absl::optional<uint32_t> GetPlayoutTimestamp(); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 156 | |
henrik.lundin | b3f1c5d | 2016-08-22 22:39:53 | [diff] [blame] | 157 | // Returns the current total delay from NetEq (packet buffer and sync buffer) |
| 158 | // in ms, with smoothing applied to even out short-time fluctuations due to |
| 159 | // jitter. The packet buffer part of the delay is not updated during DTX/CNG |
| 160 | // periods. |
| 161 | // |
| 162 | int FilteredCurrentDelayMs() const; |
| 163 | |
Henrik Lundin | abbff89 | 2017-11-29 08:14:04 | [diff] [blame] | 164 | // Returns the current target delay for NetEq in ms. |
| 165 | // |
| 166 | int TargetDelayMs() const; |
| 167 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 168 | // |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 169 | // Get payload type and format of the last non-CNG/non-DTMF received payload. |
| 170 | // If no non-CNG/non-DTMF packet is received absl::nullopt is returned. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 171 | // |
Fredrik Solenberg | f693bfa | 2018-12-11 11:22:10 | [diff] [blame] | 172 | absl::optional<std::pair<int, SdpAudioFormat>> LastDecoder() const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 173 | |
| 174 | // |
| 175 | // Enable NACK and set the maximum size of the NACK list. If NACK is already |
| 176 | // enabled then the maximum NACK list size is modified accordingly. |
| 177 | // |
Niels Möller | dc5ed5c | 2019-08-09 07:29:48 | [diff] [blame] | 178 | // If the sequence number of last received packet is N, the sequence numbers |
| 179 | // of NACK list are in the range of [N - |max_nack_list_size|, N). |
| 180 | // |
| 181 | // |max_nack_list_size| should be positive (none zero) and less than or |
| 182 | // equal to |Nack::kNackListSizeLimit|. Otherwise, No change is applied and -1 |
| 183 | // is returned. 0 is returned at success. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 184 | // |
| 185 | int EnableNack(size_t max_nack_list_size); |
| 186 | |
| 187 | // Disable NACK. |
| 188 | void DisableNack(); |
| 189 | |
| 190 | // |
Niels Möller | dc5ed5c | 2019-08-09 07:29:48 | [diff] [blame] | 191 | // Get a list of packets to be retransmitted. |round_trip_time_ms| is an |
| 192 | // estimate of the round-trip-time (in milliseconds). Missing packets which |
| 193 | // will be playout in a shorter time than the round-trip-time (with respect |
| 194 | // to the time this API is called) will not be included in the list. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 195 | // |
Niels Möller | dc5ed5c | 2019-08-09 07:29:48 | [diff] [blame] | 196 | // Negative |round_trip_time_ms| results is an error message and empty list |
| 197 | // is returned. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 198 | // |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 | [diff] [blame] | 199 | std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 200 | |
| 201 | // |
wu@webrtc.org | 24301a6 | 2013-12-13 19:17:43 | [diff] [blame] | 202 | // Get statistics of calls to GetAudio(). |
| 203 | void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const; |
| 204 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 205 | private: |
Karl Wiberg | 4b64411 | 2019-10-11 07:37:42 | [diff] [blame] | 206 | struct DecoderInfo { |
| 207 | int payload_type; |
| 208 | int sample_rate_hz; |
| 209 | int num_channels; |
| 210 | SdpAudioFormat sdp_format; |
| 211 | }; |
| 212 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 213 | uint32_t NowInTimestamp(int decoder_sampling_rate) const; |
| 214 | |
pbos | 5ad935c | 2016-01-25 11:52:44 | [diff] [blame] | 215 | rtc::CriticalSection crit_sect_; |
Karl Wiberg | 4b64411 | 2019-10-11 07:37:42 | [diff] [blame] | 216 | absl::optional<DecoderInfo> last_decoder_ RTC_GUARDED_BY(crit_sect_); |
danilchap | 56359be | 2017-09-07 14:53:45 | [diff] [blame] | 217 | ACMResampler resampler_ RTC_GUARDED_BY(crit_sect_); |
| 218 | std::unique_ptr<int16_t[]> last_audio_buffer_ RTC_GUARDED_BY(crit_sect_); |
| 219 | CallStatistics call_stats_ RTC_GUARDED_BY(crit_sect_); |
Henrik Lundin | 6af9399 | 2017-06-14 12:13:02 | [diff] [blame] | 220 | const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed. |
Sebastian Jansson | aa01f27 | 2019-01-30 10:28:59 | [diff] [blame] | 221 | Clock* const clock_; |
danilchap | 56359be | 2017-09-07 14:53:45 | [diff] [blame] | 222 | bool resampled_last_output_frame_ RTC_GUARDED_BY(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 223 | }; |
| 224 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 | [diff] [blame] | 225 | } // namespace acm2 |
| 226 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 | [diff] [blame] | 227 | } // namespace webrtc |
| 228 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 229 | #endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |