blob: 8e7d83978c0d7a13a39b62b9219cd4b4e30a30e5 [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>
turaj@webrtc.org7959e162013-09-12 18:30:26 +000018#include <vector>
19
Danil Chapovalovb6021232018-06-19 13:26:36 +020020#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "api/array_view.h"
Yves Gerey988cc082018-10-23 12:03:01 +020022#include "api/audio_codecs/audio_decoder.h"
23#include "api/audio_codecs/audio_format.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_coding/acm2/acm_resampler.h"
25#include "modules/audio_coding/acm2/call_statistics.h"
26#include "modules/audio_coding/include/audio_coding_module.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/criticalsection.h"
28#include "rtc_base/thread_annotations.h"
turaj@webrtc.org7959e162013-09-12 18:30:26 +000029
30namespace webrtc {
31
Yves Gerey988cc082018-10-23 12:03:01 +020032class Clock;
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000033struct CodecInst;
turaj@webrtc.org7959e162013-09-12 18:30:26 +000034class NetEq;
Yves Gerey988cc082018-10-23 12:03:01 +020035struct RTPHeader;
36struct WebRtcRTPHeader;
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +000037
38namespace acm2 {
39
turaj@webrtc.org7959e162013-09-12 18:30:26 +000040class AcmReceiver {
41 public:
turaj@webrtc.org7959e162013-09-12 18:30:26 +000042 // Constructor of the class
henrik.lundin@webrtc.org0bc9b5a2014-04-29 08:09:31 +000043 explicit AcmReceiver(const AudioCodingModule::Config& config);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000044
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,
kwibergee2bac22015-11-11 10:34:00 -080062 rtc::ArrayView<const uint8_t> incoming_payload);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000063
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.lundin834a6ea2016-05-13 03:45:24 -070076 // -muted : if true, the sample data in audio_frame is not
77 // populated, and must be interpreted as all zero.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000078 //
79 // Return value : 0 if OK.
80 // -1 if NetEq returned an error.
81 //
henrik.lundin834a6ea2016-05-13 03:45:24 -070082 int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted);
turaj@webrtc.org7959e162013-09-12 18:30:26 +000083
kwiberg1c07c702017-03-27 07:15:49 -070084 // Replace the current set of decoders with the specified set.
85 void SetCodecs(const std::map<int, SdpAudioFormat>& codecs);
86
turaj@webrtc.org7959e162013-09-12 18:30:26 +000087 //
88 // Adds a new codec to the NetEq codec database.
89 //
90 // Input:
kwiberg4e14f092015-08-24 05:27:22 -070091 // - acm_codec_id : ACM codec ID; -1 means external decoder.
turaj@webrtc.org7959e162013-09-12 18:30:26 +000092 // - payload_type : payload type.
Karl Wibergd8399e62015-05-25 14:39:56 +020093 // - sample_rate_hz : sample rate.
kwiberg4e14f092015-08-24 05:27:22 -070094 // - audio_decoder : pointer to a decoder object. If it's null, then
95 // NetEq will internally create a decoder object
96 // based on the value of |acm_codec_id| (which
97 // mustn't be -1). Otherwise, NetEq will use the
98 // given decoder for the given payload type. NetEq
99 // won't take ownership of the decoder; it's up to
100 // the caller to delete it when it's no longer
101 // needed.
102 //
103 // Providing an existing decoder object here is
104 // necessary for external decoders, but may also be
105 // used for built-in decoders if NetEq doesn't have
106 // all the info it needs to construct them properly
107 // (e.g. iSAC, where the decoder needs to be paired
108 // with an encoder).
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000109 //
110 // Return value : 0 if OK.
111 // <0 if NetEq returned an error.
112 //
113 int AddCodec(int acm_codec_id,
114 uint8_t payload_type,
Peter Kasting69558702016-01-12 16:26:35 -0800115 size_t channels,
Karl Wibergd8399e62015-05-25 14:39:56 +0200116 int sample_rate_hz,
henrik.lundin4cf61dd2015-12-09 06:20:58 -0800117 AudioDecoder* audio_decoder,
118 const std::string& name);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000119
kwiberg5adaf732016-10-04 09:33:27 -0700120 // Adds a new decoder to the NetEq codec database. Returns true iff
121 // successful.
122 bool AddCodec(int rtp_payload_type, const SdpAudioFormat& audio_format);
123
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124 //
125 // Sets a minimum delay for packet buffer. The given delay is maintained,
126 // unless channel condition dictates a higher delay.
127 //
128 // Input:
129 // - delay_ms : minimum delay in milliseconds.
130 //
131 // Return value : 0 if OK.
132 // <0 if NetEq returned an error.
133 //
134 int SetMinimumDelay(int delay_ms);
135
136 //
137 // Sets a maximum delay [ms] for the packet buffer. The target delay does not
138 // exceed the given value, even if channel condition requires so.
139 //
140 // Input:
141 // - delay_ms : maximum delay in milliseconds.
142 //
143 // Return value : 0 if OK.
144 // <0 if NetEq returned an error.
145 //
146 int SetMaximumDelay(int delay_ms);
147
148 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000149 // Resets the initial delay to zero.
150 //
151 void ResetInitialDelay();
152
henrik.lundin057fb892015-11-23 08:19:52 -0800153 // Returns the sample rate of the decoder associated with the last incoming
154 // packet. If no packet of a registered non-CNG codec has been received, the
155 // return value is empty. Also, if the decoder was unregistered since the last
156 // packet was inserted, the return value is empty.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200157 absl::optional<int> last_packet_sample_rate_hz() const;
henrik.lundin057fb892015-11-23 08:19:52 -0800158
henrik.lundind89814b2015-11-23 06:49:25 -0800159 // Returns last_output_sample_rate_hz from the NetEq instance.
160 int last_output_sample_rate_hz() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000161
162 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000163 // Get the current network statistics from NetEq.
164 //
165 // Output:
166 // - statistics : The current network statistics.
167 //
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000168 void GetNetworkStatistics(NetworkStatistics* statistics);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000169
170 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000171 // Flushes the NetEq packet and speech buffers.
172 //
173 void FlushBuffers();
174
175 //
176 // Removes a payload-type from the NetEq codec database.
177 //
178 // Input:
179 // - payload_type : the payload-type to be removed.
180 //
181 // Return value : 0 if OK.
182 // -1 if an error occurred.
183 //
184 int RemoveCodec(uint8_t payload_type);
185
186 //
187 // Remove all registered codecs.
188 //
kwiberg6b19b562016-09-20 04:02:25 -0700189 void RemoveAllCodecs();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000190
henrik.lundin9a410dd2016-04-06 01:39:22 -0700191 // Returns the RTP timestamp for the last sample delivered by GetAudio().
192 // The return value will be empty if no valid timestamp is available.
Danil Chapovalovb6021232018-06-19 13:26:36 +0200193 absl::optional<uint32_t> GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000194
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700195 // Returns the current total delay from NetEq (packet buffer and sync buffer)
196 // in ms, with smoothing applied to even out short-time fluctuations due to
197 // jitter. The packet buffer part of the delay is not updated during DTX/CNG
198 // periods.
199 //
200 int FilteredCurrentDelayMs() const;
201
Henrik Lundinabbff892017-11-29 09:14:04 +0100202 // Returns the current target delay for NetEq in ms.
203 //
204 int TargetDelayMs() const;
205
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000206 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000207 // Get the audio codec associated with the last non-CNG/non-DTMF received
208 // payload. If no non-CNG/non-DTMF packet is received -1 is returned,
209 // otherwise return 0.
210 //
211 int LastAudioCodec(CodecInst* codec) const;
212
Danil Chapovalovb6021232018-06-19 13:26:36 +0200213 absl::optional<SdpAudioFormat> LastAudioFormat() const;
ossue280cde2016-10-12 11:04:10 -0700214
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000215 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000216 // Get a decoder given its registered payload-type.
217 //
218 // Input:
219 // -payload_type : the payload-type of the codec to be retrieved.
220 //
221 // Output:
222 // -codec : codec associated with the given payload-type.
223 //
224 // Return value : 0 if succeeded.
225 // -1 if failed, e.g. given payload-type is not
226 // registered.
227 //
228 int DecoderByPayloadType(uint8_t payload_type,
229 CodecInst* codec) const;
Karl Wiberg377a2312018-09-24 14:52:51 +0200230 absl::optional<SdpAudioFormat> DecoderByPayloadType(int payload_type) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000231
232 //
233 // Enable NACK and set the maximum size of the NACK list. If NACK is already
234 // enabled then the maximum NACK list size is modified accordingly.
235 //
236 // Input:
237 // -max_nack_list_size : maximum NACK list size
238 // should be positive (none zero) and less than or
239 // equal to |Nack::kNackListSizeLimit|
240 // Return value
241 // : 0 if succeeded.
242 // -1 if failed
243 //
244 int EnableNack(size_t max_nack_list_size);
245
246 // Disable NACK.
247 void DisableNack();
248
249 //
250 // Get a list of packets to be retransmitted.
251 //
252 // Input:
253 // -round_trip_time_ms : estimate of the round-trip-time (in milliseconds).
254 // Return value : list of packets to be retransmitted.
255 //
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000256 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000257
258 //
wu@webrtc.org24301a62013-12-13 19:17:43 +0000259 // Get statistics of calls to GetAudio().
260 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
261
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000262 private:
kwiberg6f0f6162016-09-20 03:07:46 -0700263 struct Decoder {
264 int acm_codec_id;
265 uint8_t payload_type;
266 // This field is meaningful for codecs where both mono and
267 // stereo versions are registered under the same ID.
268 size_t channels;
269 int sample_rate_hz;
270 };
271
Danil Chapovalovb6021232018-06-19 13:26:36 +0200272 const absl::optional<CodecInst> RtpHeaderToDecoder(
273 const RTPHeader& rtp_header,
274 uint8_t first_payload_byte) const
275 RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000276
277 uint32_t NowInTimestamp(int decoder_sampling_rate) const;
278
pbos5ad935c2016-01-25 03:52:44 -0800279 rtc::CriticalSection crit_sect_;
Danil Chapovalovb6021232018-06-19 13:26:36 +0200280 absl::optional<CodecInst> last_audio_decoder_ RTC_GUARDED_BY(crit_sect_);
281 absl::optional<SdpAudioFormat> last_audio_format_ RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700282 ACMResampler resampler_ RTC_GUARDED_BY(crit_sect_);
283 std::unique_ptr<int16_t[]> last_audio_buffer_ RTC_GUARDED_BY(crit_sect_);
284 CallStatistics call_stats_ RTC_GUARDED_BY(crit_sect_);
Henrik Lundin6af93992017-06-14 14:13:02 +0200285 const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed.
Henrik Lundin02ed2012017-06-08 09:03:55 +0200286 const Clock* const clock_;
danilchap56359be2017-09-07 07:53:45 -0700287 bool resampled_last_output_frame_ RTC_GUARDED_BY(crit_sect_);
Danil Chapovalovb6021232018-06-19 13:26:36 +0200288 absl::optional<int> last_packet_sample_rate_hz_ RTC_GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000289};
290
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000291} // namespace acm2
292
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000293} // namespace webrtc
294
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200295#endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_