audio_ingress.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2020 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 AUDIO_VOIP_AUDIO_INGRESS_H_
  11. #define AUDIO_VOIP_AUDIO_INGRESS_H_
  12. #include <algorithm>
  13. #include <atomic>
  14. #include <map>
  15. #include <memory>
  16. #include <utility>
  17. #include "api/array_view.h"
  18. #include "api/audio/audio_mixer.h"
  19. #include "api/rtp_headers.h"
  20. #include "api/scoped_refptr.h"
  21. #include "audio/audio_level.h"
  22. #include "modules/audio_coding/acm2/acm_receiver.h"
  23. #include "modules/audio_coding/include/audio_coding_module.h"
  24. #include "modules/rtp_rtcp/include/receive_statistics.h"
  25. #include "modules/rtp_rtcp/include/remote_ntp_time_estimator.h"
  26. #include "modules/rtp_rtcp/source/rtp_packet_received.h"
  27. #include "modules/rtp_rtcp/source/rtp_rtcp_interface.h"
  28. #include "rtc_base/synchronization/mutex.h"
  29. #include "rtc_base/time_utils.h"
  30. namespace webrtc {
  31. // AudioIngress handles incoming RTP/RTCP packets from the remote
  32. // media endpoint. Received RTP packets are injected into AcmReceiver and
  33. // when audio output thread requests for audio samples to play through system
  34. // output such as speaker device, AudioIngress provides the samples via its
  35. // implementation on AudioMixer::Source interface.
  36. //
  37. // Note that this class is originally based on ChannelReceive in
  38. // audio/channel_receive.cc with non-audio related logic trimmed as aimed for
  39. // smaller footprint.
  40. class AudioIngress : public AudioMixer::Source {
  41. public:
  42. AudioIngress(RtpRtcpInterface* rtp_rtcp,
  43. Clock* clock,
  44. ReceiveStatistics* receive_statistics,
  45. rtc::scoped_refptr<AudioDecoderFactory> decoder_factory);
  46. ~AudioIngress() override;
  47. // Start or stop receiving operation of AudioIngress.
  48. bool StartPlay();
  49. void StopPlay() {
  50. playing_ = false;
  51. output_audio_level_.ResetLevelFullRange();
  52. }
  53. // Query the state of the AudioIngress.
  54. bool IsPlaying() const { return playing_; }
  55. // Set the decoder formats and payload type for AcmReceiver where the
  56. // key type (int) of the map is the payload type of SdpAudioFormat.
  57. void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs);
  58. // APIs to handle received RTP/RTCP packets from caller.
  59. void ReceivedRTPPacket(rtc::ArrayView<const uint8_t> rtp_packet);
  60. void ReceivedRTCPPacket(rtc::ArrayView<const uint8_t> rtcp_packet);
  61. // Retrieve highest speech output level in last 100 ms. Note that
  62. // this isn't RMS but absolute raw audio level on int16_t sample unit.
  63. // Therefore, the return value will vary between 0 ~ 0xFFFF. This type of
  64. // value may be useful to be used for measuring active speaker gauge.
  65. int GetSpeechOutputLevelFullRange() const {
  66. return output_audio_level_.LevelFullRange();
  67. }
  68. // Returns network round trip time (RTT) measued by RTCP exchange with
  69. // remote media endpoint. RTT value -1 indicates that it's not initialized.
  70. int64_t GetRoundTripTime();
  71. NetworkStatistics GetNetworkStatistics() const {
  72. NetworkStatistics stats;
  73. acm_receiver_.GetNetworkStatistics(&stats);
  74. return stats;
  75. }
  76. AudioDecodingCallStats GetDecodingStatistics() const {
  77. AudioDecodingCallStats stats;
  78. acm_receiver_.GetDecodingCallStatistics(&stats);
  79. return stats;
  80. }
  81. // Implementation of AudioMixer::Source interface.
  82. AudioMixer::Source::AudioFrameInfo GetAudioFrameWithInfo(
  83. int sampling_rate,
  84. AudioFrame* audio_frame) override;
  85. int Ssrc() const override {
  86. return rtc::dchecked_cast<int>(remote_ssrc_.load());
  87. }
  88. int PreferredSampleRate() const override {
  89. // If we haven't received any RTP packet from remote and thus
  90. // last_packet_sampling_rate is not available then use NetEq's sampling
  91. // rate as that would be what would be used for audio output sample.
  92. return std::max(acm_receiver_.last_packet_sample_rate_hz().value_or(0),
  93. acm_receiver_.last_output_sample_rate_hz());
  94. }
  95. private:
  96. // Indicates AudioIngress status as caller invokes Start/StopPlaying.
  97. // If not playing, incoming RTP data processing is skipped, thus
  98. // producing no data to output device.
  99. std::atomic<bool> playing_;
  100. // Currently active remote ssrc from remote media endpoint.
  101. std::atomic<uint32_t> remote_ssrc_;
  102. // The first rtp timestamp of the output audio frame that is used to
  103. // calculate elasped time for subsequent audio frames.
  104. std::atomic<int64_t> first_rtp_timestamp_;
  105. // Synchronizaton is handled internally by ReceiveStatistics.
  106. ReceiveStatistics* const rtp_receive_statistics_;
  107. // Synchronizaton is handled internally by RtpRtcpInterface.
  108. RtpRtcpInterface* const rtp_rtcp_;
  109. // Synchronizaton is handled internally by acm2::AcmReceiver.
  110. acm2::AcmReceiver acm_receiver_;
  111. // Synchronizaton is handled internally by voe::AudioLevel.
  112. voe::AudioLevel output_audio_level_;
  113. Mutex lock_;
  114. RemoteNtpTimeEstimator ntp_estimator_ RTC_GUARDED_BY(lock_);
  115. // For receiving RTP statistics, this tracks the sampling rate value
  116. // per payload type set when caller set via SetReceiveCodecs.
  117. std::map<int, int> receive_codec_info_ RTC_GUARDED_BY(lock_);
  118. rtc::TimestampWrapAroundHandler timestamp_wrap_handler_ RTC_GUARDED_BY(lock_);
  119. };
  120. } // namespace webrtc
  121. #endif // AUDIO_VOIP_AUDIO_INGRESS_H_