blob: 284e49b6f3a1fd71dfb04bb95ecbc15352d2339f [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
52 // a string.
53 void OnSodaEvent(const std::string& event_string);
54
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);
65
66 bool successfully_loaded_;
67
68 // Pointer handle to the internal implementation of SodaRecognizer inside
69 // the SodaLibrary.
70 void* recognizer_;
71
72 mojo::Receiver<chromeos::machine_learning::mojom::SodaRecognizer> receiver_;
73
74 mojo::SharedRemote<chromeos::machine_learning::mojom::SodaClient>
75 client_remote_;
76
77 DISALLOW_COPY_AND_ASSIGN(SodaRecognizerImpl);
78};
79
80} // namespace ml
81
82#endif // ML_SODA_RECOGNIZER_IMPL_H_