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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SPEECH_CROS_SPEECH_RECOGNITION_SERVICE_H_
#define CHROME_BROWSER_SPEECH_CROS_SPEECH_RECOGNITION_SERVICE_H_
#include <memory>
#include <string>
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "chrome/browser/speech/chrome_speech_recognition_service.h"
#include "media/mojo/mojom/speech_recognition.mojom.h"
#include "media/mojo/mojom/speech_recognition_service.mojom.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
namespace content {
class BrowserContext;
} // namespace content
namespace network {
class PendingSharedURLLoaderFactory;
} // namespace network
namespace speech {
class CrosSpeechRecognitionRecognizerImpl;
// Provides a Mojo endpoint in the browser for the CROS system. This uses ML
// Service, so is actually executing a little more in the
// browser then regular chrome.
class CrosSpeechRecognitionService
: public ChromeSpeechRecognitionService,
public media::mojom::AudioSourceSpeechRecognitionContext,
public media::mojom::SpeechRecognitionContext {
public:
using CreateCrosSpeechRecognitionRecognizerCb = base::RepeatingCallback<
std::unique_ptr<CrosSpeechRecognitionRecognizerImpl>(
mojo::PendingRemote<media::mojom::SpeechRecognitionRecognizerClient>
client,
media::mojom::SpeechRecognitionOptionsPtr options,
const base::FilePath& binary_path,
const base::flat_map<std::string, base::FilePath>& config_paths,
const std::string& primary_language_name,
const bool mask_offensive_words)>;
explicit CrosSpeechRecognitionService(content::BrowserContext* context);
CrosSpeechRecognitionService(const CrosSpeechRecognitionService&) = delete;
CrosSpeechRecognitionService& operator=(const SpeechRecognitionService&) =
delete;
~CrosSpeechRecognitionService() override;
// SpeechRecognitionService:
void BindSpeechRecognitionContext(
mojo::PendingReceiver<media::mojom::SpeechRecognitionContext> receiver)
override;
void BindAudioSourceSpeechRecognitionContext(
mojo::PendingReceiver<media::mojom::AudioSourceSpeechRecognitionContext>
receiver) override;
// media::mojom::SpeechRecognitionContext
void BindRecognizer(
mojo::PendingReceiver<media::mojom::SpeechRecognitionRecognizer> receiver,
mojo::PendingRemote<media::mojom::SpeechRecognitionRecognizerClient>
client,
media::mojom::SpeechRecognitionOptionsPtr options,
BindRecognizerCallback callback) override;
void BindWebSpeechRecognizer(
mojo::PendingReceiver<media::mojom::SpeechRecognitionSession>
session_receiver,
mojo::PendingRemote<media::mojom::SpeechRecognitionSessionClient>
session_client,
mojo::PendingReceiver<media::mojom::SpeechRecognitionAudioForwarder>
audio_forwarder,
int channel_count,
int sample_rate,
media::mojom::SpeechRecognitionOptionsPtr options,
bool continuous) override;
// media::mojom::AudioSourceSpeechRecognitionContext:
void BindAudioSourceFetcher(
mojo::PendingReceiver<media::mojom::AudioSourceFetcher> fetcher_receiver,
mojo::PendingRemote<media::mojom::SpeechRecognitionRecognizerClient>
client,
media::mojom::SpeechRecognitionOptionsPtr options,
BindRecognizerCallback callback) override;
void SetCreateCrosSpeechRecognitionRecognizerCbForTesting(
CreateCrosSpeechRecognitionRecognizerCb callback);
private:
void CreateAudioSourceFetcherForOnDeviceRecognitionOnIOThread(
mojo::PendingReceiver<media::mojom::AudioSourceFetcher> fetcher_receiver,
mojo::PendingRemote<media::mojom::SpeechRecognitionRecognizerClient>
client,
media::mojom::SpeechRecognitionOptionsPtr options,
const base::FilePath& binary_path,
const base::flat_map<std::string, base::FilePath>& config_paths,
const std::string& primary_language_name,
const bool mask_offensive_words);
void CreateAudioSourceFetcherForServerBasedRecognitionOnIOThread(
mojo::PendingReceiver<media::mojom::AudioSourceFetcher> fetcher_receiver,
mojo::PendingRemote<media::mojom::SpeechRecognitionRecognizerClient>
client,
media::mojom::SpeechRecognitionOptionsPtr options,
std::unique_ptr<network::PendingSharedURLLoaderFactory>
pending_loader_factory);
mojo::ReceiverSet<media::mojom::AudioSourceSpeechRecognitionContext>
audio_source_speech_recognition_contexts_;
mojo::ReceiverSet<media::mojom::SpeechRecognitionContext>
speech_recognition_contexts_;
CreateCrosSpeechRecognitionRecognizerCb
cros_speech_recognition_recognizer_cb_;
base::WeakPtrFactory<CrosSpeechRecognitionService> weak_factory_{this};
};
} // namespace speech
#endif // CHROME_BROWSER_SPEECH_CROS_SPEECH_RECOGNITION_SERVICE_H_
|