| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_ |
| 6 #define CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/shared_memory.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/process/process_handle.h" |
| 13 #include "base/sync_socket.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/thread_checker.h" |
| 17 #include "content/common/content_export.h" |
| 18 #include "media/audio/audio_input_controller.h" |
| 19 |
| 20 namespace base { |
| 21 class CancelableSyncSocket; |
| 22 class TimeDelta; |
| 23 } // namespace base |
| 24 |
| 25 namespace media { |
| 26 class AudioBus; |
| 27 class AudioParameters; |
| 28 } // namespace media |
| 29 |
| 30 namespace content { |
| 31 // SpeechRecognitionAudioReceiver prepares the SharedMemory and SyncSocket. |
| 32 // Call to |StartListening()| starts receiving data on |task_runner_|'s thread. |
| 33 // Each time new data is ready to be consumed, |event_handler_->OnData()| |
| 34 // is executed on the |task_runner_|'s thread. |
| 35 class CONTENT_EXPORT SpeechRecognitionAudioReceiver { |
| 36 public: |
| 37 virtual ~SpeechRecognitionAudioReceiver(); |
| 38 |
| 39 // Factory method for creating and initializing the receiver. |
| 40 // The |event_handler| and |task_runner| must outlive the receiver object. |
| 41 // Only |foreign_memory_handle| and |foreign_socket_descriptor| are |
| 42 // output parameters and are unsafe to use if return value is NULL. |
| 43 // Returns the new successfully initialized object or NULL on failure. |
| 44 static scoped_ptr<SpeechRecognitionAudioReceiver> Create( |
| 45 const media::AudioParameters& audio_params, |
| 46 const base::ProcessHandle peer_process_handle, |
| 47 media::AudioInputController::EventHandler* event_handler, |
| 48 base::SingleThreadTaskRunner* task_runner, |
| 49 base::SharedMemoryHandle* foreign_memory_handle, |
| 50 base::SyncSocket::TransitDescriptor* foreign_socket_descriptor); |
| 51 |
| 52 // Starts listening by posting the first |Receive()| task to |task_runner_|; |
| 53 void StartListening(); |
| 54 |
| 55 // Stops posting new tasks to |task_runner_|. |
| 56 void Stop(); |
| 57 |
| 58 private: |
| 59 SpeechRecognitionAudioReceiver( |
| 60 const media::AudioParameters& audio_params, |
| 61 media::AudioInputController::EventHandler* event_handler, |
| 62 base::SingleThreadTaskRunner* task_runner); |
| 63 |
| 64 // Prepares the shared memory and the sockets. Returns true on success |
| 65 // and false on any failure. |
| 66 bool Initialize( |
| 67 const base::ProcessHandle peer_process_handle, |
| 68 base::SharedMemoryHandle* foreign_memory_handle, |
| 69 base::SyncSocket::TransitDescriptor* foreign_socket_descriptor); |
| 70 |
| 71 // Runs on the |task_runner_| when receiving audio from renderer. |
| 72 void Receive(); |
| 73 |
| 74 // The audio buffer in shared memory. |
| 75 media::AudioInputBuffer* GetAudioBuffer(); |
| 76 |
| 77 // Accessed under |listening_lock_| to change the listening state. |
| 78 void SetListening(const bool listening); |
| 79 |
| 80 // Accessed under |listening_lock_| to check the listening state. |
| 81 bool IsListening(); |
| 82 |
| 83 // Audio parameters for the incoming audio stream. Used to calculate the |
| 84 // appropriate receive timeout and reposting the |Receive()| task. |
| 85 // Accessed on the BrowserThread::IO and |task_runner_|'s thread. |
| 86 const media::AudioParameters audio_params_; |
| 87 |
| 88 // The duration of the audio chunk we get on each Receive(). |
| 89 const base::TimeDelta audio_chunk_duration_; |
| 90 |
| 91 // The task runner used for receiving on the |receiving_socket_| and |
| 92 // triggering OnData() and OnError() callbacks for the |event_handler_|. |
| 93 base::SingleThreadTaskRunner* task_runner_; |
| 94 |
| 95 // Client to be notified with OnData() and OnError() events. |
| 96 media::AudioInputController::EventHandler* event_handler_; |
| 97 |
| 98 // Socket for receiving the next buffer index of audio data. |
| 99 // Prepared on the BrowserThread and accessed on the |task_runner_|'s thread. |
| 100 scoped_ptr<base::CancelableSyncSocket> receiving_socket_; |
| 101 |
| 102 // Socket for the peer process (renderer). |
| 103 scoped_ptr<base::CancelableSyncSocket> foreign_socket_; |
| 104 |
| 105 // Shared memory for passing audio data from renderer to the browser. |
| 106 scoped_ptr<base::SharedMemory> shared_memory_; |
| 107 |
| 108 // Audio bus wrapping the shared memory from the renderer. |
| 109 // Accessed on BrowserThread. |
| 110 scoped_ptr<media::AudioBus> audio_bus_; |
| 111 |
| 112 // Used to check if |Receive()| is executed on the |task_runner_|'s thread. |
| 113 base::ThreadChecker thread_checker_; |
| 114 |
| 115 // Synchronization lock for |listening_|; |
| 116 base::Lock listening_lock_; |
| 117 |
| 118 // Waitable event for a synchronous start of Receive(). |
| 119 scoped_ptr<base::WaitableEvent> start_event_; |
| 120 |
| 121 // Used for synchronizing the BrowserThread::IO and |task_runner_|'s thread. |
| 122 // We stop posting new tasks to the |task_runner_| when no longer listening. |
| 123 // Accessed under |listening_lock_|. |
| 124 bool listening_; |
| 125 |
| 126 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionAudioReceiver); |
| 127 }; |
| 128 |
| 129 } // namespace content |
| 130 |
| 131 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_ |
| OLD | NEW |