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

Unified 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: Move thread to recognizer_impl. Use PostDelayedTask. 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 side-by-side diff with in-line comments
Download patch
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..f3afd6f8eb2b82e0685e08ab4a9b837fe3b695be
--- /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_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| and |task_runner| must outlive the receiver object.
+ // 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 scoped_ptr<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();
+
+ // Stops posting new tasks to |task_runner_|.
+ void Stop();
+
+ 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(const base::TimeDelta& receive_timeout);
+
+ // The audio buffer in shared memory.
+ media::AudioInputBuffer* GetAudioBuffer();
+
+ // Accessed under |listening_lock_| to change the listening state.
+ void SetListening(const bool listening);
+
+ // Accessed under |listening_lock_| to check the listening state.
+ bool IsListening();
+
+ // 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 duration of the audio chunk we get on each Receive().
+ const base::TimeDelta audio_chunk_duration_;
+
+ // 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_;
+
+ // Used to check if |Receive()| is executed on the |task_runner_|'s thread.
+ base::ThreadChecker 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_

Powered by Google App Engine
This is Rietveld 408576698