Index: content/renderer/media/mojo_audio_output_ipc.cc |
diff --git a/content/renderer/media/mojo_audio_output_ipc.cc b/content/renderer/media/mojo_audio_output_ipc.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2f2fc576b14b2632f5fe4d58b1cfa2971b849421 |
--- /dev/null |
+++ b/content/renderer/media/mojo_audio_output_ipc.cc |
@@ -0,0 +1,229 @@ |
+// Copyright 2017 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 "content/renderer/media/mojo_audio_output_ipc.h" |
+ |
+#include <utility> |
+ |
+#include "media/audio/audio_device_description.h" |
+#include "mojo/public/cpp/system/platform_handle.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+void TrivialAuthorizedCallback(media::OutputDeviceStatus, |
+ const media::AudioParameters&, |
+ const std::string&) {} |
+ |
+} // namespace |
+ |
+// This class wraps a callback. If this class is destroyed without the callback |
+// being called, it will call it with an error. This is needed since the |
+// AudioOutputDevice could otherwise wait forever for device parameters in the |
+// case of a connection error. |
+class AuthorizationCallbackWrapper { |
+ public: |
+ using CallbackType = base::OnceCallback<void(media::OutputDeviceStatus, |
+ const media::AudioParameters&, |
+ const std::string&)>; |
+ |
+ static CallbackType Wrap(CallbackType callback) { |
+ return base::BindOnce( |
+ AuthorizationCallbackWrapper::Call, |
+ base::Passed(AuthorizationCallbackWrapper(std::move(callback)))); |
+ } |
+ |
+ AuthorizationCallbackWrapper(AuthorizationCallbackWrapper&& other) |
+ : callback_(std::move(other.callback_)) { |
+ // It's not explicitly stated that moving from a OnceCallback resets the |
+ // moved-from callback, so reset it here. |
+ other.callback_ = CallbackType(); |
+ } |
+ |
+ AuthorizationCallbackWrapper& operator=( |
+ AuthorizationCallbackWrapper&& other) = delete; |
+ |
+ ~AuthorizationCallbackWrapper() { |
+ if (callback_) { |
+ std::move(callback_).Run( |
+ media::OutputDeviceStatus::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL, |
+ media::AudioParameters::UnavailableDeviceParams(), std::string()); |
+ } |
+ } |
+ |
+ private: |
+ explicit AuthorizationCallbackWrapper(CallbackType callback) |
+ : callback_(std::move(callback)) {} |
+ |
+ static void Call(AuthorizationCallbackWrapper callback_wrapper, |
+ media::OutputDeviceStatus status, |
+ const media::AudioParameters& params, |
+ const std::string& device_id) { |
+ if (callback_wrapper.callback_) { |
+ std::move(callback_wrapper.callback_).Run(status, params, device_id); |
+ // Make sure we don't call again in destructor: |
+ callback_wrapper.callback_ = CallbackType(); |
+ } |
+ } |
+ |
+ CallbackType callback_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AuthorizationCallbackWrapper); |
+}; |
+ |
+MojoAudioOutputIPC::MojoAudioOutputIPC(FactoryAccessor factory_accessor) |
+ : factory_accessor_(std::move(factory_accessor)), weak_factory_(this) { |
+ DETACH_FROM_THREAD(thread_checker_); |
+} |
+ |
+MojoAudioOutputIPC::~MojoAudioOutputIPC() { |
+ // No thread check. |
+ DCHECK(!AuthorizationRequested()); |
+ DCHECK(!StreamCreationRequested()); |
+ // Destructing |weak_factory_| on any thread is safe since it's not used after |
+ // the final call to CloseStream, where its pointers are invalidated. |
+} |
+ |
+void MojoAudioOutputIPC::RequestDeviceAuthorization( |
+ media::AudioOutputIPCDelegate* delegate, |
+ int session_id, |
+ const std::string& device_id, |
+ const url::Origin& security_origin) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(delegate); |
+ DCHECK(!AuthorizationRequested()); |
+ DCHECK(!StreamCreationRequested()); |
+ auto* factory = factory_accessor_.Run(); |
+ if (!factory) { |
+ LOG(ERROR) << "MojoAudioOutputIPC failed to acquire factory"; |
+ delegate->OnIPCClosed(); // deletes |this|. |
+ return; |
+ } |
+ |
+ // We wrap the callback here so that we are sure to always get the |
+ // authorization reply, even if the connection is closed. |
+ factory->RequestDeviceAuthorization( |
+ MakeProviderRequest(delegate), session_id, device_id, |
+ AuthorizationCallbackWrapper::Wrap( |
+ base::Bind(&MojoAudioOutputIPC::RecievedDeviceAuthorization, |
+ weak_factory_.GetWeakPtr(), delegate))); |
+} |
+ |
+void MojoAudioOutputIPC::CreateStream(media::AudioOutputIPCDelegate* delegate, |
+ const media::AudioParameters& params) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(delegate); |
+ DCHECK(!StreamCreationRequested()); |
+ if (!AuthorizationRequested()) { |
+ // No authorization requested yet. Request one for the default device. |
+ auto* factory = factory_accessor_.Run(); |
+ if (!factory) { |
+ LOG(ERROR) << "MojoAudioOutputIPC failed to acquire factory"; |
+ delegate->OnIPCClosed(); // deletes |this|. |
+ return; |
+ } |
+ |
+ // Since the delegate didn't explicitly request authorization, we shouldn't |
+ // send a callback to it. |
+ factory->RequestDeviceAuthorization( |
+ MakeProviderRequest(delegate), 0, |
+ media::AudioDeviceDescription::kDefaultDeviceId, |
+ base::Bind(&TrivialAuthorizedCallback)); |
+ } |
+ |
+ // Since the creation callback won't fire if the provider binding is gone, |
+ // unretained is safe. |
+ stream_provider_->Acquire(mojo::MakeRequest(&stream_), params, |
+ base::Bind(&MojoAudioOutputIPC::StreamCreated, |
+ base::Unretained(this), delegate)); |
+ |
+ // Unretained is safe because |delegate| must be valid until CloseStream is |
+ // called, and |stream_| is reset in CloseStream. |
+ stream_.set_connection_error_handler(base::Bind( |
+ &media::AudioOutputIPCDelegate::OnError, base::Unretained(delegate))); |
+} |
+ |
+void MojoAudioOutputIPC::PlayStream() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ if (stream_.is_bound()) |
+ stream_->Play(); |
+} |
+ |
+void MojoAudioOutputIPC::PauseStream() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ if (stream_.is_bound()) |
+ stream_->Pause(); |
+} |
+ |
+void MojoAudioOutputIPC::CloseStream() { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ stream_provider_.reset(); |
+ stream_.reset(); |
+ |
+ // Make sure we don't send an authorization callback for this stream to the |
+ // delegate. |
+ weak_factory_.InvalidateWeakPtrs(); |
+} |
+ |
+void MojoAudioOutputIPC::SetVolume(double volume) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ if (stream_.is_bound()) |
+ stream_->SetVolume(volume); |
+} |
+ |
+bool MojoAudioOutputIPC::AuthorizationRequested() { |
+ return stream_provider_.is_bound(); |
+} |
+ |
+bool MojoAudioOutputIPC::StreamCreationRequested() { |
+ return stream_.is_bound(); |
+} |
+ |
+media::mojom::AudioOutputStreamProviderRequest |
+MojoAudioOutputIPC::MakeProviderRequest( |
+ media::AudioOutputIPCDelegate* delegate) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(!AuthorizationRequested()); |
+ media::mojom::AudioOutputStreamProviderRequest request = |
+ mojo::MakeRequest(&stream_provider_); |
+ |
+ // Unretained is safe because |delegate| owns |this|. |
o1ka
2017/05/15 13:27:08
l. 142 says "Unretained is safe because |delegate|
Max Morin
2017/05/16 15:51:35
Done.
|
+ stream_provider_.set_connection_error_handler(base::Bind( |
+ &media::AudioOutputIPCDelegate::OnError, base::Unretained(delegate))); |
+ return request; |
+} |
+ |
+void MojoAudioOutputIPC::RecievedDeviceAuthorization( |
+ media::AudioOutputIPCDelegate* delegate, |
+ media::OutputDeviceStatus status, |
+ const media::AudioParameters& params, |
+ const std::string& device_id) const { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ delegate->OnDeviceAuthorized(status, params, device_id); |
+} |
+ |
+void MojoAudioOutputIPC::StreamCreated( |
+ media::AudioOutputIPCDelegate* delegate, |
+ mojo::ScopedSharedBufferHandle shared_memory, |
+ mojo::ScopedHandle socket) { |
+ DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
+ DCHECK(socket.is_valid()); |
+ DCHECK(shared_memory.is_valid()); |
+ |
+ base::PlatformFile socket_handle; |
+ mojo::UnwrapPlatformFile(std::move(socket), &socket_handle); |
+ |
+ base::SharedMemoryHandle memory_handle; |
+ bool read_only = false; |
+ size_t memory_length = 0; |
+ auto result = mojo::UnwrapSharedMemoryHandle( |
+ std::move(shared_memory), &memory_handle, &memory_length, &read_only); |
+ DCHECK_EQ(result, MOJO_RESULT_OK); |
+ DCHECK(!read_only); |
+ |
+ delegate->OnStreamCreated(memory_handle, socket_handle, memory_length); |
+} |
+ |
+} // namespace content |