| OLD | NEW |
| (Empty) |
| 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 an existing file. A maximum file size in bytes can be | |
| 98 // specified. When the maximum file size is reached, logging is stopped and | |
| 99 // the file is closed. If max_size_bytes is set to <= 0, no limit will be | |
| 100 // used. | |
| 101 bool StartAecDump(rtc::PlatformFile file, int64_t max_size_bytes); | |
| 102 | |
| 103 // Stops AEC dump. | |
| 104 void StopAecDump(); | |
| 105 | |
| 106 // Starts recording an RtcEventLog using an existing file until 10 minutes | |
| 107 // pass or the StopRtcEventLog function is called. | |
| 108 bool StartRtcEventLog(rtc::PlatformFile file); | |
| 109 | |
| 110 // Stops recording the RtcEventLog. | |
| 111 void StopRtcEventLog(); | |
| 112 | |
| 113 private: | |
| 114 void Construct(); | |
| 115 bool InitInternal(); | |
| 116 // Every option that is "set" will be applied. Every option not "set" will be | |
| 117 // ignored. This allows us to selectively turn on and off different options | |
| 118 // easily at any time. | |
| 119 bool ApplyOptions(const AudioOptions& options); | |
| 120 void SetDefaultDevices(); | |
| 121 | |
| 122 // webrtc::TraceCallback: | |
| 123 void Print(webrtc::TraceLevel level, const char* trace, int length) override; | |
| 124 | |
| 125 void StartAecDump(const std::string& filename); | |
| 126 int CreateVoEChannel(); | |
| 127 | |
| 128 rtc::ThreadChecker signal_thread_checker_; | |
| 129 rtc::ThreadChecker worker_thread_checker_; | |
| 130 | |
| 131 // The primary instance of WebRtc VoiceEngine. | |
| 132 rtc::scoped_ptr<VoEWrapper> voe_wrapper_; | |
| 133 rtc::scoped_refptr<webrtc::AudioState> audio_state_; | |
| 134 // The external audio device manager | |
| 135 webrtc::AudioDeviceModule* adm_ = nullptr; | |
| 136 std::vector<AudioCodec> codecs_; | |
| 137 std::vector<WebRtcVoiceMediaChannel*> channels_; | |
| 138 webrtc::Config voe_config_; | |
| 139 bool initialized_ = false; | |
| 140 bool is_dumping_aec_ = false; | |
| 141 | |
| 142 webrtc::AgcConfig default_agc_config_; | |
| 143 // Cache received extended_filter_aec, delay_agnostic_aec and experimental_ns | |
| 144 // values, and apply them in case they are missing in the audio options. We | |
| 145 // need to do this because SetExtraOptions() will revert to defaults for | |
| 146 // options which are not provided. | |
| 147 rtc::Optional<bool> extended_filter_aec_; | |
| 148 rtc::Optional<bool> delay_agnostic_aec_; | |
| 149 rtc::Optional<bool> experimental_ns_; | |
| 150 | |
| 151 RTC_DISALLOW_COPY_AND_ASSIGN(WebRtcVoiceEngine); | |
| 152 }; | |
| 153 | |
| 154 // WebRtcVoiceMediaChannel is an implementation of VoiceMediaChannel that uses | |
| 155 // WebRtc Voice Engine. | |
| 156 class WebRtcVoiceMediaChannel final : public VoiceMediaChannel, | |
| 157 public webrtc::Transport { | |
| 158 public: | |
| 159 WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine, | |
| 160 const AudioOptions& options, | |
| 161 webrtc::Call* call); | |
| 162 ~WebRtcVoiceMediaChannel() override; | |
| 163 | |
| 164 const AudioOptions& options() const { return options_; } | |
| 165 | |
| 166 bool SetSendParameters(const AudioSendParameters& params) override; | |
| 167 bool SetRecvParameters(const AudioRecvParameters& params) override; | |
| 168 bool SetPlayout(bool playout) override; | |
| 169 bool PausePlayout(); | |
| 170 bool ResumePlayout(); | |
| 171 bool SetSend(SendFlags send) override; | |
| 172 bool PauseSend(); | |
| 173 bool ResumeSend(); | |
| 174 bool SetAudioSend(uint32_t ssrc, | |
| 175 bool enable, | |
| 176 const AudioOptions* options, | |
| 177 AudioRenderer* renderer) override; | |
| 178 bool AddSendStream(const StreamParams& sp) override; | |
| 179 bool RemoveSendStream(uint32_t ssrc) override; | |
| 180 bool AddRecvStream(const StreamParams& sp) override; | |
| 181 bool RemoveRecvStream(uint32_t ssrc) override; | |
| 182 bool GetActiveStreams(AudioInfo::StreamList* actives) override; | |
| 183 int GetOutputLevel() override; | |
| 184 int GetTimeSinceLastTyping() override; | |
| 185 void SetTypingDetectionParameters(int time_window, | |
| 186 int cost_per_typing, | |
| 187 int reporting_threshold, | |
| 188 int penalty_decay, | |
| 189 int type_event_delay) override; | |
| 190 bool SetOutputVolume(uint32_t ssrc, double volume) override; | |
| 191 | |
| 192 bool CanInsertDtmf() override; | |
| 193 bool InsertDtmf(uint32_t ssrc, int event, int duration) override; | |
| 194 | |
| 195 void OnPacketReceived(rtc::Buffer* packet, | |
| 196 const rtc::PacketTime& packet_time) override; | |
| 197 void OnRtcpReceived(rtc::Buffer* packet, | |
| 198 const rtc::PacketTime& packet_time) override; | |
| 199 void OnReadyToSend(bool ready) override {} | |
| 200 bool GetStats(VoiceMediaInfo* info) override; | |
| 201 | |
| 202 void SetRawAudioSink( | |
| 203 uint32_t ssrc, | |
| 204 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) override; | |
| 205 | |
| 206 // implements Transport interface | |
| 207 bool SendRtp(const uint8_t* data, | |
| 208 size_t len, | |
| 209 const webrtc::PacketOptions& options) override { | |
| 210 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, | |
| 211 kMaxRtpPacketLen); | |
| 212 rtc::PacketOptions rtc_options; | |
| 213 rtc_options.packet_id = options.packet_id; | |
| 214 return VoiceMediaChannel::SendPacket(&packet, rtc_options); | |
| 215 } | |
| 216 | |
| 217 bool SendRtcp(const uint8_t* data, size_t len) override { | |
| 218 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len, | |
| 219 kMaxRtpPacketLen); | |
| 220 return VoiceMediaChannel::SendRtcp(&packet, rtc::PacketOptions()); | |
| 221 } | |
| 222 | |
| 223 int GetReceiveChannelId(uint32_t ssrc) const; | |
| 224 int GetSendChannelId(uint32_t ssrc) const; | |
| 225 | |
| 226 private: | |
| 227 bool SetSendCodecs(const std::vector<AudioCodec>& codecs); | |
| 228 bool SetOptions(const AudioOptions& options); | |
| 229 bool SetMaxSendBandwidth(int bps); | |
| 230 bool SetRecvCodecs(const std::vector<AudioCodec>& codecs); | |
| 231 bool SetLocalRenderer(uint32_t ssrc, AudioRenderer* renderer); | |
| 232 bool MuteStream(uint32_t ssrc, bool mute); | |
| 233 | |
| 234 WebRtcVoiceEngine* engine() { return engine_; } | |
| 235 int GetLastEngineError() { return engine()->GetLastEngineError(); } | |
| 236 int GetOutputLevel(int channel); | |
| 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 transport_cc_enabled_ = false; | |
| 264 bool playout_ = false; | |
| 265 SendFlags desired_send_ = SEND_NOTHING; | |
| 266 SendFlags send_ = SEND_NOTHING; | |
| 267 webrtc::Call* const call_ = nullptr; | |
| 268 | |
| 269 // SSRC of unsignalled receive stream, or -1 if there isn't one. | |
| 270 int64_t default_recv_ssrc_ = -1; | |
| 271 // Volume for unsignalled stream, which may be set before the stream exists. | |
| 272 double default_recv_volume_ = 1.0; | |
| 273 // Sink for unsignalled stream, which may be set before the stream exists. | |
| 274 rtc::scoped_ptr<webrtc::AudioSinkInterface> default_sink_; | |
| 275 // Default SSRC to use for RTCP receiver reports in case of no signaled | |
| 276 // send streams. See: https://code.google.com/p/webrtc/issues/detail?id=4740 | |
| 277 // and https://code.google.com/p/chromium/issues/detail?id=547661 | |
| 278 uint32_t receiver_reports_ssrc_ = 0xFA17FA17u; | |
| 279 | |
| 280 class WebRtcAudioSendStream; | |
| 281 std::map<uint32_t, WebRtcAudioSendStream*> send_streams_; | |
| 282 std::vector<webrtc::RtpExtension> send_rtp_extensions_; | |
| 283 | |
| 284 class WebRtcAudioReceiveStream; | |
| 285 std::map<uint32_t, WebRtcAudioReceiveStream*> recv_streams_; | |
| 286 std::vector<webrtc::RtpExtension> recv_rtp_extensions_; | |
| 287 | |
| 288 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcVoiceMediaChannel); | |
| 289 }; | |
| 290 } // namespace cricket | |
| 291 | |
| 292 #endif // TALK_MEDIA_WEBRTCVOICEENGINE_H_ | |
| OLD | NEW |