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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
// Copyright 2012 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/tts_chromeos.h"
#include <algorithm>
#include <utility>
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chromeos/ash/experiences/arc/mojom/tts.mojom.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/tts_platform.h"
void TtsPlatformImplChromeOs::SetVoices(
std::vector<content::VoiceData> voices) {
std::sort(voices.begin(), voices.end(), [](const auto& v1, const auto& v2) {
return !v1.remote && v2.remote;
});
voices_ = std::move(voices);
received_word_event_ = false;
}
void TtsPlatformImplChromeOs::ReceivedWordEvent() {
if (received_word_event_)
return;
received_word_event_ = true;
for (auto& voice : voices_)
voice.events.insert(content::TTS_EVENT_WORD);
content::TtsController::GetInstance()->VoicesChanged();
}
TtsPlatformImplChromeOs::TtsPlatformImplChromeOs() = default;
TtsPlatformImplChromeOs::~TtsPlatformImplChromeOs() = default;
bool TtsPlatformImplChromeOs::PlatformImplSupported() {
// TODO(crbug.com/40151186): Chrome OS Platform should support background
// initialisation.
return arc::ArcServiceManager::Get() && arc::ArcServiceManager::Get()
->arc_bridge_service()
->tts()
->IsConnected();
}
bool TtsPlatformImplChromeOs::PlatformImplInitialized() {
// On Chrome OS, the extension-based voices are really the platform level
// voices. ARC++ takes a while to load, so do not block TtsController from
// processing and speaking utterances here.
return true;
}
void TtsPlatformImplChromeOs::LoadBuiltInTtsEngine(
content::BrowserContext* browser_context) {
content::TtsEngineDelegate* tts_engine_delegate =
content::TtsController::GetInstance()->GetTtsEngineDelegate();
if (tts_engine_delegate)
tts_engine_delegate->LoadBuiltInTtsEngine(browser_context);
}
void TtsPlatformImplChromeOs::Speak(
int utterance_id,
const std::string& utterance,
const std::string& lang,
const content::VoiceData& voice,
const content::UtteranceContinuousParameters& params,
base::OnceCallback<void(bool)> on_speak_finished) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Parse SSML and process speech.
content::TtsController::GetInstance()->StripSSML(
utterance, base::BindOnce(&TtsPlatformImplChromeOs::ProcessSpeech,
base::Unretained(this), utterance_id, lang,
voice, params, std::move(on_speak_finished)));
}
void TtsPlatformImplChromeOs::ProcessSpeech(
int utterance_id,
const std::string& lang,
const content::VoiceData& voice,
const content::UtteranceContinuousParameters& params,
base::OnceCallback<void(bool)> on_speak_finished,
const std::string& parsed_utterance) {
auto* const arc_service_manager = arc::ArcServiceManager::Get();
if (!arc_service_manager) {
std::move(on_speak_finished).Run(false);
return;
}
arc::mojom::TtsInstance* tts = ARC_GET_INSTANCE_FOR_METHOD(
arc_service_manager->arc_bridge_service()->tts(), Speak);
if (!tts) {
std::move(on_speak_finished).Run(false);
return;
}
arc::mojom::TtsUtterancePtr arc_utterance = arc::mojom::TtsUtterance::New();
arc_utterance->utteranceId = utterance_id;
arc_utterance->text = parsed_utterance;
arc_utterance->rate = params.rate;
arc_utterance->pitch = params.pitch;
int voice_id = 0;
if (!voice.native_voice_identifier.empty() &&
base::StringToInt(voice.native_voice_identifier, &voice_id)) {
arc_utterance->voice_id = voice_id;
}
tts->Speak(std::move(arc_utterance));
std::move(on_speak_finished).Run(true);
}
bool TtsPlatformImplChromeOs::StopSpeaking() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
auto* const arc_service_manager = arc::ArcServiceManager::Get();
if (!arc_service_manager)
return false;
arc::mojom::TtsInstance* tts = ARC_GET_INSTANCE_FOR_METHOD(
arc_service_manager->arc_bridge_service()->tts(), Stop);
if (!tts)
return false;
tts->Stop();
return true;
}
void TtsPlatformImplChromeOs::GetVoices(
std::vector<content::VoiceData>* out_voices) {
for (const auto& voice : voices_)
out_voices->push_back(voice);
}
std::string TtsPlatformImplChromeOs::GetError() {
return error_;
}
void TtsPlatformImplChromeOs::ClearError() {
error_ = std::string();
}
void TtsPlatformImplChromeOs::SetError(const std::string& error) {
error_ = error;
}
bool TtsPlatformImplChromeOs::IsSpeaking() {
return false;
}
void TtsPlatformImplChromeOs::FinalizeVoiceOrdering(
std::vector<content::VoiceData>& voices) {
// Move all Espeak voices to the end.
auto partition_point = std::stable_partition(
voices.begin(), voices.end(), [](const content::VoiceData& voice) {
return voice.engine_id !=
extension_misc::kEspeakSpeechSynthesisExtensionId;
});
// Move all native voices to the end, before Espeak voices.
std::stable_partition(
voices.begin(), partition_point,
[](const content::VoiceData& voice) { return !voice.native; });
}
void TtsPlatformImplChromeOs::RefreshVoices() {
// Android voices can be updated silently.
// If it happens, we can't return the latest voices here, but below
// eventually calls TtsController::VoicesChanged.
auto* const arc_service_manager = arc::ArcServiceManager::Get();
if (!arc_service_manager)
return;
arc::mojom::TtsInstance* tts = ARC_GET_INSTANCE_FOR_METHOD(
arc_service_manager->arc_bridge_service()->tts(), RefreshVoices);
if (!tts)
return;
tts->RefreshVoices();
}
// static
TtsPlatformImplChromeOs* TtsPlatformImplChromeOs::GetInstance() {
static base::NoDestructor<TtsPlatformImplChromeOs> tts_platform;
return tts_platform.get();
}
|