Commit cd75a470 authored by evliu's avatar evliu Committed by Commit Bot

Replace enable_soda GN flag with the kUseSodaForLiveCaption feature flag

This CL replaces the enable_soda GN flag with the kUseSodaForLiveCaption
feature flag.

Bug: 1128723
Change-Id: I8f46c85808d5744cfedca9b0141b5a8bcf1414ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2419138Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Reviewed-by: default avatarAlex Gough <ajgo@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Commit-Queue: Evan Liu <evliu@google.com>
Cr-Commit-Position: refs/heads/master@{#809836}
parent 1653aecb
......@@ -3953,7 +3953,6 @@ static_library("browser") {
"//chrome/services/sharing/public/cpp",
"//chrome/services/sharing/public/mojom",
"//chrome/services/sharing/public/proto",
"//chrome/services/speech:buildflags",
"//components/download/quarantine",
"//components/feedback",
"//components/image_fetcher/core",
......
......@@ -33,7 +33,6 @@ include_rules = [
"+chrome/services/printing/public",
"+chrome/services/sharing/public",
"+chrome/services/removable_storage_writer/public",
"+chrome/services/speech/buildflags.h",
"+chrome/services/util_win/public",
"+chromeos",
"+components/about_ui",
......
......@@ -11,7 +11,6 @@
#include "chrome/browser/component_updater/soda_en_us_component_installer.h"
#include "chrome/browser/component_updater/soda_ja_jp_component_installer.h"
#include "chrome/common/pref_names.h"
#include "chrome/services/speech/buildflags.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/soda/constants.h"
......@@ -137,64 +136,65 @@ void RegisterSODAComponent(ComponentUpdateService* cus,
if (!base::FeatureList::IsEnabled(media::kLiveCaption))
return;
#if BUILDFLAG(ENABLE_SODA)
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE,
base::BindOnce(&UpdateSODAInstallDirPref, prefs,
install_dir));
},
cus, prefs)));
if (prefs->GetBoolean(prefs::kLiveCaptionEnabled)) {
installer->Register(cus, std::move(callback));
} else {
// Register and uninstall the SODA component to delete the previously
// installed SODA files.
if (!prefs->GetFilePath(prefs::kSodaBinaryPath).empty()) {
installer->Register(
cus,
base::BindOnce(
[](ComponentUpdateService* cus, PrefService* prefs) {
if (component_updater::UninstallSODAComponent(cus, prefs)) {
prefs->SetFilePath(prefs::kSodaBinaryPath, base::FilePath());
prefs->SetFilePath(prefs::kSodaEnUsConfigPath,
base::FilePath());
}
},
cus, prefs));
},
cus, prefs)));
if (prefs->GetBoolean(prefs::kLiveCaptionEnabled)) {
installer->Register(cus, std::move(callback));
} else {
// Register and uninstall the SODA component to delete the previously
// installed SODA files.
if (!prefs->GetFilePath(prefs::kSodaBinaryPath).empty()) {
installer->Register(
cus,
base::BindOnce(
[](ComponentUpdateService* cus, PrefService* prefs) {
if (component_updater::UninstallSODAComponent(cus, prefs)) {
prefs->SetFilePath(prefs::kSodaBinaryPath,
base::FilePath());
prefs->SetFilePath(prefs::kSodaEnUsConfigPath,
base::FilePath());
}
},
cus, prefs));
}
}
}
#endif
}
void RegisterSodaLanguageComponent(ComponentUpdateService* cus,
PrefService* prefs) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
#if BUILDFLAG(ENABLE_SODA)
speech::LanguageCode language = speech::GetLanguageCode(
prefs->GetString(prefs::kLiveCaptionLanguageCode));
switch (language) {
case speech::LanguageCode::kNone:
// Do nothing.
break;
case speech::LanguageCode::kEnUs:
RegisterSodaEnUsComponent(
cus, prefs,
base::BindOnce(&SodaEnUsComponentInstallerPolicy::
UpdateSodaEnUsComponentOnDemand));
break;
case speech::LanguageCode::kJaJp:
RegisterSodaJaJpComponent(
cus, prefs,
base::BindOnce(&SodaJaJpComponentInstallerPolicy::
UpdateSodaJaJpComponentOnDemand));
break;
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
speech::LanguageCode language = speech::GetLanguageCode(
prefs->GetString(prefs::kLiveCaptionLanguageCode));
switch (language) {
case speech::LanguageCode::kNone:
// Do nothing.
break;
case speech::LanguageCode::kEnUs:
RegisterSodaEnUsComponent(
cus, prefs,
base::BindOnce(&SodaEnUsComponentInstallerPolicy::
UpdateSodaEnUsComponentOnDemand));
break;
case speech::LanguageCode::kJaJp:
RegisterSodaJaJpComponent(
cus, prefs,
base::BindOnce(&SodaJaJpComponentInstallerPolicy::
UpdateSodaJaJpComponentOnDemand));
break;
}
}
#endif
}
bool UninstallSODAComponent(ComponentUpdateService* cus, PrefService* prefs) {
......
......@@ -9,7 +9,6 @@
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "chrome/services/speech/buildflags.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/soda/constants.h"
......@@ -136,25 +135,25 @@ void RegisterSodaEnUsComponent(ComponentUpdateService* cus,
PrefService* prefs,
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
#if BUILDFLAG(ENABLE_SODA)
if (!prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
!base::FeatureList::IsEnabled(media::kLiveCaption)) {
return;
}
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SodaEnUsComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
if (!prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
!base::FeatureList::IsEnabled(media::kLiveCaption)) {
return;
}
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SodaEnUsComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE,
base::BindOnce(&UpdateSodaEnUsInstallDirPref,
prefs, install_dir));
},
cus, prefs)));
},
cus, prefs)));
installer->Register(cus, std::move(callback));
#endif
installer->Register(cus, std::move(callback));
}
}
bool UninstallSodaEnUsComponent(ComponentUpdateService* cus,
......
......@@ -9,7 +9,6 @@
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/pref_names.h"
#include "chrome/services/speech/buildflags.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/soda/constants.h"
......@@ -136,25 +135,25 @@ void RegisterSodaJaJpComponent(ComponentUpdateService* cus,
PrefService* prefs,
base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
#if BUILDFLAG(ENABLE_SODA)
if (!prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
!base::FeatureList::IsEnabled(media::kLiveCaption)) {
return;
}
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SodaJaJpComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
if (!prefs->GetBoolean(prefs::kLiveCaptionEnabled) ||
!base::FeatureList::IsEnabled(media::kLiveCaption)) {
return;
}
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SodaJaJpComponentInstallerPolicy>(base::BindRepeating(
[](ComponentUpdateService* cus, PrefService* prefs,
const base::FilePath& install_dir) {
content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE,
base::BindOnce(&UpdateSodaJaJpInstallDirPref,
prefs, install_dir));
},
cus, prefs)));
},
cus, prefs)));
installer->Register(cus, std::move(callback));
#endif
installer->Register(cus, std::move(callback));
}
}
bool UninstallSodaJaJpComponent(ComponentUpdateService* cus,
......
......@@ -7,12 +7,9 @@
#include "build/build_config.h"
#include "content/public/browser/service_process_host.h"
#include "media/base/media_switches.h"
#include "sandbox/policy/sandbox_type.h"
#if !defined(OS_ANDROID)
#include "chrome/services/speech/buildflags.h"
#endif // !defined(OS_ANDROID)
// This file maps service classes to sandbox types. Services which
// require a non-utility sandbox can be added here. See
// ServiceProcessHost::Launch() for how these templates are consumed.
......@@ -79,7 +76,6 @@ content::GetServiceSandboxType<chrome::mojom::ProfileImport>() {
// media::mojom::SpeechRecognitionService
#if !defined(OS_ANDROID)
#if BUILDFLAG(ENABLE_SODA)
namespace media {
namespace mojom {
class SpeechRecognitionService;
......@@ -89,9 +85,12 @@ class SpeechRecognitionService;
template <>
inline sandbox::policy::SandboxType
content::GetServiceSandboxType<media::mojom::SpeechRecognitionService>() {
return sandbox::policy::SandboxType::kSpeechRecognition;
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
return sandbox::policy::SandboxType::kSpeechRecognition;
} else {
return sandbox::policy::SandboxType::kUtility;
}
}
#endif // BUILDFLAG(ENABLE_SODA)
#endif // !defined(OS_ANDROID)
// printing::mojom::PrintingService
......
......@@ -22,9 +22,9 @@ constexpr base::TimeDelta kIdleProcessTimeout = base::TimeDelta::FromSeconds(5);
SpeechRecognitionService::SpeechRecognitionService(
content::BrowserContext* context)
: context_(context)
{
}
: context_(context),
enable_soda_(
base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {}
SpeechRecognitionService::~SpeechRecognitionService() = default;
......@@ -35,24 +35,26 @@ void SpeechRecognitionService::Create(
}
void SpeechRecognitionService::OnNetworkServiceDisconnect() {
#if !BUILDFLAG(ENABLE_SODA)
// If the Speech On-Device API is not enabled, pass the URL loader factory to
// the speech recognition service to allow network requests to the Open Speech
// API.
mojo::PendingRemote<network::mojom::URLLoaderFactory> url_loader_factory;
network::mojom::URLLoaderFactoryParamsPtr params =
network::mojom::URLLoaderFactoryParams::New();
params->process_id = network::mojom::kBrowserProcessId;
params->is_trusted = false;
params->automatically_assign_isolation_info = true;
network::mojom::NetworkContext* network_context =
content::BrowserContext::GetDefaultStoragePartition(context_)
->GetNetworkContext();
network_context->CreateURLLoaderFactory(
url_loader_factory.InitWithNewPipeAndPassReceiver(), std::move(params));
speech_recognition_service_->SetUrlLoaderFactory(
std::move(url_loader_factory));
#endif // !BUILDFLAG(ENABLE_SODA)
if (!enable_soda_) {
// If the Speech On-Device API
// is not enabled, pass the URL
// loader factory to
// the speech recognition service to allow network requests to the Open
// Speech API.
mojo::PendingRemote<network::mojom::URLLoaderFactory> url_loader_factory;
network::mojom::URLLoaderFactoryParamsPtr params =
network::mojom::URLLoaderFactoryParams::New();
params->process_id = network::mojom::kBrowserProcessId;
params->is_trusted = false;
params->automatically_assign_isolation_info = true;
network::mojom::NetworkContext* network_context =
content::BrowserContext::GetDefaultStoragePartition(context_)
->GetNetworkContext();
network_context->CreateURLLoaderFactory(
url_loader_factory.InitWithNewPipeAndPassReceiver(), std::move(params));
speech_recognition_service_->SetUrlLoaderFactory(
std::move(url_loader_factory));
}
}
void SpeechRecognitionService::LaunchIfNotRunning() {
......@@ -61,20 +63,22 @@ void SpeechRecognitionService::LaunchIfNotRunning() {
PrefService* prefs = user_prefs::UserPrefs::Get(context_);
DCHECK(prefs);
#if BUILDFLAG(ENABLE_SODA)
auto binary_path = prefs->GetFilePath(prefs::kSodaBinaryPath);
auto config_path = SpeechRecognitionService::GetSodaConfigPath(prefs);
if (binary_path.empty() || config_path.empty()) {
LOG(ERROR) << "Unable to find SODA files on the device.";
return;
}
#endif
content::ServiceProcessHost::Launch(
speech_recognition_service_.BindNewPipeAndPassReceiver(),
content::ServiceProcessHost::Options()
.WithDisplayName(IDS_UTILITY_PROCESS_SPEECH_RECOGNITION_SERVICE_NAME)
.Pass());
if (enable_soda_) {
content::ServiceProcessHost::Launch(
speech_recognition_service_.BindNewPipeAndPassReceiver(),
content::ServiceProcessHost::Options()
.WithDisplayName(
IDS_UTILITY_PROCESS_SPEECH_RECOGNITION_SERVICE_NAME)
.Pass());
} else {
content::ServiceProcessHost::Launch(
speech_recognition_service_.BindNewPipeAndPassReceiver(),
content::ServiceProcessHost::Options()
.WithDisplayName(
IDS_UTILITY_PROCESS_SPEECH_RECOGNITION_SERVICE_NAME)
.Pass());
}
// Ensure that if the interface is ever disconnected (e.g. the service
// process crashes) or goes idle for a short period of time -- meaning there
......@@ -85,9 +89,17 @@ void SpeechRecognitionService::LaunchIfNotRunning() {
speech_recognition_service_.reset_on_idle_timeout(kIdleProcessTimeout);
speech_recognition_service_client_.reset();
#if BUILDFLAG(ENABLE_SODA)
speech_recognition_service_->SetSodaPath(binary_path, config_path);
#endif
if (enable_soda_) {
auto binary_path = prefs->GetFilePath(prefs::kSodaBinaryPath);
auto config_path = SpeechRecognitionService::GetSodaConfigPath(prefs);
if (binary_path.empty() || config_path.empty()) {
LOG(ERROR) << "Unable to find SODA files on the device.";
return;
}
speech_recognition_service_->SetSodaPath(binary_path, config_path);
}
speech_recognition_service_->BindSpeechRecognitionServiceClient(
speech_recognition_service_client_.BindNewPipeAndPassRemote());
OnNetworkServiceDisconnect();
......
......@@ -5,7 +5,6 @@
#ifndef CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_SERVICE_H_
#define CHROME_BROWSER_SPEECH_SPEECH_RECOGNITION_SERVICE_H_
#include "chrome/services/speech/buildflags.h"
#include "components/keyed_service/core/keyed_service.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
......@@ -46,6 +45,10 @@ class SpeechRecognitionService
// The browser context associated with the keyed service.
content::BrowserContext* const context_;
// A flag indicating whether to use the Speech On-Device API (SODA) for speech
// recognition.
bool enable_soda_ = false;
// The remote to the speech recognition service. The browser will not launch a
// new speech recognition service process if this remote is already bound.
mojo::Remote<media::mojom::SpeechRecognitionService>
......
......@@ -29,6 +29,7 @@ source_set("lib") {
deps = [
":buildflags",
"//base",
"//chrome/services/speech/soda",
"//components/speech",
"//content/public/browser:proto",
"//mojo/public/cpp/bindings",
......@@ -36,10 +37,6 @@ source_set("lib") {
"//services/network/public/cpp",
"//services/network/public/mojom",
]
if (enable_soda) {
deps += [ "//chrome/services/soda/internal" ]
}
}
source_set("unit_tests") {
......
include_rules = [
"+chrome/services/soda/internal",
"+chrome/services/speech",
"+components/speech",
"+content/browser/speech",
......
......@@ -10,25 +10,22 @@
#include "base/bind.h"
#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "chrome/services/speech/soda/soda_client.h"
#include "google_apis/google_api_keys.h"
#include "media/base/audio_buffer.h"
#include "media/base/audio_sample_types.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/limits.h"
#include "media/base/media_switches.h"
#include "media/mojo/common/media_type_converters.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#if BUILDFLAG(ENABLE_SODA)
#include "chrome/services/soda/internal/soda_client.h"
#include "google_apis/google_api_keys.h"
#endif // BUILDFLAG(ENABLE_SODA)
namespace speech {
constexpr char kInvalidAudioDataError[] = "Invalid audio data received.";
namespace {
#if BUILDFLAG(ENABLE_SODA)
// Callback executed by the SODA library on a speech recognition event. The
// callback handle is a void pointer to the SpeechRecognitionRecognizerImpl that
// owns the SODA instance. SpeechRecognitionRecognizerImpl owns the SodaClient
......@@ -43,7 +40,6 @@ void RecognitionCallback(const char* result,
->recognition_event_callback()
.Run(std::string(result), is_final);
}
#endif // BUILDFLAG(ENABLE_SODA)
} // namespace
......@@ -63,11 +59,11 @@ void SpeechRecognitionRecognizerImpl::Create(
}
bool SpeechRecognitionRecognizerImpl::IsMultichannelSupported() {
#if BUILDFLAG(ENABLE_SODA)
return true;
#else
return false;
#endif // BUILDFLAG(ENABLE_SODA)
if (base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption)) {
return true;
} else {
return false;
}
}
void SpeechRecognitionRecognizerImpl::OnRecognitionEvent(
......@@ -86,13 +82,15 @@ SpeechRecognitionRecognizerImpl::SpeechRecognitionRecognizerImpl(
recognition_event_callback_ = media::BindToCurrentLoop(
base::Bind(&SpeechRecognitionRecognizerImpl::OnRecognitionEvent,
weak_factory_.GetWeakPtr()));
#if BUILDFLAG(ENABLE_SODA)
DCHECK(base::PathExists(binary_path));
soda_client_ = std::make_unique<soda::SodaClient>(binary_path);
#else
cloud_client_ = std::make_unique<CloudSpeechRecognitionClient>(
recognition_event_callback(), std::move(speech_recognition_service_impl));
#endif // BUILDFLAG(ENABLE_SODA)
enable_soda_ = base::FeatureList::IsEnabled(media::kUseSodaForLiveCaption);
if (enable_soda_) {
DCHECK(base::PathExists(binary_path));
soda_client_ = std::make_unique<soda::SodaClient>(binary_path);
} else {
cloud_client_ = std::make_unique<CloudSpeechRecognitionClient>(
recognition_event_callback(),
std::move(speech_recognition_service_impl));
}
}
void SpeechRecognitionRecognizerImpl::SendAudioToSpeechRecognitionService(
......@@ -124,41 +122,41 @@ void SpeechRecognitionRecognizerImpl::SendAudioToSpeechRecognitionService(
return;
}
#if BUILDFLAG(ENABLE_SODA)
DCHECK(soda_client_);
DCHECK(base::PathExists(config_path_));
if (!soda_client_->IsInitialized() ||
soda_client_->DidAudioPropertyChange(sample_rate, channel_count)) {
// Initialize the SODA instance.
auto api_key = google_apis::GetSodaAPIKey();
std::string language_pack_directory = config_path_.AsUTF8Unsafe();
SodaConfig config;
config.channel_count = channel_count;
config.sample_rate = sample_rate;
config.language_pack_directory = language_pack_directory.c_str();
config.callback = RecognitionCallback;
config.callback_handle = this;
config.api_key = api_key.c_str();
soda_client_->Reset(config);
}
soda_client_->AddAudio(reinterpret_cast<char*>(buffer->data.data()),
buffer_size);
#else
DCHECK(cloud_client_);
if (!cloud_client_->IsInitialized() ||
cloud_client_->DidAudioPropertyChange(sample_rate, channel_count)) {
// Initialize the stream.
CloudSpeechConfig config;
config.sample_rate = sample_rate;
config.channel_count = channel_count;
config.language_code = "en-US";
cloud_client_->Initialize(config);
if (enable_soda_) {
DCHECK(soda_client_);
DCHECK(base::PathExists(config_path_));
if (!soda_client_->IsInitialized() ||
soda_client_->DidAudioPropertyChange(sample_rate, channel_count)) {
// Initialize the SODA instance.
auto api_key = google_apis::GetSodaAPIKey();
std::string language_pack_directory = config_path_.AsUTF8Unsafe();
SodaConfig config;
config.channel_count = channel_count;
config.sample_rate = sample_rate;
config.language_pack_directory = language_pack_directory.c_str();
config.callback = RecognitionCallback;
config.callback_handle = this;
config.api_key = api_key.c_str();
soda_client_->Reset(config);
}
soda_client_->AddAudio(reinterpret_cast<char*>(buffer->data.data()),
buffer_size);
} else {
DCHECK(cloud_client_);
if (!cloud_client_->IsInitialized() ||
cloud_client_->DidAudioPropertyChange(sample_rate, channel_count)) {
// Initialize the stream.
CloudSpeechConfig config;
config.sample_rate = sample_rate;
config.channel_count = channel_count;
config.language_code = "en-US";
cloud_client_->Initialize(config);
}
cloud_client_->AddAudio(base::span<const char>(
reinterpret_cast<char*>(buffer->data.data()), buffer_size));
}
cloud_client_->AddAudio(base::span<const char>(
reinterpret_cast<char*>(buffer->data.data()), buffer_size));
#endif // BUILDFLAG(ENABLE_SODA)
}
} // namespace speech
......@@ -9,7 +9,6 @@
#include <string>
#include "base/memory/weak_ptr.h"
#include "chrome/services/speech/buildflags.h"
#include "chrome/services/speech/cloud_speech_recognition_client.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#include "mojo/public/cpp/bindings/receiver.h"
......@@ -66,9 +65,8 @@ class SpeechRecognitionRecognizerImpl
// the speech recognition service back to the renderer.
mojo::Remote<media::mojom::SpeechRecognitionRecognizerClient> client_remote_;
#if BUILDFLAG(ENABLE_SODA)
bool enable_soda_ = false;
std::unique_ptr<soda::SodaClient> soda_client_;
#endif // BUILDFLAG(ENABLE_SODA)
std::unique_ptr<CloudSpeechRecognitionClient> cloud_client_;
......
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