Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(408)

Side by Side Diff: content/browser/speech/speech_recognition_audio_receiver.h

Issue 549373003: Binding media stream audio track to speech recognition [browser] Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Audio Receiver unit test Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.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 |Listen()| starts receiving data on an |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| must outlive the receiver object.
41 // If |task_runner| is NULL, a new thread will be created and managed.
42 // Only |foreign_memory_handle| and |foreign_socket_descriptor| are
43 // output parameters and are unsafe to use if return value is NULL.
44 // Returns the new successfully initialized object or NULL on failure.
45 static SpeechRecognitionAudioReceiver* Create(
46 const media::AudioParameters& audio_params,
47 const base::ProcessHandle peer_process_handle,
48 media::AudioInputController::EventHandler* event_handler,
49 base::SingleThreadTaskRunner* task_runner,
50 base::SharedMemoryHandle* foreign_memory_handle,
51 base::SyncSocket::TransitDescriptor* foreign_socket_descriptor);
52
53 // Starts listening by posting the first |Receive()| task to |task_runner_|;
54 void Listen();
55
56 private:
57 SpeechRecognitionAudioReceiver(
58 const media::AudioParameters& audio_params,
59 media::AudioInputController::EventHandler* event_handler,
60 base::SingleThreadTaskRunner* task_runner);
61
62 // Prepares the shared memory and the sockets. Returns true on success
63 // and false on any failure.
64 bool Initialize(
65 const base::ProcessHandle peer_process_handle,
66 base::SharedMemoryHandle* foreign_memory_handle,
67 base::SyncSocket::TransitDescriptor* foreign_socket_descriptor);
68
69 // Runs on the |task_runner_| when receiving audio from renderer.
70 void Receive();
71
72 // The audio buffer in shared memory.
73 media::AudioInputBuffer* GetAudioBuffer();
74
75 // Accessed under |listening_lock_| to change the listening state.
76 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
77
78 // Accessed under |listening_lock_| to check the listening state.
79 bool listening();
no longer working on chromium 2014/10/15 12:14:29 the same here
burnik 2014/10/15 14:52:27 Done.
80
81 // Audio parameters for the incoming audio stream. Used to calculate the
82 // appropriate receive timeout and reposting the |Receive()| task.
83 // Accessed on the BrowserThread::IO and |task_runner_|'s thread.
84 const media::AudioParameters audio_params_;
85
86 // The task runner used for receiving on the |receiving_socket_| and
87 // triggering OnData() and OnError() callbacks for the |event_handler_|.
88 base::SingleThreadTaskRunner* task_runner_;
89
90 // Client to be notified with OnData() and OnError() events.
91 media::AudioInputController::EventHandler* event_handler_;
92
93 // Used for handling consecutive timeouts or closed |receiving_socket_|.
94 // Accessed on the |task_runner_|'s thread.
95 uint32 dropped_packets_;
96
97 // Socket for receiving the next buffer index of audio data.
98 // Prepared on the BrowserThread and accessed on the |task_runner_|'s thread.
99 scoped_ptr<base::CancelableSyncSocket> receiving_socket_;
100
101 // Socket for the peer process (renderer).
102 scoped_ptr<base::CancelableSyncSocket> foreign_socket_;
103
104 // Shared memory for passing audio data from renderer to the browser.
105 scoped_ptr<base::SharedMemory> shared_memory_;
106
107 // Audio bus wrapping the shared memory from the renderer.
108 // Accessed on BrowserThread.
109 scoped_ptr<media::AudioBus> audio_bus_;
110
111 // If the client does not provide a task_runner we create this
112 // thread for receiving on the |receiving_socket_|.
113 scoped_ptr<base::Thread> audio_thread_;
114
115 // Used to check if |Receive()| is executed on the |task_runner_|'s thread.
116 base::ThreadChecker audio_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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698