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 | #ifndef ML_SODA_RECOGNIZER_IMPL_H_ |
| 6 | #define ML_SODA_RECOGNIZER_IMPL_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/callback_forward.h> |
| 11 | #include <base/macros.h> |
| 12 | #include <mojo/public/cpp/bindings/pending_receiver.h> |
| 13 | #include <mojo/public/cpp/bindings/pending_remote.h> |
| 14 | #include <mojo/public/cpp/bindings/receiver.h> |
| 15 | #include <mojo/public/cpp/bindings/shared_remote.h> |
| 16 | |
| 17 | #include "ml/mojom/soda.mojom.h" |
| 18 | |
| 19 | namespace ml { |
Rob Schonberger | 48b7d06 | 2020-11-13 11:18:58 +1100 | [diff] [blame] | 20 | // Defined in ml/soda.h . Can't include in header due to fake implementation not |
| 21 | // being able to include that file. |
| 22 | class SodaLibrary; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 23 | |
| 24 | // The implementation of SodaSpeechRecognizer. |
| 25 | class SodaRecognizerImpl |
| 26 | : public chromeos::machine_learning::mojom::SodaRecognizer { |
| 27 | public: |
| 28 | // Constructs a SodaRecognizerImpl; and set disconnect handler so |
| 29 | // that the SodaRecognizerImpl will be deleted when the mojom |
| 30 | // connection is destroyed. |
| 31 | // Returns whether the object is create successfully. |
| 32 | static bool Create( |
| 33 | chromeos::machine_learning::mojom::SodaConfigPtr spec, |
| 34 | mojo::PendingRemote<chromeos::machine_learning::mojom::SodaClient> |
| 35 | soda_client, |
| 36 | mojo::PendingReceiver<chromeos::machine_learning::mojom::SodaRecognizer> |
| 37 | soda_recognizer); |
| 38 | |
| 39 | // Called when mojom connection is destroyed. |
| 40 | ~SodaRecognizerImpl(); |
| 41 | |
| 42 | // mojom::SodaRecognizer::AddAudio: |
| 43 | void AddAudio(const std::string& audio) override; |
| 44 | |
| 45 | // mojom::SodaRecognizer::Stop: |
| 46 | void Stop() override; |
| 47 | |
| 48 | // mojom::SodaRecognizer::Start: |
| 49 | void Start() override; |
| 50 | |
| 51 | // mojom::SodaRecognizer::MarkDone: |
| 52 | void MarkDone() override; |
| 53 | |
| 54 | // Used to send the event to the client. For the initial version, only accepts |
Rob Schonberger | 07ee123 | 2020-10-12 17:50:06 +1100 | [diff] [blame] | 55 | // a string, which is in an unspecified format. |
| 56 | void OnSodaEvent(const std::string& soda_response); |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | // Creates a SodaRecognizer and Binds to `receiver` inside so that |
| 60 | // Recognize can be called on the other side for a particular soda |
| 61 | // reconition query. |
| 62 | SodaRecognizerImpl( |
| 63 | chromeos::machine_learning::mojom::SodaConfigPtr spec, |
| 64 | mojo::PendingRemote<chromeos::machine_learning::mojom::SodaClient> |
| 65 | soda_client, |
| 66 | mojo::PendingReceiver<chromeos::machine_learning::mojom::SodaRecognizer> |
| 67 | receiver); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 68 | SodaRecognizerImpl(const SodaRecognizerImpl&) = delete; |
| 69 | SodaRecognizerImpl& operator=(const SodaRecognizerImpl&) = delete; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 70 | |
| 71 | bool successfully_loaded_; |
| 72 | |
| 73 | // Pointer handle to the internal implementation of SodaRecognizer inside |
| 74 | // the SodaLibrary. |
| 75 | void* recognizer_; |
Rob Schonberger | 48b7d06 | 2020-11-13 11:18:58 +1100 | [diff] [blame] | 76 | // Not owned: owned by a std::map in soda.h::GetInstanceAt. |
| 77 | SodaLibrary* soda_library_; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 78 | |
| 79 | mojo::Receiver<chromeos::machine_learning::mojom::SodaRecognizer> receiver_; |
| 80 | |
| 81 | mojo::SharedRemote<chromeos::machine_learning::mojom::SodaClient> |
| 82 | client_remote_; |
Honglin Yu | d220427 | 2020-08-26 14:21:37 +1000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | } // namespace ml |
| 86 | |
| 87 | #endif // ML_SODA_RECOGNIZER_IMPL_H_ |