Index: components/audio_modem/modem_impl.cc |
diff --git a/components/copresence/mediums/audio/audio_manager_impl.cc b/components/audio_modem/modem_impl.cc |
similarity index 71% |
rename from components/copresence/mediums/audio/audio_manager_impl.cc |
rename to components/audio_modem/modem_impl.cc |
index d1cd7d96a5f8d3d2a74e902f63ea486a68a490a9..293b1f91aea5f7e29df83d198314f51560b67a87 100644 |
--- a/components/copresence/mediums/audio/audio_manager_impl.cc |
+++ b/components/audio_modem/modem_impl.cc |
@@ -1,8 +1,8 @@ |
-// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "components/copresence/mediums/audio/audio_manager_impl.h" |
+#include "components/audio_modem/modem_impl.h" |
#include <algorithm> |
#include <limits> |
@@ -12,27 +12,26 @@ |
#include "base/bind_helpers.h" |
#include "base/command_line.h" |
#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
#include "base/run_loop.h" |
#include "base/strings/string_util.h" |
#include "base/strings/stringprintf.h" |
#include "base/strings/sys_string_conversions.h" |
#include "base/time/time.h" |
-#include "components/copresence/copresence_switches.h" |
-#include "components/copresence/mediums/audio/audio_player_impl.h" |
-#include "components/copresence/mediums/audio/audio_recorder_impl.h" |
-#include "components/copresence/public/copresence_constants.h" |
-#include "components/copresence/public/whispernet_client.h" |
+#include "components/audio_modem/audio_modem_switches.h" |
+#include "components/audio_modem/audio_player_impl.h" |
+#include "components/audio_modem/audio_recorder_impl.h" |
+#include "components/audio_modem/public/whispernet_client.h" |
#include "content/public/browser/browser_thread.h" |
#include "media/audio/audio_manager.h" |
#include "media/audio/audio_manager_base.h" |
#include "media/base/audio_bus.h" |
#include "third_party/webrtc/common_audio/wav_file.h" |
-namespace copresence { |
+namespace audio_modem { |
namespace { |
-const int kSampleExpiryTimeMs = 60 * 60 * 1000; // 60 minutes. |
const int kMaxSamples = 10000; |
const int kTokenTimeoutMs = 2000; |
const int kMonoChannelCount = 1; |
@@ -80,8 +79,7 @@ bool ReadBooleanFlag(const std::string& flag, bool default_value) { |
// Public functions. |
-AudioManagerImpl::AudioManagerImpl() |
- : whispernet_client_(nullptr), recorder_(nullptr) { |
+ModemImpl::ModemImpl() : client_(nullptr), recorder_(nullptr) { |
// TODO(rkc): Move all of these into initializer lists once it is allowed. |
should_be_playing_[AUDIBLE] = false; |
should_be_playing_[INAUDIBLE] = false; |
@@ -89,32 +87,30 @@ AudioManagerImpl::AudioManagerImpl() |
should_be_recording_[INAUDIBLE] = false; |
player_enabled_[AUDIBLE] = ReadBooleanFlag( |
- switches::kCopresenceEnableAudibleBroadcast, true); |
+ switches::kAudioModemEnableAudibleBroadcast, true); |
player_enabled_[INAUDIBLE] = ReadBooleanFlag( |
- switches::kCopresenceEnableInaudibleBroadcast, true); |
+ switches::kAudioModemEnableInaudibleBroadcast, true); |
player_[AUDIBLE] = nullptr; |
player_[INAUDIBLE] = nullptr; |
token_length_[0] = 0; |
token_length_[1] = 0; |
-} |
-void AudioManagerImpl::Initialize(WhispernetClient* whispernet_client, |
- const TokensCallback& tokens_cb) { |
- samples_cache_.resize(2); |
- samples_cache_[AUDIBLE] = new SamplesMap( |
- base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); |
- samples_cache_[INAUDIBLE] = new SamplesMap( |
- base::TimeDelta::FromMilliseconds(kSampleExpiryTimeMs), kMaxSamples); |
+ samples_caches_.resize(2); |
+ samples_caches_[AUDIBLE] = new SamplesMap(kMaxSamples); |
+ samples_caches_[INAUDIBLE] = new SamplesMap(kMaxSamples); |
+} |
- DCHECK(whispernet_client); |
- whispernet_client_ = whispernet_client; |
+void ModemImpl::Initialize(WhispernetClient* client, |
+ const TokensCallback& tokens_cb) { |
+ DCHECK(client); |
+ client_ = client; |
tokens_cb_ = tokens_cb; |
// These will be unregistered on destruction, so unretained is safe to use. |
- whispernet_client_->RegisterTokensCallback( |
- base::Bind(&AudioManagerImpl::OnTokensFound, base::Unretained(this))); |
- whispernet_client_->RegisterSamplesCallback( |
- base::Bind(&AudioManagerImpl::OnTokenEncoded, base::Unretained(this))); |
+ client_->RegisterTokensCallback( |
+ base::Bind(&ModemImpl::OnTokensFound, base::Unretained(this))); |
+ client_->RegisterSamplesCallback( |
+ base::Bind(&ModemImpl::OnTokenEncoded, base::Unretained(this))); |
if (!player_[AUDIBLE]) |
player_[AUDIBLE] = new AudioPlayerImpl(); |
@@ -125,16 +121,16 @@ void AudioManagerImpl::Initialize(WhispernetClient* whispernet_client, |
player_[INAUDIBLE]->Initialize(); |
decode_cancelable_cb_.Reset(base::Bind( |
- &AudioManagerImpl::DecodeSamplesConnector, base::Unretained(this))); |
+ &ModemImpl::DecodeSamplesConnector, base::Unretained(this))); |
if (!recorder_) |
recorder_ = new AudioRecorderImpl(); |
recorder_->Initialize(decode_cancelable_cb_.callback()); |
dump_tokens_dir_ = base::FilePath(base::CommandLine::ForCurrentProcess() |
- ->GetSwitchValueNative(switches::kCopresenceDumpTokensToDir)); |
+ ->GetSwitchValueNative(switches::kAudioModemDumpTokensToDir)); |
} |
-AudioManagerImpl::~AudioManagerImpl() { |
+ModemImpl::~ModemImpl() { |
if (player_[AUDIBLE]) |
player_[AUDIBLE]->Finalize(); |
if (player_[INAUDIBLE]) |
@@ -143,23 +139,25 @@ AudioManagerImpl::~AudioManagerImpl() { |
recorder_->Finalize(); |
// Whispernet initialization may never have completed. |
- if (whispernet_client_) { |
- whispernet_client_->RegisterTokensCallback(TokensCallback()); |
- whispernet_client_->RegisterSamplesCallback(SamplesCallback()); |
+ if (client_) { |
+ client_->RegisterTokensCallback(TokensCallback()); |
+ client_->RegisterSamplesCallback(SamplesCallback()); |
} |
} |
-void AudioManagerImpl::StartPlaying(AudioType type) { |
+void ModemImpl::StartPlaying(AudioType type) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
should_be_playing_[type] = true; |
// If we don't have our token encoded yet, this check will be false, for now. |
// Once our token is encoded, OnTokenEncoded will call UpdateToken, which |
// will call this code again (if we're still supposed to be playing). |
- if (samples_cache_[type]->HasKey(playing_token_[type])) { |
+ SamplesMap::iterator samples = |
+ samples_caches_[type]->Get(playing_token_[type]); |
+ if (samples != samples_caches_[type]->end()) { |
DCHECK(!playing_token_[type].empty()); |
if (player_enabled_[type]) { |
started_playing_[type] = base::Time::Now(); |
- player_[type]->Play(samples_cache_[type]->GetValue(playing_token_[type])); |
+ player_[type]->Play(samples->second); |
// If we're playing, we always record to hear what we are playing. |
recorder_->Record(); |
@@ -170,7 +168,7 @@ void AudioManagerImpl::StartPlaying(AudioType type) { |
} |
} |
-void AudioManagerImpl::StopPlaying(AudioType type) { |
+void ModemImpl::StopPlaying(AudioType type) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
should_be_playing_[type] = false; |
player_[type]->Stop(); |
@@ -180,34 +178,34 @@ void AudioManagerImpl::StopPlaying(AudioType type) { |
playing_token_[type] = std::string(); |
} |
-void AudioManagerImpl::StartRecording(AudioType type) { |
+void ModemImpl::StartRecording(AudioType type) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
should_be_recording_[type] = true; |
recorder_->Record(); |
} |
-void AudioManagerImpl::StopRecording(AudioType type) { |
+void ModemImpl::StopRecording(AudioType type) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
should_be_recording_[type] = false; |
recorder_->Stop(); |
} |
-void AudioManagerImpl::SetToken(AudioType type, |
+void ModemImpl::SetToken(AudioType type, |
const std::string& url_safe_token) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
std::string token = FromUrlSafe(url_safe_token); |
- if (!samples_cache_[type]->HasKey(token)) { |
- whispernet_client_->EncodeToken(token, type); |
+ if (samples_caches_[type]->Get(token) == samples_caches_[type]->end()) { |
+ client_->EncodeToken(token, type); |
} else { |
UpdateToken(type, token); |
} |
} |
-const std::string AudioManagerImpl::GetToken(AudioType type) { |
+const std::string ModemImpl::GetToken(AudioType type) { |
return playing_token_[type]; |
} |
-bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) { |
+bool ModemImpl::IsPlayingTokenHeard(AudioType type) { |
base::TimeDelta tokenTimeout = |
base::TimeDelta::FromMilliseconds(kTokenTimeoutMs); |
@@ -219,23 +217,27 @@ bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) { |
return base::Time::Now() - heard_own_token_[type] < tokenTimeout; |
} |
-void AudioManagerImpl::SetTokenLength(AudioType type, size_t token_length) { |
+void ModemImpl::SetTokenLength(AudioType type, size_t token_length) { |
token_length_[type] = token_length; |
} |
+// static |
+scoped_ptr<Modem> Modem::Create() { |
+ return make_scoped_ptr<Modem>(new ModemImpl); |
+} |
// Private functions. |
-void AudioManagerImpl::OnTokenEncoded( |
+void ModemImpl::OnTokenEncoded( |
AudioType type, |
const std::string& token, |
const scoped_refptr<media::AudioBusRefCounted>& samples) { |
- samples_cache_[type]->Add(token, samples); |
+ samples_caches_[type]->Put(token, samples); |
DumpToken(type, token, samples.get()); |
UpdateToken(type, token); |
} |
-void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { |
+void ModemImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { |
std::vector<AudioToken> tokens_to_report; |
for (const auto& token : tokens) { |
AudioType type = token.audible ? AUDIBLE : INAUDIBLE; |
@@ -253,7 +255,7 @@ void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) { |
tokens_cb_.Run(tokens_to_report); |
} |
-void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) { |
+void ModemImpl::UpdateToken(AudioType type, const std::string& token) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
if (playing_token_[type] == token) |
return; |
@@ -267,18 +269,19 @@ void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) { |
RestartPlaying(type); |
} |
-void AudioManagerImpl::RestartPlaying(AudioType type) { |
+void ModemImpl::RestartPlaying(AudioType type) { |
DCHECK(type == AUDIBLE || type == INAUDIBLE); |
// We should already have this token in the cache. This function is not |
// called from anywhere except update token and only once we have our samples |
// in the cache. |
- DCHECK(samples_cache_[type]->HasKey(playing_token_[type])); |
+ DCHECK(samples_caches_[type]->Get(playing_token_[type]) != |
+ samples_caches_[type]->end()); |
player_[type]->Stop(); |
StartPlaying(type); |
} |
-void AudioManagerImpl::DecodeSamplesConnector(const std::string& samples) { |
+void ModemImpl::DecodeSamplesConnector(const std::string& samples) { |
// If we are either supposed to be recording *or* playing, audible or |
// inaudible, we should be decoding that type. This is so that if we are |
// just playing, we will still decode our recorded token so we can check |
@@ -291,17 +294,17 @@ void AudioManagerImpl::DecodeSamplesConnector(const std::string& samples) { |
should_be_recording_[INAUDIBLE] || should_be_playing_[INAUDIBLE]; |
if (decode_audible && decode_inaudible) { |
- whispernet_client_->DecodeSamples(BOTH, samples, token_length_); |
+ client_->DecodeSamples(BOTH, samples, token_length_); |
} else if (decode_audible) { |
- whispernet_client_->DecodeSamples(AUDIBLE, samples, token_length_); |
+ client_->DecodeSamples(AUDIBLE, samples, token_length_); |
} else if (decode_inaudible) { |
- whispernet_client_->DecodeSamples(INAUDIBLE, samples, token_length_); |
+ client_->DecodeSamples(INAUDIBLE, samples, token_length_); |
} |
} |
-void AudioManagerImpl::DumpToken(AudioType audio_type, |
- const std::string& token, |
- const media::AudioBus* samples) { |
+void ModemImpl::DumpToken(AudioType audio_type, |
+ const std::string& token, |
+ const media::AudioBus* samples) { |
if (dump_tokens_dir_.empty()) |
return; |
@@ -332,4 +335,4 @@ void AudioManagerImpl::DumpToken(AudioType audio_type, |
writer.WriteSamples(int_samples.data(), int_samples.size()); |
} |
-} // namespace copresence |
+} // namespace audio_modem |