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 <map> 15 #include <memory> 16 #include <queue> 17 #include <unordered_map> 18 #include <vector> 19 20 #include "api/audio_codecs/audio_decoder_factory.h" 21 #include "api/audio_codecs/audio_encoder_factory.h" 22 #include "api/scoped_refptr.h" 23 #include "api/task_queue/task_queue_factory.h" 24 #include "api/voip/voip_base.h" 25 #include "api/voip/voip_codec.h" 26 #include "api/voip/voip_dtmf.h" 27 #include "api/voip/voip_engine.h" 28 #include "api/voip/voip_network.h" 29 #include "api/voip/voip_statistics.h" 30 #include "api/voip/voip_volume_control.h" 31 #include "audio/audio_transport_impl.h" 32 #include "audio/voip/audio_channel.h" 33 #include "modules/audio_device/include/audio_device.h" 34 #include "modules/audio_mixer/audio_mixer_impl.h" 35 #include "modules/audio_processing/include/audio_processing.h" 36 #include "rtc_base/synchronization/mutex.h" 37 38 namespace webrtc { 39 40 // VoipCore is the implementatino of VoIP APIs listed in api/voip directory. 41 // It manages a vector of AudioChannel objects where each is mapped with a 42 // ChannelId (int) type. ChannelId is the primary key to locate a specific 43 // AudioChannel object to operate requested VoIP API from the caller. 44 // 45 // This class receives required audio components from caller at construction and 46 // owns the life cycle of them to orchestrate the proper destruction sequence. 47 class VoipCore : public VoipEngine, 48 public VoipBase, 49 public VoipNetwork, 50 public VoipCodec, 51 public VoipDtmf, 52 public VoipStatistics, 53 public VoipVolumeControl { 54 public: 55 // Construct VoipCore with provided arguments. 56 VoipCore(rtc::scoped_refptr<AudioEncoderFactory> encoder_factory, 57 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, 58 std::unique_ptr<TaskQueueFactory> task_queue_factory, 59 rtc::scoped_refptr<AudioDeviceModule> audio_device_module, 60 rtc::scoped_refptr<AudioProcessing> audio_processing); 61 ~VoipCore() override = default; 62 63 // Implements VoipEngine interfaces. Base()64 VoipBase& Base() override { return *this; } Network()65 VoipNetwork& Network() override { return *this; } Codec()66 VoipCodec& Codec() override { return *this; } Dtmf()67 VoipDtmf& Dtmf() override { return *this; } Statistics()68 VoipStatistics& Statistics() override { return *this; } VolumeControl()69 VoipVolumeControl& VolumeControl() override { return *this; } 70 71 // Implements VoipBase interfaces. 72 ChannelId CreateChannel(Transport* transport, 73 absl::optional<uint32_t> local_ssrc) override; 74 VoipResult ReleaseChannel(ChannelId channel_id) override; 75 VoipResult StartSend(ChannelId channel_id) override; 76 VoipResult StopSend(ChannelId channel_id) override; 77 VoipResult StartPlayout(ChannelId channel_id) override; 78 VoipResult StopPlayout(ChannelId channel_id) override; 79 80 // Implements VoipNetwork interfaces. 81 VoipResult ReceivedRTPPacket( 82 ChannelId channel_id, 83 rtc::ArrayView<const uint8_t> rtp_packet) override; 84 VoipResult ReceivedRTCPPacket( 85 ChannelId channel_id, 86 rtc::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 rtc::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 rtc::scoped_refptr<AudioEncoderFactory> encoder_factory_; 140 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_; 141 std::unique_ptr<TaskQueueFactory> task_queue_factory_; 142 143 // Synchronization is handled internally by AudioProcessing. 144 // Must be placed before `audio_device_module_` for proper destruction. 145 rtc::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 rtc::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 rtc::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, rtc::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_ 175