Chromium Code Reviews| 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..61ce5079e93a5fd85719cd3e46989d6919847b47 |
| --- /dev/null |
| +++ b/content/browser/speech/speech_recognition_audio_receiver.h |
| @@ -0,0 +1,134 @@ |
| +// 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/threading/thread.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 |Listen()| starts receiving data on an |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| must outlive the receiver object. |
| + // If |task_runner| is NULL, a new thread will be created and managed. |
| + // 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 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 Listen(); |
| + |
| + 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 set_listening(const bool listening); |
|
no longer working on chromium
2014/10/15 12:14:28
s/set_listening/SetListening/g,
hacker_style is pr
burnik
2014/10/15 14:52:27
Done. However, this confuses me a bit according to
|
| + |
| + // Accessed under |listening_lock_| to check the listening state. |
| + bool listening(); |
|
no longer working on chromium
2014/10/15 12:14:29
the same here
burnik
2014/10/15 14:52:27
Done.
|
| + |
| + // 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 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_; |
| + |
| + // Used for handling consecutive timeouts or closed |receiving_socket_|. |
| + // Accessed on the |task_runner_|'s thread. |
| + uint32 dropped_packets_; |
| + |
| + // 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_; |
| + |
| + // If the client does not provide a task_runner we create this |
| + // thread for receiving on the |receiving_socket_|. |
| + scoped_ptr<base::Thread> audio_thread_; |
| + |
| + // Used to check if |Receive()| is executed on the |task_runner_|'s thread. |
| + base::ThreadChecker audio_thread_checker_; |
| + |
| + // Utilized when posting tasks to the |task_runner_|. |
| + base::WeakPtrFactory<SpeechRecognitionAudioReceiver> weak_ptr_factory_; |
| + |
| + // Synchronization lock for |listening_|; |
| + base::Lock listening_lock_; |
| + |
| + // 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_ |