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