File: speech_recognizer_fsm.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SPEECH_SPEECH_RECOGNIZER_FSM_H_
#define COMPONENTS_SPEECH_SPEECH_RECOGNIZER_FSM_H_

#include <vector>

#include "base/memory/ref_counted.h"
#include "components/speech/audio_buffer.h"
#include "media/mojo/mojom/audio_data.mojom.h"
#include "media/mojo/mojom/speech_recognition_error.mojom.h"
#include "media/mojo/mojom/speech_recognition_recognition_context.h"
#include "media/mojo/mojom/speech_recognition_result.mojom.h"

namespace speech {

// Interface for the speech recognizer finite-state machine used to power the
// Web Speech API.
// TODO(crbug.com/40286514): Remove the scoped_refptr usage.
class SpeechRecognizerFsm {
 public:
  // The Finite State Machine states of the recognizer in sequential order.
  enum FSMState {
    STATE_IDLE = 0,
    STATE_PREPARING,
    STATE_STARTING,
    STATE_ESTIMATING_ENVIRONMENT,
    STATE_WAITING_FOR_SPEECH,
    STATE_RECOGNIZING,
    STATE_WAITING_FINAL_RESULT,
    STATE_ENDED,
    STATE_MAX_VALUE = STATE_ENDED
  };

  // The Finite State Machine events used by the recognizer. Event dispatching
  // must be sequential, otherwise it will break the rules and the assumptions
  // of the finite state automata model.
  enum FSMEvent {
    EVENT_ABORT = 0,
    EVENT_PREPARE,
    EVENT_START,
    EVENT_STOP_CAPTURE,
    EVENT_AUDIO_DATA,
    EVENT_ENGINE_RESULT,
    EVENT_UPDATE_RECOGNITION_CONTEXT,
    EVENT_ENGINE_ERROR,
    EVENT_AUDIO_ERROR,
    EVENT_MAX_VALUE = EVENT_AUDIO_ERROR
  };

  struct FSMEventArgs {
    explicit FSMEventArgs(FSMEvent event_value);
    FSMEventArgs(const FSMEventArgs& other);
    ~FSMEventArgs();

    FSMEvent event;
    media::mojom::AudioDataS16Ptr audio_data;
    scoped_refptr<AudioChunk> audio_chunk;
    std::vector<media::mojom::WebSpeechRecognitionResultPtr> engine_results;
    media::SpeechRecognitionRecognitionContext recognition_context;
    media::mojom::SpeechRecognitionError engine_error;
  };

  // Defines the behavior of the recognizer FSM, selecting the appropriate
  // transition according to the current state and event.
  FSMState ExecuteTransitionAndGetNextState(const FSMEventArgs& args);

  // Entry point for pushing any new external event into the recognizer FSM.
  virtual void DispatchEvent(const FSMEventArgs& event_args) = 0;

  // The methods below handle transitions of the recognizer FSM.
  virtual void ProcessAudioPipeline(const FSMEventArgs& event_args) = 0;
  virtual FSMState PrepareRecognition(const FSMEventArgs&) = 0;
  virtual FSMState StartRecording(const FSMEventArgs& event_args) = 0;
  virtual FSMState StartRecognitionEngine(const FSMEventArgs& event_args) = 0;
  virtual FSMState WaitEnvironmentEstimationCompletion(
      const FSMEventArgs& event_args) = 0;
  virtual FSMState DetectUserSpeechOrTimeout(
      const FSMEventArgs& event_args) = 0;
  virtual FSMState StopCaptureAndWaitForResult(
      const FSMEventArgs& event_args) = 0;
  virtual FSMState ProcessIntermediateResult(
      const FSMEventArgs& event_args) = 0;
  virtual FSMState ProcessFinalResult(const FSMEventArgs& event_args) = 0;
  virtual FSMState AbortSilently(const FSMEventArgs& event_args) = 0;
  virtual FSMState AbortWithError(const FSMEventArgs& event_args) = 0;
  virtual FSMState Abort(const media::mojom::SpeechRecognitionError& error) = 0;
  virtual FSMState DetectEndOfSpeech(const FSMEventArgs& event_args) = 0;
  virtual FSMState UpdateRecognitionContext(const FSMEventArgs& event_args) = 0;
  virtual FSMState DoNothing(const FSMEventArgs& event_args) const = 0;
  virtual FSMState NotFeasible(const FSMEventArgs& event_args) = 0;

 protected:
  friend class base::RefCountedThreadSafe<SpeechRecognizerFsm>;
  virtual ~SpeechRecognizerFsm() = default;
  FSMState state_ = STATE_IDLE;
  bool is_dispatching_event_ = false;
};

}  // namespace speech

#endif  // COMPONENTS_SPEECH_SPEECH_RECOGNIZER_FSM_H_