diff options
author | guidou <guidou@chromium.org> | 2015-07-31 01:09:41 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-31 08:10:31 +0000 |
commit | 5754e6106d4fd8be680f1b7e177dec1e866de4ea (patch) | |
tree | d4f9f74c577e8be6c5ec347e09a9668c34498431 /media/base | |
parent | 88bd0fa432c7d136e4b3178b679ae8fb2e935464 (diff) | |
download | chromium_src-5754e6106d4fd8be680f1b7e177dec1e866de4ea.zip chromium_src-5754e6106d4fd8be680f1b7e177dec1e866de4ea.tar.gz chromium_src-5754e6106d4fd8be680f1b7e177dec1e866de4ea.tar.bz2 |
Introduce OutputDevice interface.
Move the SwitchOutputDevice method to it.
This will simplify the evolution of code related with
low-level audio-output-device operations.
BUG=515083
Review URL: https://codereview.chromium.org/1253183008
Cr-Commit-Position: refs/heads/master@{#341294}
Diffstat (limited to 'media/base')
-rw-r--r-- | media/base/BUILD.gn | 3 | ||||
-rw-r--r-- | media/base/audio_renderer_mixer.cc | 10 | ||||
-rw-r--r-- | media/base/audio_renderer_mixer.h | 8 | ||||
-rw-r--r-- | media/base/audio_renderer_mixer_input.cc | 14 | ||||
-rw-r--r-- | media/base/audio_renderer_mixer_input.h | 4 | ||||
-rw-r--r-- | media/base/audio_renderer_sink.h | 23 | ||||
-rw-r--r-- | media/base/fake_audio_renderer_sink.cc | 12 | ||||
-rw-r--r-- | media/base/fake_audio_renderer_sink.h | 7 | ||||
-rw-r--r-- | media/base/fake_output_device.cc | 21 | ||||
-rw-r--r-- | media/base/fake_output_device.h | 30 | ||||
-rw-r--r-- | media/base/mock_audio_renderer_sink.h | 7 | ||||
-rw-r--r-- | media/base/output_device.h | 56 |
12 files changed, 139 insertions, 56 deletions
diff --git a/media/base/BUILD.gn b/media/base/BUILD.gn index bedc24f..3ff3534 100644 --- a/media/base/BUILD.gn +++ b/media/base/BUILD.gn @@ -129,6 +129,7 @@ source_set("base") { "multi_channel_resampler.h", "null_video_sink.cc", "null_video_sink.h", + "output_device.h", "pipeline.cc", "pipeline.h", "pipeline_status.h", @@ -335,6 +336,8 @@ source_set("test_support") { "fake_audio_renderer_sink.h", "fake_demuxer_stream.cc", "fake_demuxer_stream.h", + "fake_output_device.cc", + "fake_output_device.h", "fake_text_track_stream.cc", "fake_text_track_stream.h", "gmock_callback_support.h", diff --git a/media/base/audio_renderer_mixer.cc b/media/base/audio_renderer_mixer.cc index 5b5fe49..984798d 100644 --- a/media/base/audio_renderer_mixer.cc +++ b/media/base/audio_renderer_mixer.cc @@ -71,14 +71,10 @@ void AudioRendererMixer::RemoveErrorCallback(const base::Closure& error_cb) { NOTREACHED(); } -void AudioRendererMixer::SwitchOutputDevice( - const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) { - DVLOG(1) << __FUNCTION__ << "(" << device_id << ", " << security_origin - << ")"; +OutputDevice* AudioRendererMixer::GetOutputDevice() { + DVLOG(1) << __FUNCTION__; base::AutoLock auto_lock(lock_); - audio_sink_->SwitchOutputDevice(device_id, security_origin, callback); + return audio_sink_->GetOutputDevice(); } int AudioRendererMixer::Render(AudioBus* audio_bus, diff --git a/media/base/audio_renderer_mixer.h b/media/base/audio_renderer_mixer.h index dc9ab92..9bc3839 100644 --- a/media/base/audio_renderer_mixer.h +++ b/media/base/audio_renderer_mixer.h @@ -40,9 +40,11 @@ class MEDIA_EXPORT AudioRendererMixer pause_delay_ = delay; } - void SwitchOutputDevice(const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback); + // TODO(guidou): remove this method. The output device of a mixer should + // never be switched, as it may result in a discrepancy between the output + // parameters of the new device and the output parameters with which the + // mixer was initialized. See crbug.com/506507 + OutputDevice* GetOutputDevice(); private: // AudioRendererSink::RenderCallback implementation. diff --git a/media/base/audio_renderer_mixer_input.cc b/media/base/audio_renderer_mixer_input.cc index 6194f21..4abec01 100644 --- a/media/base/audio_renderer_mixer_input.cc +++ b/media/base/audio_renderer_mixer_input.cc @@ -95,17 +95,9 @@ bool AudioRendererMixerInput::SetVolume(double volume) { return true; } -void AudioRendererMixerInput::SwitchOutputDevice( - const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) { - DVLOG(1) << __FUNCTION__ - << "(" << device_id << ", " << security_origin << ")"; - if (mixer_) { - mixer_->SwitchOutputDevice(device_id, security_origin, callback); - } else { - callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED); - } +OutputDevice* AudioRendererMixerInput::GetOutputDevice() { + DVLOG(1) << __FUNCTION__; + return mixer_->GetOutputDevice(); } double AudioRendererMixerInput::ProvideInput(AudioBus* audio_bus, diff --git a/media/base/audio_renderer_mixer_input.h b/media/base/audio_renderer_mixer_input.h index 06a0de1..f2b8c3e 100644 --- a/media/base/audio_renderer_mixer_input.h +++ b/media/base/audio_renderer_mixer_input.h @@ -33,9 +33,7 @@ class MEDIA_EXPORT AudioRendererMixerInput void Play() override; void Pause() override; bool SetVolume(double volume) override; - void SwitchOutputDevice(const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) override; + OutputDevice* GetOutputDevice() override; void Initialize(const AudioParameters& params, AudioRendererSink::RenderCallback* renderer) override; diff --git a/media/base/audio_renderer_sink.h b/media/base/audio_renderer_sink.h index 753135d..2d1b0aa 100644 --- a/media/base/audio_renderer_sink.h +++ b/media/base/audio_renderer_sink.h @@ -16,6 +16,7 @@ #include "media/audio/audio_parameters.h" #include "media/base/audio_bus.h" #include "media/base/media_export.h" +#include "media/base/output_device.h" #include "url/gurl.h" namespace base { @@ -24,8 +25,6 @@ class SingleThreadTaskRunner; namespace media { -typedef base::Callback<void(SwitchOutputDeviceResult)> SwitchOutputDeviceCB; - // AudioRendererSink is an interface representing the end-point for // rendered audio. An implementation is expected to // periodically call Render() on a callback object. @@ -67,20 +66,12 @@ class AudioRendererSink // Returns |true| on success. virtual bool SetVolume(double volume) = 0; - // Attempts to switch the audio output device. - // Once the attempt is finished, |callback| is invoked with the - // result of the operation passed as a parameter. The result is a value from - // the media::SwitchOutputDeviceResult enum. - // There is no guarantee about the thread where |callback| will - // be invoked, so users are advised to use media::BindToCurrentLoop() to - // ensure that |callback| runs on the correct thread. - // Note also that copy constructors and destructors for arguments bound to - // |callback| may run on arbitrary threads as |callback| is moved across - // threads. It is advisable to bind arguments such that they are released by - // |callback| when it runs in order to avoid surprises. - virtual void SwitchOutputDevice(const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) = 0; + // Returns a pointer to the internal output device. + // This pointer is not to be owned by the caller and is valid only during + // the lifetime of the AudioRendererSink. + // It can be null, which means that access to the output device is not + // supported. + virtual OutputDevice* GetOutputDevice() = 0; protected: friend class base::RefCountedThreadSafe<AudioRendererSink>; diff --git a/media/base/fake_audio_renderer_sink.cc b/media/base/fake_audio_renderer_sink.cc index 4c4233b..d8a7262 100644 --- a/media/base/fake_audio_renderer_sink.cc +++ b/media/base/fake_audio_renderer_sink.cc @@ -8,13 +8,14 @@ #include "base/location.h" #include "base/logging.h" #include "base/single_thread_task_runner.h" +#include "media/base/fake_output_device.h" namespace media { FakeAudioRendererSink::FakeAudioRendererSink() : state_(kUninitialized), - callback_(NULL) { -} + callback_(NULL), + output_device_(new FakeOutputDevice) {} FakeAudioRendererSink::~FakeAudioRendererSink() { DCHECK(!callback_); @@ -55,11 +56,8 @@ bool FakeAudioRendererSink::SetVolume(double volume) { return true; } -void FakeAudioRendererSink::SwitchOutputDevice( - const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) { - callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); +OutputDevice* FakeAudioRendererSink::GetOutputDevice() { + return output_device_.get(); } bool FakeAudioRendererSink::Render(AudioBus* dest, int audio_delay_milliseconds, diff --git a/media/base/fake_audio_renderer_sink.h b/media/base/fake_audio_renderer_sink.h index 1982ba3..0a9bd93 100644 --- a/media/base/fake_audio_renderer_sink.h +++ b/media/base/fake_audio_renderer_sink.h @@ -12,6 +12,8 @@ namespace media { +class FakeOutputDevice; + class FakeAudioRendererSink : public AudioRendererSink { public: enum State { @@ -32,9 +34,7 @@ class FakeAudioRendererSink : public AudioRendererSink { void Pause() override; void Play() override; bool SetVolume(double volume) override; - void SwitchOutputDevice(const std::string& device_id, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) override; + OutputDevice* GetOutputDevice() override; // Attempts to call Render() on the callback provided to // Initialize() with |dest| and |audio_delay_milliseconds|. @@ -57,6 +57,7 @@ class FakeAudioRendererSink : public AudioRendererSink { State state_; RenderCallback* callback_; + scoped_ptr<FakeOutputDevice> output_device_; DISALLOW_COPY_AND_ASSIGN(FakeAudioRendererSink); }; diff --git a/media/base/fake_output_device.cc b/media/base/fake_output_device.cc new file mode 100644 index 0000000..4d5136b --- /dev/null +++ b/media/base/fake_output_device.cc @@ -0,0 +1,21 @@ +// Copyright (c) 2015 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. + +#include "media/base/fake_output_device.h" + +#include "base/callback.h" + +namespace media { + +FakeOutputDevice::FakeOutputDevice() {} +FakeOutputDevice::~FakeOutputDevice() {} + +void FakeOutputDevice::SwitchOutputDevice( + const std::string& device_id, + const GURL& security_origin, + const SwitchOutputDeviceCB& callback) { + callback.Run(SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS); +} + +} // namespace media diff --git a/media/base/fake_output_device.h b/media/base/fake_output_device.h new file mode 100644 index 0000000..87d1959 --- /dev/null +++ b/media/base/fake_output_device.h @@ -0,0 +1,30 @@ +// Copyright (c) 2015 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 MEDIA_AUDIO_FAKE_OUTPUT_DEVICE_H_ +#define MEDIA_AUDIO_FAKE_OUTPUT_DEVICE_H_ + +#include <string> + +#include "media/base/output_device.h" + +namespace media { + +class FakeOutputDevice : public OutputDevice { + public: + FakeOutputDevice(); + ~FakeOutputDevice() override; + + // OutputDevice implementation. + void SwitchOutputDevice(const std::string& device_id, + const GURL& security_origin, + const SwitchOutputDeviceCB& callback) override; + + private: + DISALLOW_COPY_AND_ASSIGN(FakeOutputDevice); +}; + +} // namespace media + +#endif // MEDIA_AUDIO_FAKE_OUTPUT_DEVICE_H_ diff --git a/media/base/mock_audio_renderer_sink.h b/media/base/mock_audio_renderer_sink.h index b813257..46589fb 100644 --- a/media/base/mock_audio_renderer_sink.h +++ b/media/base/mock_audio_renderer_sink.h @@ -22,12 +22,7 @@ class MockAudioRendererSink : public AudioRendererSink { MOCK_METHOD0(Pause, void()); MOCK_METHOD0(Play, void()); MOCK_METHOD1(SetVolume, bool(double volume)); - MOCK_METHOD0(SwitchOutputDevice, void()); - void SwitchOutputDevice(const std::string&, - const GURL& security_origin, - const SwitchOutputDeviceCB& callback) override { - SwitchOutputDevice(); - } + MOCK_METHOD0(GetOutputDevice, OutputDevice*()); void Initialize(const AudioParameters& params, RenderCallback* renderer) override; diff --git a/media/base/output_device.h b/media/base/output_device.h new file mode 100644 index 0000000..c5ac791 --- /dev/null +++ b/media/base/output_device.h @@ -0,0 +1,56 @@ +// Copyright (c) 2015 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 MEDIA_BASE_OUTPUT_DEVICE_H_ +#define MEDIA_BASE_OUTPUT_DEVICE_H_ + +#include <string> + +#include "base/callback.h" +#include "media/audio/audio_parameters.h" +#include "media/base/media_export.h" +#include "url/gurl.h" + +namespace media { + +// Result of an audio output device switch operation +enum SwitchOutputDeviceResult { + SWITCH_OUTPUT_DEVICE_RESULT_SUCCESS = 0, + SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_FOUND, + SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_AUTHORIZED, + SWITCH_OUTPUT_DEVICE_RESULT_ERROR_OBSOLETE, + SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED, + SWITCH_OUTPUT_DEVICE_RESULT_LAST = + SWITCH_OUTPUT_DEVICE_RESULT_ERROR_NOT_SUPPORTED, +}; + +typedef base::Callback<void(SwitchOutputDeviceResult)> SwitchOutputDeviceCB; + +// OutputDevice is an interface that allows performing operations related +// audio output devices. + +class OutputDevice { + public: + // Attempts to switch the audio output device. + // Once the attempt is finished, |callback| is invoked with the + // result of the operation passed as a parameter. The result is a value from + // the media::SwitchOutputDeviceResult enum. + // There is no guarantee about the thread where |callback| will + // be invoked, so users are advised to use media::BindToCurrentLoop() to + // ensure that |callback| runs on the correct thread. + // Note also that copy constructors and destructors for arguments bound to + // |callback| may run on arbitrary threads as |callback| is moved across + // threads. It is advisable to bind arguments such that they are released by + // |callback| when it runs in order to avoid surprises. + virtual void SwitchOutputDevice(const std::string& device_id, + const GURL& security_origin, + const SwitchOutputDeviceCB& callback) = 0; + + protected: + virtual ~OutputDevice() {} +}; + +} // namespace media + +#endif // MEDIA_BASE_OUTPUT_DEVICE_H_ |