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