Commit ad1acbee authored by Olga Sharonova's avatar Olga Sharonova Committed by Commit Bot

Switch WebrtcAudioPrivate to UI thread-only.

There is no need in a extra jump to IO thread, since AudioSystem can
handle requests on UI one.

Change-Id: Icb0413f1ec7af23254303824fe2da28734a79b1d
Reviewed-on: https://chromium-review.googlesource.com/883128
Commit-Queue: Olga Sharonova <olka@chromium.org>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#531834}
parent d925bde7
......@@ -106,7 +106,7 @@ WebrtcAudioPrivateFunction::~WebrtcAudioPrivateFunction() {}
std::string WebrtcAudioPrivateFunction::CalculateHMAC(
const std::string& raw_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// We don't hash the default device description, and we always return
// "default" for the default device. There is code in SetActiveSink
......@@ -130,7 +130,7 @@ std::string WebrtcAudioPrivateFunction::device_id_salt() const {
}
media::AudioSystem* WebrtcAudioPrivateFunction::GetAudioSystem() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (!audio_system_)
audio_system_ = media::AudioSystem::CreateInstance();
return audio_system_.get();
......@@ -176,27 +176,17 @@ WebrtcAudioPrivateFunction::GetRenderProcessHostFromRequest(
bool WebrtcAudioPrivateGetSinksFunction::RunAsync() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
InitDeviceIDSalt();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&WebrtcAudioPrivateGetSinksFunction::
GetOutputDeviceDescriptionsOnIOThread,
this));
return true;
}
void WebrtcAudioPrivateGetSinksFunction::
GetOutputDeviceDescriptionsOnIOThread() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetAudioSystem()->GetDeviceDescriptions(
false, base::BindOnce(&WebrtcAudioPrivateGetSinksFunction::
ReceiveOutputDeviceDescriptionsOnIOThread,
this));
false,
base::BindOnce(
&WebrtcAudioPrivateGetSinksFunction::ReceiveOutputDeviceDescriptions,
this));
return true;
}
void WebrtcAudioPrivateGetSinksFunction::
ReceiveOutputDeviceDescriptionsOnIOThread(
media::AudioDeviceDescriptions sink_devices) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
void WebrtcAudioPrivateGetSinksFunction::ReceiveOutputDeviceDescriptions(
media::AudioDeviceDescriptions sink_devices) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto results = std::make_unique<SinkInfoVector>();
for (const media::AudioDeviceDescription& description : sink_devices) {
wap::SinkInfo info;
......@@ -205,15 +195,6 @@ void WebrtcAudioPrivateGetSinksFunction::
// TODO(joi): Add other parameters.
results->push_back(std::move(info));
}
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(&WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread, this,
base::Passed(&results)));
}
void WebrtcAudioPrivateGetSinksFunction::DoneOnUIThread(
std::unique_ptr<SinkInfoVector> results) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
results_ = wap::GetSinks::Results::Create(*results);
SendResponse(true);
}
......@@ -230,28 +211,17 @@ bool WebrtcAudioPrivateGetAssociatedSinkFunction::RunAsync() {
EXTENSION_FUNCTION_VALIDATE(params_.get());
InitDeviceIDSalt();
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::BindOnce(&WebrtcAudioPrivateGetAssociatedSinkFunction::
GetInputDeviceDescriptionsOnIOThread,
this));
return true;
}
void WebrtcAudioPrivateGetAssociatedSinkFunction::
GetInputDeviceDescriptionsOnIOThread() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
GetAudioSystem()->GetDeviceDescriptions(
true, base::BindOnce(&WebrtcAudioPrivateGetAssociatedSinkFunction::
ReceiveInputDeviceDescriptionsOnIOThread,
ReceiveInputDeviceDescriptions,
this));
return true;
}
void WebrtcAudioPrivateGetAssociatedSinkFunction::
ReceiveInputDeviceDescriptionsOnIOThread(
ReceiveInputDeviceDescriptions(
media::AudioDeviceDescriptions source_devices) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
url::Origin security_origin =
url::Origin::Create(GURL(params_->security_origin));
std::string source_id_in_origin(params_->source_id_in_origin);
......@@ -269,29 +239,25 @@ void WebrtcAudioPrivateGetAssociatedSinkFunction::
}
}
if (raw_source_id.empty()) {
CalculateHMACOnIOThread(base::nullopt);
CalculateHMACAndReply(base::nullopt);
return;
}
GetAudioSystem()->GetAssociatedOutputDeviceID(
raw_source_id,
base::BindOnce(
&WebrtcAudioPrivateGetAssociatedSinkFunction::CalculateHMACOnIOThread,
&WebrtcAudioPrivateGetAssociatedSinkFunction::CalculateHMACAndReply,
this));
}
void WebrtcAudioPrivateGetAssociatedSinkFunction::CalculateHMACOnIOThread(
void WebrtcAudioPrivateGetAssociatedSinkFunction::CalculateHMACAndReply(
const base::Optional<std::string>& raw_sink_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(!raw_sink_id || !raw_sink_id->empty());
// If no |raw_sink_id| is provided, the default device is used.
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::BindOnce(
&WebrtcAudioPrivateGetAssociatedSinkFunction::ReceiveHMACOnUIThread,
this, CalculateHMAC(raw_sink_id.value_or(std::string()))));
Reply(CalculateHMAC(raw_sink_id.value_or(std::string())));
}
void WebrtcAudioPrivateGetAssociatedSinkFunction::ReceiveHMACOnUIThread(
void WebrtcAudioPrivateGetAssociatedSinkFunction::Reply(
const std::string& associated_sink_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (associated_sink_id == media::AudioDeviceDescription::kDefaultDeviceId) {
......
......@@ -59,7 +59,6 @@ class WebrtcAudioPrivateFunction : public ChromeAsyncExtensionFunction {
protected:
// Calculates a single HMAC, using the extension ID as the security origin.
// Call only on IO thread.
std::string CalculateHMAC(const std::string& raw_id);
// Initializes |device_id_salt_|. Must be called on the UI thread,
......@@ -98,16 +97,10 @@ class WebrtcAudioPrivateGetSinksFunction : public WebrtcAudioPrivateFunction {
bool RunAsync() override;
// Requests output device descriptions.
void GetOutputDeviceDescriptionsOnIOThread();
// Receives output device descriptions, calculates HMACs for them and replies
// to UI thread with DoneOnUIThread().
void ReceiveOutputDeviceDescriptionsOnIOThread(
// Receives output device descriptions, calculates HMACs for them and sends
// the response.
void ReceiveOutputDeviceDescriptions(
media::AudioDeviceDescriptions sink_devices);
// Sends the response.
void DoneOnUIThread(std::unique_ptr<SinkInfoVector> results);
};
class WebrtcAudioPrivateGetAssociatedSinkFunction
......@@ -125,23 +118,17 @@ class WebrtcAudioPrivateGetAssociatedSinkFunction
// UI thread: Entry point, posts GetInputDeviceDescriptions() to IO thread.
bool RunAsync() override;
// Enumerates input devices.
void GetInputDeviceDescriptionsOnIOThread();
// Receives the input device descriptions, looks up the raw source device ID
// basing on |params|, and requests the associated raw sink ID for it.
void ReceiveInputDeviceDescriptionsOnIOThread(
void ReceiveInputDeviceDescriptions(
media::AudioDeviceDescriptions source_devices);
// IO thread: Receives the raw sink ID, calculates HMAC and replies to IO
// thread with ReceiveHMACOnUIThread().
void CalculateHMACOnIOThread(const base::Optional<std::string>& raw_sink_id);
// Receives the raw sink ID, calculates HMAC and calls Reply().
void CalculateHMACAndReply(const base::Optional<std::string>& raw_sink_id);
// Receives the associated sink ID as HMAC and sends the response.
void ReceiveHMACOnUIThread(const std::string& hmac);
void Reply(const std::string& hmac);
// Initialized on UI thread in RunAsync(), read-only access on IO thread - no
// locking needed.
std::unique_ptr<api::webrtc_audio_private::GetAssociatedSink::Params> params_;
};
......
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