Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: components/copresence/mediums/audio/audio_manager_impl.cc

Issue 704923002: Add polling and audio check to copresence. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/copresence/mediums/audio/audio_manager_impl.cc
diff --git a/components/copresence/mediums/audio/audio_manager_impl.cc b/components/copresence/mediums/audio/audio_manager_impl.cc
index bf354ca2113898752778820d933ff76ee6c2dfe2..0fc49ad9467cd477d7805714b39c6ec64a041303 100644
--- a/components/copresence/mediums/audio/audio_manager_impl.cc
+++ b/components/copresence/mediums/audio/audio_manager_impl.cc
@@ -15,6 +15,7 @@
#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 "content/public/browser/browser_thread.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_manager_base.h"
@@ -40,27 +41,36 @@ const int kMaxSamples = 10000;
// Public methods.
-AudioManagerImpl::AudioManagerImpl() : recorder_(nullptr) {
+AudioManagerImpl::AudioManagerImpl()
+ : whispernet_client_(nullptr), recorder_(nullptr) {
// TODO(rkc): Move all of these into initializer lists once it is allowed.
playing_[AUDIBLE] = false;
playing_[INAUDIBLE] = false;
recording_[AUDIBLE] = false;
recording_[INAUDIBLE] = false;
+ heard_own_token_[AUDIBLE] = false;
+ heard_own_token_[INAUDIBLE] = false;
player_[AUDIBLE] = nullptr;
player_[INAUDIBLE] = nullptr;
}
-void AudioManagerImpl::Initialize(const DecodeSamplesCallback& decode_cb,
- const EncodeTokenCallback& encode_cb) {
+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);
- decode_cb_ = decode_cb;
- encode_cb_ = encode_cb;
+ whispernet_client_ = whispernet_client;
Charlie 2014/11/06 17:28:23 DCHECK this. You can pass in stubs in from the tes
rkc 2014/11/06 19:58:24 Done.
+ 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)));
if (!player_[AUDIBLE])
player_[AUDIBLE] = new AudioPlayerImpl();
@@ -70,8 +80,9 @@ void AudioManagerImpl::Initialize(const DecodeSamplesCallback& decode_cb,
player_[INAUDIBLE] = new AudioPlayerImpl();
player_[INAUDIBLE]->Initialize();
- decode_cancelable_cb_.Reset(base::Bind(
- &AudioManagerImpl::DecodeSamplesConnector, base::Unretained(this)));
+ decode_cancelable_cb_.Reset(base::Bind(&WhispernetClient::DecodeSamples,
+ base::Unretained(whispernet_client_),
+ BOTH));
if (!recorder_)
recorder_ = new AudioRecorderImpl();
recorder_->Initialize(decode_cancelable_cb_.callback());
@@ -84,6 +95,11 @@ AudioManagerImpl::~AudioManagerImpl() {
player_[INAUDIBLE]->Finalize();
if (recorder_)
recorder_->Finalize();
+
+ if (whispernet_client_) {
Charlie 2014/11/06 17:28:23 No if since this will be DCHECK'ed
rkc 2014/11/06 19:58:24 Done but breaks the RPC handler test. Had to fix t
+ whispernet_client_->RegisterTokensCallback(TokensCallback());
+ whispernet_client_->RegisterSamplesCallback(SamplesCallback());
+ }
}
void AudioManagerImpl::StartPlaying(AudioType type) {
@@ -92,18 +108,26 @@ void AudioManagerImpl::StartPlaying(AudioType type) {
// 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(token_[type]) &&
+ if (samples_cache_[type]->HasKey(playing_token_[type]) &&
!player_[type]->IsPlaying()) {
- DCHECK(!token_[type].empty());
- player_[type]->Play(samples_cache_[type]->GetValue(token_[type]));
+ DCHECK(!playing_token_[type].empty());
+ player_[type]->Play(samples_cache_[type]->GetValue(playing_token_[type]));
+ // If we're playing, we always record to hear what we are playing.
+ if (!recorder_->IsRecording())
+ recorder_->Record();
}
}
void AudioManagerImpl::StopPlaying(AudioType type) {
DCHECK(type == AUDIBLE || type == INAUDIBLE);
playing_[type] = false;
- if (player_[type]->IsPlaying())
+ if (player_[type]->IsPlaying()) {
player_[type]->Stop();
+ // If we were only recording to hear our own played tokens, stop.
+ if (recorder_->IsRecording() && !recording_[AUDIBLE] &&
+ !recording_[INAUDIBLE])
+ recorder_->Stop();
+ }
}
void AudioManagerImpl::StartRecording(AudioType type) {
@@ -129,17 +153,14 @@ void AudioManagerImpl::SetToken(AudioType type,
// RpcHandler->DirectiveHandler->AudioDirectiveHandler. The RpcHandler
// unsets any callbacks that were set on the Whispernet client before it
// destructs, unsetting this callback too - making unretained safe to use.
- encode_cb_.Run(
- token,
- type,
- base::Bind(&AudioManagerImpl::OnTokenEncoded, base::Unretained(this)));
+ whispernet_client_->EncodeToken(token, type);
} else {
UpdateToken(type, token);
}
}
const std::string AudioManagerImpl::GetToken(AudioType type) {
- return token_[type];
+ return playing_token_[type];
}
bool AudioManagerImpl::IsRecording(AudioType type) {
@@ -150,23 +171,45 @@ bool AudioManagerImpl::IsPlaying(AudioType type) {
return playing_[type];
}
+bool AudioManagerImpl::IsPlayingTokenHeard(AudioType type) {
+ return heard_own_token_[type];
+}
+
// Private methods.
void AudioManagerImpl::OnTokenEncoded(
- const std::string& token,
AudioType type,
+ const std::string& token,
const scoped_refptr<media::AudioBusRefCounted>& samples) {
samples_cache_[type]->Add(token, samples);
UpdateToken(type, token);
}
+void AudioManagerImpl::OnTokensFound(const std::vector<AudioToken>& tokens) {
+ std::vector<AudioToken> tokens_to_report;
+ for (const auto& token : tokens) {
+ AudioType type = token.audible ? AUDIBLE : INAUDIBLE;
+ if (playing_token_[type] == token.token)
+ heard_own_token_[type] = true;
+
+ if (recording_[AUDIBLE] && token.audible) {
+ tokens_to_report.push_back(token);
+ } else if (recording_[INAUDIBLE] && !token.audible) {
+ tokens_to_report.push_back(token);
+ }
+ }
+
+ if (!tokens_cb_.is_null() && !tokens_to_report.empty())
Charlie 2014/11/06 17:28:23 Why should tokens_cb_ be null?
rkc 2014/11/06 19:58:24 Done.
+ tokens_cb_.Run(tokens_to_report);
+}
+
void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) {
DCHECK(type == AUDIBLE || type == INAUDIBLE);
- if (token_[type] == token)
+ if (playing_token_[type] == token)
return;
// Update token.
- token_[type] = token;
+ playing_token_[type] = token;
// If we are supposed to be playing this token type at this moment, switch
// out playback with the new samples.
@@ -177,17 +220,4 @@ void AudioManagerImpl::UpdateToken(AudioType type, const std::string& token) {
}
}
-void AudioManagerImpl::DecodeSamplesConnector(const std::string& samples) {
- AudioType decode_type = AUDIO_TYPE_UNKNOWN;
-
- if (recording_[AUDIBLE] && recording_[INAUDIBLE])
- decode_type = BOTH;
- else if (recording_[AUDIBLE])
- decode_type = AUDIBLE;
- else if (recording_[INAUDIBLE])
- decode_type = INAUDIBLE;
-
- decode_cb_.Run(decode_type, samples);
-}
-
} // namespace copresence

Powered by Google App Engine
This is Rietveld 408576698