Commit 0969d6da authored by Noah Rose Ledesma's avatar Noah Rose Ledesma Committed by Commit Bot

Add callback notification for WebAudioSourceProviderImpl SetClient

This change allows the WebAudioSourceProviderImpl to register callbacks
to run when a client is set.

When the WebAudioSourceProviderImpl has a client, an audio output sink
cannot be set. In this case, the Global Media Controls (GMC) UI should
not display a device picker. This change is a part of a series of
changes for appropriately hiding this UI.


Bug: 1120620
Change-Id: I2c536f8a7234116567cff1e795ca38054e29bc62
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2369660
Commit-Queue: Noah Rose Ledesma <noahrose@google.com>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#802294}
parent a8c63a2b
......@@ -11,6 +11,7 @@
#include <string>
#include "base/callback.h"
#include "base/callback_forward.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
......@@ -51,9 +52,14 @@ class BLINK_PLATFORM_EXPORT WebAudioSourceProviderImpl
uint32_t frames_delayed,
int sample_rate)>;
// Optionally provide a callback to be run the first time a
// WebAudioSourceProviderClient is attached via SetClient(). Note that the
// callback will be run once at most, however SetClient may be called any
// number of times.
WebAudioSourceProviderImpl(
scoped_refptr<media::SwitchableAudioRendererSink> sink,
media::MediaLog* media_log);
media::MediaLog* media_log,
base::OnceClosure on_set_client_callback = base::OnceClosure());
// WebAudioSourceProvider implementation.
void SetClient(WebAudioSourceProviderClient* client) override;
......@@ -116,6 +122,8 @@ class BLINK_PLATFORM_EXPORT WebAudioSourceProviderImpl
media::MediaLog* const media_log_;
base::OnceClosure on_set_client_callback_;
// NOTE: Weak pointers must be invalidated before all other member variables.
base::WeakPtrFactory<WebAudioSourceProviderImpl> weak_factory_{this};
......
......@@ -8,6 +8,7 @@
#include <utility>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/check_op.h"
#include "base/macros.h"
#include "base/memory/ptr_util.h"
......@@ -130,13 +131,15 @@ class WebAudioSourceProviderImpl::TeeFilter
WebAudioSourceProviderImpl::WebAudioSourceProviderImpl(
scoped_refptr<media::SwitchableAudioRendererSink> sink,
media::MediaLog* media_log)
media::MediaLog* media_log,
base::OnceClosure on_set_client_callback /* = base::OnceClosure()*/)
: volume_(1.0),
state_(kStopped),
client_(nullptr),
sink_(std::move(sink)),
tee_filter_(new TeeFilter()),
media_log_(media_log) {}
media_log_(media_log),
on_set_client_callback_(std::move(on_set_client_callback)) {}
WebAudioSourceProviderImpl::~WebAudioSourceProviderImpl() = default;
......@@ -170,6 +173,10 @@ void WebAudioSourceProviderImpl::SetClient(
// ensures we have the same locking order when calling into |client_|.
if (tee_filter_->initialized())
set_format_cb_.Run();
if (on_set_client_callback_)
std::move(on_set_client_callback_).Run();
return;
}
......
......@@ -87,6 +87,8 @@ class WebAudioSourceProviderImplTest : public testing::Test,
return true;
}
MOCK_METHOD0(OnClientSet, void());
// WebAudioSourceProviderClient implementation.
MOCK_METHOD2(SetFormat, void(uint32_t numberOfChannels, float sampleRate));
......@@ -114,6 +116,8 @@ class WebAudioSourceProviderImplTest : public testing::Test,
scoped_refptr<media::MockAudioRendererSink> mock_sink_;
scoped_refptr<WebAudioSourceProviderImpl> wasp_impl_;
base::WeakPtrFactory<WebAudioSourceProviderImplTest> weak_factory_{this};
DISALLOW_COPY_AND_ASSIGN(WebAudioSourceProviderImplTest);
};
......@@ -320,4 +324,32 @@ TEST_F(WebAudioSourceProviderImplTest, MultipleInitializeWithSetClient) {
ASSERT_FALSE(CompareBusses(bus1.get(), bus2.get()));
}
TEST_F(WebAudioSourceProviderImplTest, SetClientCallback) {
wasp_impl_ = new WebAudioSourceProviderImpl(
mock_sink_, &media_log_,
base::BindOnce(&WebAudioSourceProviderImplTest::OnClientSet,
weak_factory_.GetWeakPtr()));
// SetClient with a nullptr client should not trigger the callback if no
// client is set.
EXPECT_CALL(*this, OnClientSet()).Times(0);
wasp_impl_->SetClient(nullptr);
::testing::Mock::VerifyAndClearExpectations(this);
// SetClient when called with a valid client should trigger the callback once.
EXPECT_CALL(*this, OnClientSet()).Times(1);
wasp_impl_->SetClient(this);
base::RunLoop().RunUntilIdle();
::testing::Mock::VerifyAndClearExpectations(this);
// Future calls to set client should not trigger the callback.
EXPECT_CALL(*this, OnClientSet()).Times(0);
wasp_impl_->SetClient(this);
base::RunLoop().RunUntilIdle();
wasp_impl_->SetClient(nullptr);
base::RunLoop().RunUntilIdle();
wasp_impl_->SetClient(this);
base::RunLoop().RunUntilIdle();
::testing::Mock::VerifyAndClearExpectations(this);
}
} // namespace blink
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment