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