| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/copresence/mediums/audio/audio_manager_impl.h" | 5 #include "components/copresence/mediums/audio/audio_manager_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/time/time.h" |
| 15 #include "components/copresence/mediums/audio/audio_player_impl.h" | 16 #include "components/copresence/mediums/audio/audio_player_impl.h" |
| 16 #include "components/copresence/mediums/audio/audio_recorder_impl.h" | 17 #include "components/copresence/mediums/audio/audio_recorder_impl.h" |
| 17 #include "components/copresence/public/copresence_constants.h" | 18 #include "components/copresence/public/copresence_constants.h" |
| 18 #include "components/copresence/public/whispernet_client.h" | 19 #include "components/copresence/public/whispernet_client.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 #include "media/audio/audio_manager.h" | 21 #include "media/audio/audio_manager.h" |
| 21 #include "media/audio/audio_manager_base.h" | 22 #include "media/audio/audio_manager_base.h" |
| 22 #include "media/base/audio_bus.h" | 23 #include "media/base/audio_bus.h" |
| 23 | 24 |
| 24 namespace copresence { | 25 namespace copresence { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 // UrlSafe is defined as: | 29 // UrlSafe is defined as: |
| 29 // '/' represented by a '_' and '+' represented by a '-' | 30 // '/' represented by a '_' and '+' represented by a '-' |
| 30 // TODO(rkc): Move this processing to the whispernet wrapper. | 31 // TODO(rkc): Move this processing to the whispernet wrapper. |
| 31 std::string FromUrlSafe(std::string token) { | 32 std::string FromUrlSafe(std::string token) { |
| 32 base::ReplaceChars(token, "-", "+", &token); | 33 base::ReplaceChars(token, "-", "+", &token); |
| 33 base::ReplaceChars(token, "_", "/", &token); | 34 base::ReplaceChars(token, "_", "/", &token); |
| 34 return token; | 35 return token; |
| 35 } | 36 } |
| 36 | 37 |
| 37 const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes. | 38 const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes. |
| 38 const int kMaxSamples = 10000; | 39 const int kMaxSamples = 10000; |
| 40 const int kTokenTimeoutMs = 2000; |
| 39 | 41 |
| 40 } // namespace | 42 } // namespace |
| 41 | 43 |
| 42 // Public methods. | 44 // Public methods. |
| 43 | 45 |
| 44 AudioManagerImpl::AudioManagerImpl() | 46 AudioManagerImpl::AudioManagerImpl() |
| 45 : whispernet_client_(nullptr), recorder_(nullptr) { | 47 : whispernet_client_(nullptr), recorder_(nullptr) { |
| 46 // TODO(rkc): Move all of these into initializer lists once it is allowed. | 48 // TODO(rkc): Move all of these into initializer lists once it is allowed. |
| 47 playing_[AUDIBLE] = false; | 49 playing_[AUDIBLE] = false; |
| 48 playing_[INAUDIBLE] = false; | 50 playing_[INAUDIBLE] = false; |
| 49 recording_[AUDIBLE] = false; | 51 recording_[AUDIBLE] = false; |
| 50 recording_[INAUDIBLE] = false; | 52 recording_[INAUDIBLE] = false; |
| 51 heard_own_token_[AUDIBLE] = false; | |
| 52 heard_own_token_[INAUDIBLE] = false; | |
| 53 | 53 |
| 54 player_[AUDIBLE] = nullptr; | 54 player_[AUDIBLE] = nullptr; |
| 55 player_[INAUDIBLE] = nullptr; | 55 player_[INAUDIBLE] = nullptr; |
| 56 } | 56 } |
| 57 | 57 |
| 58 void AudioManagerImpl::Initialize(WhispernetClient* whispernet_client, | 58 void AudioManagerImpl::Initialize(WhispernetClient* whispernet_client, |
| 59 const TokensCallback& tokens_cb) { | 59 const TokensCallback& tokens_cb) { |
| 60 samples_cache_.resize(2); | 60 samples_cache_.resize(2); |
| 61 samples_cache_[AUDIBLE] = new SamplesMap( | 61 samples_cache_[AUDIBLE] = new SamplesMap( |
| 62 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); | 62 base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // Whispernet initialization may never have completed. | 100 // Whispernet initialization may never have completed. |
| 101 if (whispernet_client_) { | 101 if (whispernet_client_) { |
| 102 whispernet_client_->RegisterTokensCallback(TokensCallback()); | 102 whispernet_client_->RegisterTokensCallback(TokensCallback()); |
| 103 whispernet_client_->RegisterSamplesCallback(SamplesCallback()); | 103 whispernet_client_->RegisterSamplesCallback(SamplesCallback()); |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 void AudioManagerImpl::StartPlaying(AudioType type) { | 107 void AudioManagerImpl::StartPlaying(AudioType type) { |
| 108 DCHECK(type == AUDIBLE || type == INAUDIBLE); | 108 DCHECK(type == AUDIBLE || type == INAUDIBLE); |
| 109 playing_[type] = true; | 109 playing_[type] = true; |
| 110 started_playing_[type] = base::Time::Now(); |
| 110 // If we don't have our token encoded yet, this check will be false, for now. | 111 // If we don't have our token encoded yet, this check will be false, for now. |
| 111 // Once our token is encoded, OnTokenEncoded will call UpdateToken, which | 112 // Once our token is encoded, OnTokenEncoded will call UpdateToken, which |
| 112 // will call this code again (if we're still supposed to be playing). | 113 // will call this code again (if we're still supposed to be playing). |
| 113 if (samples_cache_[type]->HasKey(playing_token_[type]) && | 114 if (samples_cache_[type]->HasKey(playing_token_[type]) && |
| 114 !player_[type]->IsPlaying()) { | 115 !player_[type]->IsPlaying()) { |
| 115 DCHECK(!playing_token_[type].empty()); | 116 DCHECK(!playing_token_[type].empty()); |
| 116 player_[type]->Play(samples_cache_[type]->GetValue(playing_token_[type])); | 117 player_[type]->Play(samples_cache_[type]->GetValue(playing_token_[type])); |
| 117 // If we're playing, we always record to hear what we are playing. | 118 // If we're playing, we always record to hear what we are playing. |
| 118 if (!recorder_->IsRecording()) | 119 if (!recorder_->IsRecording()) |
| 119 recorder_->Record(); | 120 recorder_->Record(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 164 |
| 164 bool AudioManagerImpl::IsRecording(AudioType type) { | 165 bool AudioManagerImpl::IsRecording(AudioType type) { |
| 165 return recording_[type]; | 166 return recording_[type]; |
| 166 } | 167 } |
| 167 | 168 |
| 168 bool AudioManagerImpl::IsPlaying(AudioType type) { | 169 bool AudioManagerImpl::IsPlaying(AudioType type) { |
| 169 return playing_[type]; | 170 return playing_[type]; |
| 170 } | 171 } |
| 171 | 172 |
| 172 bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) { | 173 bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) { |
| 173 return heard_own_token_[type]; | 174 base::TimeDelta tokenTimeout = |
| 175 base::TimeDelta::FromMilliseconds(kTokenTimeoutMs); |
| 176 |
| 177 // This is a bit of a hack. If we haven't been playing long enough, |
| 178 // return true to avoid tripping an audio fail alarm. |
| 179 if (base::Time::Now() - started_playing_[type] < tokenTimeout) |
| 180 return true; |
| 181 |
| 182 return base::Time::Now() - heard_own_token_[type] < tokenTimeout; |
| 174 } | 183 } |
| 175 | 184 |
| 176 // Private methods. | 185 // Private methods. |
| 177 | 186 |
| 178 void AudioManagerImpl::OnTokenEncoded( | 187 void AudioManagerImpl::OnTokenEncoded( |
| 179 AudioType type, | 188 AudioType type, |
| 180 const std::string& token, | 189 const std::string& token, |
| 181 const scoped_refptr<media::AudioBusRefCounted>& samples) { | 190 const scoped_refptr<media::AudioBusRefCounted>& samples) { |
| 182 samples_cache_[type]->Add(token, samples); | 191 samples_cache_[type]->Add(token, samples); |
| 183 UpdateToken(type, token); | 192 UpdateToken(type, token); |
| 184 } | 193 } |
| 185 | 194 |
| 186 void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { | 195 void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { |
| 187 std::vector<AudioToken> tokens_to_report; | 196 std::vector<AudioToken> tokens_to_report; |
| 188 for (const auto& token : tokens) { | 197 for (const auto& token : tokens) { |
| 189 AudioType type = token.audible ? AUDIBLE : INAUDIBLE; | 198 AudioType type = token.audible ? AUDIBLE : INAUDIBLE; |
| 190 if (playing_token_[type] == token.token) | 199 if (playing_token_[type] == token.token) |
| 191 heard_own_token_[type] = true; | 200 heard_own_token_[type] = base::Time::Now(); |
| 192 | 201 |
| 193 if (recording_[AUDIBLE] && token.audible) { | 202 if (recording_[AUDIBLE] && token.audible) { |
| 194 tokens_to_report.push_back(token); | 203 tokens_to_report.push_back(token); |
| 195 } else if (recording_[INAUDIBLE] && !token.audible) { | 204 } else if (recording_[INAUDIBLE] && !token.audible) { |
| 196 tokens_to_report.push_back(token); | 205 tokens_to_report.push_back(token); |
| 197 } | 206 } |
| 198 } | 207 } |
| 199 | 208 |
| 200 if (!tokens_to_report.empty()) | 209 if (!tokens_to_report.empty()) |
| 201 tokens_cb_.Run(tokens_to_report); | 210 tokens_cb_.Run(tokens_to_report); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 212 // out playback with the new samples. | 221 // out playback with the new samples. |
| 213 // If we are supposed to be playing this token type at this moment, switch | 222 // If we are supposed to be playing this token type at this moment, switch |
| 214 if (playing_[type]) { | 223 if (playing_[type]) { |
| 215 if (player_[type]->IsPlaying()) | 224 if (player_[type]->IsPlaying()) |
| 216 player_[type]->Stop(); | 225 player_[type]->Stop(); |
| 217 StartPlaying(type); | 226 StartPlaying(type); |
| 218 } | 227 } |
| 219 } | 228 } |
| 220 | 229 |
| 221 } // namespace copresence | 230 } // namespace copresence |
| OLD | NEW |