blob: 1f449a3de9a2dbcff491055ad26a65af44955fd7 [file] [log] [blame]
turaj@webrtc.org7959e162013-09-12 18:30:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
12#define MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
turaj@webrtc.org7959e162013-09-12 18:30:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
jmarusic@webrtc.orga4bef3e2015-03-23 11:19:35 +000015#include <map>
kwiberg16c5a962016-02-15 02:27:22 -080016#include <memory>
henrik.lundin4cf61dd2015-12-09 06:20:58 -080017#include <string>
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +010018#include <utility>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000019#include <vector>
20
Danil Chapovalovb6021232018-06-19 13:26:36 +020021#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/array_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020023#include "api/audio_codecs/audio_decoder.h"
24#include "api/audio_codecs/audio_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#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 Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/thread_annotations.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000030
31namespace webrtc {
32
Yves Gerey988cc082018-10-23 12:03:01 +020033class Clock;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000034class NetEq;
Yves Gerey988cc082018-10-23 12:03:01 +020035struct RTPHeader;
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000036
37namespace acm2 {
38
turaj@webrtc.org7959e162013-09-12 18:30:26 +000039class AcmReceiver {
40 public:
turaj@webrtc.org7959e162013-09-12 18:30:26 +000041 // Constructor of the class
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000042 explicit AcmReceiver(const AudioCodingModule::Config& config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000043
44 // Destructor of the class.
45 ~AcmReceiver();
46
47 //
48 // Inserts a payload with its associated RTP-header into NetEq.
49 //
50 // Input:
51 // - rtp_header : RTP header for the incoming payload containing
52 // information about payload type, sequence number,
53 // timestamp, SSRC and marker bit.
54 // - incoming_payload : Incoming audio payload.
55 // - length_payload : Length of incoming audio payload in bytes.
56 //
57 // Return value : 0 if OK.
58 // <0 if NetEq returned an error.
59 //
Niels Möllerafb5dbb2019-02-15 15:21:47 +010060 int InsertPacket(const RTPHeader& rtp_header,
kwibergee2bac22015-11-11 10:34:00 -080061 rtc::ArrayView<const uint8_t> incoming_payload);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000062
63 //
64 // Asks NetEq for 10 milliseconds of decoded audio.
65 //
66 // Input:
67 // -desired_freq_hz : specifies the sampling rate [Hz] of the output
68 // audio. If set -1 indicates to resampling is
69 // is required and the audio returned at the
70 // sampling rate of the decoder.
71 //
72 // Output:
73 // -audio_frame : an audio frame were output data and
74 // associated parameters are written to.
henrik.lundin834a6ea2016-05-13 03:45:24 -070075 // -muted : if true, the sample data in audio_frame is not
76 // populated, and must be interpreted as all zero.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000077 //
78 // Return value : 0 if OK.
79 // -1 if NetEq returned an error.
80 //
henrik.lundin834a6ea2016-05-13 03:45:24 -070081 int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000082
kwiberg1c07c702017-03-27 07:15:49 -070083 // Replace the current set of decoders with the specified set.
84 void SetCodecs(const std::map<int, SdpAudioFormat>& codecs);
85
turaj@webrtc.org7959e162013-09-12 18:30:26 +000086 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +000087 // Sets a minimum delay for packet buffer. The given delay is maintained,
88 // unless channel condition dictates a higher delay.
89 //
90 // Input:
91 // - delay_ms : minimum delay in milliseconds.
92 //
93 // Return value : 0 if OK.
94 // <0 if NetEq returned an error.
95 //
96 int SetMinimumDelay(int delay_ms);
97
98 //
99 // Sets a maximum delay [ms] for the packet buffer. The target delay does not
100 // exceed the given value, even if channel condition requires so.
101 //
102 // Input:
103 // - delay_ms : maximum delay in milliseconds.
104 //
105 // Return value : 0 if OK.
106 // <0 if NetEq returned an error.
107 //
108 int SetMaximumDelay(int delay_ms);
109
Ruslan Burakov9bee67c2019-02-05 13:49:26 +0100110 // Sets a base minimum delay in milliseconds for the packet buffer.
111 // Base minimum delay sets lower bound minimum delay value which
112 // is set via SetMinimumDelay.
113 //
114 // Returns true if value was successfully set, false overwise.
115 bool SetBaseMinimumDelayMs(int delay_ms);
116
117 // Returns current value of base minimum delay in milliseconds.
118 int GetBaseMinimumDelayMs() const;
119
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000120 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000121 // Resets the initial delay to zero.
122 //
123 void ResetInitialDelay();
124
henrik.lundin057fb892015-11-23 08:19:52 -0800125 // Returns the sample rate of the decoder associated with the last incoming
126 // packet. If no packet of a registered non-CNG codec has been received, the
127 // return value is empty. Also, if the decoder was unregistered since the last
128 // packet was inserted, the return value is empty.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200129 absl::optional<int> last_packet_sample_rate_hz() const;
henrik.lundin057fb892015-11-23 08:19:52 -0800130
henrik.lundind89814b2015-11-23 06:49:25 -0800131 // Returns last_output_sample_rate_hz from the NetEq instance.
132 int last_output_sample_rate_hz() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000133
134 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000135 // Get the current network statistics from NetEq.
136 //
137 // Output:
138 // - statistics : The current network statistics.
139 //
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000140 void GetNetworkStatistics(NetworkStatistics* statistics);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000141
142 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000143 // Flushes the NetEq packet and speech buffers.
144 //
145 void FlushBuffers();
146
147 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000148 // Remove all registered codecs.
149 //
kwiberg6b19b562016-09-20 04:02:25 -0700150 void RemoveAllCodecs();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000151
henrik.lundin9a410dd2016-04-06 01:39:22 -0700152 // Returns the RTP timestamp for the last sample delivered by GetAudio().
153 // The return value will be empty if no valid timestamp is available.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200154 absl::optional<uint32_t> GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000155
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700156 // Returns the current total delay from NetEq (packet buffer and sync buffer)
157 // in ms, with smoothing applied to even out short-time fluctuations due to
158 // jitter. The packet buffer part of the delay is not updated during DTX/CNG
159 // periods.
160 //
161 int FilteredCurrentDelayMs() const;
162
Henrik Lundinabbff892017-11-29 09:14:04 +0100163 // Returns the current target delay for NetEq in ms.
164 //
165 int TargetDelayMs() const;
166
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000167 //
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100168 // Get payload type and format of the last non-CNG/non-DTMF received payload.
169 // If no non-CNG/non-DTMF packet is received absl::nullopt is returned.
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000170 //
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100171 absl::optional<std::pair<int, SdpAudioFormat>> LastDecoder() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000172
173 //
174 // Enable NACK and set the maximum size of the NACK list. If NACK is already
175 // enabled then the maximum NACK list size is modified accordingly.
176 //
177 // Input:
178 // -max_nack_list_size : maximum NACK list size
179 // should be positive (none zero) and less than or
180 // equal to |Nack::kNackListSizeLimit|
181 // Return value
182 // : 0 if succeeded.
183 // -1 if failed
184 //
185 int EnableNack(size_t max_nack_list_size);
186
187 // Disable NACK.
188 void DisableNack();
189
190 //
191 // Get a list of packets to be retransmitted.
192 //
193 // Input:
194 // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds).
195 // Return value : list of packets to be retransmitted.
196 //
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000197 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000198
199 //
wu@webrtc.org24301a62013-12-13 19:17:43 +0000200 // Get statistics of calls to GetAudio().
201 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
202
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000203 private:
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000204 uint32_t NowInTimestamp(int decoder_sampling_rate) const;
205
pbos5ad935c2016-01-25 03:52:44 -0800206 rtc::CriticalSection crit_sect_;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100207 absl::optional<std::pair<int, SdpAudioFormat>> last_decoder_
208 RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700209 ACMResampler resampler_ RTC_GUARDED_BY(crit_sect_);
210 std::unique_ptr<int16_t[]> last_audio_buffer_ RTC_GUARDED_BY(crit_sect_);
211 CallStatistics call_stats_ RTC_GUARDED_BY(crit_sect_);
Henrik Lundin6af93992017-06-14 14:13:02 +0200212 const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed.
Sebastian Janssonaa01f272019-01-30 11:28:59 +0100213 Clock* const clock_;
danilchap56359be2017-09-07 07:53:45 -0700214 bool resampled_last_output_frame_ RTC_GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000215};
216
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000217} // namespace acm2
218
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000219} // namespace webrtc
220
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200221#endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_