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