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