Commit a172bcbd authored by evliu's avatar evliu Committed by Commit Bot

Update the DefaultRendererFactory to use the "OnReady" speech recognition callback

This CL updates the DefaultRendererFactory to use the "OnReady" speech
recognition callback to determine if and when speech recognition is
available rather than continuously making mojo calls to the render thread.

Bug: 1098552
Change-Id: Iabf19155f350bb6bd5aac8a6b157decb457d110e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2261740Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Commit-Queue: Evan Liu <evliu@google.com>
Cr-Commit-Position: refs/heads/master@{#782064}
parent 5f9e656d
...@@ -86,6 +86,18 @@ bool ChromeSpeechRecognitionClient::IsSpeechRecognitionAvailable() { ...@@ -86,6 +86,18 @@ bool ChromeSpeechRecognitionClient::IsSpeechRecognitionAvailable() {
is_recognizer_bound_ && speech_recognition_recognizer_.is_connected(); is_recognizer_bound_ && speech_recognition_recognizer_.is_connected();
} }
// The OnReadyCallback is set by the owner of |this| and is executed when speech
// recognition becomes available. Setting the callback will override any
// existing callback.
void ChromeSpeechRecognitionClient::SetOnReadyCallback(
SpeechRecognitionClient::OnReadyCallback callback) {
on_ready_callback_ = std::move(callback);
// Immediately run the callback if speech recognition is already available.
if (IsSpeechRecognitionAvailable() && on_ready_callback_)
std::move(on_ready_callback_).Run();
}
void ChromeSpeechRecognitionClient::OnSpeechRecognitionRecognitionEvent( void ChromeSpeechRecognitionClient::OnSpeechRecognitionRecognitionEvent(
media::mojom::SpeechRecognitionResultPtr result) { media::mojom::SpeechRecognitionResultPtr result) {
caption_host_->OnTranscription( caption_host_->OnTranscription(
......
...@@ -43,6 +43,8 @@ class ChromeSpeechRecognitionClient ...@@ -43,6 +43,8 @@ class ChromeSpeechRecognitionClient
int sample_rate, int sample_rate,
media::ChannelLayout channel_layout) override; media::ChannelLayout channel_layout) override;
bool IsSpeechRecognitionAvailable() override; bool IsSpeechRecognitionAvailable() override;
void SetOnReadyCallback(
SpeechRecognitionClient::OnReadyCallback callback) override;
// Callback executed when the recognizer is bound. Sets the flag indicating // Callback executed when the recognizer is bound. Sets the flag indicating
// whether the speech recognition service supports multichannel audio. // whether the speech recognition service supports multichannel audio.
......
...@@ -29,6 +29,8 @@ class MEDIA_EXPORT SpeechRecognitionClient { ...@@ -29,6 +29,8 @@ class MEDIA_EXPORT SpeechRecognitionClient {
media::ChannelLayout channel_layout) = 0; media::ChannelLayout channel_layout) = 0;
virtual bool IsSpeechRecognitionAvailable() = 0; virtual bool IsSpeechRecognitionAvailable() = 0;
virtual void SetOnReadyCallback(OnReadyCallback callback) = 0;
}; };
} // namespace media } // namespace media
......
...@@ -41,6 +41,12 @@ DefaultRendererFactory::DefaultRendererFactory( ...@@ -41,6 +41,12 @@ DefaultRendererFactory::DefaultRendererFactory(
get_gpu_factories_cb_(get_gpu_factories_cb), get_gpu_factories_cb_(get_gpu_factories_cb),
speech_recognition_client_(std::move(speech_recognition_client)) { speech_recognition_client_(std::move(speech_recognition_client)) {
DCHECK(decoder_factory_); DCHECK(decoder_factory_);
if (speech_recognition_client_) {
// Unretained is safe because |this| owns the speech recognition client.
speech_recognition_client_->SetOnReadyCallback(media::BindToCurrentLoop(
base::BindOnce(&DefaultRendererFactory::EnableSpeechRecognition,
base::Unretained(this))));
}
} }
#endif #endif
...@@ -129,9 +135,16 @@ std::unique_ptr<Renderer> DefaultRendererFactory::CreateRenderer( ...@@ -129,9 +135,16 @@ std::unique_ptr<Renderer> DefaultRendererFactory::CreateRenderer(
void DefaultRendererFactory::TranscribeAudio( void DefaultRendererFactory::TranscribeAudio(
scoped_refptr<media::AudioBuffer> buffer) { scoped_refptr<media::AudioBuffer> buffer) {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
if (speech_recognition_client_ && if (is_speech_recognition_available_ && speech_recognition_client_)
speech_recognition_client_->IsSpeechRecognitionAvailable()) {
speech_recognition_client_->AddAudio(std::move(buffer)); speech_recognition_client_->AddAudio(std::move(buffer));
#endif
}
void DefaultRendererFactory::EnableSpeechRecognition() {
#if !defined(OS_ANDROID)
if (speech_recognition_client_) {
is_speech_recognition_available_ =
speech_recognition_client_->IsSpeechRecognitionAvailable();
} }
#endif #endif
} }
......
...@@ -72,6 +72,8 @@ class MEDIA_EXPORT DefaultRendererFactory : public RendererFactory { ...@@ -72,6 +72,8 @@ class MEDIA_EXPORT DefaultRendererFactory : public RendererFactory {
const gfx::ColorSpace& target_color_space, const gfx::ColorSpace& target_color_space,
GpuVideoAcceleratorFactories* gpu_factories); GpuVideoAcceleratorFactories* gpu_factories);
void EnableSpeechRecognition();
MediaLog* media_log_; MediaLog* media_log_;
// Factory to create extra audio and video decoders. // Factory to create extra audio and video decoders.
...@@ -83,6 +85,7 @@ class MEDIA_EXPORT DefaultRendererFactory : public RendererFactory { ...@@ -83,6 +85,7 @@ class MEDIA_EXPORT DefaultRendererFactory : public RendererFactory {
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
std::unique_ptr<SpeechRecognitionClient> speech_recognition_client_; std::unique_ptr<SpeechRecognitionClient> speech_recognition_client_;
bool is_speech_recognition_available_ = false;
#endif #endif
DISALLOW_COPY_AND_ASSIGN(DefaultRendererFactory); DISALLOW_COPY_AND_ASSIGN(DefaultRendererFactory);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment