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