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

Unified Diff: content/renderer/media/audio_ipc_factory.cc

Issue 2821203005: Add a mojo implementation of AudioOutputIPC. (Closed)
Patch Set: Fix test issues Created 3 years, 7 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/renderer/media/audio_ipc_factory.cc
diff --git a/content/renderer/media/audio_ipc_factory.cc b/content/renderer/media/audio_ipc_factory.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e083c73866b1733a387d6d5d8c3df358a351358f
--- /dev/null
+++ b/content/renderer/media/audio_ipc_factory.cc
@@ -0,0 +1,107 @@
+// 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/audio_ipc_factory.h"
+
+#include <utility>
+
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "content/renderer/media/audio_message_filter.h"
+#include "content/renderer/media/mojo_audio_output_ipc.h"
+#include "services/service_manager/public/cpp/interface_provider.h"
+
+namespace content {
+
+AudioIPCFactory* AudioIPCFactory::instance_ = nullptr;
+
+AudioIPCFactory::AudioIPCFactory(
+ const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
+ AudioMessageFilter* audio_message_filter)
+ : audio_message_filter_(audio_message_filter),
+ io_task_runner_(io_task_runner) {
+ // TODO(maxmorin): In the future, set |audio_message_filter_| to nullptr based
+ // on a feature.
+ DCHECK(!instance_);
+ instance_ = this;
+}
+
+AudioIPCFactory::~AudioIPCFactory() {
+ DCHECK_EQ(instance_, this);
+ instance_ = nullptr;
+}
+
+bool AudioIPCFactory::UsingMojoFactories() const {
+ return audio_message_filter_ == nullptr;
+}
+
+std::unique_ptr<media::AudioOutputIPC> AudioIPCFactory::CreateAudioOutputIPC(
+ int frame_id) const {
+ if (UsingMojoFactories())
+ return base::MakeUnique<MojoAudioOutputIPC>(base::Bind(
+ &AudioIPCFactory::GetRemoteFactory, base::Unretained(this), frame_id));
+ return audio_message_filter_->CreateAudioOutputIPC(frame_id);
+}
+
+void AudioIPCFactory::RegisterRemoteFactory(
+ int frame_id,
+ mojom::RendererAudioOutputStreamFactoryPtr factory) {
+ DCHECK(UsingMojoFactories());
+ // PassInterface unbinds the message pipe from the current thread. This
+ // allows us to bind it to the IO thread.
+ // Unretained is safe because |this| is owned by the RenderThreadImpl which is
o1ka 2017/05/11 10:58:40 If "not being destroyed ever" is a requirement for
Max Morin 2017/05/11 15:31:17 Added header comment and updated these comments. D
o1ka 2017/05/15 13:27:08 It still a bit confusing, since the comment says
Max Morin 2017/05/16 15:51:35 Ok, I expanded the header comment to include the c
+ // leaked.
+ io_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&AudioIPCFactory::RegisterRemoteFactoryOnIOThread,
+ base::Unretained(this), frame_id,
+ base::Passed(factory.PassInterface())));
+}
+
+void AudioIPCFactory::MaybeDeregisterRemoteFactory(int frame_id) {
+ if (!UsingMojoFactories())
+ return;
+ if (!io_task_runner_->BelongsToCurrentThread()) {
+ // Unretained is safe because |this| is owned by the RenderThreadImpl which
+ // is leaked.
+ io_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory,
+ base::Unretained(this), frame_id));
+ return;
+ }
+ factory_ptrs_.erase(frame_id);
+}
+
+mojom::RendererAudioOutputStreamFactory* AudioIPCFactory::GetRemoteFactory(
+ int frame_id) const {
+ DCHECK(io_task_runner_->BelongsToCurrentThread());
+ DCHECK(UsingMojoFactories());
+ auto it = factory_ptrs_.find(frame_id);
+ return it == factory_ptrs_.end() ? nullptr : it->second.get();
+}
+
+void AudioIPCFactory::RegisterRemoteFactoryOnIOThread(
+ int frame_id,
+ mojom::RendererAudioOutputStreamFactoryPtrInfo factory_ptr_info) {
+ DCHECK(io_task_runner_->BelongsToCurrentThread());
+ DCHECK(UsingMojoFactories());
+ std::pair<StreamFactoryMap::iterator, bool> emplace_result =
+ factory_ptrs_.emplace(frame_id,
+ mojo::MakeProxy(std::move(factory_ptr_info)));
+
+ DCHECK(emplace_result.second) << "Attempt to register a factory for a "
+ "frame which already has a factory "
+ "registered.";
+
+ auto& emplaced_factory = emplace_result.first->second;
+ DCHECK(emplaced_factory.is_bound())
+ << "Factory is not bound to a remote implementation.";
+
+ // Unretained is safe because |this| owns the binding, so a connection error
+ // cannot trigger after destruction.
+ emplaced_factory.set_connection_error_handler(
+ base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory,
+ base::Unretained(this), frame_id));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698