henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | #ifdef HAVE_CONFIG_H |
| 29 | #include <config.h> |
| 30 | #endif |
| 31 | |
| 32 | #ifdef HAVE_WEBRTC_VOICE |
| 33 | |
| 34 | #include "talk/media/webrtc/webrtcvoiceengine.h" |
| 35 | |
| 36 | #include <algorithm> |
| 37 | #include <cstdio> |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "talk/base/base64.h" |
| 42 | #include "talk/base/byteorder.h" |
| 43 | #include "talk/base/common.h" |
| 44 | #include "talk/base/helpers.h" |
| 45 | #include "talk/base/logging.h" |
| 46 | #include "talk/base/stringencode.h" |
| 47 | #include "talk/base/stringutils.h" |
| 48 | #include "talk/media/base/audiorenderer.h" |
| 49 | #include "talk/media/base/constants.h" |
| 50 | #include "talk/media/base/streamparams.h" |
| 51 | #include "talk/media/base/voiceprocessor.h" |
| 52 | #include "talk/media/webrtc/webrtcvoe.h" |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 53 | #include "webrtc/common.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 54 | #include "webrtc/modules/audio_processing/include/audio_processing.h" |
| 55 | |
| 56 | #ifdef WIN32 |
| 57 | #include <objbase.h> // NOLINT |
| 58 | #endif |
| 59 | |
| 60 | namespace cricket { |
| 61 | |
| 62 | struct CodecPref { |
| 63 | const char* name; |
| 64 | int clockrate; |
| 65 | int channels; |
| 66 | int payload_type; |
| 67 | bool is_multi_rate; |
| 68 | }; |
| 69 | |
| 70 | static const CodecPref kCodecPrefs[] = { |
| 71 | { "OPUS", 48000, 2, 111, true }, |
| 72 | { "ISAC", 16000, 1, 103, true }, |
| 73 | { "ISAC", 32000, 1, 104, true }, |
| 74 | { "CELT", 32000, 1, 109, true }, |
| 75 | { "CELT", 32000, 2, 110, true }, |
| 76 | { "G722", 16000, 1, 9, false }, |
| 77 | { "ILBC", 8000, 1, 102, false }, |
| 78 | { "PCMU", 8000, 1, 0, false }, |
| 79 | { "PCMA", 8000, 1, 8, false }, |
| 80 | { "CN", 48000, 1, 107, false }, |
| 81 | { "CN", 32000, 1, 106, false }, |
| 82 | { "CN", 16000, 1, 105, false }, |
| 83 | { "CN", 8000, 1, 13, false }, |
| 84 | { "red", 8000, 1, 127, false }, |
| 85 | { "telephone-event", 8000, 1, 126, false }, |
| 86 | }; |
| 87 | |
| 88 | // For Linux/Mac, using the default device is done by specifying index 0 for |
| 89 | // VoE 4.0 and not -1 (which was the case for VoE 3.5). |
| 90 | // |
| 91 | // On Windows Vista and newer, Microsoft introduced the concept of "Default |
| 92 | // Communications Device". This means that there are two types of default |
| 93 | // devices (old Wave Audio style default and Default Communications Device). |
| 94 | // |
| 95 | // On Windows systems which only support Wave Audio style default, uses either |
| 96 | // -1 or 0 to select the default device. |
| 97 | // |
| 98 | // On Windows systems which support both "Default Communication Device" and |
| 99 | // old Wave Audio style default, use -1 for Default Communications Device and |
| 100 | // -2 for Wave Audio style default, which is what we want to use for clips. |
| 101 | // It's not clear yet whether the -2 index is handled properly on other OSes. |
| 102 | |
| 103 | #ifdef WIN32 |
| 104 | static const int kDefaultAudioDeviceId = -1; |
| 105 | static const int kDefaultSoundclipDeviceId = -2; |
| 106 | #else |
| 107 | static const int kDefaultAudioDeviceId = 0; |
| 108 | #endif |
| 109 | |
| 110 | // extension header for audio levels, as defined in |
| 111 | // http://tools.ietf.org/html/draft-ietf-avtext-client-to-mixer-audio-level-03 |
| 112 | static const char kRtpAudioLevelHeaderExtension[] = |
| 113 | "urn:ietf:params:rtp-hdrext:ssrc-audio-level"; |
| 114 | static const int kRtpAudioLevelHeaderExtensionId = 1; |
| 115 | |
| 116 | static const char kIsacCodecName[] = "ISAC"; |
| 117 | static const char kL16CodecName[] = "L16"; |
| 118 | // Codec parameters for Opus. |
| 119 | static const int kOpusMonoBitrate = 32000; |
| 120 | // Parameter used for NACK. |
| 121 | // This value is equivalent to 5 seconds of audio data at 20 ms per packet. |
| 122 | static const int kNackMaxPackets = 250; |
| 123 | static const int kOpusStereoBitrate = 64000; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 124 | // draft-spittka-payload-rtp-opus-03 |
| 125 | // Opus bitrate should be in the range between 6000 and 510000. |
| 126 | static const int kOpusMinBitrate = 6000; |
| 127 | static const int kOpusMaxBitrate = 510000; |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 128 | // Default audio dscp value. |
| 129 | // See http://tools.ietf.org/html/rfc2474 for details. |
| 130 | // See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00 |
| 131 | static const talk_base::DiffServCodePoint kAudioDscpValue = talk_base::DSCP_EF; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 132 | |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 133 | // Ensure we open the file in a writeable path on ChromeOS and Android. This |
| 134 | // workaround can be removed when it's possible to specify a filename for audio |
| 135 | // option based AEC dumps. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 136 | // |
| 137 | // TODO(grunell): Use a string in the options instead of hardcoding it here |
| 138 | // and let the embedder choose the filename (crbug.com/264223). |
| 139 | // |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 140 | // NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified |
| 141 | // below. |
| 142 | #if defined(CHROMEOS) |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 143 | static const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump"; |
sergeyu@chromium.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 144 | #elif defined(ANDROID) |
| 145 | static const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump"; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 146 | #else |
| 147 | static const char kAecDumpByAudioOptionFilename[] = "audio.aecdump"; |
| 148 | #endif |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 149 | |
| 150 | // Dumps an AudioCodec in RFC 2327-ish format. |
| 151 | static std::string ToString(const AudioCodec& codec) { |
| 152 | std::stringstream ss; |
| 153 | ss << codec.name << "/" << codec.clockrate << "/" << codec.channels |
| 154 | << " (" << codec.id << ")"; |
| 155 | return ss.str(); |
| 156 | } |
| 157 | static std::string ToString(const webrtc::CodecInst& codec) { |
| 158 | std::stringstream ss; |
| 159 | ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels |
| 160 | << " (" << codec.pltype << ")"; |
| 161 | return ss.str(); |
| 162 | } |
| 163 | |
| 164 | static void LogMultiline(talk_base::LoggingSeverity sev, char* text) { |
| 165 | const char* delim = "\r\n"; |
| 166 | for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) { |
| 167 | LOG_V(sev) << tok; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Severity is an integer because it comes is assumed to be from command line. |
| 172 | static int SeverityToFilter(int severity) { |
| 173 | int filter = webrtc::kTraceNone; |
| 174 | switch (severity) { |
| 175 | case talk_base::LS_VERBOSE: |
| 176 | filter |= webrtc::kTraceAll; |
| 177 | case talk_base::LS_INFO: |
| 178 | filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo); |
| 179 | case talk_base::LS_WARNING: |
| 180 | filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning); |
| 181 | case talk_base::LS_ERROR: |
| 182 | filter |= (webrtc::kTraceError | webrtc::kTraceCritical); |
| 183 | } |
| 184 | return filter; |
| 185 | } |
| 186 | |
| 187 | static bool IsCodecMultiRate(const webrtc::CodecInst& codec) { |
| 188 | for (size_t i = 0; i < ARRAY_SIZE(kCodecPrefs); ++i) { |
| 189 | if (_stricmp(kCodecPrefs[i].name, codec.plname) == 0 && |
| 190 | kCodecPrefs[i].clockrate == codec.plfreq) { |
| 191 | return kCodecPrefs[i].is_multi_rate; |
| 192 | } |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | static bool FindCodec(const std::vector<AudioCodec>& codecs, |
| 198 | const AudioCodec& codec, |
| 199 | AudioCodec* found_codec) { |
| 200 | for (std::vector<AudioCodec>::const_iterator it = codecs.begin(); |
| 201 | it != codecs.end(); ++it) { |
| 202 | if (it->Matches(codec)) { |
| 203 | if (found_codec != NULL) { |
| 204 | *found_codec = *it; |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | } |
| 209 | return false; |
| 210 | } |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 211 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 212 | static bool IsNackEnabled(const AudioCodec& codec) { |
| 213 | return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack, |
| 214 | kParamValueEmpty)); |
| 215 | } |
| 216 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 217 | // Gets the default set of options applied to the engine. Historically, these |
| 218 | // were supplied as a combination of flags from the channel manager (ec, agc, |
| 219 | // ns, and highpass) and the rest hardcoded in InitInternal. |
| 220 | static AudioOptions GetDefaultEngineOptions() { |
| 221 | AudioOptions options; |
| 222 | options.echo_cancellation.Set(true); |
| 223 | options.auto_gain_control.Set(true); |
| 224 | options.noise_suppression.Set(true); |
| 225 | options.highpass_filter.Set(true); |
| 226 | options.stereo_swapping.Set(false); |
| 227 | options.typing_detection.Set(true); |
| 228 | options.conference_mode.Set(false); |
| 229 | options.adjust_agc_delta.Set(0); |
| 230 | options.experimental_agc.Set(false); |
| 231 | options.experimental_aec.Set(false); |
| 232 | options.aec_dump.Set(false); |
| 233 | return options; |
| 234 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | |
| 236 | class WebRtcSoundclipMedia : public SoundclipMedia { |
| 237 | public: |
| 238 | explicit WebRtcSoundclipMedia(WebRtcVoiceEngine *engine) |
| 239 | : engine_(engine), webrtc_channel_(-1) { |
| 240 | engine_->RegisterSoundclip(this); |
| 241 | } |
| 242 | |
| 243 | virtual ~WebRtcSoundclipMedia() { |
| 244 | engine_->UnregisterSoundclip(this); |
| 245 | if (webrtc_channel_ != -1) { |
| 246 | // We shouldn't have to call Disable() here. DeleteChannel() should call |
| 247 | // StopPlayout() while deleting the channel. We should fix the bug |
| 248 | // inside WebRTC and remove the Disable() call bellow. This work is |
| 249 | // tracked by bug http://b/issue?id=5382855. |
| 250 | PlaySound(NULL, 0, 0); |
| 251 | Disable(); |
| 252 | if (engine_->voe_sc()->base()->DeleteChannel(webrtc_channel_) |
| 253 | == -1) { |
| 254 | LOG_RTCERR1(DeleteChannel, webrtc_channel_); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | bool Init() { |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 260 | if (!engine_->voe_sc()) { |
| 261 | return false; |
| 262 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 263 | webrtc_channel_ = engine_->voe_sc()->base()->CreateChannel(); |
| 264 | if (webrtc_channel_ == -1) { |
| 265 | LOG_RTCERR0(CreateChannel); |
| 266 | return false; |
| 267 | } |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | bool Enable() { |
| 272 | if (engine_->voe_sc()->base()->StartPlayout(webrtc_channel_) == -1) { |
| 273 | LOG_RTCERR1(StartPlayout, webrtc_channel_); |
| 274 | return false; |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool Disable() { |
| 280 | if (engine_->voe_sc()->base()->StopPlayout(webrtc_channel_) == -1) { |
| 281 | LOG_RTCERR1(StopPlayout, webrtc_channel_); |
| 282 | return false; |
| 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | virtual bool PlaySound(const char *buf, int len, int flags) { |
| 288 | // The voe file api is not available in chrome. |
| 289 | if (!engine_->voe_sc()->file()) { |
| 290 | return false; |
| 291 | } |
| 292 | // Must stop playing the current sound (if any), because we are about to |
| 293 | // modify the stream. |
| 294 | if (engine_->voe_sc()->file()->StopPlayingFileLocally(webrtc_channel_) |
| 295 | == -1) { |
| 296 | LOG_RTCERR1(StopPlayingFileLocally, webrtc_channel_); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | if (buf) { |
| 301 | stream_.reset(new WebRtcSoundclipStream(buf, len)); |
| 302 | stream_->set_loop((flags & SF_LOOP) != 0); |
| 303 | stream_->Rewind(); |
| 304 | |
| 305 | // Play it. |
| 306 | if (engine_->voe_sc()->file()->StartPlayingFileLocally( |
| 307 | webrtc_channel_, stream_.get()) == -1) { |
| 308 | LOG_RTCERR2(StartPlayingFileLocally, webrtc_channel_, stream_.get()); |
| 309 | LOG(LS_ERROR) << "Unable to start soundclip"; |
| 310 | return false; |
| 311 | } |
| 312 | } else { |
| 313 | stream_.reset(); |
| 314 | } |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | int GetLastEngineError() const { return engine_->voe_sc()->error(); } |
| 319 | |
| 320 | private: |
| 321 | WebRtcVoiceEngine *engine_; |
| 322 | int webrtc_channel_; |
| 323 | talk_base::scoped_ptr<WebRtcSoundclipStream> stream_; |
| 324 | }; |
| 325 | |
| 326 | WebRtcVoiceEngine::WebRtcVoiceEngine() |
| 327 | : voe_wrapper_(new VoEWrapper()), |
| 328 | voe_wrapper_sc_(new VoEWrapper()), |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 329 | voe_wrapper_sc_initialized_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 330 | tracing_(new VoETraceWrapper()), |
| 331 | adm_(NULL), |
| 332 | adm_sc_(NULL), |
| 333 | log_filter_(SeverityToFilter(kDefaultLogSeverity)), |
| 334 | is_dumping_aec_(false), |
| 335 | desired_local_monitor_enable_(false), |
| 336 | tx_processor_ssrc_(0), |
| 337 | rx_processor_ssrc_(0) { |
| 338 | Construct(); |
| 339 | } |
| 340 | |
| 341 | WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper, |
| 342 | VoEWrapper* voe_wrapper_sc, |
| 343 | VoETraceWrapper* tracing) |
| 344 | : voe_wrapper_(voe_wrapper), |
| 345 | voe_wrapper_sc_(voe_wrapper_sc), |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 346 | voe_wrapper_sc_initialized_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 347 | tracing_(tracing), |
| 348 | adm_(NULL), |
| 349 | adm_sc_(NULL), |
| 350 | log_filter_(SeverityToFilter(kDefaultLogSeverity)), |
| 351 | is_dumping_aec_(false), |
| 352 | desired_local_monitor_enable_(false), |
| 353 | tx_processor_ssrc_(0), |
| 354 | rx_processor_ssrc_(0) { |
| 355 | Construct(); |
| 356 | } |
| 357 | |
| 358 | void WebRtcVoiceEngine::Construct() { |
| 359 | SetTraceFilter(log_filter_); |
| 360 | initialized_ = false; |
| 361 | LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine"; |
| 362 | SetTraceOptions(""); |
| 363 | if (tracing_->SetTraceCallback(this) == -1) { |
| 364 | LOG_RTCERR0(SetTraceCallback); |
| 365 | } |
| 366 | if (voe_wrapper_->base()->RegisterVoiceEngineObserver(*this) == -1) { |
| 367 | LOG_RTCERR0(RegisterVoiceEngineObserver); |
| 368 | } |
| 369 | // Clear the default agc state. |
| 370 | memset(&default_agc_config_, 0, sizeof(default_agc_config_)); |
| 371 | |
| 372 | // Load our audio codec list. |
| 373 | ConstructCodecs(); |
| 374 | |
| 375 | // Load our RTP Header extensions. |
| 376 | rtp_header_extensions_.push_back( |
| 377 | RtpHeaderExtension(kRtpAudioLevelHeaderExtension, |
| 378 | kRtpAudioLevelHeaderExtensionId)); |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 379 | options_ = GetDefaultEngineOptions(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | static bool IsOpus(const AudioCodec& codec) { |
| 383 | return (_stricmp(codec.name.c_str(), kOpusCodecName) == 0); |
| 384 | } |
| 385 | |
| 386 | static bool IsIsac(const AudioCodec& codec) { |
| 387 | return (_stricmp(codec.name.c_str(), kIsacCodecName) == 0); |
| 388 | } |
| 389 | |
| 390 | // True if params["stereo"] == "1" |
| 391 | static bool IsOpusStereoEnabled(const AudioCodec& codec) { |
| 392 | CodecParameterMap::const_iterator param = |
| 393 | codec.params.find(kCodecParamStereo); |
| 394 | if (param == codec.params.end()) { |
| 395 | return false; |
| 396 | } |
| 397 | return param->second == kParamValueTrue; |
| 398 | } |
| 399 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 400 | static bool IsValidOpusBitrate(int bitrate) { |
| 401 | return (bitrate >= kOpusMinBitrate && bitrate <= kOpusMaxBitrate); |
| 402 | } |
| 403 | |
| 404 | // Returns 0 if params[kCodecParamMaxAverageBitrate] is not defined or invalid. |
| 405 | // Returns the value of params[kCodecParamMaxAverageBitrate] otherwise. |
| 406 | static int GetOpusBitrateFromParams(const AudioCodec& codec) { |
| 407 | int bitrate = 0; |
| 408 | if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) { |
| 409 | return 0; |
| 410 | } |
| 411 | if (!IsValidOpusBitrate(bitrate)) { |
| 412 | LOG(LS_WARNING) << "Codec parameter \"maxaveragebitrate\" has an " |
| 413 | << "invalid value: " << bitrate; |
| 414 | return 0; |
| 415 | } |
| 416 | return bitrate; |
| 417 | } |
| 418 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 419 | void WebRtcVoiceEngine::ConstructCodecs() { |
| 420 | LOG(LS_INFO) << "WebRtc VoiceEngine codecs:"; |
| 421 | int ncodecs = voe_wrapper_->codec()->NumOfCodecs(); |
| 422 | for (int i = 0; i < ncodecs; ++i) { |
| 423 | webrtc::CodecInst voe_codec; |
| 424 | if (voe_wrapper_->codec()->GetCodec(i, voe_codec) != -1) { |
| 425 | // Skip uncompressed formats. |
| 426 | if (_stricmp(voe_codec.plname, kL16CodecName) == 0) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | const CodecPref* pref = NULL; |
| 431 | for (size_t j = 0; j < ARRAY_SIZE(kCodecPrefs); ++j) { |
| 432 | if (_stricmp(kCodecPrefs[j].name, voe_codec.plname) == 0 && |
| 433 | kCodecPrefs[j].clockrate == voe_codec.plfreq && |
| 434 | kCodecPrefs[j].channels == voe_codec.channels) { |
| 435 | pref = &kCodecPrefs[j]; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (pref) { |
| 441 | // Use the payload type that we've configured in our pref table; |
| 442 | // use the offset in our pref table to determine the sort order. |
| 443 | AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq, |
| 444 | voe_codec.rate, voe_codec.channels, |
| 445 | ARRAY_SIZE(kCodecPrefs) - (pref - kCodecPrefs)); |
| 446 | LOG(LS_INFO) << ToString(codec); |
| 447 | if (IsIsac(codec)) { |
| 448 | // Indicate auto-bandwidth in signaling. |
| 449 | codec.bitrate = 0; |
| 450 | } |
| 451 | if (IsOpus(codec)) { |
| 452 | // Only add fmtp parameters that differ from the spec. |
| 453 | if (kPreferredMinPTime != kOpusDefaultMinPTime) { |
| 454 | codec.params[kCodecParamMinPTime] = |
| 455 | talk_base::ToString(kPreferredMinPTime); |
| 456 | } |
| 457 | if (kPreferredMaxPTime != kOpusDefaultMaxPTime) { |
| 458 | codec.params[kCodecParamMaxPTime] = |
| 459 | talk_base::ToString(kPreferredMaxPTime); |
| 460 | } |
| 461 | // TODO(hellner): Add ptime, sprop-stereo, stereo and useinbandfec |
| 462 | // when they can be set to values other than the default. |
| 463 | } |
| 464 | codecs_.push_back(codec); |
| 465 | } else { |
| 466 | LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | // Make sure they are in local preference order. |
| 471 | std::sort(codecs_.begin(), codecs_.end(), &AudioCodec::Preferable); |
| 472 | } |
| 473 | |
| 474 | WebRtcVoiceEngine::~WebRtcVoiceEngine() { |
| 475 | LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine"; |
| 476 | if (voe_wrapper_->base()->DeRegisterVoiceEngineObserver() == -1) { |
| 477 | LOG_RTCERR0(DeRegisterVoiceEngineObserver); |
| 478 | } |
| 479 | if (adm_) { |
| 480 | voe_wrapper_.reset(); |
| 481 | adm_->Release(); |
| 482 | adm_ = NULL; |
| 483 | } |
| 484 | if (adm_sc_) { |
| 485 | voe_wrapper_sc_.reset(); |
| 486 | adm_sc_->Release(); |
| 487 | adm_sc_ = NULL; |
| 488 | } |
| 489 | |
| 490 | // Test to see if the media processor was deregistered properly |
| 491 | ASSERT(SignalRxMediaFrame.is_empty()); |
| 492 | ASSERT(SignalTxMediaFrame.is_empty()); |
| 493 | |
| 494 | tracing_->SetTraceCallback(NULL); |
| 495 | } |
| 496 | |
| 497 | bool WebRtcVoiceEngine::Init(talk_base::Thread* worker_thread) { |
| 498 | LOG(LS_INFO) << "WebRtcVoiceEngine::Init"; |
| 499 | bool res = InitInternal(); |
| 500 | if (res) { |
| 501 | LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!"; |
| 502 | } else { |
| 503 | LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed"; |
| 504 | Terminate(); |
| 505 | } |
| 506 | return res; |
| 507 | } |
| 508 | |
| 509 | bool WebRtcVoiceEngine::InitInternal() { |
| 510 | // Temporarily turn logging level up for the Init call |
| 511 | int old_filter = log_filter_; |
| 512 | int extended_filter = log_filter_ | SeverityToFilter(talk_base::LS_INFO); |
| 513 | SetTraceFilter(extended_filter); |
| 514 | SetTraceOptions(""); |
| 515 | |
| 516 | // Init WebRtc VoiceEngine. |
| 517 | if (voe_wrapper_->base()->Init(adm_) == -1) { |
| 518 | LOG_RTCERR0_EX(Init, voe_wrapper_->error()); |
| 519 | SetTraceFilter(old_filter); |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | SetTraceFilter(old_filter); |
| 524 | SetTraceOptions(log_options_); |
| 525 | |
| 526 | // Log the VoiceEngine version info |
| 527 | char buffer[1024] = ""; |
| 528 | voe_wrapper_->base()->GetVersion(buffer); |
| 529 | LOG(LS_INFO) << "WebRtc VoiceEngine Version:"; |
| 530 | LogMultiline(talk_base::LS_INFO, buffer); |
| 531 | |
| 532 | // Save the default AGC configuration settings. This must happen before |
| 533 | // calling SetOptions or the default will be overwritten. |
| 534 | if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) { |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 535 | LOG_RTCERR0(GetAgcConfig); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 539 | // Set defaults for options, so that ApplyOptions applies them explicitly |
| 540 | // when we clear option (channel) overrides. External clients can still |
| 541 | // modify the defaults via SetOptions (on the media engine). |
| 542 | if (!SetOptions(GetDefaultEngineOptions())) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 543 | return false; |
| 544 | } |
| 545 | |
| 546 | // Print our codec list again for the call diagnostic log |
| 547 | LOG(LS_INFO) << "WebRtc VoiceEngine codecs:"; |
| 548 | for (std::vector<AudioCodec>::const_iterator it = codecs_.begin(); |
| 549 | it != codecs_.end(); ++it) { |
| 550 | LOG(LS_INFO) << ToString(*it); |
| 551 | } |
| 552 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 553 | // Disable the DTMF playout when a tone is sent. |
| 554 | // PlayDtmfTone will be used if local playout is needed. |
| 555 | if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) { |
| 556 | LOG_RTCERR1(SetDtmfFeedbackStatus, false); |
| 557 | } |
| 558 | |
| 559 | initialized_ = true; |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | bool WebRtcVoiceEngine::EnsureSoundclipEngineInit() { |
| 564 | if (voe_wrapper_sc_initialized_) { |
| 565 | return true; |
| 566 | } |
| 567 | // Note that, if initialization fails, voe_wrapper_sc_initialized_ will still |
| 568 | // be false, so subsequent calls to EnsureSoundclipEngineInit will |
| 569 | // probably just fail again. That's acceptable behavior. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 570 | #if defined(LINUX) && !defined(HAVE_LIBPULSE) |
| 571 | voe_wrapper_sc_->hw()->SetAudioDeviceLayer(webrtc::kAudioLinuxAlsa); |
| 572 | #endif |
| 573 | |
| 574 | // Initialize the VoiceEngine instance that we'll use to play out sound clips. |
| 575 | if (voe_wrapper_sc_->base()->Init(adm_sc_) == -1) { |
| 576 | LOG_RTCERR0_EX(Init, voe_wrapper_sc_->error()); |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | // On Windows, tell it to use the default sound (not communication) devices. |
| 581 | // First check whether there is a valid sound device for playback. |
| 582 | // TODO(juberti): Clean this up when we support setting the soundclip device. |
| 583 | #ifdef WIN32 |
| 584 | // The SetPlayoutDevice may not be implemented in the case of external ADM. |
| 585 | // TODO(ronghuawu): We should only check the adm_sc_ here, but current |
| 586 | // PeerConnection interface never set the adm_sc_, so need to check both |
| 587 | // in order to determine if the external adm is used. |
| 588 | if (!adm_ && !adm_sc_) { |
| 589 | int num_of_devices = 0; |
| 590 | if (voe_wrapper_sc_->hw()->GetNumOfPlayoutDevices(num_of_devices) != -1 && |
| 591 | num_of_devices > 0) { |
| 592 | if (voe_wrapper_sc_->hw()->SetPlayoutDevice(kDefaultSoundclipDeviceId) |
| 593 | == -1) { |
| 594 | LOG_RTCERR1_EX(SetPlayoutDevice, kDefaultSoundclipDeviceId, |
| 595 | voe_wrapper_sc_->error()); |
| 596 | return false; |
| 597 | } |
| 598 | } else { |
| 599 | LOG(LS_WARNING) << "No valid sound playout device found."; |
| 600 | } |
| 601 | } |
| 602 | #endif |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 603 | voe_wrapper_sc_initialized_ = true; |
| 604 | LOG(LS_INFO) << "Initialized WebRtc soundclip engine."; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 605 | return true; |
| 606 | } |
| 607 | |
| 608 | void WebRtcVoiceEngine::Terminate() { |
| 609 | LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate"; |
| 610 | initialized_ = false; |
| 611 | |
| 612 | StopAecDump(); |
| 613 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 614 | if (voe_wrapper_sc_) { |
| 615 | voe_wrapper_sc_initialized_ = false; |
| 616 | voe_wrapper_sc_->base()->Terminate(); |
| 617 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 618 | voe_wrapper_->base()->Terminate(); |
| 619 | desired_local_monitor_enable_ = false; |
| 620 | } |
| 621 | |
| 622 | int WebRtcVoiceEngine::GetCapabilities() { |
| 623 | return AUDIO_SEND | AUDIO_RECV; |
| 624 | } |
| 625 | |
| 626 | VoiceMediaChannel *WebRtcVoiceEngine::CreateChannel() { |
| 627 | WebRtcVoiceMediaChannel* ch = new WebRtcVoiceMediaChannel(this); |
| 628 | if (!ch->valid()) { |
| 629 | delete ch; |
| 630 | ch = NULL; |
| 631 | } |
| 632 | return ch; |
| 633 | } |
| 634 | |
| 635 | SoundclipMedia *WebRtcVoiceEngine::CreateSoundclip() { |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 636 | if (!EnsureSoundclipEngineInit()) { |
| 637 | LOG(LS_ERROR) << "Unable to create soundclip: soundclip engine failed to " |
| 638 | << "initialize."; |
| 639 | return NULL; |
| 640 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 641 | WebRtcSoundclipMedia *soundclip = new WebRtcSoundclipMedia(this); |
| 642 | if (!soundclip->Init() || !soundclip->Enable()) { |
| 643 | delete soundclip; |
| 644 | return NULL; |
| 645 | } |
| 646 | return soundclip; |
| 647 | } |
| 648 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 649 | bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 650 | if (!ApplyOptions(options)) { |
| 651 | return false; |
| 652 | } |
| 653 | options_ = options; |
| 654 | return true; |
| 655 | } |
| 656 | |
| 657 | bool WebRtcVoiceEngine::SetOptionOverrides(const AudioOptions& overrides) { |
| 658 | LOG(LS_INFO) << "Setting option overrides: " << overrides.ToString(); |
| 659 | if (!ApplyOptions(overrides)) { |
| 660 | return false; |
| 661 | } |
| 662 | option_overrides_ = overrides; |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | bool WebRtcVoiceEngine::ClearOptionOverrides() { |
| 667 | LOG(LS_INFO) << "Clearing option overrides."; |
| 668 | AudioOptions options = options_; |
| 669 | // Only call ApplyOptions if |options_overrides_| contains overrided options. |
| 670 | // ApplyOptions affects NS, AGC other options that is shared between |
| 671 | // all WebRtcVoiceEngineChannels. |
| 672 | if (option_overrides_ == AudioOptions()) { |
| 673 | return true; |
| 674 | } |
| 675 | |
| 676 | if (!ApplyOptions(options)) { |
| 677 | return false; |
| 678 | } |
| 679 | option_overrides_ = AudioOptions(); |
| 680 | return true; |
| 681 | } |
| 682 | |
| 683 | // AudioOptions defaults are set in InitInternal (for options with corresponding |
| 684 | // MediaEngineInterface flags) and in SetOptions(int) for flagless options. |
| 685 | bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) { |
| 686 | AudioOptions options = options_in; // The options are modified below. |
| 687 | // kEcConference is AEC with high suppression. |
| 688 | webrtc::EcModes ec_mode = webrtc::kEcConference; |
| 689 | webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone; |
| 690 | webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog; |
| 691 | webrtc::NsModes ns_mode = webrtc::kNsHighSuppression; |
| 692 | bool aecm_comfort_noise = false; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 693 | if (options.aecm_generate_comfort_noise.Get(&aecm_comfort_noise)) { |
| 694 | LOG(LS_VERBOSE) << "Comfort noise explicitly set to " |
| 695 | << aecm_comfort_noise << " (default is false)."; |
| 696 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 697 | |
| 698 | #if defined(IOS) |
| 699 | // On iOS, VPIO provides built-in EC and AGC. |
| 700 | options.echo_cancellation.Set(false); |
| 701 | options.auto_gain_control.Set(false); |
| 702 | #elif defined(ANDROID) |
| 703 | ec_mode = webrtc::kEcAecm; |
| 704 | #endif |
| 705 | |
| 706 | #if defined(IOS) || defined(ANDROID) |
| 707 | // Set the AGC mode for iOS as well despite disabling it above, to avoid |
| 708 | // unsupported configuration errors from webrtc. |
| 709 | agc_mode = webrtc::kAgcFixedDigital; |
| 710 | options.typing_detection.Set(false); |
| 711 | options.experimental_agc.Set(false); |
| 712 | options.experimental_aec.Set(false); |
| 713 | #endif |
| 714 | |
| 715 | LOG(LS_INFO) << "Applying audio options: " << options.ToString(); |
| 716 | |
| 717 | webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing(); |
| 718 | |
| 719 | bool echo_cancellation; |
| 720 | if (options.echo_cancellation.Get(&echo_cancellation)) { |
| 721 | if (voep->SetEcStatus(echo_cancellation, ec_mode) == -1) { |
| 722 | LOG_RTCERR2(SetEcStatus, echo_cancellation, ec_mode); |
| 723 | return false; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 724 | } else { |
| 725 | LOG(LS_VERBOSE) << "Echo control set to " << echo_cancellation |
| 726 | << " with mode " << ec_mode; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 727 | } |
| 728 | #if !defined(ANDROID) |
| 729 | // TODO(ajm): Remove the error return on Android from webrtc. |
| 730 | if (voep->SetEcMetricsStatus(echo_cancellation) == -1) { |
| 731 | LOG_RTCERR1(SetEcMetricsStatus, echo_cancellation); |
| 732 | return false; |
| 733 | } |
| 734 | #endif |
| 735 | if (ec_mode == webrtc::kEcAecm) { |
| 736 | if (voep->SetAecmMode(aecm_mode, aecm_comfort_noise) != 0) { |
| 737 | LOG_RTCERR2(SetAecmMode, aecm_mode, aecm_comfort_noise); |
| 738 | return false; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | bool auto_gain_control; |
| 744 | if (options.auto_gain_control.Get(&auto_gain_control)) { |
| 745 | if (voep->SetAgcStatus(auto_gain_control, agc_mode) == -1) { |
| 746 | LOG_RTCERR2(SetAgcStatus, auto_gain_control, agc_mode); |
| 747 | return false; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 748 | } else { |
| 749 | LOG(LS_VERBOSE) << "Auto gain set to " << auto_gain_control |
| 750 | << " with mode " << agc_mode; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (options.tx_agc_target_dbov.IsSet() || |
| 755 | options.tx_agc_digital_compression_gain.IsSet() || |
| 756 | options.tx_agc_limiter.IsSet()) { |
| 757 | // Override default_agc_config_. Generally, an unset option means "leave |
| 758 | // the VoE bits alone" in this function, so we want whatever is set to be |
| 759 | // stored as the new "default". If we didn't, then setting e.g. |
| 760 | // tx_agc_target_dbov would reset digital compression gain and limiter |
| 761 | // settings. |
| 762 | // Also, if we don't update default_agc_config_, then adjust_agc_delta |
| 763 | // would be an offset from the original values, and not whatever was set |
| 764 | // explicitly. |
| 765 | default_agc_config_.targetLeveldBOv = |
| 766 | options.tx_agc_target_dbov.GetWithDefaultIfUnset( |
| 767 | default_agc_config_.targetLeveldBOv); |
| 768 | default_agc_config_.digitalCompressionGaindB = |
| 769 | options.tx_agc_digital_compression_gain.GetWithDefaultIfUnset( |
| 770 | default_agc_config_.digitalCompressionGaindB); |
| 771 | default_agc_config_.limiterEnable = |
| 772 | options.tx_agc_limiter.GetWithDefaultIfUnset( |
| 773 | default_agc_config_.limiterEnable); |
| 774 | if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) { |
| 775 | LOG_RTCERR3(SetAgcConfig, |
| 776 | default_agc_config_.targetLeveldBOv, |
| 777 | default_agc_config_.digitalCompressionGaindB, |
| 778 | default_agc_config_.limiterEnable); |
| 779 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
| 783 | bool noise_suppression; |
| 784 | if (options.noise_suppression.Get(&noise_suppression)) { |
| 785 | if (voep->SetNsStatus(noise_suppression, ns_mode) == -1) { |
| 786 | LOG_RTCERR2(SetNsStatus, noise_suppression, ns_mode); |
| 787 | return false; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 788 | } else { |
| 789 | LOG(LS_VERBOSE) << "Noise suppression set to " << noise_suppression |
| 790 | << " with mode " << ns_mode; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
| 794 | bool highpass_filter; |
| 795 | if (options.highpass_filter.Get(&highpass_filter)) { |
| 796 | if (voep->EnableHighPassFilter(highpass_filter) == -1) { |
| 797 | LOG_RTCERR1(SetHighpassFilterStatus, highpass_filter); |
| 798 | return false; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | bool stereo_swapping; |
| 803 | if (options.stereo_swapping.Get(&stereo_swapping)) { |
| 804 | voep->EnableStereoChannelSwapping(stereo_swapping); |
| 805 | if (voep->IsStereoChannelSwappingEnabled() != stereo_swapping) { |
| 806 | LOG_RTCERR1(EnableStereoChannelSwapping, stereo_swapping); |
| 807 | return false; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | bool typing_detection; |
| 812 | if (options.typing_detection.Get(&typing_detection)) { |
| 813 | if (voep->SetTypingDetectionStatus(typing_detection) == -1) { |
| 814 | // In case of error, log the info and continue |
| 815 | LOG_RTCERR1(SetTypingDetectionStatus, typing_detection); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | int adjust_agc_delta; |
| 820 | if (options.adjust_agc_delta.Get(&adjust_agc_delta)) { |
| 821 | if (!AdjustAgcLevel(adjust_agc_delta)) { |
| 822 | return false; |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | bool aec_dump; |
| 827 | if (options.aec_dump.Get(&aec_dump)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 828 | if (aec_dump) |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 829 | StartAecDump(kAecDumpByAudioOptionFilename); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 830 | else |
| 831 | StopAecDump(); |
| 832 | } |
| 833 | |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 834 | bool experimental_aec; |
| 835 | if (options.experimental_aec.Get(&experimental_aec)) { |
| 836 | webrtc::AudioProcessing* audioproc = |
| 837 | voe_wrapper_->base()->audio_processing(); |
| 838 | // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine |
| 839 | // returns NULL on audio_processing(). |
| 840 | if (audioproc) { |
| 841 | webrtc::Config config; |
| 842 | config.Set<webrtc::DelayCorrection>( |
| 843 | new webrtc::DelayCorrection(experimental_aec)); |
| 844 | audioproc->SetExtraOptions(config); |
| 845 | } |
| 846 | } |
| 847 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 848 | uint32 recording_sample_rate; |
| 849 | if (options.recording_sample_rate.Get(&recording_sample_rate)) { |
| 850 | if (voe_wrapper_->hw()->SetRecordingSampleRate(recording_sample_rate)) { |
| 851 | LOG_RTCERR1(SetRecordingSampleRate, recording_sample_rate); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | uint32 playout_sample_rate; |
| 856 | if (options.playout_sample_rate.Get(&playout_sample_rate)) { |
| 857 | if (voe_wrapper_->hw()->SetPlayoutSampleRate(playout_sample_rate)) { |
| 858 | LOG_RTCERR1(SetPlayoutSampleRate, playout_sample_rate); |
| 859 | } |
| 860 | } |
| 861 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 862 | |
| 863 | return true; |
| 864 | } |
| 865 | |
| 866 | bool WebRtcVoiceEngine::SetDelayOffset(int offset) { |
| 867 | voe_wrapper_->processing()->SetDelayOffsetMs(offset); |
| 868 | if (voe_wrapper_->processing()->DelayOffsetMs() != offset) { |
| 869 | LOG_RTCERR1(SetDelayOffsetMs, offset); |
| 870 | return false; |
| 871 | } |
| 872 | |
| 873 | return true; |
| 874 | } |
| 875 | |
| 876 | struct ResumeEntry { |
| 877 | ResumeEntry(WebRtcVoiceMediaChannel *c, bool p, SendFlags s) |
| 878 | : channel(c), |
| 879 | playout(p), |
| 880 | send(s) { |
| 881 | } |
| 882 | |
| 883 | WebRtcVoiceMediaChannel *channel; |
| 884 | bool playout; |
| 885 | SendFlags send; |
| 886 | }; |
| 887 | |
| 888 | // TODO(juberti): Refactor this so that the core logic can be used to set the |
| 889 | // soundclip device. At that time, reinstate the soundclip pause/resume code. |
| 890 | bool WebRtcVoiceEngine::SetDevices(const Device* in_device, |
| 891 | const Device* out_device) { |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 892 | #if !defined(IOS) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 893 | int in_id = in_device ? talk_base::FromString<int>(in_device->id) : |
| 894 | kDefaultAudioDeviceId; |
| 895 | int out_id = out_device ? talk_base::FromString<int>(out_device->id) : |
| 896 | kDefaultAudioDeviceId; |
| 897 | // The device manager uses -1 as the default device, which was the case for |
| 898 | // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac. |
| 899 | #ifndef WIN32 |
| 900 | if (-1 == in_id) { |
| 901 | in_id = kDefaultAudioDeviceId; |
| 902 | } |
| 903 | if (-1 == out_id) { |
| 904 | out_id = kDefaultAudioDeviceId; |
| 905 | } |
| 906 | #endif |
| 907 | |
| 908 | std::string in_name = (in_id != kDefaultAudioDeviceId) ? |
| 909 | in_device->name : "Default device"; |
| 910 | std::string out_name = (out_id != kDefaultAudioDeviceId) ? |
| 911 | out_device->name : "Default device"; |
| 912 | LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name |
| 913 | << ") and speaker to (id=" << out_id << ", name=" << out_name |
| 914 | << ")"; |
| 915 | |
| 916 | // If we're running the local monitor, we need to stop it first. |
| 917 | bool ret = true; |
| 918 | if (!PauseLocalMonitor()) { |
| 919 | LOG(LS_WARNING) << "Failed to pause local monitor"; |
| 920 | ret = false; |
| 921 | } |
| 922 | |
| 923 | // Must also pause all audio playback and capture. |
| 924 | for (ChannelList::const_iterator i = channels_.begin(); |
| 925 | i != channels_.end(); ++i) { |
| 926 | WebRtcVoiceMediaChannel *channel = *i; |
| 927 | if (!channel->PausePlayout()) { |
| 928 | LOG(LS_WARNING) << "Failed to pause playout"; |
| 929 | ret = false; |
| 930 | } |
| 931 | if (!channel->PauseSend()) { |
| 932 | LOG(LS_WARNING) << "Failed to pause send"; |
| 933 | ret = false; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | // Find the recording device id in VoiceEngine and set recording device. |
| 938 | if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) { |
| 939 | ret = false; |
| 940 | } |
| 941 | if (ret) { |
| 942 | if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) { |
| 943 | LOG_RTCERR2(SetRecordingDevice, in_device->name, in_id); |
| 944 | ret = false; |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // Find the playout device id in VoiceEngine and set playout device. |
| 949 | if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) { |
| 950 | LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name; |
| 951 | ret = false; |
| 952 | } |
| 953 | if (ret) { |
| 954 | if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) { |
| 955 | LOG_RTCERR2(SetPlayoutDevice, out_device->name, out_id); |
| 956 | ret = false; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | // Resume all audio playback and capture. |
| 961 | for (ChannelList::const_iterator i = channels_.begin(); |
| 962 | i != channels_.end(); ++i) { |
| 963 | WebRtcVoiceMediaChannel *channel = *i; |
| 964 | if (!channel->ResumePlayout()) { |
| 965 | LOG(LS_WARNING) << "Failed to resume playout"; |
| 966 | ret = false; |
| 967 | } |
| 968 | if (!channel->ResumeSend()) { |
| 969 | LOG(LS_WARNING) << "Failed to resume send"; |
| 970 | ret = false; |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | // Resume local monitor. |
| 975 | if (!ResumeLocalMonitor()) { |
| 976 | LOG(LS_WARNING) << "Failed to resume local monitor"; |
| 977 | ret = false; |
| 978 | } |
| 979 | |
| 980 | if (ret) { |
| 981 | LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name |
| 982 | << ") and speaker to (id="<< out_id << " name=" << out_name |
| 983 | << ")"; |
| 984 | } |
| 985 | |
| 986 | return ret; |
| 987 | #else |
| 988 | return true; |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 989 | #endif // !IOS |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId( |
| 993 | bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) { |
| 994 | // In Linux, VoiceEngine uses the same device dev_id as the device manager. |
wu@webrtc.org | cecfd18 | 2013-10-30 05:18:12 +0000 | [diff] [blame] | 995 | #if defined(LINUX) || defined(ANDROID) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 996 | *rtc_id = dev_id; |
| 997 | return true; |
| 998 | #else |
| 999 | // In Windows and Mac, we need to find the VoiceEngine device id by name |
| 1000 | // unless the input dev_id is the default device id. |
| 1001 | if (kDefaultAudioDeviceId == dev_id) { |
| 1002 | *rtc_id = dev_id; |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
| 1006 | // Get the number of VoiceEngine audio devices. |
| 1007 | int count = 0; |
| 1008 | if (is_input) { |
| 1009 | if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) { |
| 1010 | LOG_RTCERR0(GetNumOfRecordingDevices); |
| 1011 | return false; |
| 1012 | } |
| 1013 | } else { |
| 1014 | if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) { |
| 1015 | LOG_RTCERR0(GetNumOfPlayoutDevices); |
| 1016 | return false; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | for (int i = 0; i < count; ++i) { |
| 1021 | char name[128]; |
| 1022 | char guid[128]; |
| 1023 | if (is_input) { |
| 1024 | voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid); |
| 1025 | LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name; |
| 1026 | } else { |
| 1027 | voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid); |
| 1028 | LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name; |
| 1029 | } |
| 1030 | |
| 1031 | std::string webrtc_name(name); |
| 1032 | if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) { |
| 1033 | *rtc_id = i; |
| 1034 | return true; |
| 1035 | } |
| 1036 | } |
| 1037 | LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name; |
| 1038 | return false; |
| 1039 | #endif |
| 1040 | } |
| 1041 | |
| 1042 | bool WebRtcVoiceEngine::GetOutputVolume(int* level) { |
| 1043 | unsigned int ulevel; |
| 1044 | if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) { |
| 1045 | LOG_RTCERR1(GetSpeakerVolume, level); |
| 1046 | return false; |
| 1047 | } |
| 1048 | *level = ulevel; |
| 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | bool WebRtcVoiceEngine::SetOutputVolume(int level) { |
| 1053 | ASSERT(level >= 0 && level <= 255); |
| 1054 | if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) { |
| 1055 | LOG_RTCERR1(SetSpeakerVolume, level); |
| 1056 | return false; |
| 1057 | } |
| 1058 | return true; |
| 1059 | } |
| 1060 | |
| 1061 | int WebRtcVoiceEngine::GetInputLevel() { |
| 1062 | unsigned int ulevel; |
| 1063 | return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ? |
| 1064 | static_cast<int>(ulevel) : -1; |
| 1065 | } |
| 1066 | |
| 1067 | bool WebRtcVoiceEngine::SetLocalMonitor(bool enable) { |
| 1068 | desired_local_monitor_enable_ = enable; |
| 1069 | return ChangeLocalMonitor(desired_local_monitor_enable_); |
| 1070 | } |
| 1071 | |
| 1072 | bool WebRtcVoiceEngine::ChangeLocalMonitor(bool enable) { |
| 1073 | // The voe file api is not available in chrome. |
| 1074 | if (!voe_wrapper_->file()) { |
| 1075 | return false; |
| 1076 | } |
| 1077 | if (enable && !monitor_) { |
| 1078 | monitor_.reset(new WebRtcMonitorStream); |
| 1079 | if (voe_wrapper_->file()->StartRecordingMicrophone(monitor_.get()) == -1) { |
| 1080 | LOG_RTCERR1(StartRecordingMicrophone, monitor_.get()); |
| 1081 | // Must call Stop() because there are some cases where Start will report |
| 1082 | // failure but still change the state, and if we leave VE in the on state |
| 1083 | // then it could crash later when trying to invoke methods on our monitor. |
| 1084 | voe_wrapper_->file()->StopRecordingMicrophone(); |
| 1085 | monitor_.reset(); |
| 1086 | return false; |
| 1087 | } |
| 1088 | } else if (!enable && monitor_) { |
| 1089 | voe_wrapper_->file()->StopRecordingMicrophone(); |
| 1090 | monitor_.reset(); |
| 1091 | } |
| 1092 | return true; |
| 1093 | } |
| 1094 | |
| 1095 | bool WebRtcVoiceEngine::PauseLocalMonitor() { |
| 1096 | return ChangeLocalMonitor(false); |
| 1097 | } |
| 1098 | |
| 1099 | bool WebRtcVoiceEngine::ResumeLocalMonitor() { |
| 1100 | return ChangeLocalMonitor(desired_local_monitor_enable_); |
| 1101 | } |
| 1102 | |
| 1103 | const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() { |
| 1104 | return codecs_; |
| 1105 | } |
| 1106 | |
| 1107 | bool WebRtcVoiceEngine::FindCodec(const AudioCodec& in) { |
| 1108 | return FindWebRtcCodec(in, NULL); |
| 1109 | } |
| 1110 | |
| 1111 | // Get the VoiceEngine codec that matches |in|, with the supplied settings. |
| 1112 | bool WebRtcVoiceEngine::FindWebRtcCodec(const AudioCodec& in, |
| 1113 | webrtc::CodecInst* out) { |
| 1114 | int ncodecs = voe_wrapper_->codec()->NumOfCodecs(); |
| 1115 | for (int i = 0; i < ncodecs; ++i) { |
| 1116 | webrtc::CodecInst voe_codec; |
| 1117 | if (voe_wrapper_->codec()->GetCodec(i, voe_codec) != -1) { |
| 1118 | AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq, |
| 1119 | voe_codec.rate, voe_codec.channels, 0); |
| 1120 | bool multi_rate = IsCodecMultiRate(voe_codec); |
| 1121 | // Allow arbitrary rates for ISAC to be specified. |
| 1122 | if (multi_rate) { |
| 1123 | // Set codec.bitrate to 0 so the check for codec.Matches() passes. |
| 1124 | codec.bitrate = 0; |
| 1125 | } |
| 1126 | if (codec.Matches(in)) { |
| 1127 | if (out) { |
| 1128 | // Fixup the payload type. |
| 1129 | voe_codec.pltype = in.id; |
| 1130 | |
| 1131 | // Set bitrate if specified. |
| 1132 | if (multi_rate && in.bitrate != 0) { |
| 1133 | voe_codec.rate = in.bitrate; |
| 1134 | } |
| 1135 | |
| 1136 | // Apply codec-specific settings. |
| 1137 | if (IsIsac(codec)) { |
| 1138 | // If ISAC and an explicit bitrate is not specified, |
| 1139 | // enable auto bandwidth adjustment. |
| 1140 | voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1; |
| 1141 | } |
| 1142 | *out = voe_codec; |
| 1143 | } |
| 1144 | return true; |
| 1145 | } |
| 1146 | } |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | const std::vector<RtpHeaderExtension>& |
| 1151 | WebRtcVoiceEngine::rtp_header_extensions() const { |
| 1152 | return rtp_header_extensions_; |
| 1153 | } |
| 1154 | |
| 1155 | void WebRtcVoiceEngine::SetLogging(int min_sev, const char* filter) { |
| 1156 | // if min_sev == -1, we keep the current log level. |
| 1157 | if (min_sev >= 0) { |
| 1158 | SetTraceFilter(SeverityToFilter(min_sev)); |
| 1159 | } |
| 1160 | log_options_ = filter; |
| 1161 | SetTraceOptions(initialized_ ? log_options_ : ""); |
| 1162 | } |
| 1163 | |
| 1164 | int WebRtcVoiceEngine::GetLastEngineError() { |
| 1165 | return voe_wrapper_->error(); |
| 1166 | } |
| 1167 | |
| 1168 | void WebRtcVoiceEngine::SetTraceFilter(int filter) { |
| 1169 | log_filter_ = filter; |
| 1170 | tracing_->SetTraceFilter(filter); |
| 1171 | } |
| 1172 | |
| 1173 | // We suppport three different logging settings for VoiceEngine: |
| 1174 | // 1. Observer callback that goes into talk diagnostic logfile. |
| 1175 | // Use --logfile and --loglevel |
| 1176 | // |
| 1177 | // 2. Encrypted VoiceEngine log for debugging VoiceEngine. |
| 1178 | // Use --voice_loglevel --voice_logfilter "tracefile file_name" |
| 1179 | // |
| 1180 | // 3. EC log and dump for debugging QualityEngine. |
| 1181 | // Use --voice_loglevel --voice_logfilter "recordEC file_name" |
| 1182 | // |
| 1183 | // For more details see: "https://sites.google.com/a/google.com/wavelet/Home/ |
| 1184 | // Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters" |
| 1185 | void WebRtcVoiceEngine::SetTraceOptions(const std::string& options) { |
| 1186 | // Set encrypted trace file. |
| 1187 | std::vector<std::string> opts; |
| 1188 | talk_base::tokenize(options, ' ', '"', '"', &opts); |
| 1189 | std::vector<std::string>::iterator tracefile = |
| 1190 | std::find(opts.begin(), opts.end(), "tracefile"); |
| 1191 | if (tracefile != opts.end() && ++tracefile != opts.end()) { |
| 1192 | // Write encrypted debug output (at same loglevel) to file |
| 1193 | // EncryptedTraceFile no longer supported. |
| 1194 | if (tracing_->SetTraceFile(tracefile->c_str()) == -1) { |
| 1195 | LOG_RTCERR1(SetTraceFile, *tracefile); |
| 1196 | } |
| 1197 | } |
| 1198 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 1199 | // Allow trace options to override the trace filter. We default |
| 1200 | // it to log_filter_ (as a translation of libjingle log levels) |
| 1201 | // elsewhere, but this allows clients to explicitly set webrtc |
| 1202 | // log levels. |
| 1203 | std::vector<std::string>::iterator tracefilter = |
| 1204 | std::find(opts.begin(), opts.end(), "tracefilter"); |
| 1205 | if (tracefilter != opts.end() && ++tracefilter != opts.end()) { |
| 1206 | if (!tracing_->SetTraceFilter(talk_base::FromString<int>(*tracefilter))) { |
| 1207 | LOG_RTCERR1(SetTraceFilter, *tracefilter); |
| 1208 | } |
| 1209 | } |
| 1210 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1211 | // Set AEC dump file |
| 1212 | std::vector<std::string>::iterator recordEC = |
| 1213 | std::find(opts.begin(), opts.end(), "recordEC"); |
| 1214 | if (recordEC != opts.end()) { |
| 1215 | ++recordEC; |
| 1216 | if (recordEC != opts.end()) |
| 1217 | StartAecDump(recordEC->c_str()); |
| 1218 | else |
| 1219 | StopAecDump(); |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | // Ignore spammy trace messages, mostly from the stats API when we haven't |
| 1224 | // gotten RTCP info yet from the remote side. |
| 1225 | bool WebRtcVoiceEngine::ShouldIgnoreTrace(const std::string& trace) { |
| 1226 | static const char* kTracesToIgnore[] = { |
| 1227 | "\tfailed to GetReportBlockInformation", |
| 1228 | "GetRecCodec() failed to get received codec", |
| 1229 | "GetReceivedRtcpStatistics: Could not get received RTP statistics", |
| 1230 | "GetRemoteRTCPData() failed to measure statistics due to lack of received RTP and/or RTCP packets", // NOLINT |
| 1231 | "GetRemoteRTCPData() failed to retrieve sender info for remote side", |
| 1232 | "GetRTPStatistics() failed to measure RTT since no RTP packets have been received yet", // NOLINT |
| 1233 | "GetRTPStatistics() failed to read RTP statistics from the RTP/RTCP module", |
| 1234 | "GetRTPStatistics() failed to retrieve RTT from the RTP/RTCP module", |
| 1235 | "SenderInfoReceived No received SR", |
| 1236 | "StatisticsRTP() no statistics available", |
| 1237 | "TransmitMixer::TypingDetection() VE_TYPING_NOISE_WARNING message has been posted", // NOLINT |
| 1238 | "TransmitMixer::TypingDetection() pending noise-saturation warning exists", // NOLINT |
| 1239 | "GetRecPayloadType() failed to retrieve RX payload type (error=10026)", // NOLINT |
| 1240 | "StopPlayingFileAsMicrophone() isnot playing (error=8088)", |
| 1241 | NULL |
| 1242 | }; |
| 1243 | for (const char* const* p = kTracesToIgnore; *p; ++p) { |
| 1244 | if (trace.find(*p) != std::string::npos) { |
| 1245 | return true; |
| 1246 | } |
| 1247 | } |
| 1248 | return false; |
| 1249 | } |
| 1250 | |
| 1251 | void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace, |
| 1252 | int length) { |
| 1253 | talk_base::LoggingSeverity sev = talk_base::LS_VERBOSE; |
| 1254 | if (level == webrtc::kTraceError || level == webrtc::kTraceCritical) |
| 1255 | sev = talk_base::LS_ERROR; |
| 1256 | else if (level == webrtc::kTraceWarning) |
| 1257 | sev = talk_base::LS_WARNING; |
| 1258 | else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo) |
| 1259 | sev = talk_base::LS_INFO; |
| 1260 | else if (level == webrtc::kTraceTerseInfo) |
| 1261 | sev = talk_base::LS_INFO; |
| 1262 | |
| 1263 | // Skip past boilerplate prefix text |
| 1264 | if (length < 72) { |
| 1265 | std::string msg(trace, length); |
| 1266 | LOG(LS_ERROR) << "Malformed webrtc log message: "; |
| 1267 | LOG_V(sev) << msg; |
| 1268 | } else { |
| 1269 | std::string msg(trace + 71, length - 72); |
| 1270 | if (!ShouldIgnoreTrace(msg)) { |
| 1271 | LOG_V(sev) << "webrtc: " << msg; |
| 1272 | } |
| 1273 | } |
| 1274 | } |
| 1275 | |
| 1276 | void WebRtcVoiceEngine::CallbackOnError(int channel_num, int err_code) { |
| 1277 | talk_base::CritScope lock(&channels_cs_); |
| 1278 | WebRtcVoiceMediaChannel* channel = NULL; |
| 1279 | uint32 ssrc = 0; |
| 1280 | LOG(LS_WARNING) << "VoiceEngine error " << err_code << " reported on channel " |
| 1281 | << channel_num << "."; |
| 1282 | if (FindChannelAndSsrc(channel_num, &channel, &ssrc)) { |
| 1283 | ASSERT(channel != NULL); |
| 1284 | channel->OnError(ssrc, err_code); |
| 1285 | } else { |
| 1286 | LOG(LS_ERROR) << "VoiceEngine channel " << channel_num |
| 1287 | << " could not be found in channel list when error reported."; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | bool WebRtcVoiceEngine::FindChannelAndSsrc( |
| 1292 | int channel_num, WebRtcVoiceMediaChannel** channel, uint32* ssrc) const { |
| 1293 | ASSERT(channel != NULL && ssrc != NULL); |
| 1294 | |
| 1295 | *channel = NULL; |
| 1296 | *ssrc = 0; |
| 1297 | // Find corresponding channel and ssrc |
| 1298 | for (ChannelList::const_iterator it = channels_.begin(); |
| 1299 | it != channels_.end(); ++it) { |
| 1300 | ASSERT(*it != NULL); |
| 1301 | if ((*it)->FindSsrc(channel_num, ssrc)) { |
| 1302 | *channel = *it; |
| 1303 | return true; |
| 1304 | } |
| 1305 | } |
| 1306 | |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
| 1310 | // This method will search through the WebRtcVoiceMediaChannels and |
| 1311 | // obtain the voice engine's channel number. |
| 1312 | bool WebRtcVoiceEngine::FindChannelNumFromSsrc( |
| 1313 | uint32 ssrc, MediaProcessorDirection direction, int* channel_num) { |
| 1314 | ASSERT(channel_num != NULL); |
| 1315 | ASSERT(direction == MPD_RX || direction == MPD_TX); |
| 1316 | |
| 1317 | *channel_num = -1; |
| 1318 | // Find corresponding channel for ssrc. |
| 1319 | for (ChannelList::const_iterator it = channels_.begin(); |
| 1320 | it != channels_.end(); ++it) { |
| 1321 | ASSERT(*it != NULL); |
| 1322 | if (direction & MPD_RX) { |
| 1323 | *channel_num = (*it)->GetReceiveChannelNum(ssrc); |
| 1324 | } |
| 1325 | if (*channel_num == -1 && (direction & MPD_TX)) { |
| 1326 | *channel_num = (*it)->GetSendChannelNum(ssrc); |
| 1327 | } |
| 1328 | if (*channel_num != -1) { |
| 1329 | return true; |
| 1330 | } |
| 1331 | } |
| 1332 | LOG(LS_WARNING) << "FindChannelFromSsrc. No Channel Found for Ssrc: " << ssrc; |
| 1333 | return false; |
| 1334 | } |
| 1335 | |
| 1336 | void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel *channel) { |
| 1337 | talk_base::CritScope lock(&channels_cs_); |
| 1338 | channels_.push_back(channel); |
| 1339 | } |
| 1340 | |
| 1341 | void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel *channel) { |
| 1342 | talk_base::CritScope lock(&channels_cs_); |
| 1343 | ChannelList::iterator i = std::find(channels_.begin(), |
| 1344 | channels_.end(), |
| 1345 | channel); |
| 1346 | if (i != channels_.end()) { |
| 1347 | channels_.erase(i); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | void WebRtcVoiceEngine::RegisterSoundclip(WebRtcSoundclipMedia *soundclip) { |
| 1352 | soundclips_.push_back(soundclip); |
| 1353 | } |
| 1354 | |
| 1355 | void WebRtcVoiceEngine::UnregisterSoundclip(WebRtcSoundclipMedia *soundclip) { |
| 1356 | SoundclipList::iterator i = std::find(soundclips_.begin(), |
| 1357 | soundclips_.end(), |
| 1358 | soundclip); |
| 1359 | if (i != soundclips_.end()) { |
| 1360 | soundclips_.erase(i); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | // Adjusts the default AGC target level by the specified delta. |
| 1365 | // NB: If we start messing with other config fields, we'll want |
| 1366 | // to save the current webrtc::AgcConfig as well. |
| 1367 | bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) { |
| 1368 | webrtc::AgcConfig config = default_agc_config_; |
| 1369 | config.targetLeveldBOv -= delta; |
| 1370 | |
| 1371 | LOG(LS_INFO) << "Adjusting AGC level from default -" |
| 1372 | << default_agc_config_.targetLeveldBOv << "dB to -" |
| 1373 | << config.targetLeveldBOv << "dB"; |
| 1374 | |
| 1375 | if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) { |
| 1376 | LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv); |
| 1377 | return false; |
| 1378 | } |
| 1379 | return true; |
| 1380 | } |
| 1381 | |
| 1382 | bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm, |
| 1383 | webrtc::AudioDeviceModule* adm_sc) { |
| 1384 | if (initialized_) { |
| 1385 | LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init."; |
| 1386 | return false; |
| 1387 | } |
| 1388 | if (adm_) { |
| 1389 | adm_->Release(); |
| 1390 | adm_ = NULL; |
| 1391 | } |
| 1392 | if (adm) { |
| 1393 | adm_ = adm; |
| 1394 | adm_->AddRef(); |
| 1395 | } |
| 1396 | |
| 1397 | if (adm_sc_) { |
| 1398 | adm_sc_->Release(); |
| 1399 | adm_sc_ = NULL; |
| 1400 | } |
| 1401 | if (adm_sc) { |
| 1402 | adm_sc_ = adm_sc; |
| 1403 | adm_sc_->AddRef(); |
| 1404 | } |
| 1405 | return true; |
| 1406 | } |
| 1407 | |
| 1408 | bool WebRtcVoiceEngine::RegisterProcessor( |
| 1409 | uint32 ssrc, |
| 1410 | VoiceProcessor* voice_processor, |
| 1411 | MediaProcessorDirection direction) { |
| 1412 | bool register_with_webrtc = false; |
| 1413 | int channel_id = -1; |
| 1414 | bool success = false; |
| 1415 | uint32* processor_ssrc = NULL; |
| 1416 | bool found_channel = FindChannelNumFromSsrc(ssrc, direction, &channel_id); |
| 1417 | if (voice_processor == NULL || !found_channel) { |
| 1418 | LOG(LS_WARNING) << "Media Processing Registration Failed. ssrc: " << ssrc |
| 1419 | << " foundChannel: " << found_channel; |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | webrtc::ProcessingTypes processing_type; |
| 1424 | { |
| 1425 | talk_base::CritScope cs(&signal_media_critical_); |
| 1426 | if (direction == MPD_RX) { |
| 1427 | processing_type = webrtc::kPlaybackAllChannelsMixed; |
| 1428 | if (SignalRxMediaFrame.is_empty()) { |
| 1429 | register_with_webrtc = true; |
| 1430 | processor_ssrc = &rx_processor_ssrc_; |
| 1431 | } |
| 1432 | SignalRxMediaFrame.connect(voice_processor, |
| 1433 | &VoiceProcessor::OnFrame); |
| 1434 | } else { |
| 1435 | processing_type = webrtc::kRecordingPerChannel; |
| 1436 | if (SignalTxMediaFrame.is_empty()) { |
| 1437 | register_with_webrtc = true; |
| 1438 | processor_ssrc = &tx_processor_ssrc_; |
| 1439 | } |
| 1440 | SignalTxMediaFrame.connect(voice_processor, |
| 1441 | &VoiceProcessor::OnFrame); |
| 1442 | } |
| 1443 | } |
| 1444 | if (register_with_webrtc) { |
| 1445 | // TODO(janahan): when registering consider instantiating a |
| 1446 | // a VoeMediaProcess object and not make the engine extend the interface. |
| 1447 | if (voe()->media() && voe()->media()-> |
| 1448 | RegisterExternalMediaProcessing(channel_id, |
| 1449 | processing_type, |
| 1450 | *this) != -1) { |
| 1451 | LOG(LS_INFO) << "Media Processing Registration Succeeded. channel:" |
| 1452 | << channel_id; |
| 1453 | *processor_ssrc = ssrc; |
| 1454 | success = true; |
| 1455 | } else { |
| 1456 | LOG_RTCERR2(RegisterExternalMediaProcessing, |
| 1457 | channel_id, |
| 1458 | processing_type); |
| 1459 | success = false; |
| 1460 | } |
| 1461 | } else { |
| 1462 | // If we don't have to register with the engine, we just needed to |
| 1463 | // connect a new processor, set success to true; |
| 1464 | success = true; |
| 1465 | } |
| 1466 | return success; |
| 1467 | } |
| 1468 | |
| 1469 | bool WebRtcVoiceEngine::UnregisterProcessorChannel( |
| 1470 | MediaProcessorDirection channel_direction, |
| 1471 | uint32 ssrc, |
| 1472 | VoiceProcessor* voice_processor, |
| 1473 | MediaProcessorDirection processor_direction) { |
| 1474 | bool success = true; |
| 1475 | FrameSignal* signal; |
| 1476 | webrtc::ProcessingTypes processing_type; |
| 1477 | uint32* processor_ssrc = NULL; |
| 1478 | if (channel_direction == MPD_RX) { |
| 1479 | signal = &SignalRxMediaFrame; |
| 1480 | processing_type = webrtc::kPlaybackAllChannelsMixed; |
| 1481 | processor_ssrc = &rx_processor_ssrc_; |
| 1482 | } else { |
| 1483 | signal = &SignalTxMediaFrame; |
| 1484 | processing_type = webrtc::kRecordingPerChannel; |
| 1485 | processor_ssrc = &tx_processor_ssrc_; |
| 1486 | } |
| 1487 | |
| 1488 | int deregister_id = -1; |
| 1489 | { |
| 1490 | talk_base::CritScope cs(&signal_media_critical_); |
| 1491 | if ((processor_direction & channel_direction) != 0 && !signal->is_empty()) { |
| 1492 | signal->disconnect(voice_processor); |
| 1493 | int channel_id = -1; |
| 1494 | bool found_channel = FindChannelNumFromSsrc(ssrc, |
| 1495 | channel_direction, |
| 1496 | &channel_id); |
| 1497 | if (signal->is_empty() && found_channel) { |
| 1498 | deregister_id = channel_id; |
| 1499 | } |
| 1500 | } |
| 1501 | } |
| 1502 | if (deregister_id != -1) { |
| 1503 | if (voe()->media() && |
| 1504 | voe()->media()->DeRegisterExternalMediaProcessing(deregister_id, |
| 1505 | processing_type) != -1) { |
| 1506 | *processor_ssrc = 0; |
| 1507 | LOG(LS_INFO) << "Media Processing DeRegistration Succeeded. channel:" |
| 1508 | << deregister_id; |
| 1509 | } else { |
| 1510 | LOG_RTCERR2(DeRegisterExternalMediaProcessing, |
| 1511 | deregister_id, |
| 1512 | processing_type); |
| 1513 | success = false; |
| 1514 | } |
| 1515 | } |
| 1516 | return success; |
| 1517 | } |
| 1518 | |
| 1519 | bool WebRtcVoiceEngine::UnregisterProcessor( |
| 1520 | uint32 ssrc, |
| 1521 | VoiceProcessor* voice_processor, |
| 1522 | MediaProcessorDirection direction) { |
| 1523 | bool success = true; |
| 1524 | if (voice_processor == NULL) { |
| 1525 | LOG(LS_WARNING) << "Media Processing Deregistration Failed. ssrc: " |
| 1526 | << ssrc; |
| 1527 | return false; |
| 1528 | } |
| 1529 | if (!UnregisterProcessorChannel(MPD_RX, ssrc, voice_processor, direction)) { |
| 1530 | success = false; |
| 1531 | } |
| 1532 | if (!UnregisterProcessorChannel(MPD_TX, ssrc, voice_processor, direction)) { |
| 1533 | success = false; |
| 1534 | } |
| 1535 | return success; |
| 1536 | } |
| 1537 | |
| 1538 | // Implementing method from WebRtc VoEMediaProcess interface |
| 1539 | // Do not lock mux_channel_cs_ in this callback. |
| 1540 | void WebRtcVoiceEngine::Process(int channel, |
| 1541 | webrtc::ProcessingTypes type, |
| 1542 | int16_t audio10ms[], |
| 1543 | int length, |
| 1544 | int sampling_freq, |
| 1545 | bool is_stereo) { |
| 1546 | talk_base::CritScope cs(&signal_media_critical_); |
| 1547 | AudioFrame frame(audio10ms, length, sampling_freq, is_stereo); |
| 1548 | if (type == webrtc::kPlaybackAllChannelsMixed) { |
| 1549 | SignalRxMediaFrame(rx_processor_ssrc_, MPD_RX, &frame); |
| 1550 | } else if (type == webrtc::kRecordingPerChannel) { |
| 1551 | SignalTxMediaFrame(tx_processor_ssrc_, MPD_TX, &frame); |
| 1552 | } else { |
| 1553 | LOG(LS_WARNING) << "Media Processing invoked unexpectedly." |
| 1554 | << " channel: " << channel << " type: " << type |
| 1555 | << " tx_ssrc: " << tx_processor_ssrc_ |
| 1556 | << " rx_ssrc: " << rx_processor_ssrc_; |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | void WebRtcVoiceEngine::StartAecDump(const std::string& filename) { |
| 1561 | if (!is_dumping_aec_) { |
| 1562 | // Start dumping AEC when we are not dumping. |
| 1563 | if (voe_wrapper_->processing()->StartDebugRecording( |
| 1564 | filename.c_str()) != webrtc::AudioProcessing::kNoError) { |
| 1565 | LOG_RTCERR0(StartDebugRecording); |
| 1566 | } else { |
| 1567 | is_dumping_aec_ = true; |
| 1568 | } |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | void WebRtcVoiceEngine::StopAecDump() { |
| 1573 | if (is_dumping_aec_) { |
| 1574 | // Stop dumping AEC when we are dumping. |
| 1575 | if (voe_wrapper_->processing()->StopDebugRecording() != |
| 1576 | webrtc::AudioProcessing::kNoError) { |
| 1577 | LOG_RTCERR0(StopDebugRecording); |
| 1578 | } |
| 1579 | is_dumping_aec_ = false; |
| 1580 | } |
| 1581 | } |
| 1582 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1583 | // This struct relies on the generated copy constructor and assignment operator |
| 1584 | // since it is used in an stl::map. |
| 1585 | struct WebRtcVoiceMediaChannel::WebRtcVoiceChannelInfo { |
| 1586 | WebRtcVoiceChannelInfo() : channel(-1), renderer(NULL) {} |
| 1587 | WebRtcVoiceChannelInfo(int ch, AudioRenderer* r) |
| 1588 | : channel(ch), |
| 1589 | renderer(r) {} |
| 1590 | ~WebRtcVoiceChannelInfo() {} |
| 1591 | |
| 1592 | int channel; |
| 1593 | AudioRenderer* renderer; |
| 1594 | }; |
| 1595 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1596 | // WebRtcVoiceMediaChannel |
| 1597 | WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine *engine) |
| 1598 | : WebRtcMediaChannel<VoiceMediaChannel, WebRtcVoiceEngine>( |
| 1599 | engine, |
| 1600 | engine->voe()->base()->CreateChannel()), |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1601 | send_bw_setting_(false), |
| 1602 | send_autobw_(false), |
| 1603 | send_bw_bps_(0), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1604 | options_(), |
| 1605 | dtmf_allowed_(false), |
| 1606 | desired_playout_(false), |
| 1607 | nack_enabled_(false), |
| 1608 | playout_(false), |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 1609 | typing_noise_detected_(false), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1610 | desired_send_(SEND_NOTHING), |
| 1611 | send_(SEND_NOTHING), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1612 | default_receive_ssrc_(0) { |
| 1613 | engine->RegisterChannel(this); |
| 1614 | LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel " |
| 1615 | << voe_channel(); |
| 1616 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1617 | ConfigureSendChannel(voe_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() { |
| 1621 | LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel " |
| 1622 | << voe_channel(); |
| 1623 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1624 | // Remove any remaining send streams, the default channel will be deleted |
| 1625 | // later. |
| 1626 | while (!send_channels_.empty()) |
| 1627 | RemoveSendStream(send_channels_.begin()->first); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1628 | |
| 1629 | // Unregister ourselves from the engine. |
| 1630 | engine()->UnregisterChannel(this); |
| 1631 | // Remove any remaining streams. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1632 | while (!receive_channels_.empty()) { |
| 1633 | RemoveRecvStream(receive_channels_.begin()->first); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1636 | // Delete the default channel. |
| 1637 | DeleteChannel(voe_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) { |
| 1641 | LOG(LS_INFO) << "Setting voice channel options: " |
| 1642 | << options.ToString(); |
| 1643 | |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1644 | // Check if DSCP value is changed from previous. |
| 1645 | bool dscp_option_changed = (options_.dscp != options.dscp); |
| 1646 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1647 | // TODO(xians): Add support to set different options for different send |
| 1648 | // streams after we support multiple APMs. |
| 1649 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1650 | // We retain all of the existing options, and apply the given ones |
| 1651 | // on top. This means there is no way to "clear" options such that |
| 1652 | // they go back to the engine default. |
| 1653 | options_.SetAll(options); |
| 1654 | |
| 1655 | if (send_ != SEND_NOTHING) { |
| 1656 | if (!engine()->SetOptionOverrides(options_)) { |
| 1657 | LOG(LS_WARNING) << |
| 1658 | "Failed to engine SetOptionOverrides during channel SetOptions."; |
| 1659 | return false; |
| 1660 | } |
| 1661 | } else { |
| 1662 | // Will be interpreted when appropriate. |
| 1663 | } |
| 1664 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 1665 | // Receiver-side auto gain control happens per channel, so set it here from |
| 1666 | // options. Note that, like conference mode, setting it on the engine won't |
| 1667 | // have the desired effect, since voice channels don't inherit options from |
| 1668 | // the media engine when those options are applied per-channel. |
| 1669 | bool rx_auto_gain_control; |
| 1670 | if (options.rx_auto_gain_control.Get(&rx_auto_gain_control)) { |
| 1671 | if (engine()->voe()->processing()->SetRxAgcStatus( |
| 1672 | voe_channel(), rx_auto_gain_control, |
| 1673 | webrtc::kAgcFixedDigital) == -1) { |
| 1674 | LOG_RTCERR1(SetRxAgcStatus, rx_auto_gain_control); |
| 1675 | return false; |
| 1676 | } else { |
| 1677 | LOG(LS_VERBOSE) << "Rx auto gain set to " << rx_auto_gain_control |
| 1678 | << " with mode " << webrtc::kAgcFixedDigital; |
| 1679 | } |
| 1680 | } |
| 1681 | if (options.rx_agc_target_dbov.IsSet() || |
| 1682 | options.rx_agc_digital_compression_gain.IsSet() || |
| 1683 | options.rx_agc_limiter.IsSet()) { |
| 1684 | webrtc::AgcConfig config; |
| 1685 | // If only some of the options are being overridden, get the current |
| 1686 | // settings for the channel and bail if they aren't available. |
| 1687 | if (!options.rx_agc_target_dbov.IsSet() || |
| 1688 | !options.rx_agc_digital_compression_gain.IsSet() || |
| 1689 | !options.rx_agc_limiter.IsSet()) { |
| 1690 | if (engine()->voe()->processing()->GetRxAgcConfig( |
| 1691 | voe_channel(), config) != 0) { |
| 1692 | LOG(LS_ERROR) << "Failed to get default rx agc configuration for " |
| 1693 | << "channel " << voe_channel() << ". Since not all rx " |
| 1694 | << "agc options are specified, unable to safely set rx " |
| 1695 | << "agc options."; |
| 1696 | return false; |
| 1697 | } |
| 1698 | } |
| 1699 | config.targetLeveldBOv = |
| 1700 | options.rx_agc_target_dbov.GetWithDefaultIfUnset( |
| 1701 | config.targetLeveldBOv); |
| 1702 | config.digitalCompressionGaindB = |
| 1703 | options.rx_agc_digital_compression_gain.GetWithDefaultIfUnset( |
| 1704 | config.digitalCompressionGaindB); |
| 1705 | config.limiterEnable = options.rx_agc_limiter.GetWithDefaultIfUnset( |
| 1706 | config.limiterEnable); |
| 1707 | if (engine()->voe()->processing()->SetRxAgcConfig( |
| 1708 | voe_channel(), config) == -1) { |
| 1709 | LOG_RTCERR4(SetRxAgcConfig, voe_channel(), config.targetLeveldBOv, |
| 1710 | config.digitalCompressionGaindB, config.limiterEnable); |
| 1711 | return false; |
| 1712 | } |
| 1713 | } |
wu@webrtc.org | de30501 | 2013-10-31 15:40:38 +0000 | [diff] [blame] | 1714 | if (dscp_option_changed) { |
| 1715 | talk_base::DiffServCodePoint dscp = talk_base::DSCP_DEFAULT; |
| 1716 | if (options.dscp.GetWithDefaultIfUnset(false)) |
| 1717 | dscp = kAudioDscpValue; |
| 1718 | if (MediaChannel::SetDscp(dscp) != 0) { |
| 1719 | LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel"; |
| 1720 | } |
| 1721 | } |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 1722 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1723 | LOG(LS_INFO) << "Set voice channel options. Current options: " |
| 1724 | << options_.ToString(); |
| 1725 | return true; |
| 1726 | } |
| 1727 | |
| 1728 | bool WebRtcVoiceMediaChannel::SetRecvCodecs( |
| 1729 | const std::vector<AudioCodec>& codecs) { |
| 1730 | // Set the payload types to be used for incoming media. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1731 | LOG(LS_INFO) << "Setting receive voice codecs:"; |
| 1732 | |
| 1733 | std::vector<AudioCodec> new_codecs; |
| 1734 | // Find all new codecs. We allow adding new codecs but don't allow changing |
| 1735 | // the payload type of codecs that is already configured since we might |
| 1736 | // already be receiving packets with that payload type. |
| 1737 | for (std::vector<AudioCodec>::const_iterator it = codecs.begin(); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1738 | it != codecs.end(); ++it) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1739 | AudioCodec old_codec; |
| 1740 | if (FindCodec(recv_codecs_, *it, &old_codec)) { |
| 1741 | if (old_codec.id != it->id) { |
| 1742 | LOG(LS_ERROR) << it->name << " payload type changed."; |
| 1743 | return false; |
| 1744 | } |
| 1745 | } else { |
| 1746 | new_codecs.push_back(*it); |
| 1747 | } |
| 1748 | } |
| 1749 | if (new_codecs.empty()) { |
| 1750 | // There are no new codecs to configure. Already configured codecs are |
| 1751 | // never removed. |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | if (playout_) { |
| 1756 | // Receive codecs can not be changed while playing. So we temporarily |
| 1757 | // pause playout. |
| 1758 | PausePlayout(); |
| 1759 | } |
| 1760 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1761 | bool ret = true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1762 | for (std::vector<AudioCodec>::const_iterator it = new_codecs.begin(); |
| 1763 | it != new_codecs.end() && ret; ++it) { |
| 1764 | webrtc::CodecInst voe_codec; |
| 1765 | if (engine()->FindWebRtcCodec(*it, &voe_codec)) { |
| 1766 | LOG(LS_INFO) << ToString(*it); |
| 1767 | voe_codec.pltype = it->id; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1768 | if (default_receive_ssrc_ == 0) { |
| 1769 | // Set the receive codecs on the default channel explicitly if the |
| 1770 | // default channel is not used by |receive_channels_|, this happens in |
| 1771 | // conference mode or in non-conference mode when there is no playout |
| 1772 | // channel. |
| 1773 | // TODO(xians): Figure out how we use the default channel in conference |
| 1774 | // mode. |
| 1775 | if (engine()->voe()->codec()->SetRecPayloadType( |
| 1776 | voe_channel(), voe_codec) == -1) { |
| 1777 | LOG_RTCERR2(SetRecPayloadType, voe_channel(), ToString(voe_codec)); |
| 1778 | ret = false; |
| 1779 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
| 1782 | // Set the receive codecs on all receiving channels. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1783 | for (ChannelMap::iterator it = receive_channels_.begin(); |
| 1784 | it != receive_channels_.end() && ret; ++it) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1785 | if (engine()->voe()->codec()->SetRecPayloadType( |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1786 | it->second.channel, voe_codec) == -1) { |
| 1787 | LOG_RTCERR2(SetRecPayloadType, it->second.channel, |
| 1788 | ToString(voe_codec)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1789 | ret = false; |
| 1790 | } |
| 1791 | } |
| 1792 | } else { |
| 1793 | LOG(LS_WARNING) << "Unknown codec " << ToString(*it); |
| 1794 | ret = false; |
| 1795 | } |
| 1796 | } |
| 1797 | if (ret) { |
| 1798 | recv_codecs_ = codecs; |
| 1799 | } |
| 1800 | |
| 1801 | if (desired_playout_ && !playout_) { |
| 1802 | ResumePlayout(); |
| 1803 | } |
| 1804 | return ret; |
| 1805 | } |
| 1806 | |
| 1807 | bool WebRtcVoiceMediaChannel::SetSendCodecs( |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1808 | int channel, const std::vector<AudioCodec>& codecs) { |
| 1809 | // Disable VAD, and FEC unless we know the other side wants them. |
| 1810 | engine()->voe()->codec()->SetVADStatus(channel, false); |
| 1811 | engine()->voe()->rtp()->SetNACKStatus(channel, false, 0); |
| 1812 | engine()->voe()->rtp()->SetFECStatus(channel, false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Scan through the list to figure out the codec to use for sending, along |
| 1815 | // with the proper configuration for VAD and DTMF. |
| 1816 | bool first = true; |
| 1817 | webrtc::CodecInst send_codec; |
| 1818 | memset(&send_codec, 0, sizeof(send_codec)); |
| 1819 | |
| 1820 | for (std::vector<AudioCodec>::const_iterator it = codecs.begin(); |
| 1821 | it != codecs.end(); ++it) { |
| 1822 | // Ignore codecs we don't know about. The negotiation step should prevent |
| 1823 | // this, but double-check to be sure. |
| 1824 | webrtc::CodecInst voe_codec; |
| 1825 | if (!engine()->FindWebRtcCodec(*it, &voe_codec)) { |
| 1826 | LOG(LS_WARNING) << "Unknown codec " << ToString(voe_codec); |
| 1827 | continue; |
| 1828 | } |
| 1829 | |
| 1830 | // If OPUS, change what we send according to the "stereo" codec |
| 1831 | // parameter, and not the "channels" parameter. We set |
| 1832 | // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If |
| 1833 | // the bitrate is not specified, i.e. is zero, we set it to the |
| 1834 | // appropriate default value for mono or stereo Opus. |
| 1835 | if (IsOpus(*it)) { |
| 1836 | if (IsOpusStereoEnabled(*it)) { |
| 1837 | voe_codec.channels = 2; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1838 | if (!IsValidOpusBitrate(it->bitrate)) { |
| 1839 | if (it->bitrate != 0) { |
| 1840 | LOG(LS_WARNING) << "Overrides the invalid supplied bitrate(" |
| 1841 | << it->bitrate |
| 1842 | << ") with default opus stereo bitrate: " |
| 1843 | << kOpusStereoBitrate; |
| 1844 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1845 | voe_codec.rate = kOpusStereoBitrate; |
| 1846 | } |
| 1847 | } else { |
| 1848 | voe_codec.channels = 1; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1849 | if (!IsValidOpusBitrate(it->bitrate)) { |
| 1850 | if (it->bitrate != 0) { |
| 1851 | LOG(LS_WARNING) << "Overrides the invalid supplied bitrate(" |
| 1852 | << it->bitrate |
| 1853 | << ") with default opus mono bitrate: " |
| 1854 | << kOpusMonoBitrate; |
| 1855 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1856 | voe_codec.rate = kOpusMonoBitrate; |
| 1857 | } |
| 1858 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1859 | int bitrate_from_params = GetOpusBitrateFromParams(*it); |
| 1860 | if (bitrate_from_params != 0) { |
| 1861 | voe_codec.rate = bitrate_from_params; |
| 1862 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1865 | // Find the DTMF telephone event "codec" and tell VoiceEngine channels |
| 1866 | // about it. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1867 | if (_stricmp(it->name.c_str(), "telephone-event") == 0 || |
| 1868 | _stricmp(it->name.c_str(), "audio/telephone-event") == 0) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1869 | if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType( |
| 1870 | channel, it->id) == -1) { |
| 1871 | LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, it->id); |
| 1872 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1873 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | // Turn voice activity detection/comfort noise on if supported. |
| 1877 | // Set the wideband CN payload type appropriately. |
| 1878 | // (narrowband always uses the static payload type 13). |
| 1879 | if (_stricmp(it->name.c_str(), "CN") == 0) { |
| 1880 | webrtc::PayloadFrequencies cn_freq; |
| 1881 | switch (it->clockrate) { |
| 1882 | case 8000: |
| 1883 | cn_freq = webrtc::kFreq8000Hz; |
| 1884 | break; |
| 1885 | case 16000: |
| 1886 | cn_freq = webrtc::kFreq16000Hz; |
| 1887 | break; |
| 1888 | case 32000: |
| 1889 | cn_freq = webrtc::kFreq32000Hz; |
| 1890 | break; |
| 1891 | default: |
| 1892 | LOG(LS_WARNING) << "CN frequency " << it->clockrate |
| 1893 | << " not supported."; |
| 1894 | continue; |
| 1895 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1896 | // Set the CN payloadtype and the VAD status. |
| 1897 | // The CN payload type for 8000 Hz clockrate is fixed at 13. |
| 1898 | if (cn_freq != webrtc::kFreq8000Hz) { |
| 1899 | if (engine()->voe()->codec()->SetSendCNPayloadType( |
| 1900 | channel, it->id, cn_freq) == -1) { |
| 1901 | LOG_RTCERR3(SetSendCNPayloadType, channel, it->id, cn_freq); |
| 1902 | // TODO(ajm): This failure condition will be removed from VoE. |
| 1903 | // Restore the return here when we update to a new enough webrtc. |
| 1904 | // |
| 1905 | // Not returning false because the SetSendCNPayloadType will fail if |
| 1906 | // the channel is already sending. |
| 1907 | // This can happen if the remote description is applied twice, for |
| 1908 | // example in the case of ROAP on top of JSEP, where both side will |
| 1909 | // send the offer. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1910 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1911 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 1912 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1913 | // Only turn on VAD if we have a CN payload type that matches the |
| 1914 | // clockrate for the codec we are going to use. |
| 1915 | if (it->clockrate == send_codec.plfreq) { |
| 1916 | LOG(LS_INFO) << "Enabling VAD"; |
| 1917 | if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) { |
| 1918 | LOG_RTCERR2(SetVADStatus, channel, true); |
| 1919 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | // We'll use the first codec in the list to actually send audio data. |
| 1925 | // Be sure to use the payload type requested by the remote side. |
| 1926 | // "red", for FEC audio, is a special case where the actual codec to be |
| 1927 | // used is specified in params. |
| 1928 | if (first) { |
| 1929 | if (_stricmp(it->name.c_str(), "red") == 0) { |
| 1930 | // Parse out the RED parameters. If we fail, just ignore RED; |
| 1931 | // we don't support all possible params/usage scenarios. |
| 1932 | if (!GetRedSendCodec(*it, codecs, &send_codec)) { |
| 1933 | continue; |
| 1934 | } |
| 1935 | |
| 1936 | // Enable redundant encoding of the specified codec. Treat any |
| 1937 | // failure as a fatal internal error. |
| 1938 | LOG(LS_INFO) << "Enabling FEC"; |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1939 | if (engine()->voe()->rtp()->SetFECStatus(channel, true, it->id) == -1) { |
| 1940 | LOG_RTCERR3(SetFECStatus, channel, true, it->id); |
| 1941 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1942 | } |
| 1943 | } else { |
| 1944 | send_codec = voe_codec; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 1945 | nack_enabled_ = IsNackEnabled(*it); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1946 | SetNack(channel, nack_enabled_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1947 | } |
| 1948 | first = false; |
| 1949 | // Set the codec immediately, since SetVADStatus() depends on whether |
| 1950 | // the current codec is mono or stereo. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1951 | if (!SetSendCodec(channel, send_codec)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1952 | return false; |
| 1953 | } |
| 1954 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1955 | |
| 1956 | // If we're being asked to set an empty list of codecs, due to a buggy client, |
| 1957 | // choose the most common format: PCMU |
| 1958 | if (first) { |
| 1959 | LOG(LS_WARNING) << "Received empty list of codecs; using PCMU/8000"; |
| 1960 | AudioCodec codec(0, "PCMU", 8000, 0, 1, 0); |
| 1961 | engine()->FindWebRtcCodec(codec, &send_codec); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1962 | if (!SetSendCodec(channel, send_codec)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1963 | return false; |
| 1964 | } |
| 1965 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1966 | // Always update the |send_codec_| to the currently set send codec. |
| 1967 | send_codec_.reset(new webrtc::CodecInst(send_codec)); |
| 1968 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 1969 | if (send_bw_setting_) { |
| 1970 | SetSendBandwidthInternal(send_autobw_, send_bw_bps_); |
| 1971 | } |
| 1972 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | bool WebRtcVoiceMediaChannel::SetSendCodecs( |
| 1977 | const std::vector<AudioCodec>& codecs) { |
| 1978 | dtmf_allowed_ = false; |
| 1979 | for (std::vector<AudioCodec>::const_iterator it = codecs.begin(); |
| 1980 | it != codecs.end(); ++it) { |
| 1981 | // Find the DTMF telephone event "codec". |
| 1982 | if (_stricmp(it->name.c_str(), "telephone-event") == 0 || |
| 1983 | _stricmp(it->name.c_str(), "audio/telephone-event") == 0) { |
| 1984 | dtmf_allowed_ = true; |
| 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | // Cache the codecs in order to configure the channel created later. |
| 1989 | send_codecs_ = codecs; |
| 1990 | for (ChannelMap::iterator iter = send_channels_.begin(); |
| 1991 | iter != send_channels_.end(); ++iter) { |
| 1992 | if (!SetSendCodecs(iter->second.channel, codecs)) { |
| 1993 | return false; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | SetNack(receive_channels_, nack_enabled_); |
| 1998 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1999 | return true; |
| 2000 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2001 | |
| 2002 | void WebRtcVoiceMediaChannel::SetNack(const ChannelMap& channels, |
| 2003 | bool nack_enabled) { |
| 2004 | for (ChannelMap::const_iterator it = channels.begin(); |
| 2005 | it != channels.end(); ++it) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2006 | SetNack(it->second.channel, nack_enabled); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2010 | void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2011 | if (nack_enabled) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2012 | LOG(LS_INFO) << "Enabling NACK for channel " << channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2013 | engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets); |
| 2014 | } else { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2015 | LOG(LS_INFO) << "Disabling NACK for channel " << channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2016 | engine()->voe()->rtp()->SetNACKStatus(channel, false, 0); |
| 2017 | } |
| 2018 | } |
| 2019 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2020 | bool WebRtcVoiceMediaChannel::SetSendCodec( |
| 2021 | const webrtc::CodecInst& send_codec) { |
| 2022 | LOG(LS_INFO) << "Selected voice codec " << ToString(send_codec) |
| 2023 | << ", bitrate=" << send_codec.rate; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2024 | for (ChannelMap::iterator iter = send_channels_.begin(); |
| 2025 | iter != send_channels_.end(); ++iter) { |
| 2026 | if (!SetSendCodec(iter->second.channel, send_codec)) |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2030 | return true; |
| 2031 | } |
| 2032 | |
| 2033 | bool WebRtcVoiceMediaChannel::SetSendCodec( |
| 2034 | int channel, const webrtc::CodecInst& send_codec) { |
| 2035 | LOG(LS_INFO) << "Send channel " << channel << " selected voice codec " |
| 2036 | << ToString(send_codec) << ", bitrate=" << send_codec.rate; |
| 2037 | |
| 2038 | if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) { |
| 2039 | LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2040 | return false; |
| 2041 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2042 | return true; |
| 2043 | } |
| 2044 | |
| 2045 | bool WebRtcVoiceMediaChannel::SetRecvRtpHeaderExtensions( |
| 2046 | const std::vector<RtpHeaderExtension>& extensions) { |
| 2047 | // We don't support any incoming extensions headers right now. |
| 2048 | return true; |
| 2049 | } |
| 2050 | |
| 2051 | bool WebRtcVoiceMediaChannel::SetSendRtpHeaderExtensions( |
| 2052 | const std::vector<RtpHeaderExtension>& extensions) { |
| 2053 | // Enable the audio level extension header if requested. |
| 2054 | std::vector<RtpHeaderExtension>::const_iterator it; |
| 2055 | for (it = extensions.begin(); it != extensions.end(); ++it) { |
| 2056 | if (it->uri == kRtpAudioLevelHeaderExtension) { |
| 2057 | break; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | bool enable = (it != extensions.end()); |
| 2062 | int id = 0; |
| 2063 | |
| 2064 | if (enable) { |
| 2065 | id = it->id; |
| 2066 | if (id < kMinRtpHeaderExtensionId || |
| 2067 | id > kMaxRtpHeaderExtensionId) { |
| 2068 | LOG(LS_WARNING) << "Invalid RTP header extension id " << id; |
| 2069 | return false; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | LOG(LS_INFO) << "Enabling audio level header extension with ID " << id; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2074 | for (ChannelMap::const_iterator iter = send_channels_.begin(); |
| 2075 | iter != send_channels_.end(); ++iter) { |
| 2076 | if (engine()->voe()->rtp()->SetRTPAudioLevelIndicationStatus( |
| 2077 | iter->second.channel, enable, id) == -1) { |
| 2078 | LOG_RTCERR3(SetRTPAudioLevelIndicationStatus, |
| 2079 | iter->second.channel, enable, id); |
| 2080 | return false; |
| 2081 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | return true; |
| 2085 | } |
| 2086 | |
| 2087 | bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) { |
| 2088 | desired_playout_ = playout; |
| 2089 | return ChangePlayout(desired_playout_); |
| 2090 | } |
| 2091 | |
| 2092 | bool WebRtcVoiceMediaChannel::PausePlayout() { |
| 2093 | return ChangePlayout(false); |
| 2094 | } |
| 2095 | |
| 2096 | bool WebRtcVoiceMediaChannel::ResumePlayout() { |
| 2097 | return ChangePlayout(desired_playout_); |
| 2098 | } |
| 2099 | |
| 2100 | bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) { |
| 2101 | if (playout_ == playout) { |
| 2102 | return true; |
| 2103 | } |
| 2104 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2105 | // Change the playout of all channels to the new state. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2106 | bool result = true; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2107 | if (receive_channels_.empty()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2108 | // Only toggle the default channel if we don't have any other channels. |
| 2109 | result = SetPlayout(voe_channel(), playout); |
| 2110 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2111 | for (ChannelMap::iterator it = receive_channels_.begin(); |
| 2112 | it != receive_channels_.end() && result; ++it) { |
| 2113 | if (!SetPlayout(it->second.channel, playout)) { |
| 2114 | LOG(LS_ERROR) << "SetPlayout " << playout << " on channel " |
| 2115 | << it->second.channel << " failed"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2116 | result = false; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | if (result) { |
| 2121 | playout_ = playout; |
| 2122 | } |
| 2123 | return result; |
| 2124 | } |
| 2125 | |
| 2126 | bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) { |
| 2127 | desired_send_ = send; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2128 | if (!send_channels_.empty()) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2129 | return ChangeSend(desired_send_); |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
| 2133 | bool WebRtcVoiceMediaChannel::PauseSend() { |
| 2134 | return ChangeSend(SEND_NOTHING); |
| 2135 | } |
| 2136 | |
| 2137 | bool WebRtcVoiceMediaChannel::ResumeSend() { |
| 2138 | return ChangeSend(desired_send_); |
| 2139 | } |
| 2140 | |
| 2141 | bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) { |
| 2142 | if (send_ == send) { |
| 2143 | return true; |
| 2144 | } |
| 2145 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2146 | // Change the settings on each send channel. |
| 2147 | if (send == SEND_MICROPHONE) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2148 | engine()->SetOptionOverrides(options_); |
| 2149 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2150 | // Change the settings on each send channel. |
| 2151 | for (ChannelMap::iterator iter = send_channels_.begin(); |
| 2152 | iter != send_channels_.end(); ++iter) { |
| 2153 | if (!ChangeSend(iter->second.channel, send)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2154 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2155 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2156 | |
| 2157 | // Clear up the options after stopping sending. |
| 2158 | if (send == SEND_NOTHING) |
| 2159 | engine()->ClearOptionOverrides(); |
| 2160 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2161 | send_ = send; |
| 2162 | return true; |
| 2163 | } |
| 2164 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2165 | bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) { |
| 2166 | if (send == SEND_MICROPHONE) { |
| 2167 | if (engine()->voe()->base()->StartSend(channel) == -1) { |
| 2168 | LOG_RTCERR1(StartSend, channel); |
| 2169 | return false; |
| 2170 | } |
| 2171 | if (engine()->voe()->file() && |
| 2172 | engine()->voe()->file()->StopPlayingFileAsMicrophone(channel) == -1) { |
| 2173 | LOG_RTCERR1(StopPlayingFileAsMicrophone, channel); |
| 2174 | return false; |
| 2175 | } |
| 2176 | } else { // SEND_NOTHING |
| 2177 | ASSERT(send == SEND_NOTHING); |
| 2178 | if (engine()->voe()->base()->StopSend(channel) == -1) { |
| 2179 | LOG_RTCERR1(StopSend, channel); |
| 2180 | return false; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2184 | return true; |
| 2185 | } |
| 2186 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2187 | void WebRtcVoiceMediaChannel::ConfigureSendChannel(int channel) { |
| 2188 | if (engine()->voe()->network()->RegisterExternalTransport( |
| 2189 | channel, *this) == -1) { |
| 2190 | LOG_RTCERR2(RegisterExternalTransport, channel, this); |
| 2191 | } |
| 2192 | |
| 2193 | // Enable RTCP (for quality stats and feedback messages) |
| 2194 | EnableRtcp(channel); |
| 2195 | |
| 2196 | // Reset all recv codecs; they will be enabled via SetRecvCodecs. |
| 2197 | ResetRecvCodecs(channel); |
| 2198 | } |
| 2199 | |
| 2200 | bool WebRtcVoiceMediaChannel::DeleteChannel(int channel) { |
| 2201 | if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) { |
| 2202 | LOG_RTCERR1(DeRegisterExternalTransport, channel); |
| 2203 | } |
| 2204 | |
| 2205 | if (engine()->voe()->base()->DeleteChannel(channel) == -1) { |
| 2206 | LOG_RTCERR1(DeleteChannel, channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2207 | return false; |
| 2208 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2209 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2210 | return true; |
| 2211 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2212 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2213 | bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) { |
| 2214 | // If the default channel is already used for sending create a new channel |
| 2215 | // otherwise use the default channel for sending. |
| 2216 | int channel = GetSendChannelNum(sp.first_ssrc()); |
| 2217 | if (channel != -1) { |
| 2218 | LOG(LS_ERROR) << "Stream already exists with ssrc " << sp.first_ssrc(); |
| 2219 | return false; |
| 2220 | } |
| 2221 | |
| 2222 | bool default_channel_is_available = true; |
| 2223 | for (ChannelMap::const_iterator iter = send_channels_.begin(); |
| 2224 | iter != send_channels_.end(); ++iter) { |
| 2225 | if (IsDefaultChannel(iter->second.channel)) { |
| 2226 | default_channel_is_available = false; |
| 2227 | break; |
| 2228 | } |
| 2229 | } |
| 2230 | if (default_channel_is_available) { |
| 2231 | channel = voe_channel(); |
| 2232 | } else { |
| 2233 | // Create a new channel for sending audio data. |
| 2234 | channel = engine()->voe()->base()->CreateChannel(); |
| 2235 | if (channel == -1) { |
| 2236 | LOG_RTCERR0(CreateChannel); |
| 2237 | return false; |
| 2238 | } |
| 2239 | |
| 2240 | ConfigureSendChannel(channel); |
| 2241 | } |
| 2242 | |
| 2243 | // Save the channel to send_channels_, so that RemoveSendStream() can still |
| 2244 | // delete the channel in case failure happens below. |
| 2245 | send_channels_[sp.first_ssrc()] = WebRtcVoiceChannelInfo(channel, NULL); |
| 2246 | |
| 2247 | // Set the send (local) SSRC. |
| 2248 | // If there are multiple send SSRCs, we can only set the first one here, and |
| 2249 | // the rest of the SSRC(s) need to be set after SetSendCodec has been called |
| 2250 | // (with a codec requires multiple SSRC(s)). |
| 2251 | if (engine()->voe()->rtp()->SetLocalSSRC(channel, sp.first_ssrc()) == -1) { |
| 2252 | LOG_RTCERR2(SetSendSSRC, channel, sp.first_ssrc()); |
| 2253 | return false; |
| 2254 | } |
| 2255 | |
| 2256 | // At this point the channel's local SSRC has been updated. If the channel is |
| 2257 | // the default channel make sure that all the receive channels are updated as |
| 2258 | // well. Receive channels have to have the same SSRC as the default channel in |
| 2259 | // order to send receiver reports with this SSRC. |
| 2260 | if (IsDefaultChannel(channel)) { |
| 2261 | for (ChannelMap::const_iterator it = receive_channels_.begin(); |
| 2262 | it != receive_channels_.end(); ++it) { |
| 2263 | // Only update the SSRC for non-default channels. |
| 2264 | if (!IsDefaultChannel(it->second.channel)) { |
| 2265 | if (engine()->voe()->rtp()->SetLocalSSRC(it->second.channel, |
| 2266 | sp.first_ssrc()) != 0) { |
| 2267 | LOG_RTCERR2(SetLocalSSRC, it->second.channel, sp.first_ssrc()); |
| 2268 | return false; |
| 2269 | } |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | if (engine()->voe()->rtp()->SetRTCP_CNAME(channel, sp.cname.c_str()) == -1) { |
| 2275 | LOG_RTCERR2(SetRTCP_CNAME, channel, sp.cname); |
| 2276 | return false; |
| 2277 | } |
| 2278 | |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2279 | // Set the current codecs to be used for the new channel. |
| 2280 | if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2281 | return false; |
| 2282 | |
| 2283 | return ChangeSend(channel, desired_send_); |
| 2284 | } |
| 2285 | |
| 2286 | bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32 ssrc) { |
| 2287 | ChannelMap::iterator it = send_channels_.find(ssrc); |
| 2288 | if (it == send_channels_.end()) { |
| 2289 | LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc |
| 2290 | << " which doesn't exist."; |
| 2291 | return false; |
| 2292 | } |
| 2293 | |
| 2294 | int channel = it->second.channel; |
| 2295 | ChangeSend(channel, SEND_NOTHING); |
| 2296 | |
| 2297 | // Notify the audio renderer that the send channel is going away. |
| 2298 | if (it->second.renderer) |
| 2299 | it->second.renderer->RemoveChannel(channel); |
| 2300 | |
| 2301 | if (IsDefaultChannel(channel)) { |
| 2302 | // Do not delete the default channel since the receive channels depend on |
| 2303 | // the default channel, recycle it instead. |
| 2304 | ChangeSend(channel, SEND_NOTHING); |
| 2305 | } else { |
| 2306 | // Clean up and delete the send channel. |
| 2307 | LOG(LS_INFO) << "Removing audio send stream " << ssrc |
| 2308 | << " with VoiceEngine channel #" << channel << "."; |
| 2309 | if (!DeleteChannel(channel)) |
| 2310 | return false; |
| 2311 | } |
| 2312 | |
| 2313 | send_channels_.erase(it); |
| 2314 | if (send_channels_.empty()) |
| 2315 | ChangeSend(SEND_NOTHING); |
| 2316 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2317 | return true; |
| 2318 | } |
| 2319 | |
| 2320 | bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2321 | talk_base::CritScope lock(&receive_channels_cs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (!VERIFY(sp.ssrcs.size() == 1)) |
| 2324 | return false; |
| 2325 | uint32 ssrc = sp.first_ssrc(); |
| 2326 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2327 | if (ssrc == 0) { |
| 2328 | LOG(LS_WARNING) << "AddRecvStream with 0 ssrc is not supported."; |
| 2329 | return false; |
| 2330 | } |
| 2331 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2332 | if (receive_channels_.find(ssrc) != receive_channels_.end()) { |
| 2333 | LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2334 | return false; |
| 2335 | } |
| 2336 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2337 | // Reuse default channel for recv stream in non-conference mode call |
| 2338 | // when the default channel is not being used. |
| 2339 | if (!InConferenceMode() && default_receive_ssrc_ == 0) { |
| 2340 | LOG(LS_INFO) << "Recv stream " << sp.first_ssrc() |
| 2341 | << " reuse default channel"; |
| 2342 | default_receive_ssrc_ = sp.first_ssrc(); |
| 2343 | receive_channels_.insert(std::make_pair( |
| 2344 | default_receive_ssrc_, WebRtcVoiceChannelInfo(voe_channel(), NULL))); |
| 2345 | return SetPlayout(voe_channel(), playout_); |
| 2346 | } |
| 2347 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2348 | // Create a new channel for receiving audio data. |
| 2349 | int channel = engine()->voe()->base()->CreateChannel(); |
| 2350 | if (channel == -1) { |
| 2351 | LOG_RTCERR0(CreateChannel); |
| 2352 | return false; |
| 2353 | } |
| 2354 | |
wu@webrtc.org | 7818752 | 2013-10-07 23:32:02 +0000 | [diff] [blame] | 2355 | if (!ConfigureRecvChannel(channel)) { |
| 2356 | DeleteChannel(channel); |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
| 2360 | receive_channels_.insert( |
| 2361 | std::make_pair(ssrc, WebRtcVoiceChannelInfo(channel, NULL))); |
| 2362 | |
| 2363 | LOG(LS_INFO) << "New audio stream " << ssrc |
| 2364 | << " registered to VoiceEngine channel #" |
| 2365 | << channel << "."; |
| 2366 | return true; |
| 2367 | } |
| 2368 | |
| 2369 | bool WebRtcVoiceMediaChannel::ConfigureRecvChannel(int channel) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2370 | // Configure to use external transport, like our default channel. |
| 2371 | if (engine()->voe()->network()->RegisterExternalTransport( |
| 2372 | channel, *this) == -1) { |
| 2373 | LOG_RTCERR2(SetExternalTransport, channel, this); |
| 2374 | return false; |
| 2375 | } |
| 2376 | |
| 2377 | // Use the same SSRC as our default channel (so the RTCP reports are correct). |
| 2378 | unsigned int send_ssrc; |
| 2379 | webrtc::VoERTP_RTCP* rtp = engine()->voe()->rtp(); |
| 2380 | if (rtp->GetLocalSSRC(voe_channel(), send_ssrc) == -1) { |
| 2381 | LOG_RTCERR2(GetSendSSRC, channel, send_ssrc); |
| 2382 | return false; |
| 2383 | } |
| 2384 | if (rtp->SetLocalSSRC(channel, send_ssrc) == -1) { |
| 2385 | LOG_RTCERR2(SetSendSSRC, channel, send_ssrc); |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
| 2389 | // Use the same recv payload types as our default channel. |
| 2390 | ResetRecvCodecs(channel); |
| 2391 | if (!recv_codecs_.empty()) { |
| 2392 | for (std::vector<AudioCodec>::const_iterator it = recv_codecs_.begin(); |
| 2393 | it != recv_codecs_.end(); ++it) { |
| 2394 | webrtc::CodecInst voe_codec; |
| 2395 | if (engine()->FindWebRtcCodec(*it, &voe_codec)) { |
| 2396 | voe_codec.pltype = it->id; |
| 2397 | voe_codec.rate = 0; // Needed to make GetRecPayloadType work for ISAC |
| 2398 | if (engine()->voe()->codec()->GetRecPayloadType( |
| 2399 | voe_channel(), voe_codec) != -1) { |
| 2400 | if (engine()->voe()->codec()->SetRecPayloadType( |
| 2401 | channel, voe_codec) == -1) { |
| 2402 | LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec)); |
| 2403 | return false; |
| 2404 | } |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2410 | if (InConferenceMode()) { |
| 2411 | // To be in par with the video, voe_channel() is not used for receiving in |
| 2412 | // a conference call. |
| 2413 | if (receive_channels_.empty() && default_receive_ssrc_ == 0 && playout_) { |
| 2414 | // This is the first stream in a multi user meeting. We can now |
| 2415 | // disable playback of the default stream. This since the default |
| 2416 | // stream will probably have received some initial packets before |
| 2417 | // the new stream was added. This will mean that the CN state from |
| 2418 | // the default channel will be mixed in with the other streams |
| 2419 | // throughout the whole meeting, which might be disturbing. |
| 2420 | LOG(LS_INFO) << "Disabling playback on the default voice channel"; |
| 2421 | SetPlayout(voe_channel(), false); |
| 2422 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2423 | } |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2424 | SetNack(channel, nack_enabled_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2425 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2426 | return SetPlayout(channel, playout_); |
| 2427 | } |
| 2428 | |
| 2429 | bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32 ssrc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2430 | talk_base::CritScope lock(&receive_channels_cs_); |
| 2431 | ChannelMap::iterator it = receive_channels_.find(ssrc); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2432 | if (it == receive_channels_.end()) { |
| 2433 | LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc |
| 2434 | << " which doesn't exist."; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2435 | return false; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2436 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2437 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2438 | if (ssrc == default_receive_ssrc_) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2439 | ASSERT(IsDefaultChannel(it->second.channel)); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2440 | // Recycle the default channel is for recv stream. |
| 2441 | if (playout_) |
| 2442 | SetPlayout(voe_channel(), false); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2443 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2444 | if (it->second.renderer) |
| 2445 | it->second.renderer->RemoveChannel(voe_channel()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2446 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2447 | default_receive_ssrc_ = 0; |
| 2448 | receive_channels_.erase(it); |
| 2449 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2450 | } |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2451 | |
| 2452 | // Non default channel. |
| 2453 | // Notify the renderer that channel is going away. |
| 2454 | if (it->second.renderer) |
| 2455 | it->second.renderer->RemoveChannel(it->second.channel); |
| 2456 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2457 | LOG(LS_INFO) << "Removing audio stream " << ssrc |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2458 | << " with VoiceEngine channel #" << it->second.channel << "."; |
| 2459 | if (!DeleteChannel(it->second.channel)) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2460 | // Erase the entry anyhow. |
| 2461 | receive_channels_.erase(it); |
| 2462 | return false; |
| 2463 | } |
| 2464 | |
| 2465 | receive_channels_.erase(it); |
| 2466 | bool enable_default_channel_playout = false; |
| 2467 | if (receive_channels_.empty()) { |
| 2468 | // The last stream was removed. We can now enable the default |
| 2469 | // channel for new channels to be played out immediately without |
| 2470 | // waiting for AddStream messages. |
| 2471 | // We do this for both conference mode and non-conference mode. |
| 2472 | // TODO(oja): Does the default channel still have it's CN state? |
| 2473 | enable_default_channel_playout = true; |
| 2474 | } |
| 2475 | if (!InConferenceMode() && receive_channels_.size() == 1 && |
| 2476 | default_receive_ssrc_ != 0) { |
| 2477 | // Only the default channel is active, enable the playout on default |
| 2478 | // channel. |
| 2479 | enable_default_channel_playout = true; |
| 2480 | } |
| 2481 | if (enable_default_channel_playout && playout_) { |
| 2482 | LOG(LS_INFO) << "Enabling playback on the default voice channel"; |
| 2483 | SetPlayout(voe_channel(), true); |
| 2484 | } |
| 2485 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2486 | return true; |
| 2487 | } |
| 2488 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2489 | bool WebRtcVoiceMediaChannel::SetRemoteRenderer(uint32 ssrc, |
| 2490 | AudioRenderer* renderer) { |
| 2491 | ChannelMap::iterator it = receive_channels_.find(ssrc); |
| 2492 | if (it == receive_channels_.end()) { |
| 2493 | if (renderer) { |
| 2494 | // Return an error if trying to set a valid renderer with an invalid ssrc. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2495 | LOG(LS_ERROR) << "SetRemoteRenderer failed with ssrc "<< ssrc; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2496 | return false; |
| 2497 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2498 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2499 | // The channel likely has gone away, do nothing. |
| 2500 | return true; |
| 2501 | } |
| 2502 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2503 | AudioRenderer* remote_renderer = it->second.renderer; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2504 | if (renderer) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2505 | ASSERT(remote_renderer == NULL || remote_renderer == renderer); |
| 2506 | if (!remote_renderer) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2507 | renderer->AddChannel(it->second.channel); |
| 2508 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2509 | } else if (remote_renderer) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2510 | // |renderer| == NULL, remove the channel from the renderer. |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2511 | remote_renderer->RemoveChannel(it->second.channel); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2514 | // Assign the new value to the struct. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2515 | it->second.renderer = renderer; |
| 2516 | return true; |
| 2517 | } |
| 2518 | |
| 2519 | bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32 ssrc, |
| 2520 | AudioRenderer* renderer) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2521 | ChannelMap::iterator it = send_channels_.find(ssrc); |
| 2522 | if (it == send_channels_.end()) { |
| 2523 | if (renderer) { |
| 2524 | // Return an error if trying to set a valid renderer with an invalid ssrc. |
| 2525 | LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc; |
| 2526 | return false; |
| 2527 | } |
| 2528 | |
| 2529 | // The channel likely has gone away, do nothing. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2530 | return true; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2531 | } |
| 2532 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2533 | AudioRenderer* local_renderer = it->second.renderer; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2534 | if (renderer) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2535 | ASSERT(local_renderer == NULL || local_renderer == renderer); |
| 2536 | if (!local_renderer) |
| 2537 | renderer->AddChannel(it->second.channel); |
| 2538 | } else if (local_renderer) { |
| 2539 | local_renderer->RemoveChannel(it->second.channel); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2542 | // Assign the new value to the struct. |
| 2543 | it->second.renderer = renderer; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2544 | return true; |
| 2545 | } |
| 2546 | |
| 2547 | bool WebRtcVoiceMediaChannel::GetActiveStreams( |
| 2548 | AudioInfo::StreamList* actives) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2549 | // In conference mode, the default channel should not be in |
| 2550 | // |receive_channels_|. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2551 | actives->clear(); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2552 | for (ChannelMap::iterator it = receive_channels_.begin(); |
| 2553 | it != receive_channels_.end(); ++it) { |
| 2554 | int level = GetOutputLevel(it->second.channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2555 | if (level > 0) { |
| 2556 | actives->push_back(std::make_pair(it->first, level)); |
| 2557 | } |
| 2558 | } |
| 2559 | return true; |
| 2560 | } |
| 2561 | |
| 2562 | int WebRtcVoiceMediaChannel::GetOutputLevel() { |
| 2563 | // return the highest output level of all streams |
| 2564 | int highest = GetOutputLevel(voe_channel()); |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2565 | for (ChannelMap::iterator it = receive_channels_.begin(); |
| 2566 | it != receive_channels_.end(); ++it) { |
| 2567 | int level = GetOutputLevel(it->second.channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2568 | highest = talk_base::_max(level, highest); |
| 2569 | } |
| 2570 | return highest; |
| 2571 | } |
| 2572 | |
| 2573 | int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() { |
| 2574 | int ret; |
| 2575 | if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) { |
| 2576 | // In case of error, log the info and continue |
| 2577 | LOG_RTCERR0(TimeSinceLastTyping); |
| 2578 | ret = -1; |
| 2579 | } else { |
| 2580 | ret *= 1000; // We return ms, webrtc returns seconds. |
| 2581 | } |
| 2582 | return ret; |
| 2583 | } |
| 2584 | |
| 2585 | void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window, |
| 2586 | int cost_per_typing, int reporting_threshold, int penalty_decay, |
| 2587 | int type_event_delay) { |
| 2588 | if (engine()->voe()->processing()->SetTypingDetectionParameters( |
| 2589 | time_window, cost_per_typing, |
| 2590 | reporting_threshold, penalty_decay, type_event_delay) == -1) { |
| 2591 | // In case of error, log the info and continue |
| 2592 | LOG_RTCERR5(SetTypingDetectionParameters, time_window, |
| 2593 | cost_per_typing, reporting_threshold, penalty_decay, |
| 2594 | type_event_delay); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | bool WebRtcVoiceMediaChannel::SetOutputScaling( |
| 2599 | uint32 ssrc, double left, double right) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2600 | talk_base::CritScope lock(&receive_channels_cs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2601 | // Collect the channels to scale the output volume. |
| 2602 | std::vector<int> channels; |
| 2603 | if (0 == ssrc) { // Collect all channels, including the default one. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2604 | // Default channel is not in receive_channels_ if it is not being used for |
| 2605 | // playout. |
| 2606 | if (default_receive_ssrc_ == 0) |
| 2607 | channels.push_back(voe_channel()); |
| 2608 | for (ChannelMap::const_iterator it = receive_channels_.begin(); |
| 2609 | it != receive_channels_.end(); ++it) { |
| 2610 | channels.push_back(it->second.channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2611 | } |
| 2612 | } else { // Collect only the channel of the specified ssrc. |
| 2613 | int channel = GetReceiveChannelNum(ssrc); |
| 2614 | if (-1 == channel) { |
| 2615 | LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc; |
| 2616 | return false; |
| 2617 | } |
| 2618 | channels.push_back(channel); |
| 2619 | } |
| 2620 | |
| 2621 | // Scale the output volume for the collected channels. We first normalize to |
| 2622 | // scale the volume and then set the left and right pan. |
| 2623 | float scale = static_cast<float>(talk_base::_max(left, right)); |
| 2624 | if (scale > 0.0001f) { |
| 2625 | left /= scale; |
| 2626 | right /= scale; |
| 2627 | } |
| 2628 | for (std::vector<int>::const_iterator it = channels.begin(); |
| 2629 | it != channels.end(); ++it) { |
| 2630 | if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling( |
| 2631 | *it, scale)) { |
| 2632 | LOG_RTCERR2(SetChannelOutputVolumeScaling, *it, scale); |
| 2633 | return false; |
| 2634 | } |
| 2635 | if (-1 == engine()->voe()->volume()->SetOutputVolumePan( |
| 2636 | *it, static_cast<float>(left), static_cast<float>(right))) { |
| 2637 | LOG_RTCERR3(SetOutputVolumePan, *it, left, right); |
| 2638 | // Do not return if fails. SetOutputVolumePan is not available for all |
| 2639 | // pltforms. |
| 2640 | } |
| 2641 | LOG(LS_INFO) << "SetOutputScaling to left=" << left * scale |
| 2642 | << " right=" << right * scale |
| 2643 | << " for channel " << *it << " and ssrc " << ssrc; |
| 2644 | } |
| 2645 | return true; |
| 2646 | } |
| 2647 | |
| 2648 | bool WebRtcVoiceMediaChannel::GetOutputScaling( |
| 2649 | uint32 ssrc, double* left, double* right) { |
| 2650 | if (!left || !right) return false; |
| 2651 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 2652 | talk_base::CritScope lock(&receive_channels_cs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2653 | // Determine which channel based on ssrc. |
| 2654 | int channel = (0 == ssrc) ? voe_channel() : GetReceiveChannelNum(ssrc); |
| 2655 | if (channel == -1) { |
| 2656 | LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc; |
| 2657 | return false; |
| 2658 | } |
| 2659 | |
| 2660 | float scaling; |
| 2661 | if (-1 == engine()->voe()->volume()->GetChannelOutputVolumeScaling( |
| 2662 | channel, scaling)) { |
| 2663 | LOG_RTCERR2(GetChannelOutputVolumeScaling, channel, scaling); |
| 2664 | return false; |
| 2665 | } |
| 2666 | |
| 2667 | float left_pan; |
| 2668 | float right_pan; |
| 2669 | if (-1 == engine()->voe()->volume()->GetOutputVolumePan( |
| 2670 | channel, left_pan, right_pan)) { |
| 2671 | LOG_RTCERR3(GetOutputVolumePan, channel, left_pan, right_pan); |
| 2672 | // If GetOutputVolumePan fails, we use the default left and right pan. |
| 2673 | left_pan = 1.0f; |
| 2674 | right_pan = 1.0f; |
| 2675 | } |
| 2676 | |
| 2677 | *left = scaling * left_pan; |
| 2678 | *right = scaling * right_pan; |
| 2679 | return true; |
| 2680 | } |
| 2681 | |
| 2682 | bool WebRtcVoiceMediaChannel::SetRingbackTone(const char *buf, int len) { |
| 2683 | ringback_tone_.reset(new WebRtcSoundclipStream(buf, len)); |
| 2684 | return true; |
| 2685 | } |
| 2686 | |
| 2687 | bool WebRtcVoiceMediaChannel::PlayRingbackTone(uint32 ssrc, |
| 2688 | bool play, bool loop) { |
| 2689 | if (!ringback_tone_) { |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
| 2693 | // The voe file api is not available in chrome. |
| 2694 | if (!engine()->voe()->file()) { |
| 2695 | return false; |
| 2696 | } |
| 2697 | |
| 2698 | // Determine which VoiceEngine channel to play on. |
| 2699 | int channel = (ssrc == 0) ? voe_channel() : GetReceiveChannelNum(ssrc); |
| 2700 | if (channel == -1) { |
| 2701 | return false; |
| 2702 | } |
| 2703 | |
| 2704 | // Make sure the ringtone is cued properly, and play it out. |
| 2705 | if (play) { |
| 2706 | ringback_tone_->set_loop(loop); |
| 2707 | ringback_tone_->Rewind(); |
| 2708 | if (engine()->voe()->file()->StartPlayingFileLocally(channel, |
| 2709 | ringback_tone_.get()) == -1) { |
| 2710 | LOG_RTCERR2(StartPlayingFileLocally, channel, ringback_tone_.get()); |
| 2711 | LOG(LS_ERROR) << "Unable to start ringback tone"; |
| 2712 | return false; |
| 2713 | } |
| 2714 | ringback_channels_.insert(channel); |
| 2715 | LOG(LS_INFO) << "Started ringback on channel " << channel; |
| 2716 | } else { |
| 2717 | if (engine()->voe()->file()->IsPlayingFileLocally(channel) == 1 && |
| 2718 | engine()->voe()->file()->StopPlayingFileLocally(channel) == -1) { |
| 2719 | LOG_RTCERR1(StopPlayingFileLocally, channel); |
| 2720 | return false; |
| 2721 | } |
| 2722 | LOG(LS_INFO) << "Stopped ringback on channel " << channel; |
| 2723 | ringback_channels_.erase(channel); |
| 2724 | } |
| 2725 | |
| 2726 | return true; |
| 2727 | } |
| 2728 | |
| 2729 | bool WebRtcVoiceMediaChannel::CanInsertDtmf() { |
| 2730 | return dtmf_allowed_; |
| 2731 | } |
| 2732 | |
| 2733 | bool WebRtcVoiceMediaChannel::InsertDtmf(uint32 ssrc, int event, |
| 2734 | int duration, int flags) { |
| 2735 | if (!dtmf_allowed_) { |
| 2736 | return false; |
| 2737 | } |
| 2738 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2739 | // Send the event. |
| 2740 | if (flags & cricket::DF_SEND) { |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 2741 | int channel = -1; |
| 2742 | if (ssrc == 0) { |
| 2743 | bool default_channel_is_inuse = false; |
| 2744 | for (ChannelMap::const_iterator iter = send_channels_.begin(); |
| 2745 | iter != send_channels_.end(); ++iter) { |
| 2746 | if (IsDefaultChannel(iter->second.channel)) { |
| 2747 | default_channel_is_inuse = true; |
| 2748 | break; |
| 2749 | } |
| 2750 | } |
| 2751 | if (default_channel_is_inuse) { |
| 2752 | channel = voe_channel(); |
| 2753 | } else if (!send_channels_.empty()) { |
| 2754 | channel = send_channels_.begin()->second.channel; |
| 2755 | } |
| 2756 | } else { |
| 2757 | channel = GetSendChannelNum(ssrc); |
| 2758 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2759 | if (channel == -1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2760 | LOG(LS_WARNING) << "InsertDtmf - The specified ssrc " |
| 2761 | << ssrc << " is not in use."; |
| 2762 | return false; |
| 2763 | } |
| 2764 | // Send DTMF using out-of-band DTMF. ("true", as 3rd arg) |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2765 | if (engine()->voe()->dtmf()->SendTelephoneEvent( |
| 2766 | channel, event, true, duration) == -1) { |
| 2767 | LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2768 | return false; |
| 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | // Play the event. |
| 2773 | if (flags & cricket::DF_PLAY) { |
| 2774 | // Play DTMF tone locally. |
| 2775 | if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) { |
| 2776 | LOG_RTCERR2(PlayDtmfTone, event, duration); |
| 2777 | return false; |
| 2778 | } |
| 2779 | } |
| 2780 | |
| 2781 | return true; |
| 2782 | } |
| 2783 | |
| 2784 | void WebRtcVoiceMediaChannel::OnPacketReceived(talk_base::Buffer* packet) { |
| 2785 | // Pick which channel to send this packet to. If this packet doesn't match |
| 2786 | // any multiplexed streams, just send it to the default channel. Otherwise, |
| 2787 | // send it to the specific decoder instance for that stream. |
| 2788 | int which_channel = GetReceiveChannelNum( |
| 2789 | ParseSsrc(packet->data(), packet->length(), false)); |
| 2790 | if (which_channel == -1) { |
| 2791 | which_channel = voe_channel(); |
| 2792 | } |
| 2793 | |
| 2794 | // Stop any ringback that might be playing on the channel. |
| 2795 | // It's possible the ringback has already stopped, ih which case we'll just |
| 2796 | // use the opportunity to remove the channel from ringback_channels_. |
| 2797 | if (engine()->voe()->file()) { |
| 2798 | const std::set<int>::iterator it = ringback_channels_.find(which_channel); |
| 2799 | if (it != ringback_channels_.end()) { |
| 2800 | if (engine()->voe()->file()->IsPlayingFileLocally( |
| 2801 | which_channel) == 1) { |
| 2802 | engine()->voe()->file()->StopPlayingFileLocally(which_channel); |
| 2803 | LOG(LS_INFO) << "Stopped ringback on channel " << which_channel |
| 2804 | << " due to incoming media"; |
| 2805 | } |
| 2806 | ringback_channels_.erase(which_channel); |
| 2807 | } |
| 2808 | } |
| 2809 | |
| 2810 | // Pass it off to the decoder. |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 2811 | engine()->voe()->network()->ReceivedRTPPacket( |
| 2812 | which_channel, |
| 2813 | packet->data(), |
| 2814 | static_cast<unsigned int>(packet->length())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | void WebRtcVoiceMediaChannel::OnRtcpReceived(talk_base::Buffer* packet) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2818 | // Sending channels need all RTCP packets with feedback information. |
| 2819 | // Even sender reports can contain attached report blocks. |
| 2820 | // Receiving channels need sender reports in order to create |
| 2821 | // correct receiver reports. |
| 2822 | int type = 0; |
| 2823 | if (!GetRtcpType(packet->data(), packet->length(), &type)) { |
| 2824 | LOG(LS_WARNING) << "Failed to parse type from received RTCP packet"; |
| 2825 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2828 | // If it is a sender report, find the channel that is listening. |
| 2829 | bool has_sent_to_default_channel = false; |
| 2830 | if (type == kRtcpTypeSR) { |
| 2831 | int which_channel = GetReceiveChannelNum( |
| 2832 | ParseSsrc(packet->data(), packet->length(), true)); |
| 2833 | if (which_channel != -1) { |
| 2834 | engine()->voe()->network()->ReceivedRTCPPacket( |
| 2835 | which_channel, |
| 2836 | packet->data(), |
| 2837 | static_cast<unsigned int>(packet->length())); |
| 2838 | |
| 2839 | if (IsDefaultChannel(which_channel)) |
| 2840 | has_sent_to_default_channel = true; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | // SR may continue RR and any RR entry may correspond to any one of the send |
| 2845 | // channels. So all RTCP packets must be forwarded all send channels. VoE |
| 2846 | // will filter out RR internally. |
| 2847 | for (ChannelMap::iterator iter = send_channels_.begin(); |
| 2848 | iter != send_channels_.end(); ++iter) { |
| 2849 | // Make sure not sending the same packet to default channel more than once. |
| 2850 | if (IsDefaultChannel(iter->second.channel) && has_sent_to_default_channel) |
| 2851 | continue; |
| 2852 | |
| 2853 | engine()->voe()->network()->ReceivedRTCPPacket( |
| 2854 | iter->second.channel, |
| 2855 | packet->data(), |
| 2856 | static_cast<unsigned int>(packet->length())); |
| 2857 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | bool WebRtcVoiceMediaChannel::MuteStream(uint32 ssrc, bool muted) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2861 | int channel = (ssrc == 0) ? voe_channel() : GetSendChannelNum(ssrc); |
| 2862 | if (channel == -1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2863 | LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use."; |
| 2864 | return false; |
| 2865 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2866 | if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) { |
| 2867 | LOG_RTCERR2(SetInputMute, channel, muted); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2868 | return false; |
| 2869 | } |
| 2870 | return true; |
| 2871 | } |
| 2872 | |
| 2873 | bool WebRtcVoiceMediaChannel::SetSendBandwidth(bool autobw, int bps) { |
| 2874 | LOG(LS_INFO) << "WebRtcVoiceMediaChanne::SetSendBandwidth."; |
| 2875 | |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 2876 | send_bw_setting_ = true; |
| 2877 | send_autobw_ = autobw; |
| 2878 | send_bw_bps_ = bps; |
| 2879 | |
| 2880 | return SetSendBandwidthInternal(send_autobw_, send_bw_bps_); |
| 2881 | } |
| 2882 | |
| 2883 | bool WebRtcVoiceMediaChannel::SetSendBandwidthInternal(bool autobw, int bps) { |
| 2884 | LOG(LS_INFO) << "WebRtcVoiceMediaChanne::SetSendBandwidthInternal."; |
| 2885 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2886 | if (!send_codec_) { |
wu@webrtc.org | 1d1ffc9 | 2013-10-16 18:12:02 +0000 | [diff] [blame] | 2887 | LOG(LS_INFO) << "The send codec has not been set up yet. " |
| 2888 | << "The send bandwidth setting will be applied later."; |
| 2889 | return true; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | // Bandwidth is auto by default. |
| 2893 | if (autobw || bps <= 0) |
| 2894 | return true; |
| 2895 | |
| 2896 | webrtc::CodecInst codec = *send_codec_; |
| 2897 | bool is_multi_rate = IsCodecMultiRate(codec); |
| 2898 | |
| 2899 | if (is_multi_rate) { |
| 2900 | // If codec is multi-rate then just set the bitrate. |
| 2901 | codec.rate = bps; |
| 2902 | if (!SetSendCodec(codec)) { |
| 2903 | LOG(LS_INFO) << "Failed to set codec " << codec.plname |
| 2904 | << " to bitrate " << bps << " bps."; |
| 2905 | return false; |
| 2906 | } |
| 2907 | return true; |
| 2908 | } else { |
| 2909 | // If codec is not multi-rate and |bps| is less than the fixed bitrate |
| 2910 | // then fail. If codec is not multi-rate and |bps| exceeds or equal the |
| 2911 | // fixed bitrate then ignore. |
| 2912 | if (bps < codec.rate) { |
| 2913 | LOG(LS_INFO) << "Failed to set codec " << codec.plname |
| 2914 | << " to bitrate " << bps << " bps" |
| 2915 | << ", requires at least " << codec.rate << " bps."; |
| 2916 | return false; |
| 2917 | } |
| 2918 | return true; |
| 2919 | } |
| 2920 | } |
| 2921 | |
| 2922 | bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2923 | bool echo_metrics_on = false; |
| 2924 | // These can take on valid negative values, so use the lowest possible level |
| 2925 | // as default rather than -1. |
| 2926 | int echo_return_loss = -100; |
| 2927 | int echo_return_loss_enhancement = -100; |
| 2928 | // These can also be negative, but in practice -1 is only used to signal |
| 2929 | // insufficient data, since the resolution is limited to multiples of 4 ms. |
| 2930 | int echo_delay_median_ms = -1; |
| 2931 | int echo_delay_std_ms = -1; |
| 2932 | if (engine()->voe()->processing()->GetEcMetricsStatus( |
| 2933 | echo_metrics_on) != -1 && echo_metrics_on) { |
| 2934 | // TODO(ajm): we may want to use VoECallReport::GetEchoMetricsSummary |
| 2935 | // here, but it appears to be unsuitable currently. Revisit after this is |
| 2936 | // investigated: http://b/issue?id=5666755 |
| 2937 | int erl, erle, rerl, anlp; |
| 2938 | if (engine()->voe()->processing()->GetEchoMetrics( |
| 2939 | erl, erle, rerl, anlp) != -1) { |
| 2940 | echo_return_loss = erl; |
| 2941 | echo_return_loss_enhancement = erle; |
| 2942 | } |
| 2943 | |
| 2944 | int median, std; |
| 2945 | if (engine()->voe()->processing()->GetEcDelayMetrics(median, std) != -1) { |
| 2946 | echo_delay_median_ms = median; |
| 2947 | echo_delay_std_ms = std; |
| 2948 | } |
| 2949 | } |
| 2950 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2951 | webrtc::CallStatistics cs; |
| 2952 | unsigned int ssrc; |
| 2953 | webrtc::CodecInst codec; |
| 2954 | unsigned int level; |
| 2955 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2956 | for (ChannelMap::const_iterator channel_iter = send_channels_.begin(); |
| 2957 | channel_iter != send_channels_.end(); ++channel_iter) { |
| 2958 | const int channel = channel_iter->second.channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2959 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2960 | // Fill in the sender info, based on what we know, and what the |
| 2961 | // remote side told us it got from its RTCP report. |
| 2962 | VoiceSenderInfo sinfo; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2963 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2964 | if (engine()->voe()->rtp()->GetRTCPStatistics(channel, cs) == -1 || |
| 2965 | engine()->voe()->rtp()->GetLocalSSRC(channel, ssrc) == -1) { |
| 2966 | continue; |
| 2967 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 2968 | |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 2969 | sinfo.ssrc = ssrc; |
| 2970 | sinfo.codec_name = send_codec_.get() ? send_codec_->plname : ""; |
| 2971 | sinfo.bytes_sent = cs.bytesSent; |
| 2972 | sinfo.packets_sent = cs.packetsSent; |
| 2973 | // RTT isn't known until a RTCP report is received. Until then, VoiceEngine |
| 2974 | // returns 0 to indicate an error value. |
| 2975 | sinfo.rtt_ms = (cs.rttMs > 0) ? cs.rttMs : -1; |
| 2976 | |
| 2977 | // Get data from the last remote RTCP report. Use default values if no data |
| 2978 | // available. |
| 2979 | sinfo.fraction_lost = -1.0; |
| 2980 | sinfo.jitter_ms = -1; |
| 2981 | sinfo.packets_lost = -1; |
| 2982 | sinfo.ext_seqnum = -1; |
| 2983 | std::vector<webrtc::ReportBlock> receive_blocks; |
| 2984 | if (engine()->voe()->rtp()->GetRemoteRTCPReportBlocks( |
| 2985 | channel, &receive_blocks) != -1 && |
| 2986 | engine()->voe()->codec()->GetSendCodec(channel, codec) != -1) { |
| 2987 | std::vector<webrtc::ReportBlock>::iterator iter; |
| 2988 | for (iter = receive_blocks.begin(); iter != receive_blocks.end(); |
| 2989 | ++iter) { |
| 2990 | // Lookup report for send ssrc only. |
| 2991 | if (iter->source_SSRC == sinfo.ssrc) { |
| 2992 | // Convert Q8 to floating point. |
| 2993 | sinfo.fraction_lost = static_cast<float>(iter->fraction_lost) / 256; |
| 2994 | // Convert samples to milliseconds. |
| 2995 | if (codec.plfreq / 1000 > 0) { |
| 2996 | sinfo.jitter_ms = iter->interarrival_jitter / (codec.plfreq / 1000); |
| 2997 | } |
| 2998 | sinfo.packets_lost = iter->cumulative_num_packets_lost; |
| 2999 | sinfo.ext_seqnum = iter->extended_highest_sequence_number; |
| 3000 | break; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3001 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3002 | } |
| 3003 | } |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3004 | |
| 3005 | // Local speech level. |
| 3006 | sinfo.audio_level = (engine()->voe()->volume()-> |
| 3007 | GetSpeechInputLevelFullRange(level) != -1) ? level : -1; |
| 3008 | |
| 3009 | // TODO(xians): We are injecting the same APM logging to all the send |
| 3010 | // channels here because there is no good way to know which send channel |
| 3011 | // is using the APM. The correct fix is to allow the send channels to have |
| 3012 | // their own APM so that we can feed the correct APM logging to different |
| 3013 | // send channels. See issue crbug/264611 . |
| 3014 | sinfo.echo_return_loss = echo_return_loss; |
| 3015 | sinfo.echo_return_loss_enhancement = echo_return_loss_enhancement; |
| 3016 | sinfo.echo_delay_median_ms = echo_delay_median_ms; |
| 3017 | sinfo.echo_delay_std_ms = echo_delay_std_ms; |
mallinath@webrtc.org | a27be8e | 2013-09-27 23:04:10 +0000 | [diff] [blame] | 3018 | // TODO(ajm): Re-enable this metric once we have a reliable implementation. |
| 3019 | sinfo.aec_quality_min = -1; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 3020 | sinfo.typing_noise_detected = typing_noise_detected_; |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3021 | |
| 3022 | info->senders.push_back(sinfo); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3025 | // Build the list of receivers, one for each receiving channel, or 1 in |
| 3026 | // a 1:1 call. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3027 | std::vector<int> channels; |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3028 | for (ChannelMap::const_iterator it = receive_channels_.begin(); |
| 3029 | it != receive_channels_.end(); ++it) { |
| 3030 | channels.push_back(it->second.channel); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3031 | } |
| 3032 | if (channels.empty()) { |
| 3033 | channels.push_back(voe_channel()); |
| 3034 | } |
| 3035 | |
| 3036 | // Get the SSRC and stats for each receiver, based on our own calculations. |
| 3037 | for (std::vector<int>::const_iterator it = channels.begin(); |
| 3038 | it != channels.end(); ++it) { |
| 3039 | memset(&cs, 0, sizeof(cs)); |
| 3040 | if (engine()->voe()->rtp()->GetRemoteSSRC(*it, ssrc) != -1 && |
| 3041 | engine()->voe()->rtp()->GetRTCPStatistics(*it, cs) != -1 && |
| 3042 | engine()->voe()->codec()->GetRecCodec(*it, codec) != -1) { |
| 3043 | VoiceReceiverInfo rinfo; |
| 3044 | rinfo.ssrc = ssrc; |
| 3045 | rinfo.bytes_rcvd = cs.bytesReceived; |
| 3046 | rinfo.packets_rcvd = cs.packetsReceived; |
| 3047 | // The next four fields are from the most recently sent RTCP report. |
| 3048 | // Convert Q8 to floating point. |
| 3049 | rinfo.fraction_lost = static_cast<float>(cs.fractionLost) / (1 << 8); |
| 3050 | rinfo.packets_lost = cs.cumulativeLost; |
| 3051 | rinfo.ext_seqnum = cs.extendedMax; |
| 3052 | // Convert samples to milliseconds. |
| 3053 | if (codec.plfreq / 1000 > 0) { |
| 3054 | rinfo.jitter_ms = cs.jitterSamples / (codec.plfreq / 1000); |
| 3055 | } |
| 3056 | |
| 3057 | // Get jitter buffer and total delay (alg + jitter + playout) stats. |
| 3058 | webrtc::NetworkStatistics ns; |
| 3059 | if (engine()->voe()->neteq() && |
| 3060 | engine()->voe()->neteq()->GetNetworkStatistics( |
| 3061 | *it, ns) != -1) { |
| 3062 | rinfo.jitter_buffer_ms = ns.currentBufferSize; |
| 3063 | rinfo.jitter_buffer_preferred_ms = ns.preferredBufferSize; |
| 3064 | rinfo.expand_rate = |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 3065 | static_cast<float>(ns.currentExpandRate) / (1 << 14); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3066 | } |
| 3067 | if (engine()->voe()->sync()) { |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 3068 | int jitter_buffer_delay_ms = 0; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3069 | int playout_buffer_delay_ms = 0; |
| 3070 | engine()->voe()->sync()->GetDelayEstimate( |
sergeyu@chromium.org | a23f0ca | 2013-11-13 22:48:52 +0000 | [diff] [blame^] | 3071 | *it, &jitter_buffer_delay_ms, &playout_buffer_delay_ms); |
| 3072 | rinfo.delay_estimate_ms = jitter_buffer_delay_ms + |
| 3073 | playout_buffer_delay_ms; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | // Get speech level. |
| 3077 | rinfo.audio_level = (engine()->voe()->volume()-> |
| 3078 | GetSpeechOutputLevelFullRange(*it, level) != -1) ? level : -1; |
| 3079 | info->receivers.push_back(rinfo); |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | return true; |
| 3084 | } |
| 3085 | |
| 3086 | void WebRtcVoiceMediaChannel::GetLastMediaError( |
| 3087 | uint32* ssrc, VoiceMediaChannel::Error* error) { |
| 3088 | ASSERT(ssrc != NULL); |
| 3089 | ASSERT(error != NULL); |
| 3090 | FindSsrc(voe_channel(), ssrc); |
| 3091 | *error = WebRtcErrorToChannelError(GetLastEngineError()); |
| 3092 | } |
| 3093 | |
| 3094 | bool WebRtcVoiceMediaChannel::FindSsrc(int channel_num, uint32* ssrc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3095 | talk_base::CritScope lock(&receive_channels_cs_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3096 | ASSERT(ssrc != NULL); |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3097 | if (channel_num == -1 && send_ != SEND_NOTHING) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3098 | // Sometimes the VoiceEngine core will throw error with channel_num = -1. |
| 3099 | // This means the error is not limited to a specific channel. Signal the |
| 3100 | // message using ssrc=0. If the current channel is sending, use this |
| 3101 | // channel for sending the message. |
| 3102 | *ssrc = 0; |
| 3103 | return true; |
| 3104 | } else { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3105 | // Check whether this is a sending channel. |
| 3106 | for (ChannelMap::const_iterator it = send_channels_.begin(); |
| 3107 | it != send_channels_.end(); ++it) { |
| 3108 | if (it->second.channel == channel_num) { |
| 3109 | // This is a sending channel. |
| 3110 | uint32 local_ssrc = 0; |
| 3111 | if (engine()->voe()->rtp()->GetLocalSSRC( |
| 3112 | channel_num, local_ssrc) != -1) { |
| 3113 | *ssrc = local_ssrc; |
| 3114 | } |
| 3115 | return true; |
| 3116 | } |
| 3117 | } |
| 3118 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3119 | // Check whether this is a receiving channel. |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3120 | for (ChannelMap::const_iterator it = receive_channels_.begin(); |
| 3121 | it != receive_channels_.end(); ++it) { |
| 3122 | if (it->second.channel == channel_num) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3123 | *ssrc = it->first; |
| 3124 | return true; |
| 3125 | } |
| 3126 | } |
| 3127 | } |
| 3128 | return false; |
| 3129 | } |
| 3130 | |
| 3131 | void WebRtcVoiceMediaChannel::OnError(uint32 ssrc, int error) { |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 3132 | if (error == VE_TYPING_NOISE_WARNING) { |
| 3133 | typing_noise_detected_ = true; |
| 3134 | } else if (error == VE_TYPING_NOISE_OFF_WARNING) { |
| 3135 | typing_noise_detected_ = false; |
| 3136 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3137 | SignalMediaError(ssrc, WebRtcErrorToChannelError(error)); |
| 3138 | } |
| 3139 | |
| 3140 | int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) { |
| 3141 | unsigned int ulevel; |
| 3142 | int ret = |
| 3143 | engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel); |
| 3144 | return (ret == 0) ? static_cast<int>(ulevel) : -1; |
| 3145 | } |
| 3146 | |
| 3147 | int WebRtcVoiceMediaChannel::GetReceiveChannelNum(uint32 ssrc) { |
henrike@webrtc.org | 1e09a71 | 2013-07-26 19:17:59 +0000 | [diff] [blame] | 3148 | ChannelMap::iterator it = receive_channels_.find(ssrc); |
| 3149 | if (it != receive_channels_.end()) |
| 3150 | return it->second.channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3151 | return (ssrc == default_receive_ssrc_) ? voe_channel() : -1; |
| 3152 | } |
| 3153 | |
| 3154 | int WebRtcVoiceMediaChannel::GetSendChannelNum(uint32 ssrc) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3155 | ChannelMap::iterator it = send_channels_.find(ssrc); |
| 3156 | if (it != send_channels_.end()) |
| 3157 | return it->second.channel; |
| 3158 | |
| 3159 | return -1; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
| 3162 | bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec, |
| 3163 | const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) { |
| 3164 | // Get the RED encodings from the parameter with no name. This may |
| 3165 | // change based on what is discussed on the Jingle list. |
| 3166 | // The encoding parameter is of the form "a/b"; we only support where |
| 3167 | // a == b. Verify this and parse out the value into red_pt. |
| 3168 | // If the parameter value is absent (as it will be until we wire up the |
| 3169 | // signaling of this message), use the second codec specified (i.e. the |
| 3170 | // one after "red") as the encoding parameter. |
| 3171 | int red_pt = -1; |
| 3172 | std::string red_params; |
| 3173 | CodecParameterMap::const_iterator it = red_codec.params.find(""); |
| 3174 | if (it != red_codec.params.end()) { |
| 3175 | red_params = it->second; |
| 3176 | std::vector<std::string> red_pts; |
| 3177 | if (talk_base::split(red_params, '/', &red_pts) != 2 || |
| 3178 | red_pts[0] != red_pts[1] || |
| 3179 | !talk_base::FromString(red_pts[0], &red_pt)) { |
| 3180 | LOG(LS_WARNING) << "RED params " << red_params << " not supported."; |
| 3181 | return false; |
| 3182 | } |
| 3183 | } else if (red_codec.params.empty()) { |
| 3184 | LOG(LS_WARNING) << "RED params not present, using defaults"; |
| 3185 | if (all_codecs.size() > 1) { |
| 3186 | red_pt = all_codecs[1].id; |
| 3187 | } |
| 3188 | } |
| 3189 | |
| 3190 | // Try to find red_pt in |codecs|. |
| 3191 | std::vector<AudioCodec>::const_iterator codec; |
| 3192 | for (codec = all_codecs.begin(); codec != all_codecs.end(); ++codec) { |
| 3193 | if (codec->id == red_pt) |
| 3194 | break; |
| 3195 | } |
| 3196 | |
| 3197 | // If we find the right codec, that will be the codec we pass to |
| 3198 | // SetSendCodec, with the desired payload type. |
| 3199 | if (codec != all_codecs.end() && |
| 3200 | engine()->FindWebRtcCodec(*codec, send_codec)) { |
| 3201 | } else { |
| 3202 | LOG(LS_WARNING) << "RED params " << red_params << " are invalid."; |
| 3203 | return false; |
| 3204 | } |
| 3205 | |
| 3206 | return true; |
| 3207 | } |
| 3208 | |
| 3209 | bool WebRtcVoiceMediaChannel::EnableRtcp(int channel) { |
| 3210 | if (engine()->voe()->rtp()->SetRTCPStatus(channel, true) == -1) { |
wu@webrtc.org | 9dba525 | 2013-08-05 20:36:57 +0000 | [diff] [blame] | 3211 | LOG_RTCERR2(SetRTCPStatus, channel, 1); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3212 | return false; |
| 3213 | } |
| 3214 | // TODO(juberti): Enable VQMon and RTCP XR reports, once we know what |
| 3215 | // what we want to do with them. |
| 3216 | // engine()->voe().EnableVQMon(voe_channel(), true); |
| 3217 | // engine()->voe().EnableRTCP_XR(voe_channel(), true); |
| 3218 | return true; |
| 3219 | } |
| 3220 | |
| 3221 | bool WebRtcVoiceMediaChannel::ResetRecvCodecs(int channel) { |
| 3222 | int ncodecs = engine()->voe()->codec()->NumOfCodecs(); |
| 3223 | for (int i = 0; i < ncodecs; ++i) { |
| 3224 | webrtc::CodecInst voe_codec; |
| 3225 | if (engine()->voe()->codec()->GetCodec(i, voe_codec) != -1) { |
| 3226 | voe_codec.pltype = -1; |
| 3227 | if (engine()->voe()->codec()->SetRecPayloadType( |
| 3228 | channel, voe_codec) == -1) { |
| 3229 | LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec)); |
| 3230 | return false; |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | return true; |
| 3235 | } |
| 3236 | |
| 3237 | bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) { |
| 3238 | if (playout) { |
| 3239 | LOG(LS_INFO) << "Starting playout for channel #" << channel; |
| 3240 | if (engine()->voe()->base()->StartPlayout(channel) == -1) { |
| 3241 | LOG_RTCERR1(StartPlayout, channel); |
| 3242 | return false; |
| 3243 | } |
| 3244 | } else { |
| 3245 | LOG(LS_INFO) << "Stopping playout for channel #" << channel; |
| 3246 | engine()->voe()->base()->StopPlayout(channel); |
| 3247 | } |
| 3248 | return true; |
| 3249 | } |
| 3250 | |
| 3251 | uint32 WebRtcVoiceMediaChannel::ParseSsrc(const void* data, size_t len, |
| 3252 | bool rtcp) { |
| 3253 | size_t ssrc_pos = (!rtcp) ? 8 : 4; |
| 3254 | uint32 ssrc = 0; |
| 3255 | if (len >= (ssrc_pos + sizeof(ssrc))) { |
| 3256 | ssrc = talk_base::GetBE32(static_cast<const char*>(data) + ssrc_pos); |
| 3257 | } |
| 3258 | return ssrc; |
| 3259 | } |
| 3260 | |
| 3261 | // Convert VoiceEngine error code into VoiceMediaChannel::Error enum. |
| 3262 | VoiceMediaChannel::Error |
| 3263 | WebRtcVoiceMediaChannel::WebRtcErrorToChannelError(int err_code) { |
| 3264 | switch (err_code) { |
| 3265 | case 0: |
| 3266 | return ERROR_NONE; |
| 3267 | case VE_CANNOT_START_RECORDING: |
| 3268 | case VE_MIC_VOL_ERROR: |
| 3269 | case VE_GET_MIC_VOL_ERROR: |
| 3270 | case VE_CANNOT_ACCESS_MIC_VOL: |
| 3271 | return ERROR_REC_DEVICE_OPEN_FAILED; |
| 3272 | case VE_SATURATION_WARNING: |
| 3273 | return ERROR_REC_DEVICE_SATURATION; |
| 3274 | case VE_REC_DEVICE_REMOVED: |
| 3275 | return ERROR_REC_DEVICE_REMOVED; |
| 3276 | case VE_RUNTIME_REC_WARNING: |
| 3277 | case VE_RUNTIME_REC_ERROR: |
| 3278 | return ERROR_REC_RUNTIME_ERROR; |
| 3279 | case VE_CANNOT_START_PLAYOUT: |
| 3280 | case VE_SPEAKER_VOL_ERROR: |
| 3281 | case VE_GET_SPEAKER_VOL_ERROR: |
| 3282 | case VE_CANNOT_ACCESS_SPEAKER_VOL: |
| 3283 | return ERROR_PLAY_DEVICE_OPEN_FAILED; |
| 3284 | case VE_RUNTIME_PLAY_WARNING: |
| 3285 | case VE_RUNTIME_PLAY_ERROR: |
| 3286 | return ERROR_PLAY_RUNTIME_ERROR; |
| 3287 | case VE_TYPING_NOISE_WARNING: |
| 3288 | return ERROR_REC_TYPING_NOISE_DETECTED; |
| 3289 | default: |
| 3290 | return VoiceMediaChannel::ERROR_OTHER; |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | int WebRtcSoundclipStream::Read(void *buf, int len) { |
| 3295 | size_t res = 0; |
| 3296 | mem_.Read(buf, len, &res, NULL); |
henrike@webrtc.org | 28654cb | 2013-07-22 21:07:49 +0000 | [diff] [blame] | 3297 | return static_cast<int>(res); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | int WebRtcSoundclipStream::Rewind() { |
| 3301 | mem_.Rewind(); |
| 3302 | // Return -1 to keep VoiceEngine from looping. |
| 3303 | return (loop_) ? 0 : -1; |
| 3304 | } |
| 3305 | |
| 3306 | } // namespace cricket |
| 3307 | |
| 3308 | #endif // HAVE_WEBRTC_VOICE |