blob: 309980f7a3d3a0474dbe70b130c028a54faf6cf4 [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#ifndef ML_SODA_RECOGNIZER_IMPL_H_
6#define ML_SODA_RECOGNIZER_IMPL_H_
7
8#include <string>
Rob Schonbergerd3f25fc2020-12-02 10:57:17 +11009#include <vector>
Honglin Yud2204272020-08-26 14:21:37 +100010
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
20namespace ml {
Rob Schonberger48b7d062020-11-13 11:18:58 +110021// Defined in ml/soda.h . Can't include in header due to fake implementation not
22// being able to include that file.
23class SodaLibrary;
Honglin Yud2204272020-08-26 14:21:37 +100024
25// The implementation of SodaSpeechRecognizer.
26class 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 Schonbergerd3f25fc2020-12-02 10:57:17 +110044 void AddAudio(const std::vector<uint8_t>& audio) override;
Honglin Yud2204272020-08-26 14:21:37 +100045
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 Schonberger07ee1232020-10-12 17:50:06 +110056 // a string, which is in an unspecified format.
57 void OnSodaEvent(const std::string& soda_response);
Honglin Yud2204272020-08-26 14:21:37 +100058
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 Fan6bc59e12020-11-11 02:51:06 +090069 SodaRecognizerImpl(const SodaRecognizerImpl&) = delete;
70 SodaRecognizerImpl& operator=(const SodaRecognizerImpl&) = delete;
Honglin Yud2204272020-08-26 14:21:37 +100071
72 bool successfully_loaded_;
73
74 // Pointer handle to the internal implementation of SodaRecognizer inside
75 // the SodaLibrary.
76 void* recognizer_;
Rob Schonberger48b7d062020-11-13 11:18:58 +110077 // Not owned: owned by a std::map in soda.h::GetInstanceAt.
78 SodaLibrary* soda_library_;
Honglin Yud2204272020-08-26 14:21:37 +100079
80 mojo::Receiver<chromeos::machine_learning::mojom::SodaRecognizer> receiver_;
81
82 mojo::SharedRemote<chromeos::machine_learning::mojom::SodaClient>
83 client_remote_;
Honglin Yud2204272020-08-26 14:21:37 +100084};
85
86} // namespace ml
87
88#endif // ML_SODA_RECOGNIZER_IMPL_H_