1 /* 2 * libjingle 3 * Copyright 2004 Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TALK_MEDIA_WEBRTCVOICEENGINE_H_ 29 #define TALK_MEDIA_WEBRTCVOICEENGINE_H_ 30 31 #include <map> 32 #include <string> 33 #include <vector> 34 35 #include "talk/media/base/rtputils.h" 36 #include "talk/media/webrtc/webrtccommon.h" 37 #include "talk/media/webrtc/webrtcvoe.h" 38 #include "talk/session/media/channel.h" 39 #include "webrtc/audio_state.h" 40 #include "webrtc/base/buffer.h" 41 #include "webrtc/base/scoped_ptr.h" 42 #include "webrtc/base/stream.h" 43 #include "webrtc/base/thread_checker.h" 44 #include "webrtc/call.h" 45 #include "webrtc/common.h" 46 #include "webrtc/config.h" 47 48 namespace cricket { 49 50 class AudioDeviceModule; 51 class AudioRenderer; 52 class VoEWrapper; 53 class WebRtcVoiceMediaChannel; 54 55 // WebRtcVoiceEngine is a class to be used with CompositeMediaEngine. 56 // It uses the WebRtc VoiceEngine library for audio handling. 57 class WebRtcVoiceEngine final : public webrtc::TraceCallback { 58 friend class WebRtcVoiceMediaChannel; 59 public: 60 // Exposed for the WVoE/MC unit test. 61 static bool ToCodecInst(const AudioCodec& in, webrtc::CodecInst* out); 62 63 WebRtcVoiceEngine(); 64 // Dependency injection for testing. 65 explicit WebRtcVoiceEngine(VoEWrapper* voe_wrapper); 66 ~WebRtcVoiceEngine(); 67 bool Init(rtc::Thread* worker_thread); 68 void Terminate(); 69 70 rtc::scoped_refptr<webrtc::AudioState> GetAudioState() const; 71 VoiceMediaChannel* CreateChannel(webrtc::Call* call, 72 const AudioOptions& options); 73 74 bool GetOutputVolume(int* level); 75 bool SetOutputVolume(int level); 76 int GetInputLevel(); 77 78 const std::vector<AudioCodec>& codecs(); 79 RtpCapabilities GetCapabilities() const; 80 81 // For tracking WebRtc channels. Needed because we have to pause them 82 // all when switching devices. 83 // May only be called by WebRtcVoiceMediaChannel. 84 void RegisterChannel(WebRtcVoiceMediaChannel* channel); 85 void UnregisterChannel(WebRtcVoiceMediaChannel* channel); 86 87 // Called by WebRtcVoiceMediaChannel to set a gain offset from 88 // the default AGC target level. 89 bool AdjustAgcLevel(int delta); 90 91 VoEWrapper* voe() { return voe_wrapper_.get(); } 92 int GetLastEngineError(); 93 94 // Set the external ADM. This can only be called before Init. 95 bool SetAudioDeviceModule(webrtc::AudioDeviceModule* adm); 96 97 // Starts AEC dump using existing file. 98 bool StartAecDump(rtc::PlatformFile file); 99 100 // Stops AEC dump. 101 void StopAecDump(); 102 103 // Starts recording an RtcEventLog using an existing file until 10 minutes 104 // pass or the StopRtcEventLog function is called. 105 bool StartRtcEventLog(rtc::PlatformFile file); 106 107 // Stops recording the RtcEventLog. 108 void StopRtcEventLog(); 109 110 private: 111 void Construct(); 112 bool InitInternal(); 113 // Every option that is "set" will be applied. Every option not "set" will be 114 // ignored. This allows us to selectively turn on and off different options 115 // easily at any time. 116 bool ApplyOptions(const AudioOptions& options); 117 void SetDefaultDevices(); 118 119 // webrtc::TraceCallback: 120 void Print(webrtc::TraceLevel level, const char* trace, int length) override; 121 122 void StartAecDump(const std::string& filename); 123 int CreateVoEChannel(); 124 125 rtc::ThreadChecker signal_thread_checker_; 126 rtc::ThreadChecker worker_thread_checker_; 127 128 // The primary instance of WebRtc VoiceEngine. 129 rtc::scoped_ptr<VoEWrapper> voe_wrapper_; 130 rtc::scoped_refptr<webrtc::AudioState> audio_state_; 131 // The external audio device manager 132 webrtc::AudioDeviceModule* adm_ = nullptr; 133 std::vector<AudioCodec> codecs_; 134 std::vector<WebRtcVoiceMediaChannel*> channels_; 135 webrtc::Config voe_config_; 136 bool initialized_ = false; 137 bool is_dumping_aec_ = false; 138 139 webrtc::AgcConfig default_agc_config_; 140 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns 141 // values, and apply them in case they are missing in the audio options. We 142 // need to do this because SetExtraOptions() will revert to defaults for 143 // options which are not provided. 144 rtc::Optional<bool> extended_filter_aec_; 145 rtc::Optional<bool> delay_agnostic_aec_; 146 rtc::Optional<bool> experimental_ns_; 147 148 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine); 149 }; 150 151 // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses 152 // WebRtc Voice Engine. 153 class WebRtcVoiceMediaChannel final : public VoiceMediaChannel, 154 public webrtc::Transport { 155 public: 156 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, 157 const AudioOptions& options, 158 webrtc::Call* call); 159 ~WebRtcVoiceMediaChannel() override; 160 161 const AudioOptions& options() const { return options_; } 162 163 bool SetSendParameters(const AudioSendParameters& params) override; 164 bool SetRecvParameters(const AudioRecvParameters& params) override; 165 bool SetPlayout(bool playout) override; 166 bool PausePlayout(); 167 bool ResumePlayout(); 168 bool SetSend(SendFlags send) override; 169 bool PauseSend(); 170 bool ResumeSend(); 171 bool SetAudioSend(uint32_t ssrc, 172 bool enable, 173 const AudioOptions* options, 174 AudioRenderer* renderer) override; 175 bool AddSendStream(const StreamParams& sp) override; 176 bool RemoveSendStream(uint32_t ssrc) override; 177 bool AddRecvStream(const StreamParams& sp) override; 178 bool RemoveRecvStream(uint32_t ssrc) override; 179 bool GetActiveStreams(AudioInfo::StreamList* actives) override; 180 int GetOutputLevel() override; 181 int GetTimeSinceLastTyping() override; 182 void SetTypingDetectionParameters(int time_window, 183 int cost_per_typing, 184 int reporting_threshold, 185 int penalty_decay, 186 int type_event_delay) override; 187 bool SetOutputVolume(uint32_t ssrc, double volume) override; 188 189 bool CanInsertDtmf() override; 190 bool InsertDtmf(uint32_t ssrc, int event, int duration) override; 191 192 void OnPacketReceived(rtc::Buffer* packet, 193 const rtc::PacketTime& packet_time) override; 194 void OnRtcpReceived(rtc::Buffer* packet, 195 const rtc::PacketTime& packet_time) override; 196 void OnReadyToSend(bool ready) override {} 197 bool GetStats(VoiceMediaInfo* info) override; 198 199 void SetRawAudioSink( 200 uint32_t ssrc, 201 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) override; 202 203 // implements Transport interface 204 bool SendRtp(const uint8_t* data, 205 size_t len, 206 const webrtc::PacketOptions& options) override { 207 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, 208 kMaxRtpPacketLen); 209 rtc::PacketOptions rtc_options; 210 rtc_options.packet_id = options.packet_id; 211 return VoiceMediaChannel::SendPacket(&packet, rtc_options); 212 } 213 214 bool SendRtcp(const uint8_t* data, size_t len) override { 215 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, 216 kMaxRtpPacketLen); 217 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions()); 218 } 219 220 int GetReceiveChannelId(uint32_t ssrc) const; 221 int GetSendChannelId(uint32_t ssrc) const; 222 223 private: 224 bool SetSendCodecs(const std::vector<AudioCodec>& codecs); 225 bool SetOptions(const AudioOptions& options); 226 bool SetMaxSendBandwidth(int bps); 227 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); 228 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer); 229 bool MuteStream(uint32_t ssrc, bool mute); 230 231 WebRtcVoiceEngine* engine() { return engine_; } 232 int GetLastEngineError() { return engine()->GetLastEngineError(); } 233 int GetOutputLevel(int channel); 234 bool GetRedSendCodec(const AudioCodec& red_codec, 235 const std::vector<AudioCodec>& all_codecs, 236 webrtc::CodecInst* send_codec); 237 bool SetPlayout(int channel, bool playout); 238 void SetNack(int channel, bool nack_enabled); 239 bool SetSendCodec(int channel, const webrtc::CodecInst& send_codec); 240 bool ChangePlayout(bool playout); 241 bool ChangeSend(SendFlags send); 242 bool ChangeSend(int channel, SendFlags send); 243 int CreateVoEChannel(); 244 bool DeleteVoEChannel(int channel); 245 bool IsDefaultRecvStream(uint32_t ssrc) { 246 return default_recv_ssrc_ == static_cast<int64_t>(ssrc); 247 } 248 bool SetSendCodecs(int channel, const std::vector<AudioCodec>& codecs); 249 bool SetSendBitrateInternal(int bps); 250 251 rtc::ThreadChecker worker_thread_checker_; 252 253 WebRtcVoiceEngine* const engine_ = nullptr; 254 std::vector<AudioCodec> recv_codecs_; 255 std::vector<AudioCodec> send_codecs_; 256 rtc::scoped_ptr<webrtc::CodecInst> send_codec_; 257 bool send_bitrate_setting_ = false; 258 int send_bitrate_bps_ = 0; 259 AudioOptions options_; 260 rtc::Optional<int> dtmf_payload_type_; 261 bool desired_playout_ = false; 262 bool nack_enabled_ = false; 263 bool playout_ = false; 264 SendFlags desired_send_ = SEND_NOTHING; 265 SendFlags send_ = SEND_NOTHING; 266 webrtc::Call* const call_ = nullptr; 267 268 // SSRC of unsignalled receive stream, or -1 if there isn't one. 269 int64_t default_recv_ssrc_ = -1; 270 // Volume for unsignalled stream, which may be set before the stream exists. 271 double default_recv_volume_ = 1.0; 272 // Default SSRC to use for RTCP receiver reports in case of no signaled 273 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740 274 // and https://code.google.com/p/chromium/issues/detail?id=547661 275 uint32_t receiver_reports_ssrc_ = 0xFA17FA17u; 276 277 class WebRtcAudioSendStream; 278 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_; 279 std::vector<webrtc::RtpExtension> send_rtp_extensions_; 280 281 class WebRtcAudioReceiveStream; 282 std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_; 283 std::vector<webrtc::RtpExtension> recv_rtp_extensions_; 284 285 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel); 286 }; 287 } // namespace cricket 288 289 #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ 290