blob: f45e657442aa6fb3ddcf260c98638a170b00ff4a [file] [log] [blame]
Honglin Yud2204272020-08-26 14:21:37 +10001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains the implementation of `SodaRecognizerImpl` when SODA is
6// not intended to be supported.
7
8// Suppress unused-private-field since this dummy class impl is deliberately
9// non-functional and needn't reference all variables.
10#pragma GCC diagnostic ignored "-Wunused-private-field"
11
12#include "ml/soda_recognizer_impl.h"
13
14namespace ml {
15namespace {
16
Rob Schonbergerd930aa62020-10-06 17:42:15 +110017using ::chromeos::machine_learning::mojom::EndpointReason;
18using ::chromeos::machine_learning::mojom::FinalResult;
19using ::chromeos::machine_learning::mojom::FinalResultPtr;
Honglin Yud2204272020-08-26 14:21:37 +100020using ::chromeos::machine_learning::mojom::SodaClient;
21using ::chromeos::machine_learning::mojom::SodaConfigPtr;
22using ::chromeos::machine_learning::mojom::SodaRecognizer;
Rob Schonbergerd930aa62020-10-06 17:42:15 +110023using ::chromeos::machine_learning::mojom::SpeechRecognizerEvent;
24using ::chromeos::machine_learning::mojom::SpeechRecognizerEventPtr;
Honglin Yud2204272020-08-26 14:21:37 +100025
26constexpr char kOnDeviceSpeechNotSupportedMessage[] =
27 "On-device speech is not supported.";
28
29void SodaCallback(const char* soda_response_str,
30 int size,
31 void* soda_recognizer_impl) {
32 reinterpret_cast<SodaRecognizerImpl*>(soda_recognizer_impl)
Rob Schonberger07ee1232020-10-12 17:50:06 +110033 ->OnSodaEvent(std::string());
Honglin Yud2204272020-08-26 14:21:37 +100034}
35
36} // namespace
37
38bool SodaRecognizerImpl::Create(
39 SodaConfigPtr spec,
40 mojo::PendingRemote<SodaClient> soda_client,
41 mojo::PendingReceiver<SodaRecognizer> soda_recognizer) {
42 auto recognizer_impl = new SodaRecognizerImpl(
43 std::move(spec), std::move(soda_client), std::move(soda_recognizer));
44 // Set the disconnection handler to strongly bind `recognizer_impl` to delete
45 // `recognizer_impl` when the connection is gone.
46 recognizer_impl->receiver_.set_disconnect_handler(base::Bind(
47 [](const SodaRecognizerImpl* const recognizer_impl) {
48 delete recognizer_impl;
49 },
50 base::Unretained(recognizer_impl)));
51
52 return recognizer_impl->successfully_loaded_;
53}
54
55void SodaRecognizerImpl::AddAudio(const std::string& audio) {
56 // Immediately sends the error message to client.
57 SodaCallback(nullptr, 0, this);
58}
59
60void SodaRecognizerImpl::Stop() {
61 // Immediately sends the error message to client.
62 SodaCallback(nullptr, 0, this);
63}
64
65void SodaRecognizerImpl::Start() {
66 // Immediately sends the error message to client.
67 SodaCallback(nullptr, 0, this);
68}
69
70void SodaRecognizerImpl::MarkDone() {
71 // Immediately sends the error message to client.
72 SodaCallback(nullptr, 0, this);
73}
74
Rob Schonberger07ee1232020-10-12 17:50:06 +110075void SodaRecognizerImpl::OnSodaEvent(const std::string& ignored_response) {
Rob Schonbergerd930aa62020-10-06 17:42:15 +110076 SpeechRecognizerEventPtr event = SpeechRecognizerEvent::New();
77 FinalResultPtr final_result = FinalResult::New();
Rob Schonberger07ee1232020-10-12 17:50:06 +110078 final_result->final_hypotheses.push_back(kOnDeviceSpeechNotSupportedMessage);
Rob Schonbergerd930aa62020-10-06 17:42:15 +110079 final_result->endpoint_reason = EndpointReason::ENDPOINT_UNKNOWN;
80 event->set_final_result(std::move(final_result));
81 client_remote_->OnSpeechRecognizerEvent(std::move(event));
Honglin Yud2204272020-08-26 14:21:37 +100082}
83
84SodaRecognizerImpl::SodaRecognizerImpl(
85 SodaConfigPtr spec,
86 mojo::PendingRemote<SodaClient> soda_client,
87 mojo::PendingReceiver<SodaRecognizer> soda_recognizer)
88 : successfully_loaded_(true),
89 recognizer_(nullptr),
90 receiver_(this, std::move(soda_recognizer)),
91 client_remote_(std::move(soda_client)) {}
92
93SodaRecognizerImpl::~SodaRecognizerImpl() = default;
94
95} // namespace ml