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