blob: 79c3e99111e7d9c74706e373ce79c744853b28d1 [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>
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
19namespace ml {
20
21// The implementation of SodaSpeechRecognizer.
22class SodaRecognizerImpl
23 : public chromeos::machine_learning::mojom::SodaRecognizer {
24 public:
25 // Constructs a SodaRecognizerImpl; and set disconnect handler so
26 // that the SodaRecognizerImpl will be deleted when the mojom
27 // connection is destroyed.
28 // Returns whether the object is create successfully.
29 static bool Create(
30 chromeos::machine_learning::mojom::SodaConfigPtr spec,
31 mojo::PendingRemote<chromeos::machine_learning::mojom::SodaClient>
32 soda_client,
33 mojo::PendingReceiver<chromeos::machine_learning::mojom::SodaRecognizer>
34 soda_recognizer);
35
36 // Called when mojom connection is destroyed.
37 ~SodaRecognizerImpl();
38
39 // mojom::SodaRecognizer::AddAudio:
40 void AddAudio(const std::string& audio) override;
41
42 // mojom::SodaRecognizer::Stop:
43 void Stop() override;
44
45 // mojom::SodaRecognizer::Start:
46 void Start() override;
47
48 // mojom::SodaRecognizer::MarkDone:
49 void MarkDone() override;
50
51 // Used to send the event to the client. For the initial version, only accepts
Rob Schonberger07ee1232020-10-12 17:50:06 +110052 // a string, which is in an unspecified format.
53 void OnSodaEvent(const std::string& soda_response);
Honglin Yud2204272020-08-26 14:21:37 +100054
55 private:
56 // Creates a SodaRecognizer and Binds to `receiver` inside so that
57 // Recognize can be called on the other side for a particular soda
58 // reconition query.
59 SodaRecognizerImpl(
60 chromeos::machine_learning::mojom::SodaConfigPtr spec,
61 mojo::PendingRemote<chromeos::machine_learning::mojom::SodaClient>
62 soda_client,
63 mojo::PendingReceiver<chromeos::machine_learning::mojom::SodaRecognizer>
64 receiver);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090065 SodaRecognizerImpl(const SodaRecognizerImpl&) = delete;
66 SodaRecognizerImpl& operator=(const SodaRecognizerImpl&) = delete;
Honglin Yud2204272020-08-26 14:21:37 +100067
68 bool successfully_loaded_;
69
70 // Pointer handle to the internal implementation of SodaRecognizer inside
71 // the SodaLibrary.
72 void* recognizer_;
73
74 mojo::Receiver<chromeos::machine_learning::mojom::SodaRecognizer> receiver_;
75
76 mojo::SharedRemote<chromeos::machine_learning::mojom::SodaClient>
77 client_remote_;
Honglin Yud2204272020-08-26 14:21:37 +100078};
79
80} // namespace ml
81
82#endif // ML_SODA_RECOGNIZER_IMPL_H_