Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/speech/speech_recognition_session.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "base/functional/bind.h" |
| 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "content/browser/speech/speech_recognition_manager_impl.h" |
| 12 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 13 | #include "mojo/public/cpp/bindings/remote.h" |
| 14 | |
| 15 | namespace content { |
| 16 | |
| 17 | SpeechRecognitionSession::SpeechRecognitionSession( |
Evan Liu | d725228 | 2024-05-16 20:10:32 | [diff] [blame] | 18 | mojo::PendingRemote<media::mojom::SpeechRecognitionSessionClient> client) |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 19 | : client_(std::move(client)) { |
| 20 | client_.set_disconnect_handler( |
| 21 | base::BindOnce(&SpeechRecognitionSession::ConnectionErrorHandler, |
| 22 | base::Unretained(this))); |
| 23 | } |
| 24 | |
| 25 | SpeechRecognitionSession::~SpeechRecognitionSession() { |
| 26 | // If a connection error happens and the session hasn't been stopped yet, |
| 27 | // abort it. |
| 28 | if (!stopped_) { |
| 29 | Abort(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | base::WeakPtr<SpeechRecognitionSession> SpeechRecognitionSession::AsWeakPtr() { |
| 34 | return weak_factory_.GetWeakPtr(); |
| 35 | } |
| 36 | |
| 37 | void SpeechRecognitionSession::Abort() { |
| 38 | SpeechRecognitionManager::GetInstance()->AbortSession(session_id_); |
| 39 | stopped_ = true; |
| 40 | } |
| 41 | |
| 42 | void SpeechRecognitionSession::StopCapture() { |
| 43 | SpeechRecognitionManager::GetInstance()->StopAudioCaptureForSession( |
| 44 | session_id_); |
| 45 | stopped_ = true; |
| 46 | } |
| 47 | |
Yiren Wang | 2399856 | 2025-01-28 21:31:05 | [diff] [blame] | 48 | void SpeechRecognitionSession::UpdateRecognitionContext( |
| 49 | const media::SpeechRecognitionRecognitionContext& recognition_context) { |
| 50 | SpeechRecognitionManager::GetInstance()->UpdateRecognitionContextForSession( |
| 51 | session_id_, recognition_context); |
| 52 | } |
| 53 | |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 54 | // -------- SpeechRecognitionEventListener interface implementation ----------- |
| 55 | |
| 56 | void SpeechRecognitionSession::OnRecognitionStart(int session_id) { |
| 57 | client_->Started(); |
| 58 | } |
| 59 | |
| 60 | void SpeechRecognitionSession::OnAudioStart(int session_id) { |
| 61 | client_->AudioStarted(); |
| 62 | } |
| 63 | |
| 64 | void SpeechRecognitionSession::OnSoundStart(int session_id) { |
| 65 | client_->SoundStarted(); |
| 66 | } |
| 67 | |
| 68 | void SpeechRecognitionSession::OnSoundEnd(int session_id) { |
| 69 | client_->SoundEnded(); |
| 70 | } |
| 71 | |
| 72 | void SpeechRecognitionSession::OnAudioEnd(int session_id) { |
| 73 | client_->AudioEnded(); |
| 74 | } |
| 75 | |
| 76 | void SpeechRecognitionSession::OnRecognitionEnd(int session_id) { |
| 77 | client_->Ended(); |
| 78 | stopped_ = true; |
| 79 | client_.reset(); |
| 80 | } |
| 81 | |
| 82 | void SpeechRecognitionSession::OnRecognitionResults( |
| 83 | int session_id, |
Evan Liu | d725228 | 2024-05-16 20:10:32 | [diff] [blame] | 84 | const std::vector<media::mojom::WebSpeechRecognitionResultPtr>& results) { |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 85 | client_->ResultRetrieved(mojo::Clone(results)); |
| 86 | } |
| 87 | |
| 88 | void SpeechRecognitionSession::OnRecognitionError( |
| 89 | int session_id, |
Evan Liu | d725228 | 2024-05-16 20:10:32 | [diff] [blame] | 90 | const media::mojom::SpeechRecognitionError& error) { |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 91 | if (!client_.is_bound()) { |
| 92 | return; |
| 93 | } |
Evan Liu | d725228 | 2024-05-16 20:10:32 | [diff] [blame] | 94 | client_->ErrorOccurred(media::mojom::SpeechRecognitionError::New(error)); |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // The events below are currently not used by speech JS APIs implementation. |
| 98 | void SpeechRecognitionSession::OnAudioLevelsChange(int session_id, |
| 99 | float volume, |
| 100 | float noise_volume) {} |
| 101 | |
Evan Liu | 28ec8bd | 2024-04-26 16:53:03 | [diff] [blame] | 102 | void SpeechRecognitionSession::ConnectionErrorHandler() { |
| 103 | if (!stopped_) { |
| 104 | Abort(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | } // namespace content |