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