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