| 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..020b83327258bc2d9daf4db970e362c79c9eb783
|
| --- /dev/null
|
| +++ b/content/browser/speech/speech_recognition_audio_receiver.h
|
| @@ -0,0 +1,131 @@
|
| +// 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/memory/weak_ptr.h"
|
| +#include "base/process/process_handle.h"
|
| +#include "base/sync_socket.h"
|
| +#include "base/synchronization/lock.h"
|
| +#include "base/synchronization/waitable_event.h"
|
| +#include "base/threading/thread_checker.h"
|
| +#include "content/common/content_export.h"
|
| +#include "media/audio/audio_input_controller.h"
|
| +
|
| +namespace base {
|
| +class CancelableSyncSocket;
|
| +class TimeDelta;
|
| +} // namespace base
|
| +
|
| +namespace media {
|
| +class AudioBus;
|
| +class AudioParameters;
|
| +} // namespace media
|
| +
|
| +namespace content {
|
| +// SpeechRecognitionAudioReceiver prepares the SharedMemory and SyncSocket.
|
| +// Call to |StartListening()| starts receiving data on |task_runner_|'s thread.
|
| +// Each time new data is ready to be consumed, |event_handler_->OnData()|
|
| +// is executed on the |task_runner_|'s thread.
|
| +class CONTENT_EXPORT SpeechRecognitionAudioReceiver {
|
| + public:
|
| + virtual ~SpeechRecognitionAudioReceiver();
|
| +
|
| + // Factory method for creating and initializing the receiver.
|
| + // The |event_handler| and |task_runner| must outlive the receiver object.
|
| + // Only |foreign_memory_handle| and |foreign_socket_descriptor| are
|
| + // output parameters and are unsafe to use if return value is NULL.
|
| + // Returns the new successfully initialized object or NULL on failure.
|
| + static scoped_ptr<SpeechRecognitionAudioReceiver> Create(
|
| + const media::AudioParameters& audio_params,
|
| + const base::ProcessHandle peer_process_handle,
|
| + media::AudioInputController::EventHandler* event_handler,
|
| + base::SingleThreadTaskRunner* task_runner,
|
| + base::SharedMemoryHandle* foreign_memory_handle,
|
| + base::SyncSocket::TransitDescriptor* foreign_socket_descriptor);
|
| +
|
| + // Starts listening by posting the first |Receive()| task to |task_runner_|;
|
| + void StartListening();
|
| +
|
| + // Stops posting new tasks to |task_runner_|.
|
| + void Stop();
|
| +
|
| + private:
|
| + SpeechRecognitionAudioReceiver(
|
| + const media::AudioParameters& audio_params,
|
| + media::AudioInputController::EventHandler* event_handler,
|
| + base::SingleThreadTaskRunner* task_runner);
|
| +
|
| + // Prepares the shared memory and the sockets. Returns true on success
|
| + // and false on any failure.
|
| + bool Initialize(
|
| + const base::ProcessHandle peer_process_handle,
|
| + base::SharedMemoryHandle* foreign_memory_handle,
|
| + base::SyncSocket::TransitDescriptor* foreign_socket_descriptor);
|
| +
|
| + // Runs on the |task_runner_| when receiving audio from renderer.
|
| + void Receive();
|
| +
|
| + // The audio buffer in shared memory.
|
| + media::AudioInputBuffer* GetAudioBuffer();
|
| +
|
| + // Accessed under |listening_lock_| to change the listening state.
|
| + void SetListening(const bool listening);
|
| +
|
| + // Accessed under |listening_lock_| to check the listening state.
|
| + bool IsListening();
|
| +
|
| + // Audio parameters for the incoming audio stream. Used to calculate the
|
| + // appropriate receive timeout and reposting the |Receive()| task.
|
| + // Accessed on the BrowserThread::IO and |task_runner_|'s thread.
|
| + const media::AudioParameters audio_params_;
|
| +
|
| + // The duration of the audio chunk we get on each Receive().
|
| + const base::TimeDelta audio_chunk_duration_;
|
| +
|
| + // The task runner used for receiving on the |receiving_socket_| and
|
| + // triggering OnData() and OnError() callbacks for the |event_handler_|.
|
| + base::SingleThreadTaskRunner* task_runner_;
|
| +
|
| + // Client to be notified with OnData() and OnError() events.
|
| + media::AudioInputController::EventHandler* event_handler_;
|
| +
|
| + // Socket for receiving the next buffer index of audio data.
|
| + // Prepared on the BrowserThread and accessed on the |task_runner_|'s thread.
|
| + scoped_ptr<base::CancelableSyncSocket> receiving_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 to check if |Receive()| is executed on the |task_runner_|'s thread.
|
| + base::ThreadChecker thread_checker_;
|
| +
|
| + // Synchronization lock for |listening_|;
|
| + base::Lock listening_lock_;
|
| +
|
| + // Waitable event for a synchronous start of Receive().
|
| + scoped_ptr<base::WaitableEvent> start_event_;
|
| +
|
| + // Used for synchronizing the BrowserThread::IO and |task_runner_|'s thread.
|
| + // We stop posting new tasks to the |task_runner_| when no longer listening.
|
| + // Accessed under |listening_lock_|.
|
| + bool listening_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionAudioReceiver);
|
| +};
|
| +
|
| +} // namespace content
|
| +
|
| +#endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_
|
|
|