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