turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [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 06:47:31 +0200 | [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 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
jmarusic@webrtc.org | a4bef3e | 2015-03-23 11:19:35 +0000 | [diff] [blame] | 15 | #include <map> |
kwiberg | 16c5a96 | 2016-02-15 02:27:22 -0800 | [diff] [blame] | 16 | #include <memory> |
henrik.lundin | 4cf61dd | 2015-12-09 06:20:58 -0800 | [diff] [blame] | 17 | #include <string> |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 18 | #include <utility> |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "api/array_view.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 23 | #include "api/audio_codecs/audio_decoder.h" |
| 24 | #include "api/audio_codecs/audio_format.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "modules/audio_coding/acm2/acm_resampler.h" |
| 26 | #include "modules/audio_coding/acm2/call_statistics.h" |
| 27 | #include "modules/audio_coding/include/audio_coding_module.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 28 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/thread_annotations.h" |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 30 | |
| 31 | namespace webrtc { |
| 32 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 33 | class Clock; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 34 | class NetEq; |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 35 | struct RTPHeader; |
| 36 | struct WebRtcRTPHeader; |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 37 | |
| 38 | namespace acm2 { |
| 39 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 40 | class AcmReceiver { |
| 41 | public: |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 42 | // Constructor of the class |
henrik.lundin@webrtc.org | 0bc9b5a | 2014-04-29 08:09:31 +0000 | [diff] [blame] | 43 | explicit AcmReceiver(const AudioCodingModule::Config& config); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [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 | // |
| 61 | int InsertPacket(const WebRtcRTPHeader& rtp_header, |
kwiberg | ee2bac2 | 2015-11-11 10:34:00 -0800 | [diff] [blame] | 62 | rtc::ArrayView<const uint8_t> incoming_payload); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [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 03:45:24 -0700 | [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 +0000 | [diff] [blame] | 78 | // |
| 79 | // Return value : 0 if OK. |
| 80 | // -1 if NetEq returned an error. |
| 81 | // |
henrik.lundin | 834a6ea | 2016-05-13 03:45:24 -0700 | [diff] [blame] | 82 | int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 83 | |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [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 +0000 | [diff] [blame] | 87 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [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 | |
| 111 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 112 | // Resets the initial delay to zero. |
| 113 | // |
| 114 | void ResetInitialDelay(); |
| 115 | |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 116 | // Returns the sample rate of the decoder associated with the last incoming |
| 117 | // packet. If no packet of a registered non-CNG codec has been received, the |
| 118 | // return value is empty. Also, if the decoder was unregistered since the last |
| 119 | // packet was inserted, the return value is empty. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 120 | absl::optional<int> last_packet_sample_rate_hz() const; |
henrik.lundin | 057fb89 | 2015-11-23 08:19:52 -0800 | [diff] [blame] | 121 | |
henrik.lundin | d89814b | 2015-11-23 06:49:25 -0800 | [diff] [blame] | 122 | // Returns last_output_sample_rate_hz from the NetEq instance. |
| 123 | int last_output_sample_rate_hz() const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 124 | |
| 125 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 126 | // Get the current network statistics from NetEq. |
| 127 | // |
| 128 | // Output: |
| 129 | // - statistics : The current network statistics. |
| 130 | // |
minyue@webrtc.org | c0bd7be | 2015-02-18 15:24:13 +0000 | [diff] [blame] | 131 | void GetNetworkStatistics(NetworkStatistics* statistics); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 132 | |
| 133 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 134 | // Flushes the NetEq packet and speech buffers. |
| 135 | // |
| 136 | void FlushBuffers(); |
| 137 | |
| 138 | // |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 139 | // Remove all registered codecs. |
| 140 | // |
kwiberg | 6b19b56 | 2016-09-20 04:02:25 -0700 | [diff] [blame] | 141 | void RemoveAllCodecs(); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 142 | |
henrik.lundin | 9a410dd | 2016-04-06 01:39:22 -0700 | [diff] [blame] | 143 | // Returns the RTP timestamp for the last sample delivered by GetAudio(). |
| 144 | // The return value will be empty if no valid timestamp is available. |
Danil Chapovalov | b602123 | 2018-06-19 13:26:36 +0200 | [diff] [blame] | 145 | absl::optional<uint32_t> GetPlayoutTimestamp(); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 146 | |
henrik.lundin | b3f1c5d | 2016-08-22 15:39:53 -0700 | [diff] [blame] | 147 | // Returns the current total delay from NetEq (packet buffer and sync buffer) |
| 148 | // in ms, with smoothing applied to even out short-time fluctuations due to |
| 149 | // jitter. The packet buffer part of the delay is not updated during DTX/CNG |
| 150 | // periods. |
| 151 | // |
| 152 | int FilteredCurrentDelayMs() const; |
| 153 | |
Henrik Lundin | abbff89 | 2017-11-29 09:14:04 +0100 | [diff] [blame] | 154 | // Returns the current target delay for NetEq in ms. |
| 155 | // |
| 156 | int TargetDelayMs() const; |
| 157 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 158 | // |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 159 | // Get payload type and format of the last non-CNG/non-DTMF received payload. |
| 160 | // If no non-CNG/non-DTMF packet is received absl::nullopt is returned. |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 161 | // |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 162 | absl::optional<std::pair<int, SdpAudioFormat>> LastDecoder() const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 163 | |
| 164 | // |
| 165 | // Enable NACK and set the maximum size of the NACK list. If NACK is already |
| 166 | // enabled then the maximum NACK list size is modified accordingly. |
| 167 | // |
| 168 | // Input: |
| 169 | // -max_nack_list_size : maximum NACK list size |
| 170 | // should be positive (none zero) and less than or |
| 171 | // equal to |Nack::kNackListSizeLimit| |
| 172 | // Return value |
| 173 | // : 0 if succeeded. |
| 174 | // -1 if failed |
| 175 | // |
| 176 | int EnableNack(size_t max_nack_list_size); |
| 177 | |
| 178 | // Disable NACK. |
| 179 | void DisableNack(); |
| 180 | |
| 181 | // |
| 182 | // Get a list of packets to be retransmitted. |
| 183 | // |
| 184 | // Input: |
| 185 | // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds). |
| 186 | // Return value : list of packets to be retransmitted. |
| 187 | // |
pkasting@chromium.org | 16825b1 | 2015-01-12 21:51:21 +0000 | [diff] [blame] | 188 | std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const; |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 189 | |
| 190 | // |
wu@webrtc.org | 24301a6 | 2013-12-13 19:17:43 +0000 | [diff] [blame] | 191 | // Get statistics of calls to GetAudio(). |
| 192 | void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const; |
| 193 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 194 | private: |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 195 | uint32_t NowInTimestamp(int decoder_sampling_rate) const; |
| 196 | |
pbos | 5ad935c | 2016-01-25 03:52:44 -0800 | [diff] [blame] | 197 | rtc::CriticalSection crit_sect_; |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 198 | absl::optional<std::pair<int, SdpAudioFormat>> last_decoder_ |
| 199 | RTC_GUARDED_BY(crit_sect_); |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 200 | ACMResampler resampler_ RTC_GUARDED_BY(crit_sect_); |
| 201 | std::unique_ptr<int16_t[]> last_audio_buffer_ RTC_GUARDED_BY(crit_sect_); |
| 202 | CallStatistics call_stats_ RTC_GUARDED_BY(crit_sect_); |
Henrik Lundin | 6af9399 | 2017-06-14 14:13:02 +0200 | [diff] [blame] | 203 | const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed. |
Henrik Lundin | 02ed201 | 2017-06-08 09:03:55 +0200 | [diff] [blame] | 204 | const Clock* const clock_; |
danilchap | 56359be | 2017-09-07 07:53:45 -0700 | [diff] [blame] | 205 | bool resampled_last_output_frame_ RTC_GUARDED_BY(crit_sect_); |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
turaj@webrtc.org | 6d5d248 | 2013-10-06 04:47:28 +0000 | [diff] [blame] | 208 | } // namespace acm2 |
| 209 | |
turaj@webrtc.org | 7959e16 | 2013-09-12 18:30:26 +0000 | [diff] [blame] | 210 | } // namespace webrtc |
| 211 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 212 | #endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_ |