Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 1 | // 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 | |
| 14 | namespace ml { |
| 15 | namespace { |
| 16 | |
Rob Schonberger | d930aa6 | 2020-10-06 17:42:15 +1100 | [diff] [blame] | 17 | using ::chromeos::machine_learning::mojom::EndpointReason; |
| 18 | using ::chromeos::machine_learning::mojom::FinalResult; |
| 19 | using ::chromeos::machine_learning::mojom::FinalResultPtr; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 20 | using ::chromeos::machine_learning::mojom::SodaClient; |
| 21 | using ::chromeos::machine_learning::mojom::SodaConfigPtr; |
| 22 | using ::chromeos::machine_learning::mojom::SodaRecognizer; |
Rob Schonberger | d930aa6 | 2020-10-06 17:42:15 +1100 | [diff] [blame] | 23 | using ::chromeos::machine_learning::mojom::SpeechRecognizerEvent; |
| 24 | using ::chromeos::machine_learning::mojom::SpeechRecognizerEventPtr; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 25 | |
| 26 | constexpr char kOnDeviceSpeechNotSupportedMessage[] = |
| 27 | "On-device speech is not supported."; |
| 28 | |
| 29 | void SodaCallback(const char* soda_response_str, |
| 30 | int size, |
| 31 | void* soda_recognizer_impl) { |
| 32 | reinterpret_cast<SodaRecognizerImpl*>(soda_recognizer_impl) |
Rob Schonberger | 07ee123 | 2020-10-12 17:50:06 +1100 | [diff] [blame^] | 33 | ->OnSodaEvent(std::string()); |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | } // namespace |
| 37 | |
| 38 | bool 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 | |
| 55 | void SodaRecognizerImpl::AddAudio(const std::string& audio) { |
| 56 | // Immediately sends the error message to client. |
| 57 | SodaCallback(nullptr, 0, this); |
| 58 | } |
| 59 | |
| 60 | void SodaRecognizerImpl::Stop() { |
| 61 | // Immediately sends the error message to client. |
| 62 | SodaCallback(nullptr, 0, this); |
| 63 | } |
| 64 | |
| 65 | void SodaRecognizerImpl::Start() { |
| 66 | // Immediately sends the error message to client. |
| 67 | SodaCallback(nullptr, 0, this); |
| 68 | } |
| 69 | |
| 70 | void SodaRecognizerImpl::MarkDone() { |
| 71 | // Immediately sends the error message to client. |
| 72 | SodaCallback(nullptr, 0, this); |
| 73 | } |
| 74 | |
Rob Schonberger | 07ee123 | 2020-10-12 17:50:06 +1100 | [diff] [blame^] | 75 | void SodaRecognizerImpl::OnSodaEvent(const std::string& ignored_response) { |
Rob Schonberger | d930aa6 | 2020-10-06 17:42:15 +1100 | [diff] [blame] | 76 | SpeechRecognizerEventPtr event = SpeechRecognizerEvent::New(); |
| 77 | FinalResultPtr final_result = FinalResult::New(); |
Rob Schonberger | 07ee123 | 2020-10-12 17:50:06 +1100 | [diff] [blame^] | 78 | final_result->final_hypotheses.push_back(kOnDeviceSpeechNotSupportedMessage); |
Rob Schonberger | d930aa6 | 2020-10-06 17:42:15 +1100 | [diff] [blame] | 79 | final_result->endpoint_reason = EndpointReason::ENDPOINT_UNKNOWN; |
| 80 | event->set_final_result(std::move(final_result)); |
| 81 | client_remote_->OnSpeechRecognizerEvent(std::move(event)); |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | SodaRecognizerImpl::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 | |
| 93 | SodaRecognizerImpl::~SodaRecognizerImpl() = default; |
| 94 | |
| 95 | } // namespace ml |