blob: d3b41c862e6bf68a5731d4fb1d3429b4919e867f [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;
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 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +000088 // 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.org7959e162013-09-12 18:30:26 +0000112 // Resets the initial delay to zero.
113 //
114 void ResetInitialDelay();
115
henrik.lundin057fb892015-11-23 08:19:52 -0800116 // 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 Chapovalovb6021232018-06-19 13:26:36 +0200120 absl::optional<int> last_packet_sample_rate_hz() const;
henrik.lundin057fb892015-11-23 08:19:52 -0800121
henrik.lundind89814b2015-11-23 06:49:25 -0800122 // Returns last_output_sample_rate_hz from the NetEq instance.
123 int last_output_sample_rate_hz() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000124
125 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000126 // Get the current network statistics from NetEq.
127 //
128 // Output:
129 // - statistics : The current network statistics.
130 //
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +0000131 void GetNetworkStatistics(NetworkStatistics* statistics);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000132
133 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000134 // Flushes the NetEq packet and speech buffers.
135 //
136 void FlushBuffers();
137
138 //
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000139 // Remove all registered codecs.
140 //
kwiberg6b19b562016-09-20 04:02:25 -0700141 void RemoveAllCodecs();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000142
henrik.lundin9a410dd2016-04-06 01:39:22 -0700143 // 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 Chapovalovb6021232018-06-19 13:26:36 +0200145 absl::optional<uint32_t> GetPlayoutTimestamp();
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000146
henrik.lundinb3f1c5d2016-08-22 15:39:53 -0700147 // 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 Lundinabbff892017-11-29 09:14:04 +0100154 // Returns the current target delay for NetEq in ms.
155 //
156 int TargetDelayMs() const;
157
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000158 //
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100159 // 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.org7959e162013-09-12 18:30:26 +0000161 //
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100162 absl::optional<std::pair<int, SdpAudioFormat>> LastDecoder() const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000163
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.org16825b12015-01-12 21:51:21 +0000188 std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000189
190 //
wu@webrtc.org24301a62013-12-13 19:17:43 +0000191 // Get statistics of calls to GetAudio().
192 void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
193
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000194 private:
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000195 uint32_t NowInTimestamp(int decoder_sampling_rate) const;
196
pbos5ad935c2016-01-25 03:52:44 -0800197 rtc::CriticalSection crit_sect_;
Fredrik Solenbergf693bfa2018-12-11 12:22:10 +0100198 absl::optional<std::pair<int, SdpAudioFormat>> last_decoder_
199 RTC_GUARDED_BY(crit_sect_);
danilchap56359be2017-09-07 07:53:45 -0700200 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 Lundin6af93992017-06-14 14:13:02 +0200203 const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed.
Henrik Lundin02ed2012017-06-08 09:03:55 +0200204 const Clock* const clock_;
danilchap56359be2017-09-07 07:53:45 -0700205 bool resampled_last_output_frame_ RTC_GUARDED_BY(crit_sect_);
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000206};
207
turaj@webrtc.org6d5d2482013-10-06 04:47:28 +0000208} // namespace acm2
209
turaj@webrtc.org7959e162013-09-12 18:30:26 +0000210} // namespace webrtc
211
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200212#endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_