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