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

Unified Diff: media/audio/audio_input_controller.h

Issue 2624403002: Refactor AudioInputController and related interfaces. (Closed)
Patch Set: Address comments Created 3 years, 11 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
« no previous file with comments | « content/browser/speech/speech_recognizer_impl_unittest.cc ('k') | media/audio/audio_input_controller.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/audio/audio_input_controller.h
diff --git a/media/audio/audio_input_controller.h b/media/audio/audio_input_controller.h
index ef691f1cf13679192e4deaca17c644ba5f7843ee..cfd65926cd61c85e95599696bf0fd3708d05b8ae 100644
--- a/media/audio/audio_input_controller.h
+++ b/media/audio/audio_input_controller.h
@@ -11,15 +11,8 @@
#include <memory>
#include <string>
-#include "base/atomicops.h"
-#include "base/callback.h"
#include "base/files/file.h"
-#include "base/macros.h"
-#include "base/memory/ref_counted.h"
-#include "base/synchronization/lock.h"
-#include "base/synchronization/waitable_event.h"
-#include "base/threading/thread.h"
-#include "build/build_config.h"
+#include "base/memory/weak_ptr.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager_base.h"
#include "media/base/audio_bus.h"
@@ -55,14 +48,11 @@
// AudioInputStream::Open()
// .- - - - - - - - - - - - -> OnError()
// .-------------------------> OnCreated()
-// kCreated
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Record() ==> DoRecord()
// AudioInputStream::Start()
-// kRecording
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Close() ==> DoClose()
-// state_ = kClosed
// AudioInputStream::Stop()
// AudioInputStream::Close()
// SyncWriter::Close()
@@ -85,8 +75,7 @@ class AudioFileWriter;
class UserInputMonitor;
class MEDIA_EXPORT AudioInputController
- : public base::RefCountedThreadSafe<AudioInputController>,
- public AudioInputStream::AudioInputCallback {
+ : public base::RefCountedThreadSafe<AudioInputController> {
public:
// Error codes to make native logging more clear. These error codes are added
// to generic error strings to provide a higher degree of details.
@@ -114,8 +103,6 @@ class MEDIA_EXPORT AudioInputController
virtual void OnCreated(AudioInputController* controller) = 0;
virtual void OnError(AudioInputController* controller,
ErrorCode error_code) = 0;
- virtual void OnData(AudioInputController* controller,
- const AudioBus* data) = 0;
virtual void OnLog(AudioInputController* controller,
const std::string& message) = 0;
@@ -164,6 +151,7 @@ class MEDIA_EXPORT AudioInputController
static scoped_refptr<AudioInputController> Create(
AudioManager* audio_manager,
EventHandler* event_handler,
+ SyncWriter* sync_writer,
const AudioParameters& params,
const std::string& device_id,
UserInputMonitor* user_input_monitor);
@@ -221,16 +209,6 @@ class MEDIA_EXPORT AudioInputController
// to muted and 1.0 to maximum volume.
virtual void SetVolume(double volume);
- // AudioInputCallback implementation. Threading details depends on the
- // device-specific implementation.
- void OnData(AudioInputStream* stream,
- const AudioBus* source,
- uint32_t hardware_delay_bytes,
- double volume) override;
- void OnError(AudioInputStream* stream) override;
-
- bool SharedMemoryAndSyncSocketMode() const { return sync_writer_ != NULL; }
-
// Enable debug recording of audio input.
virtual void EnableDebugRecording(const base::FilePath& file_name);
@@ -260,13 +238,6 @@ class MEDIA_EXPORT AudioInputController
CAPTURE_STARTUP_RESULT_MAX = CAPTURE_STARTUP_NEVER_GOT_DATA
};
- // Internal state of the source.
- enum State {
- CREATED,
- RECORDING,
- CLOSED
- };
-
#if defined(AUDIO_POWER_MONITORING)
// Used to log a silence report (see OnData).
// Elements in this enum should not be deleted or rearranged; the only
@@ -287,13 +258,22 @@ class MEDIA_EXPORT AudioInputController
};
#endif
- AudioInputController(EventHandler* handler,
+ AudioInputController(scoped_refptr<base::SingleThreadTaskRunner> task_runner,
+ EventHandler* handler,
SyncWriter* sync_writer,
std::unique_ptr<AudioFileWriter> debug_writer,
UserInputMonitor* user_input_monitor,
const bool agc_is_enabled);
- ~AudioInputController() override;
+ virtual ~AudioInputController();
+
+ const scoped_refptr<base::SingleThreadTaskRunner>& GetTaskRunnerForTesting()
+ const {
+ return task_runner_;
+ }
+ EventHandler* GetHandlerForTesting() const { return handler_; }
+
+ private:
// Methods called on the audio thread (owned by the AudioManager).
void DoCreate(AudioManager* audio_manager,
const AudioParameters& params,
@@ -306,12 +286,8 @@ class MEDIA_EXPORT AudioInputController
void DoClose();
void DoReportError();
void DoSetVolume(double volume);
- void DoOnData(std::unique_ptr<AudioBus> data);
void DoLogAudioLevels(float level_dbfs, int microphone_volume_percent);
- // Helper method that stops, closes, and NULL:s |*stream_|.
- void DoStopCloseAndClearStream();
-
#if defined(AUDIO_POWER_MONITORING)
// Updates the silence state, see enum SilenceState above for state
// transitions.
@@ -336,44 +312,49 @@ class MEDIA_EXPORT AudioInputController
// Called by the stream with log messages.
void LogMessage(const std::string& message);
+ // Called on the hw callback thread. Checks for keyboard input if
+ // user_input_monitor_ is set otherwise returns false.
+ bool CheckForKeyboardInput();
+
+ // Does power monitoring on supported platforms.
+ // Called on the hw callback thread.
+ // Returns true iff average power and mic volume was returned and should
+ // be posted to DoLogAudioLevels on the audio thread.
+ // Returns false if either power measurements are disabled or aren't needed
+ // right now (they're done periodically).
+ bool CheckAudioPower(const AudioBus* source,
+ double volume,
+ float* average_power_dbfs,
+ int* mic_volume_percent);
+
// Gives access to the task runner of the creating thread.
- scoped_refptr<base::SingleThreadTaskRunner> creator_task_runner_;
+ scoped_refptr<base::SingleThreadTaskRunner> const creator_task_runner_;
// The task runner of audio-manager thread that this object runs on.
- scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
+ scoped_refptr<base::SingleThreadTaskRunner> const task_runner_;
// Contains the AudioInputController::EventHandler which receives state
// notifications from this class.
- EventHandler* handler_;
+ EventHandler* const handler_;
// Pointer to the audio input stream object.
+ // Only used on the audio thread.
AudioInputStream* stream_;
- // Flag for whether CaptureStartupResults shall be reported.
- // A value of 1 means that stats shall be reported,
- // any other value means that stats have already been reported.
- base::AtomicRefCount should_report_stats;
-
- // |state_| is written on the audio thread and is read on the hardware audio
- // thread. These operations need to be locked. But lock is not required for
- // reading on the audio input controller thread.
- State state_;
-
- base::Lock lock_;
-
// SyncWriter is used only in low-latency mode for synchronous writing.
- SyncWriter* sync_writer_;
+ SyncWriter* const sync_writer_;
static Factory* factory_;
double max_volume_;
- UserInputMonitor* user_input_monitor_;
+ UserInputMonitor* const user_input_monitor_;
const bool agc_is_enabled_;
#if defined(AUDIO_POWER_MONITORING)
- // Enabled in DoCrete() but not in DoCreateForStream().
+ // Will be set to true if an AGC is supported and enabled (see DoCreate and
+ // DoCreateForStream). By default set to false.
bool power_measurement_is_enabled_;
// Updated each time a power measurement is performed.
@@ -394,7 +375,29 @@ class MEDIA_EXPORT AudioInputController
// Used for audio debug recordings. Accessed on audio thread.
const std::unique_ptr<AudioFileWriter> debug_writer_;
- private:
+ class AudioCallback;
+ // Holds a pointer to the callback object that receives audio data from
+ // the lower audio layer. Valid only while 'recording' (between calls to
+ // stream_->Start() and stream_->Stop()).
+ // The value of this pointer is only set and read on the audio thread while
+ // the callbacks themselves occur on the hw callback thread. More details
+ // in the AudioCallback class in the cc file.
+ std::unique_ptr<AudioCallback> audio_callback_;
+
+ // A weak pointer factory that we use when posting tasks to the audio thread
+ // that we want to be automatically discarded after Close() has been called
+ // and that we do not want to keep the AudioInputController instance alive
+ // beyond what is desired by the user of the instance (e.g.
+ // AudioInputRendererHost). An example of where this is important is when
+ // we fire error notifications from the hw callback thread, post them to
+ // the audio thread. In that case, we do not want the error notification to
+ // keep the AudioInputController alive for as long as the error notification
+ // is pending and then make a callback from an AudioInputController that has
+ // already been closed.
+ // The weak_ptr_factory_ and all outstanding weak pointers, are invalidated
+ // at the end of DoClose.
+ base::WeakPtrFactory<AudioInputController> weak_ptr_factory_;
+
DISALLOW_COPY_AND_ASSIGN(AudioInputController);
};
« no previous file with comments | « content/browser/speech/speech_recognizer_impl_unittest.cc ('k') | media/audio/audio_input_controller.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698