blob: 0084ef9de0cb87e7a22eb700568891e07a1c7f8c [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 {
Rob Schonberger48b7d062020-11-13 11:18:58 +110020// Defined in ml/soda.h . Can't include in header due to fake implementation not
21// being able to include that file.
22class SodaLibrary;
Honglin Yud2204272020-08-26 14:21:37 +100023
24// The implementation of SodaSpeechRecognizer.
25class 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 Schonberger07ee1232020-10-12 17:50:06 +110055 // a string, which is in an unspecified format.
56 void OnSodaEvent(const std::string& soda_response);
Honglin Yud2204272020-08-26 14:21:37 +100057
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 Fan6bc59e12020-11-11 02:51:06 +090068 SodaRecognizerImpl(const SodaRecognizerImpl&) = delete;
69 SodaRecognizerImpl& operator=(const SodaRecognizerImpl&) = delete;
Honglin Yud2204272020-08-26 14:21:37 +100070
71 bool successfully_loaded_;
72
73 // Pointer handle to the internal implementation of SodaRecognizer inside
74 // the SodaLibrary.
75 void* recognizer_;
Rob Schonberger48b7d062020-11-13 11:18:58 +110076 // Not owned: owned by a std::map in soda.h::GetInstanceAt.
77 SodaLibrary* soda_library_;
Honglin Yud2204272020-08-26 14:21:37 +100078
79 mojo::Receiver<chromeos::machine_learning::mojom::SodaRecognizer> receiver_;
80
81 mojo::SharedRemote<chromeos::machine_learning::mojom::SodaClient>
82 client_remote_;
Honglin Yud2204272020-08-26 14:21:37 +100083};
84
85} // namespace ml
86
87#endif // ML_SODA_RECOGNIZER_IMPL_H_