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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/media/audio_ipc_factory.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "content/renderer/media/audio_message_filter.h"
12 #include "content/renderer/media/mojo_audio_output_ipc.h"
13 #include "services/service_manager/public/cpp/interface_provider.h"
14
15 namespace content {
16
17 AudioIPCFactory* AudioIPCFactory::instance_ = nullptr;
18
19 AudioIPCFactory::AudioIPCFactory(
20 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
21 AudioMessageFilter* audio_message_filter)
22 : audio_message_filter_(audio_message_filter),
23 io_task_runner_(io_task_runner) {
24 // TODO(maxmorin): In the future, set |audio_message_filter_| to nullptr based
25 // on a feature.
26 DCHECK(!instance_);
27 instance_ = this;
28 }
29
30 AudioIPCFactory::~AudioIPCFactory() {
31 DCHECK_EQ(instance_, this);
32 instance_ = nullptr;
33 }
34
35 bool AudioIPCFactory::UsingMojoFactories() const {
36 return audio_message_filter_ == nullptr;
37 }
38
39 std::unique_ptr<media::AudioOutputIPC> AudioIPCFactory::CreateAudioOutputIPC(
40 int frame_id) const {
41 if (UsingMojoFactories())
42 return base::MakeUnique<MojoAudioOutputIPC>(base::Bind(
43 &AudioIPCFactory::GetRemoteFactory, base::Unretained(this), frame_id));
44 return audio_message_filter_->CreateAudioOutputIPC(frame_id);
45 }
46
47 void AudioIPCFactory::RegisterRemoteFactory(
48 int frame_id,
49 mojom::RendererAudioOutputStreamFactoryPtr factory) {
50 DCHECK(UsingMojoFactories());
51 // PassInterface unbinds the message pipe from the current thread. This
52 // allows us to bind it to the IO thread.
53 // 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
54 // leaked.
55 io_task_runner_->PostTask(
56 FROM_HERE, base::Bind(&AudioIPCFactory::RegisterRemoteFactoryOnIOThread,
57 base::Unretained(this), frame_id,
58 base::Passed(factory.PassInterface())));
59 }
60
61 void AudioIPCFactory::MaybeDeregisterRemoteFactory(int frame_id) {
62 if (!UsingMojoFactories())
63 return;
64 if (!io_task_runner_->BelongsToCurrentThread()) {
65 // Unretained is safe because |this| is owned by the RenderThreadImpl which
66 // is leaked.
67 io_task_runner_->PostTask(
68 FROM_HERE, base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory,
69 base::Unretained(this), frame_id));
70 return;
71 }
72 factory_ptrs_.erase(frame_id);
73 }
74
75 mojom::RendererAudioOutputStreamFactory* AudioIPCFactory::GetRemoteFactory(
76 int frame_id) const {
77 DCHECK(io_task_runner_->BelongsToCurrentThread());
78 DCHECK(UsingMojoFactories());
79 auto it = factory_ptrs_.find(frame_id);
80 return it == factory_ptrs_.end() ? nullptr : it->second.get();
81 }
82
83 void AudioIPCFactory::RegisterRemoteFactoryOnIOThread(
84 int frame_id,
85 mojom::RendererAudioOutputStreamFactoryPtrInfo factory_ptr_info) {
86 DCHECK(io_task_runner_->BelongsToCurrentThread());
87 DCHECK(UsingMojoFactories());
88 std::pair<StreamFactoryMap::iterator, bool> emplace_result =
89 factory_ptrs_.emplace(frame_id,
90 mojo::MakeProxy(std::move(factory_ptr_info)));
91
92 DCHECK(emplace_result.second) << "Attempt to register a factory for a "
93 "frame which already has a factory "
94 "registered.";
95
96 auto& emplaced_factory = emplace_result.first->second;
97 DCHECK(emplaced_factory.is_bound())
98 << "Factory is not bound to a remote implementation.";
99
100 // Unretained is safe because |this| owns the binding, so a connection error
101 // cannot trigger after destruction.
102 emplaced_factory.set_connection_error_handler(
103 base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory,
104 base::Unretained(this), frame_id));
105 }
106
107 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698