File: chrome_speech_recognition_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (170 lines) | stat: -rw-r--r-- 6,336 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/speech/chrome_speech_recognition_service.h"

#include <string>
#include <unordered_set>

#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/metrics/histogram_functions.h"
#include "base/scoped_observation.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/soda_language_pack_component_installer.h"
#include "chrome/grit/generated_resources.h"
#include "components/live_caption/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/soda/constants.h"
#include "components/soda/soda_installer.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/service_process_host.h"
#include "media/base/media_switches.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

namespace speech {

constexpr base::TimeDelta kIdleProcessTimeout = base::Seconds(5);

ChromeSpeechRecognitionService::ChromeSpeechRecognitionService(
    content::BrowserContext* context)
    : context_(context) {
#if BUILDFLAG(IS_CHROMEOS)
  if (!base::FeatureList::IsEnabled(
          ash::features::kOnDeviceSpeechRecognition)) {
    return;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  auto* soda_installer = speech::SodaInstaller::GetInstance();

  // The SodaInstaller might not exist in unit tests.
  if (soda_installer) {
    soda_installer_observer_.Observe(soda_installer);
  }
}

ChromeSpeechRecognitionService::~ChromeSpeechRecognitionService() = default;

void ChromeSpeechRecognitionService::BindSpeechRecognitionContext(
    mojo::PendingReceiver<media::mojom::SpeechRecognitionContext> receiver) {
  LaunchIfNotRunning();

  if (speech_recognition_service_.is_bound()) {
    speech_recognition_service_->BindSpeechRecognitionContext(
        std::move(receiver));
  }
}

void ChromeSpeechRecognitionService::BindAudioSourceSpeechRecognitionContext(
    mojo::PendingReceiver<media::mojom::AudioSourceSpeechRecognitionContext>
        receiver) {
  LaunchIfNotRunning();

  if (speech_recognition_service_.is_bound()) {
    speech_recognition_service_->BindAudioSourceSpeechRecognitionContext(
        std::move(receiver));
  }
}

void ChromeSpeechRecognitionService::OnSodaInstalled(
    speech::LanguageCode language_code) {
  if (speech_recognition_service_.is_bound()) {
    speech_recognition_service_->SetSodaConfigPaths(
        ChromeSpeechRecognitionService::GetSodaConfigPaths());
  }
}

void ChromeSpeechRecognitionService::OnSodaInstallError(
    speech::LanguageCode language_code,
    speech::SodaInstaller::ErrorCode error_code) {}

void ChromeSpeechRecognitionService::OnSodaProgress(
    speech::LanguageCode language_code,
    int progress) {}

void ChromeSpeechRecognitionService::LaunchIfNotRunning() {
  if (speech_recognition_service_.is_bound()) {
    return;
  }

  PrefService* profile_prefs = user_prefs::UserPrefs::Get(context_);
  PrefService* global_prefs = g_browser_process->local_state();
  DCHECK(profile_prefs);
  DCHECK(global_prefs);

  // TODO(crbug.com/40162502): Language pack path should be configurable per
  // SpeechRecognitionRecognizer to allow multiple features to use Speech
  // recognition. For now, only Live Caption uses SpeechRecognitionService on
  // non-Chrome OS Chrome, so hard-coding to the Live Caption language code.
  const std::string language_name =
      prefs::GetLiveCaptionLanguageCode(profile_prefs);

  std::optional<speech::SodaLanguagePackComponentConfig> language_config =
      speech::GetLanguageComponentConfig(language_name);
  CHECK(language_config);
  base::UmaHistogramEnumeration("Accessibility.LiveCaption.SodaLanguage",
                                language_config.value().language_code);

  base::FilePath binary_path;
  binary_path = global_prefs->GetFilePath(prefs::kSodaBinaryPath);
  base::flat_map<std::string, base::FilePath> config_paths =
      ChromeSpeechRecognitionService::GetSodaConfigPaths();

  if (binary_path.empty() || config_paths[language_name].empty()) {
    LOG(ERROR) << "Unable to find SODA files on the device.";
    return;
  }

  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
  // are no in-flight messages and no other interfaces bound through this
  // one -- then we will reset |remote|, causing the service process to be
  // terminated if it isn't already.
  speech_recognition_service_.reset_on_disconnect();
  speech_recognition_service_.reset_on_idle_timeout(kIdleProcessTimeout);
  speech_recognition_service_->SetSodaPaths(binary_path, config_paths,
                                            language_name);

  bool mask_offensive_words =
      profile_prefs->GetBoolean(prefs::kLiveCaptionMaskOffensiveWords);
  speech_recognition_service_->SetSodaParams(mask_offensive_words);
}

base::flat_map<std::string, base::FilePath>
ChromeSpeechRecognitionService::GetSodaConfigPaths() {
  base::flat_map<std::string, base::FilePath> config_file_paths;
  std::unordered_set<std::string> registered_language_packs;
  for (const auto& language : g_browser_process->local_state()->GetList(
           prefs::kSodaRegisteredLanguagePacks)) {
    registered_language_packs.insert(language.GetString());
  }

  for (const SodaLanguagePackComponentConfig& config :
       kLanguageComponentConfigs) {
    base::FilePath config_path =
        g_browser_process->local_state()->GetFilePath(config.config_path_pref);

    if (!config_path.empty() &&
        base::Contains(registered_language_packs, config.language_name)) {
      config_file_paths[config.language_name] = config_path;
    }
  }

  return config_file_paths;
}
}  // namespace speech