| 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/process/process_handle.h" |
| 12 #include "base/sync_socket.h" |
| 13 #include "base/threading/thread.h" |
| 14 #include "content/common/content_export.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 |
| 17 namespace base { |
| 18 class Thread; |
| 19 class ThreadChecker; |
| 20 class TimeDelta; |
| 21 class CancelableSyncSocket; |
| 22 } // namespace base |
| 23 |
| 24 namespace media { |
| 25 class AudioBus; |
| 26 class AudioInputController; |
| 27 class AudioParameters; |
| 28 } // namespace media |
| 29 |
| 30 namespace content { |
| 31 |
| 32 // The SpeechRecognitionAudioReceiver prepares the SharedMemory and SyncSocket |
| 33 // and starts receiving data on an |audio_thread_|. |
| 34 // Once the |audio_socket_| receives a buffer index, it posts a task on the |
| 35 // browser thread to run the |on_data_cb_|. |
| 36 // The audio bus can be accessed on the browser thread via |audio_bus()|. |
| 37 // Client should check if the receive started with |started()|. |
| 38 class CONTENT_EXPORT SpeechRecognitionAudioReceiver { |
| 39 public: |
| 40 // OnDataCB matches AudioInputController::EventHandler::OnData. |
| 41 // Although the AudioInputController is not used (passed as NULL), |
| 42 // this is a handy signature to easily attach to an existing |
| 43 // AudioInputController::EventHandler implementation. |
| 44 typedef base::Callback<void(media::AudioInputController*, |
| 45 const media::AudioBus*)> OnDataCB; |
| 46 |
| 47 SpeechRecognitionAudioReceiver( |
| 48 const media::AudioParameters& params, |
| 49 const base::ProcessHandle peer_process_handle, |
| 50 const base::TimeDelta receive_timeout, |
| 51 const OnDataCB& on_data_callback, |
| 52 base::SharedMemoryHandle* foreign_memory_handle, |
| 53 base::SyncSocket::TransitDescriptor* foreign_socket_descriptor); |
| 54 |
| 55 virtual ~SpeechRecognitionAudioReceiver(); |
| 56 |
| 57 // Returns true if successfully initialized and posted the receiving task. |
| 58 virtual bool listening(); |
| 59 |
| 60 private: |
| 61 // Runs on separate |audio_thread_| when receiving audio from renderer. |
| 62 virtual void OnData(); |
| 63 |
| 64 // Used to check if everything was initialized and audio thread started. |
| 65 bool listening_; |
| 66 |
| 67 // The timeout for the |audio_socket_| receive. |
| 68 const base::TimeDelta receive_timeout_; |
| 69 |
| 70 // Callback to run once audio data is ready. Runs on the |audio_thread_|. |
| 71 OnDataCB on_data_cb_; |
| 72 |
| 73 // Used for handling consecutive timeouts or detect closed |audio_socket_|. |
| 74 // Accessed on the |audio_thread|. |
| 75 uint32 dropped_packets_; |
| 76 |
| 77 // Socket for receiving a buffer index. |
| 78 // Prepared on the BrowserThread and accessed on the |audio_thread_|. |
| 79 // No lock is required. |
| 80 scoped_ptr<base::CancelableSyncSocket> audio_socket_; |
| 81 |
| 82 // Socket for the peer process (renderer). |
| 83 scoped_ptr<base::CancelableSyncSocket> foreign_socket_; |
| 84 |
| 85 // Shared memory for passing audio data from renderer to the browser. |
| 86 scoped_ptr<base::SharedMemory> shared_memory_; |
| 87 |
| 88 // Audio bus wrapping the shared memory from the renderer. |
| 89 // Accessed on BrowserThread. |
| 90 scoped_ptr<media::AudioBus> audio_bus_; |
| 91 |
| 92 // Used for synchronization when consuming audio on the |audio_bus_|. |
| 93 // Initialized on the BrowserThread and accessed on the |audio_thread_|. |
| 94 // Since no further reads/writes are done on the browser thread after |
| 95 // initialization, no lock is required. |
| 96 uint32* buffer_index_; |
| 97 |
| 98 // Thread for receiving audio buffer indices from the renderer. |
| 99 scoped_ptr<base::Thread> audio_thread_; |
| 100 |
| 101 // Used to check if |OnData()| is executed on |audio_thread_|. |
| 102 base::ThreadChecker audio_thread_checker_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(SpeechRecognitionAudioReceiver); |
| 105 }; |
| 106 |
| 107 } // namespace content |
| 108 |
| 109 #endif // CONTENT_BROWSER_SPEECH_SPEECH_RECOGNITION_AUDIO_RECEIVER_H_ |
| OLD | NEW |