acm_receiver.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. #ifndef MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
  11. #define MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_
  12. #include <stdint.h>
  13. #include <map>
  14. #include <memory>
  15. #include <string>
  16. #include <utility>
  17. #include <vector>
  18. #include "absl/types/optional.h"
  19. #include "api/array_view.h"
  20. #include "api/audio_codecs/audio_decoder.h"
  21. #include "api/audio_codecs/audio_format.h"
  22. #include "modules/audio_coding/acm2/acm_resampler.h"
  23. #include "modules/audio_coding/acm2/call_statistics.h"
  24. #include "modules/audio_coding/include/audio_coding_module.h"
  25. #include "rtc_base/synchronization/mutex.h"
  26. #include "rtc_base/thread_annotations.h"
  27. namespace webrtc {
  28. class Clock;
  29. class NetEq;
  30. struct RTPHeader;
  31. namespace acm2 {
  32. class AcmReceiver {
  33. public:
  34. // Constructor of the class
  35. explicit AcmReceiver(const AudioCodingModule::Config& config);
  36. // Destructor of the class.
  37. ~AcmReceiver();
  38. //
  39. // Inserts a payload with its associated RTP-header into NetEq.
  40. //
  41. // Input:
  42. // - rtp_header : RTP header for the incoming payload containing
  43. // information about payload type, sequence number,
  44. // timestamp, SSRC and marker bit.
  45. // - incoming_payload : Incoming audio payload.
  46. // - length_payload : Length of incoming audio payload in bytes.
  47. //
  48. // Return value : 0 if OK.
  49. // <0 if NetEq returned an error.
  50. //
  51. int InsertPacket(const RTPHeader& rtp_header,
  52. rtc::ArrayView<const uint8_t> incoming_payload);
  53. //
  54. // Asks NetEq for 10 milliseconds of decoded audio.
  55. //
  56. // Input:
  57. // -desired_freq_hz : specifies the sampling rate [Hz] of the output
  58. // audio. If set -1 indicates to resampling is
  59. // is required and the audio returned at the
  60. // sampling rate of the decoder.
  61. //
  62. // Output:
  63. // -audio_frame : an audio frame were output data and
  64. // associated parameters are written to.
  65. // -muted : if true, the sample data in audio_frame is not
  66. // populated, and must be interpreted as all zero.
  67. //
  68. // Return value : 0 if OK.
  69. // -1 if NetEq returned an error.
  70. //
  71. int GetAudio(int desired_freq_hz, AudioFrame* audio_frame, bool* muted);
  72. // Replace the current set of decoders with the specified set.
  73. void SetCodecs(const std::map<int, SdpAudioFormat>& codecs);
  74. //
  75. // Sets a minimum delay for packet buffer. The given delay is maintained,
  76. // unless channel condition dictates a higher delay.
  77. //
  78. // Input:
  79. // - delay_ms : minimum delay in milliseconds.
  80. //
  81. // Return value : 0 if OK.
  82. // <0 if NetEq returned an error.
  83. //
  84. int SetMinimumDelay(int delay_ms);
  85. //
  86. // Sets a maximum delay [ms] for the packet buffer. The target delay does not
  87. // exceed the given value, even if channel condition requires so.
  88. //
  89. // Input:
  90. // - delay_ms : maximum delay in milliseconds.
  91. //
  92. // Return value : 0 if OK.
  93. // <0 if NetEq returned an error.
  94. //
  95. int SetMaximumDelay(int delay_ms);
  96. // Sets a base minimum delay in milliseconds for the packet buffer.
  97. // Base minimum delay sets lower bound minimum delay value which
  98. // is set via SetMinimumDelay.
  99. //
  100. // Returns true if value was successfully set, false overwise.
  101. bool SetBaseMinimumDelayMs(int delay_ms);
  102. // Returns current value of base minimum delay in milliseconds.
  103. int GetBaseMinimumDelayMs() const;
  104. //
  105. // Resets the initial delay to zero.
  106. //
  107. void ResetInitialDelay();
  108. // Returns the sample rate of the decoder associated with the last incoming
  109. // packet. If no packet of a registered non-CNG codec has been received, the
  110. // return value is empty. Also, if the decoder was unregistered since the last
  111. // packet was inserted, the return value is empty.
  112. absl::optional<int> last_packet_sample_rate_hz() const;
  113. // Returns last_output_sample_rate_hz from the NetEq instance.
  114. int last_output_sample_rate_hz() const;
  115. //
  116. // Get the current network statistics from NetEq.
  117. //
  118. // Output:
  119. // - statistics : The current network statistics.
  120. //
  121. void GetNetworkStatistics(NetworkStatistics* statistics,
  122. bool get_and_clear_legacy_stats = true) const;
  123. //
  124. // Flushes the NetEq packet and speech buffers.
  125. //
  126. void FlushBuffers();
  127. //
  128. // Remove all registered codecs.
  129. //
  130. void RemoveAllCodecs();
  131. // Returns the RTP timestamp for the last sample delivered by GetAudio().
  132. // The return value will be empty if no valid timestamp is available.
  133. absl::optional<uint32_t> GetPlayoutTimestamp();
  134. // Returns the current total delay from NetEq (packet buffer and sync buffer)
  135. // in ms, with smoothing applied to even out short-time fluctuations due to
  136. // jitter. The packet buffer part of the delay is not updated during DTX/CNG
  137. // periods.
  138. //
  139. int FilteredCurrentDelayMs() const;
  140. // Returns the current target delay for NetEq in ms.
  141. //
  142. int TargetDelayMs() const;
  143. //
  144. // Get payload type and format of the last non-CNG/non-DTMF received payload.
  145. // If no non-CNG/non-DTMF packet is received absl::nullopt is returned.
  146. //
  147. absl::optional<std::pair<int, SdpAudioFormat>> LastDecoder() const;
  148. //
  149. // Enable NACK and set the maximum size of the NACK list. If NACK is already
  150. // enabled then the maximum NACK list size is modified accordingly.
  151. //
  152. // If the sequence number of last received packet is N, the sequence numbers
  153. // of NACK list are in the range of [N - |max_nack_list_size|, N).
  154. //
  155. // |max_nack_list_size| should be positive (none zero) and less than or
  156. // equal to |Nack::kNackListSizeLimit|. Otherwise, No change is applied and -1
  157. // is returned. 0 is returned at success.
  158. //
  159. int EnableNack(size_t max_nack_list_size);
  160. // Disable NACK.
  161. void DisableNack();
  162. //
  163. // Get a list of packets to be retransmitted. |round_trip_time_ms| is an
  164. // estimate of the round-trip-time (in milliseconds). Missing packets which
  165. // will be playout in a shorter time than the round-trip-time (with respect
  166. // to the time this API is called) will not be included in the list.
  167. //
  168. // Negative |round_trip_time_ms| results is an error message and empty list
  169. // is returned.
  170. //
  171. std::vector<uint16_t> GetNackList(int64_t round_trip_time_ms) const;
  172. //
  173. // Get statistics of calls to GetAudio().
  174. void GetDecodingCallStatistics(AudioDecodingCallStats* stats) const;
  175. private:
  176. struct DecoderInfo {
  177. int payload_type;
  178. int sample_rate_hz;
  179. int num_channels;
  180. SdpAudioFormat sdp_format;
  181. };
  182. uint32_t NowInTimestamp(int decoder_sampling_rate) const;
  183. mutable Mutex mutex_;
  184. absl::optional<DecoderInfo> last_decoder_ RTC_GUARDED_BY(mutex_);
  185. ACMResampler resampler_ RTC_GUARDED_BY(mutex_);
  186. std::unique_ptr<int16_t[]> last_audio_buffer_ RTC_GUARDED_BY(mutex_);
  187. CallStatistics call_stats_ RTC_GUARDED_BY(mutex_);
  188. const std::unique_ptr<NetEq> neteq_; // NetEq is thread-safe; no lock needed.
  189. Clock* const clock_;
  190. bool resampled_last_output_frame_ RTC_GUARDED_BY(mutex_);
  191. };
  192. } // namespace acm2
  193. } // namespace webrtc
  194. #endif // MODULES_AUDIO_CODING_ACM2_ACM_RECEIVER_H_