voip_core.h (7444B)
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_VOIP_CORE_H_ 12 #define AUDIO_VOIP_VOIP_CORE_H_ 13 14 #include <cstdint> 15 #include <map> 16 #include <memory> 17 #include <optional> 18 #include <unordered_map> 19 20 #include "api/array_view.h" 21 #include "api/audio/audio_device.h" 22 #include "api/audio/audio_mixer.h" 23 #include "api/audio/audio_processing.h" 24 #include "api/audio_codecs/audio_decoder_factory.h" 25 #include "api/audio_codecs/audio_encoder_factory.h" 26 #include "api/audio_codecs/audio_format.h" 27 #include "api/environment/environment.h" 28 #include "api/scoped_refptr.h" 29 #include "api/voip/voip_base.h" 30 #include "api/voip/voip_codec.h" 31 #include "api/voip/voip_dtmf.h" 32 #include "api/voip/voip_engine.h" 33 #include "api/voip/voip_network.h" 34 #include "api/voip/voip_statistics.h" 35 #include "api/voip/voip_volume_control.h" 36 #include "audio/audio_transport_impl.h" 37 #include "audio/voip/audio_channel.h" 38 #include "rtc_base/synchronization/mutex.h" 39 #include "rtc_base/thread_annotations.h" 40 41 namespace webrtc { 42 43 // VoipCore is the implementatino of VoIP APIs listed in api/voip directory. 44 // It manages a vector of AudioChannel objects where each is mapped with a 45 // ChannelId (int) type. ChannelId is the primary key to locate a specific 46 // AudioChannel object to operate requested VoIP API from the caller. 47 // 48 // This class receives required audio components from caller at construction and 49 // owns the life cycle of them to orchestrate the proper destruction sequence. 50 class VoipCore : public VoipEngine, 51 public VoipBase, 52 public VoipNetwork, 53 public VoipCodec, 54 public VoipDtmf, 55 public VoipStatistics, 56 public VoipVolumeControl { 57 public: 58 VoipCore(const Environment& env, 59 scoped_refptr<AudioEncoderFactory> encoder_factory, 60 scoped_refptr<AudioDecoderFactory> decoder_factory, 61 scoped_refptr<AudioDeviceModule> audio_device_module, 62 scoped_refptr<AudioProcessing> audio_processing); 63 ~VoipCore() override = default; 64 65 // Implements VoipEngine interfaces. 66 VoipBase& Base() override { return *this; } 67 VoipNetwork& Network() override { return *this; } 68 VoipCodec& Codec() override { return *this; } 69 VoipDtmf& Dtmf() override { return *this; } 70 VoipStatistics& Statistics() override { return *this; } 71 VoipVolumeControl& VolumeControl() override { return *this; } 72 73 // Implements VoipBase interfaces. 74 ChannelId CreateChannel(Transport* transport, 75 std::optional<uint32_t> local_ssrc) override; 76 VoipResult ReleaseChannel(ChannelId channel_id) override; 77 VoipResult StartSend(ChannelId channel_id) override; 78 VoipResult StopSend(ChannelId channel_id) override; 79 VoipResult StartPlayout(ChannelId channel_id) override; 80 VoipResult StopPlayout(ChannelId channel_id) override; 81 82 // Implements VoipNetwork interfaces. 83 VoipResult ReceivedRTPPacket(ChannelId channel_id, 84 ArrayView<const uint8_t> rtp_packet) override; 85 VoipResult ReceivedRTCPPacket(ChannelId channel_id, 86 ArrayView<const uint8_t> rtcp_packet) override; 87 88 // Implements VoipCodec interfaces. 89 VoipResult SetSendCodec(ChannelId channel_id, 90 int payload_type, 91 const SdpAudioFormat& encoder_format) override; 92 VoipResult SetReceiveCodecs( 93 ChannelId channel_id, 94 const std::map<int, SdpAudioFormat>& decoder_specs) override; 95 96 // Implements VoipDtmf interfaces. 97 VoipResult RegisterTelephoneEventType(ChannelId channel_id, 98 int rtp_payload_type, 99 int sample_rate_hz) override; 100 VoipResult SendDtmfEvent(ChannelId channel_id, 101 DtmfEvent dtmf_event, 102 int duration_ms) override; 103 104 // Implements VoipStatistics interfaces. 105 VoipResult GetIngressStatistics(ChannelId channel_id, 106 IngressStatistics& ingress_stats) override; 107 VoipResult GetChannelStatistics(ChannelId channe_id, 108 ChannelStatistics& channel_stats) override; 109 110 // Implements VoipVolumeControl interfaces. 111 VoipResult SetInputMuted(ChannelId channel_id, bool enable) override; 112 VoipResult GetInputVolumeInfo(ChannelId channel_id, 113 VolumeInfo& volume_info) override; 114 VoipResult GetOutputVolumeInfo(ChannelId channel_id, 115 VolumeInfo& volume_info) override; 116 117 private: 118 // Initialize ADM and default audio device if needed. 119 // Returns true if ADM is successfully initialized or already in such state 120 // (e.g called more than once). Returns false when ADM fails to initialize 121 // which would presumably render further processing useless. Note that such 122 // failure won't necessarily succeed in next initialization attempt as it 123 // would mean changing the ADM implementation. From Android N and onwards, the 124 // mobile app may not be able to gain microphone access when in background 125 // mode. Therefore it would be better to delay the logic as late as possible. 126 bool InitializeIfNeeded(); 127 128 // Fetches the corresponding AudioChannel assigned with given `channel`. 129 // Returns nullptr if not found. 130 scoped_refptr<AudioChannel> GetChannel(ChannelId channel_id); 131 132 // Updates AudioTransportImpl with a new set of actively sending AudioSender 133 // (AudioEgress). This needs to be invoked whenever StartSend/StopSend is 134 // involved by caller. Returns false when the selected audio device fails to 135 // initialize where it can't expect to deliver any audio input sample. 136 bool UpdateAudioTransportWithSenders(); 137 138 // Synchronization for these are handled internally. 139 const Environment env_; 140 scoped_refptr<AudioEncoderFactory> encoder_factory_; 141 scoped_refptr<AudioDecoderFactory> decoder_factory_; 142 143 // Synchronization is handled internally by AudioProcessing. 144 // Must be placed before `audio_device_module_` for proper destruction. 145 scoped_refptr<AudioProcessing> audio_processing_; 146 147 // Synchronization is handled internally by AudioMixer. 148 // Must be placed before `audio_device_module_` for proper destruction. 149 scoped_refptr<AudioMixer> audio_mixer_; 150 151 // Synchronization is handled internally by AudioTransportImpl. 152 // Must be placed before `audio_device_module_` for proper destruction. 153 std::unique_ptr<AudioTransportImpl> audio_transport_; 154 155 // Synchronization is handled internally by AudioDeviceModule. 156 scoped_refptr<AudioDeviceModule> audio_device_module_; 157 158 Mutex lock_; 159 160 // Member to track a next ChannelId for new AudioChannel. 161 int next_channel_id_ RTC_GUARDED_BY(lock_) = 0; 162 163 // Container to track currently active AudioChannel objects mapped by 164 // ChannelId. 165 std::unordered_map<ChannelId, scoped_refptr<AudioChannel>> channels_ 166 RTC_GUARDED_BY(lock_); 167 168 // Boolean flag to ensure initialization only occurs once. 169 bool initialized_ RTC_GUARDED_BY(lock_) = false; 170 }; 171 172 } // namespace webrtc 173 174 #endif // AUDIO_VOIP_VOIP_CORE_H_