audio_channel.h (5079B)
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 11 #ifndef AUDIO_VOIP_AUDIO_CHANNEL_H_ 12 #define AUDIO_VOIP_AUDIO_CHANNEL_H_ 13 14 #include <cstdint> 15 #include <map> 16 #include <memory> 17 #include <optional> 18 #include <utility> 19 20 #include "api/array_view.h" 21 #include "api/audio/audio_mixer.h" 22 #include "api/audio_codecs/audio_decoder_factory.h" 23 #include "api/audio_codecs/audio_encoder.h" 24 #include "api/audio_codecs/audio_format.h" 25 #include "api/environment/environment.h" 26 #include "api/ref_count.h" 27 #include "api/scoped_refptr.h" 28 #include "api/voip/voip_base.h" 29 #include "api/voip/voip_statistics.h" 30 #include "audio/voip/audio_egress.h" 31 #include "audio/voip/audio_ingress.h" 32 #include "call/audio_sender.h" 33 #include "modules/rtp_rtcp/include/receive_statistics.h" 34 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" 35 #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" 36 #include "rtc_base/checks.h" 37 38 namespace webrtc { 39 40 // AudioChannel represents a single media session and provides APIs over 41 // AudioIngress and AudioEgress. Note that a single RTP stack is shared with 42 // these two classes as it has both sending and receiving capabilities. 43 class AudioChannel : public RefCountInterface { 44 public: 45 AudioChannel(const Environment& env, 46 Transport* transport, 47 uint32_t local_ssrc, 48 AudioMixer* audio_mixer, 49 scoped_refptr<AudioDecoderFactory> decoder_factory); 50 ~AudioChannel() override; 51 52 // Set and get ChannelId that this audio channel belongs for debugging and 53 // logging purpose. 54 void SetId(ChannelId id) { id_ = id; } 55 ChannelId GetId() const { return id_; } 56 57 // APIs to start/stop audio channel on each direction. 58 // StartSend/StartPlay returns false if encoder/decoders 59 // have not been set, respectively. 60 bool StartSend(); 61 void StopSend(); 62 bool StartPlay(); 63 void StopPlay(); 64 65 // APIs relayed to AudioEgress. 66 bool IsSendingMedia() const { return egress_->IsSending(); } 67 AudioSender* GetAudioSender() { return egress_.get(); } 68 void SetEncoder(int payload_type, 69 const SdpAudioFormat& encoder_format, 70 std::unique_ptr<AudioEncoder> encoder) { 71 egress_->SetEncoder(payload_type, encoder_format, std::move(encoder)); 72 } 73 std::optional<SdpAudioFormat> GetEncoderFormat() const { 74 return egress_->GetEncoderFormat(); 75 } 76 void RegisterTelephoneEventType(int rtp_payload_type, int sample_rate_hz) { 77 egress_->RegisterTelephoneEventType(rtp_payload_type, sample_rate_hz); 78 } 79 bool SendTelephoneEvent(int dtmf_event, int duration_ms) { 80 return egress_->SendTelephoneEvent(dtmf_event, duration_ms); 81 } 82 void SetMute(bool enable) { egress_->SetMute(enable); } 83 84 // APIs relayed to AudioIngress. 85 bool IsPlaying() const { return ingress_->IsPlaying(); } 86 void ReceivedRTPPacket(ArrayView<const uint8_t> rtp_packet) { 87 ingress_->ReceivedRTPPacket(rtp_packet); 88 } 89 void ReceivedRTCPPacket(ArrayView<const uint8_t> rtcp_packet) { 90 ingress_->ReceivedRTCPPacket(rtcp_packet); 91 } 92 void SetReceiveCodecs(const std::map<int, SdpAudioFormat>& codecs) { 93 ingress_->SetReceiveCodecs(codecs); 94 } 95 IngressStatistics GetIngressStatistics(); 96 ChannelStatistics GetChannelStatistics(); 97 98 // See comments on the methods used from AudioEgress and AudioIngress. 99 // Conversion to double is following what is done in 100 // DoubleAudioLevelFromIntAudioLevel method in rtc_stats_collector.cc to be 101 // consistent. 102 double GetInputAudioLevel() const { 103 return egress_->GetInputAudioLevel() / 32767.0; 104 } 105 double GetInputTotalEnergy() const { return egress_->GetInputTotalEnergy(); } 106 double GetInputTotalDuration() const { 107 return egress_->GetInputTotalDuration(); 108 } 109 double GetOutputAudioLevel() const { 110 return ingress_->GetOutputAudioLevel() / 32767.0; 111 } 112 double GetOutputTotalEnergy() const { 113 return ingress_->GetOutputTotalEnergy(); 114 } 115 double GetOutputTotalDuration() const { 116 return ingress_->GetOutputTotalDuration(); 117 } 118 119 // Internal API for testing purpose. 120 void SendRTCPReportForTesting(RTCPPacketType type) { 121 int32_t result = rtp_rtcp_->SendRTCP(type); 122 RTC_DCHECK(result == 0); 123 } 124 125 private: 126 // ChannelId that this audio channel belongs for logging purpose. 127 ChannelId id_; 128 129 // Synchronization is handled internally by AudioMixer. 130 AudioMixer* audio_mixer_; 131 132 // Listed in order for safe destruction of AudioChannel object. 133 // Synchronization for these are handled internally. 134 std::unique_ptr<ReceiveStatistics> receive_statistics_; 135 std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp_; 136 std::unique_ptr<AudioIngress> ingress_; 137 std::unique_ptr<AudioEgress> egress_; 138 }; 139 140 } // namespace webrtc 141 142 #endif // AUDIO_VOIP_AUDIO_CHANNEL_H_