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