| Index: content/browser/speech/speech_recognition_audio_receiver.h
|
| diff --git a/content/browser/speech/speech_recognition_audio_receiver.h b/content/browser/speech/speech_recognition_audio_receiver.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..70f693fa8b25741f820ef95a5a2bac04e01b9688
|
| --- /dev/null
|
| +++ b/content/browser/speech/speech_recognition_audio_receiver.h
|
| @@ -0,0 +1,109 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_
|
| +#define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_
|
| +
|
| +#include "base/basictypes.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "base/memory/shared_memory.h"
|
| +#include "base/process/process_handle.h"
|
| +#include "base/sync_socket.h"
|
| +#include "base/threading/thread.h"
|
| +#include "content/common/content_export.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +
|
| +namespace base {
|
| +class Thread;
|
| +class ThreadChecker;
|
| +class TimeDelta;
|
| +class CancelableSyncSocket;
|
| +} // namespace base
|
| +
|
| +namespace media {
|
| +class AudioBus;
|
| +class AudioInputController;
|
| +class AudioParameters;
|
| +} // namespace media
|
| +
|
| +namespace content {
|
| +
|
| +// The SpeechRecognitionAudioReceiver prepares the SharedMemory and SyncSocket
|
| +// and starts receiving data on an |audio_thread_|.
|
| +// Once the |audio_socket_| receives a buffer index, it posts a task on the
|
| +// browser thread to run the |on_data_cb_|.
|
| +// The audio bus can be accessed on the browser thread via |audio_bus()|.
|
| +// Client should check if the receive started with |started()|.
|
| +class CONTENT_EXPORT SpeechRecognitionAudioReceiver {
|
| + public:
|
| + // OnDataCB matches AudioInputController::EventHandler::OnData.
|
| + // Although the AudioInputController is not used (passed as NULL),
|
| + // this is a handy signature to easily attach to an existing
|
| + // AudioInputController::EventHandler implementation.
|
| + typedef base::Callback<void(media::AudioInputController*,
|
| + const media::AudioBus*)> OnDataCB;
|
| +
|
| + SpeechRecognitionAudioReceiver(
|
| + const media::AudioParameters& params,
|
| + const base::ProcessHandle peer_process_handle,
|
| + const base::TimeDelta receive_timeout,
|
| + const OnDataCB& on_data_callback,
|
| + base::SharedMemoryHandle* foreign_memory_handle,
|
| + base::SyncSocket::TransitDescriptor* foreign_socket_descriptor);
|
| +
|
| + virtual ~SpeechRecognitionAudioReceiver();
|
| +
|
| + // Returns true if successfully initialized and posted the receiving task.
|
| + virtual bool listening();
|
| +
|
| + private:
|
| + // Runs on separate |audio_thread_| when receiving audio from renderer.
|
| + virtual void OnData();
|
| +
|
| + // Used to check if everything was initialized and audio thread started.
|
| + bool listening_;
|
| +
|
| + // The timeout for the |audio_socket_| receive.
|
| + const base::TimeDelta receive_timeout_;
|
| +
|
| + // Callback to run once audio data is ready. Runs on the |audio_thread_|.
|
| + OnDataCB on_data_cb_;
|
| +
|
| + // Used for handling consecutive timeouts or detect closed |audio_socket_|.
|
| + // Accessed on the |audio_thread|.
|
| + uint32 dropped_packets_;
|
| +
|
| + // Socket for receiving a buffer index.
|
| + // Prepared on the BrowserThread and accessed on the |audio_thread_|.
|
| + // No lock is required.
|
| + scoped_ptr<base::CancelableSyncSocket> audio_socket_;
|
| +
|
| + // Socket for the peer process (renderer).
|
| + scoped_ptr<base::CancelableSyncSocket> foreign_socket_;
|
| +
|
| + // Shared memory for passing audio data from renderer to the browser.
|
| + scoped_ptr<base::SharedMemory> shared_memory_;
|
| +
|
| + // Audio bus wrapping the shared memory from the renderer.
|
| + // Accessed on BrowserThread.
|
| + scoped_ptr<media::AudioBus> audio_bus_;
|
| +
|
| + // Used for synchronization when consuming audio on the |audio_bus_|.
|
| + // Initialized on the BrowserThread and accessed on the |audio_thread_|.
|
| + // Since no further reads/writes are done on the browser thread after
|
| + // initialization, no lock is required.
|
| + uint32* buffer_index_;
|
| +
|
| + // Thread for receiving audio buffer indices from the renderer.
|
| + scoped_ptr<base::Thread> audio_thread_;
|
| +
|
| + // Used to check if |OnData()| is executed on |audio_thread_|.
|
| + base::ThreadChecker audio_thread_checker_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionAudioReceiver);
|
| +};
|
| +
|
| +} // namespace content
|
| +
|
| +#endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_
|
|
|