| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/copresence/mediums/audio/audio_manager_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <limits> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/bind_helpers.h" | |
| 13 #include "base/command_line.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "base/strings/sys_string_conversions.h" | |
| 19 #include "base/time/time.h" | |
| 20 #include "components/copresence/copresence_switches.h" | |
| 21 #include "components/copresence/mediums/audio/audio_player_impl.h" | |
| 22 #include "components/copresence/mediums/audio/audio_recorder_impl.h" | |
| 23 #include "components/copresence/public/copresence_constants.h" | |
| 24 #include "components/copresence/public/whispernet_client.h" | |
| 25 #include "content/public/browser/browser_thread.h" | |
| 26 #include "media/audio/audio_manager.h" | |
| 27 #include "media/audio/audio_manager_base.h" | |
| 28 #include "media/base/audio_bus.h" | |
| 29 #include "third_party/webrtc/common_audio/wav_file.h" | |
| 30 | |
| 31 namespace copresence { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes. | |
| 36 const int kMaxSamples = 10000; | |
| 37 const int kTokenTimeoutMs = 2000; | |
| 38 const int kMonoChannelCount = 1; | |
| 39 | |
| 40 // UrlSafe is defined as: | |
| 41 // '/' represented by a '_' and '+' represented by a '-' | |
| 42 // TODO(ckehoe): Move this to a central place. | |
| 43 std::string FromUrlSafe(std::string token) { | |
| 44 base::ReplaceChars(token, "-", "+", &token); | |
| 45 base::ReplaceChars(token, "_", "/", &token); | |
| 46 return token; | |
| 47 } | |
| 48 std::string ToUrlSafe(std::string token) { | |
| 49 base::ReplaceChars(token, "+", "-", &token); | |
| 50 base::ReplaceChars(token, "/", "_", &token); | |
| 51 return token; | |
| 52 } | |
| 53 | |
| 54 // TODO(ckehoe): Move this to a central place. | |
| 55 std::string AudioTypeToString(AudioType audio_type) { | |
| 56 if (audio_type == AUDIBLE) | |
| 57 return "audible"; | |
| 58 if (audio_type == INAUDIBLE) | |
| 59 return "inaudible"; | |
| 60 | |
| 61 NOTREACHED() << "Got unexpected token type " << audio_type; | |
| 62 return std::string(); | |
| 63 } | |
| 64 | |
| 65 bool ReadBooleanFlag(const std::string& flag, bool default_value) { | |
| 66 const std::string flag_value = base::StringToLowerASCII( | |
| 67 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(flag)); | |
| 68 if (flag_value == "true" || flag_value == "1") | |
| 69 return true; | |
| 70 if (flag_value == "false" || flag_value == "0") | |
| 71 return false; | |
| 72 LOG_IF(ERROR, !flag_value.empty()) | |
| 73 << "Unrecognized value \"" << flag_value << " for flag " | |
| 74 << flag << ". Defaulting to " << default_value; | |
| 75 return default_value; | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 | |
| 81 // Public functions. | |
| 82 | |
| 83 AudioManagerImpl::AudioManagerImpl() | |
| 84 : whispernet_client_(nullptr), recorder_(nullptr) { | |
| 85 // TODO(rkc): Move all of these into initializer lists once it is allowed. | |
| 86 should_be_playing_[AUDIBLE] = false; | |
| 87 should_be_playing_[INAUDIBLE] = false; | |
| 88 should_be_recording_[AUDIBLE] = false; | |
| 89 should_be_recording_[INAUDIBLE] = false; | |
| 90 | |
| 91 player_enabled_[AUDIBLE] = ReadBooleanFlag( | |
| 92 switches::kCopresenceEnableAudibleBroadcast, true); | |
| 93 player_enabled_[INAUDIBLE] = ReadBooleanFlag( | |
| 94 switches::kCopresenceEnableInaudibleBroadcast, true); | |
| 95 player_[AUDIBLE] = nullptr; | |
| 96 player_[INAUDIBLE] = nullptr; | |
| 97 token_length_[0] = 0; | |
| 98 token_length_[1] = 0; | |
| 99 } | |
| 100 | |
| 101 void AudioManagerImpl::Initialize(WhispernetClient* whispernet_client, | |
| 102 const TokensCallback& tokens_cb) { | |
| 103 samples_cache_.resize(2); | |
| 104 samples_cache_[AUDIBLE] = new SamplesMap( | |
| 105 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); | |
| 106 samples_cache_[INAUDIBLE] = new SamplesMap( | |
| 107 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); | |
| 108 | |
| 109 DCHECK(whispernet_client); | |
| 110 whispernet_client_ = whispernet_client; | |
| 111 tokens_cb_ = tokens_cb; | |
| 112 | |
| 113 // These will be unregistered on destruction, so unretained is safe to use. | |
| 114 whispernet_client_->RegisterTokensCallback( | |
| 115 base::Bind(&AudioManagerImpl::OnTokensFound, base::Unretained(this))); | |
| 116 whispernet_client_->RegisterSamplesCallback( | |
| 117 base::Bind(&AudioManagerImpl::OnTokenEncoded, base::Unretained(this))); | |
| 118 | |
| 119 if (!player_[AUDIBLE]) | |
| 120 player_[AUDIBLE] = new AudioPlayerImpl(); | |
| 121 player_[AUDIBLE]->Initialize(); | |
| 122 | |
| 123 if (!player_[INAUDIBLE]) | |
| 124 player_[INAUDIBLE] = new AudioPlayerImpl(); | |
| 125 player_[INAUDIBLE]->Initialize(); | |
| 126 | |
| 127 decode_cancelable_cb_.Reset(base::Bind( | |
| 128 &AudioManagerImpl::DecodeSamplesConnector, base::Unretained(this))); | |
| 129 if (!recorder_) | |
| 130 recorder_ = new AudioRecorderImpl(); | |
| 131 recorder_->Initialize(decode_cancelable_cb_.callback()); | |
| 132 | |
| 133 dump_tokens_dir_ = base::FilePath(base::CommandLine::ForCurrentProcess() | |
| 134 ->GetSwitchValueNative(switches::kCopresenceDumpTokensToDir)); | |
| 135 } | |
| 136 | |
| 137 AudioManagerImpl::~AudioManagerImpl() { | |
| 138 if (player_[AUDIBLE]) | |
| 139 player_[AUDIBLE]->Finalize(); | |
| 140 if (player_[INAUDIBLE]) | |
| 141 player_[INAUDIBLE]->Finalize(); | |
| 142 if (recorder_) | |
| 143 recorder_->Finalize(); | |
| 144 | |
| 145 // Whispernet initialization may never have completed. | |
| 146 if (whispernet_client_) { | |
| 147 whispernet_client_->RegisterTokensCallback(TokensCallback()); | |
| 148 whispernet_client_->RegisterSamplesCallback(SamplesCallback()); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 void AudioManagerImpl::StartPlaying(AudioType type) { | |
| 153 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 154 should_be_playing_[type] = true; | |
| 155 // If we don't have our token encoded yet, this check will be false, for now. | |
| 156 // Once our token is encoded, OnTokenEncoded will call UpdateToken, which | |
| 157 // will call this code again (if we're still supposed to be playing). | |
| 158 if (samples_cache_[type]->HasKey(playing_token_[type])) { | |
| 159 DCHECK(!playing_token_[type].empty()); | |
| 160 if (player_enabled_[type]) { | |
| 161 started_playing_[type] = base::Time::Now(); | |
| 162 player_[type]->Play(samples_cache_[type]->GetValue(playing_token_[type])); | |
| 163 | |
| 164 // If we're playing, we always record to hear what we are playing. | |
| 165 recorder_->Record(); | |
| 166 } else { | |
| 167 DVLOG(3) << "Skipping playback for disabled " << AudioTypeToString(type) | |
| 168 << " player."; | |
| 169 } | |
| 170 } | |
| 171 } | |
| 172 | |
| 173 void AudioManagerImpl::StopPlaying(AudioType type) { | |
| 174 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 175 should_be_playing_[type] = false; | |
| 176 player_[type]->Stop(); | |
| 177 // If we were only recording to hear our own played tokens, stop. | |
| 178 if (!should_be_recording_[AUDIBLE] && !should_be_recording_[INAUDIBLE]) | |
| 179 recorder_->Stop(); | |
| 180 playing_token_[type] = std::string(); | |
| 181 } | |
| 182 | |
| 183 void AudioManagerImpl::StartRecording(AudioType type) { | |
| 184 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 185 should_be_recording_[type] = true; | |
| 186 recorder_->Record(); | |
| 187 } | |
| 188 | |
| 189 void AudioManagerImpl::StopRecording(AudioType type) { | |
| 190 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 191 should_be_recording_[type] = false; | |
| 192 recorder_->Stop(); | |
| 193 } | |
| 194 | |
| 195 void AudioManagerImpl::SetToken(AudioType type, | |
| 196 const std::string& url_safe_token) { | |
| 197 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 198 std::string token = FromUrlSafe(url_safe_token); | |
| 199 if (!samples_cache_[type]->HasKey(token)) { | |
| 200 whispernet_client_->EncodeToken(token, type); | |
| 201 } else { | |
| 202 UpdateToken(type, token); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 const std::string AudioManagerImpl::GetToken(AudioType type) { | |
| 207 return playing_token_[type]; | |
| 208 } | |
| 209 | |
| 210 bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) { | |
| 211 base::TimeDelta tokenTimeout = | |
| 212 base::TimeDelta::FromMilliseconds(kTokenTimeoutMs); | |
| 213 | |
| 214 // This is a bit of a hack. If we haven't been playing long enough, | |
| 215 // return true to avoid tripping an audio fail alarm. | |
| 216 if (base::Time::Now() - started_playing_[type] < tokenTimeout) | |
| 217 return true; | |
| 218 | |
| 219 return base::Time::Now() - heard_own_token_[type] < tokenTimeout; | |
| 220 } | |
| 221 | |
| 222 void AudioManagerImpl::SetTokenLength(AudioType type, size_t token_length) { | |
| 223 token_length_[type] = token_length; | |
| 224 } | |
| 225 | |
| 226 | |
| 227 // Private functions. | |
| 228 | |
| 229 void AudioManagerImpl::OnTokenEncoded( | |
| 230 AudioType type, | |
| 231 const std::string& token, | |
| 232 const scoped_refptr<media::AudioBusRefCounted>& samples) { | |
| 233 samples_cache_[type]->Add(token, samples); | |
| 234 DumpToken(type, token, samples.get()); | |
| 235 UpdateToken(type, token); | |
| 236 } | |
| 237 | |
| 238 void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { | |
| 239 std::vector<AudioToken> tokens_to_report; | |
| 240 for (const auto& token : tokens) { | |
| 241 AudioType type = token.audible ? AUDIBLE : INAUDIBLE; | |
| 242 if (playing_token_[type] == token.token) | |
| 243 heard_own_token_[type] = base::Time::Now(); | |
| 244 | |
| 245 if (should_be_recording_[AUDIBLE] && token.audible) { | |
| 246 tokens_to_report.push_back(token); | |
| 247 } else if (should_be_recording_[INAUDIBLE] && !token.audible) { | |
| 248 tokens_to_report.push_back(token); | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 if (!tokens_to_report.empty()) | |
| 253 tokens_cb_.Run(tokens_to_report); | |
| 254 } | |
| 255 | |
| 256 void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) { | |
| 257 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 258 if (playing_token_[type] == token) | |
| 259 return; | |
| 260 | |
| 261 // Update token. | |
| 262 playing_token_[type] = token; | |
| 263 | |
| 264 // If we are supposed to be playing this token type at this moment, switch | |
| 265 // out playback with the new samples. | |
| 266 if (should_be_playing_[type]) | |
| 267 RestartPlaying(type); | |
| 268 } | |
| 269 | |
| 270 void AudioManagerImpl::RestartPlaying(AudioType type) { | |
| 271 DCHECK(type == AUDIBLE || type == INAUDIBLE); | |
| 272 // We should already have this token in the cache. This function is not | |
| 273 // called from anywhere except update token and only once we have our samples | |
| 274 // in the cache. | |
| 275 DCHECK(samples_cache_[type]->HasKey(playing_token_[type])); | |
| 276 | |
| 277 player_[type]->Stop(); | |
| 278 StartPlaying(type); | |
| 279 } | |
| 280 | |
| 281 void AudioManagerImpl::DecodeSamplesConnector(const std::string& samples) { | |
| 282 // If we are either supposed to be recording *or* playing, audible or | |
| 283 // inaudible, we should be decoding that type. This is so that if we are | |
| 284 // just playing, we will still decode our recorded token so we can check | |
| 285 // if we heard our own token. Whether or not we report the token to the | |
| 286 // server is checked for and handled in OnTokensFound. | |
| 287 | |
| 288 bool decode_audible = | |
| 289 should_be_recording_[AUDIBLE] || should_be_playing_[AUDIBLE]; | |
| 290 bool decode_inaudible = | |
| 291 should_be_recording_[INAUDIBLE] || should_be_playing_[INAUDIBLE]; | |
| 292 | |
| 293 if (decode_audible && decode_inaudible) { | |
| 294 whispernet_client_->DecodeSamples(BOTH, samples, token_length_); | |
| 295 } else if (decode_audible) { | |
| 296 whispernet_client_->DecodeSamples(AUDIBLE, samples, token_length_); | |
| 297 } else if (decode_inaudible) { | |
| 298 whispernet_client_->DecodeSamples(INAUDIBLE, samples, token_length_); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 void AudioManagerImpl::DumpToken(AudioType audio_type, | |
| 303 const std::string& token, | |
| 304 const media::AudioBus* samples) { | |
| 305 if (dump_tokens_dir_.empty()) | |
| 306 return; | |
| 307 | |
| 308 // Convert the samples to 16-bit integers. | |
| 309 std::vector<int16_t> int_samples; | |
| 310 int_samples.reserve(samples->frames()); | |
| 311 for (int i = 0; i < samples->frames(); i++) { | |
| 312 int_samples.push_back(round( | |
| 313 samples->channel(0)[i] * std::numeric_limits<int16_t>::max())); | |
| 314 } | |
| 315 DCHECK_EQ(static_cast<int>(int_samples.size()), samples->frames()); | |
| 316 DCHECK_EQ(kMonoChannelCount, samples->channels()); | |
| 317 | |
| 318 const std::string filename = base::StringPrintf("%s %s.wav", | |
| 319 AudioTypeToString(audio_type).c_str(), ToUrlSafe(token).c_str()); | |
| 320 DVLOG(3) << "Dumping token " << filename; | |
| 321 | |
| 322 std::string file_str; | |
| 323 #if defined(OS_WIN) | |
| 324 base::FilePath file_path = dump_tokens_dir_.Append( | |
| 325 base::SysNativeMBToWide(filename)); | |
| 326 file_str = base::SysWideToNativeMB(file_path.value()); | |
| 327 #else | |
| 328 file_str = dump_tokens_dir_.Append(filename).value(); | |
| 329 #endif | |
| 330 | |
| 331 webrtc::WavWriter writer(file_str, kDefaultSampleRate, kMonoChannelCount); | |
| 332 writer.WriteSamples(int_samples.data(), int_samples.size()); | |
| 333 } | |
| 334 | |
| 335 } // namespace copresence | |
| OLD | NEW |