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

Side by Side Diff: content/renderer/media/audio_ipc_factory.cc

Issue 2890753003: Introduce AudioIPCFactory. (Closed)
Patch Set: Dale's comments. Created 3 years, 6 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 "base/single_thread_task_runner.h"
12 #include "content/renderer/media/audio_message_filter.h"
13 #include "content/renderer/media/mojo_audio_output_ipc.h"
14 #include "services/service_manager/public/cpp/interface_provider.h"
15
16 namespace content {
17
18 AudioIPCFactory* AudioIPCFactory::instance_ = nullptr;
19
20 AudioIPCFactory::AudioIPCFactory(
21 scoped_refptr<AudioMessageFilter> audio_message_filter,
22 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
23 : audio_message_filter_(std::move(audio_message_filter)),
24 io_task_runner_(std::move(io_task_runner)) {
25 // TODO(maxmorin): In the future, use a feature to control whether
26 // audio_message_filter should be used.
27 DCHECK(!instance_);
28 instance_ = this;
29 }
30
31 AudioIPCFactory::~AudioIPCFactory() {
32 // Allow destruction in tests.
33 DCHECK(factory_ptrs_.empty());
34 DCHECK_EQ(instance_, this);
35 instance_ = nullptr;
36 }
37
38 std::unique_ptr<media::AudioOutputIPC> AudioIPCFactory::CreateAudioOutputIPC(
39 int frame_id) const {
40 if (UsingMojoFactories()) {
41 // Unretained is safe due to the contract at the top of the header file.
42 return base::MakeUnique<MojoAudioOutputIPC>(base::BindRepeating(
43 &AudioIPCFactory::GetRemoteFactory, base::Unretained(this), frame_id));
44 }
45 return audio_message_filter_->CreateAudioOutputIPC(frame_id);
46 }
47
48 void AudioIPCFactory::MaybeRegisterRemoteFactory(
49 int frame_id,
50 service_manager::InterfaceProvider* interface_provider) {
51 if (!UsingMojoFactories())
52 return;
53 mojom::RendererAudioOutputStreamFactoryPtr factory_ptr;
54 interface_provider->GetInterface(&factory_ptr);
55 // PassInterface unbinds the message pipe from the current thread. This
56 // allows us to bind it to the IO thread.
57 // Unretained is safe due to the contract at the top of the header file.
58 io_task_runner_->PostTask(
59 FROM_HERE,
60 base::BindOnce(&AudioIPCFactory::RegisterRemoteFactoryOnIOThread,
61 base::Unretained(this), frame_id,
62 factory_ptr.PassInterface()));
63 }
64
65 void AudioIPCFactory::MaybeDeregisterRemoteFactory(int frame_id) {
66 if (!UsingMojoFactories())
67 return;
68 io_task_runner_->PostTask(
69 FROM_HERE,
70 base::BindOnce(&AudioIPCFactory::MaybeDeregisterRemoteFactoryOnIOThread,
71 base::Unretained(this), frame_id));
72 }
73
74 mojom::RendererAudioOutputStreamFactory* AudioIPCFactory::GetRemoteFactory(
75 int frame_id) const {
76 DCHECK(io_task_runner_->BelongsToCurrentThread());
77 DCHECK(UsingMojoFactories());
78 auto it = factory_ptrs_.find(frame_id);
79 return it == factory_ptrs_.end() ? nullptr : it->second.get();
80 }
81
82 void AudioIPCFactory::RegisterRemoteFactoryOnIOThread(
83 int frame_id,
84 mojom::RendererAudioOutputStreamFactoryPtrInfo factory_ptr_info) {
85 DCHECK(io_task_runner_->BelongsToCurrentThread());
86 DCHECK(UsingMojoFactories());
87 std::pair<StreamFactoryMap::iterator, bool> emplace_result =
88 factory_ptrs_.emplace(frame_id,
89 mojo::MakeProxy(std::move(factory_ptr_info)));
90
91 DCHECK(emplace_result.second) << "Attempt to register a factory for a "
92 "frame which already has a factory "
93 "registered.";
94
95 auto& emplaced_factory = emplace_result.first->second;
96 DCHECK(emplaced_factory.is_bound())
97 << "Factory is not bound to a remote implementation.";
98
99 // Unretained is safe because |this| owns the binding, so a connection error
100 // cannot trigger after destruction.
101 emplaced_factory.set_connection_error_handler(
102 base::Bind(&AudioIPCFactory::MaybeDeregisterRemoteFactory,
103 base::Unretained(this), frame_id));
104 }
105
106 void AudioIPCFactory::MaybeDeregisterRemoteFactoryOnIOThread(int frame_id) {
107 DCHECK(io_task_runner_->BelongsToCurrentThread());
108 DCHECK(UsingMojoFactories());
109 // This function can be called both by the frame and the connection error
110 // handler of the factory pointer. Calling erase multiple times even though
111 // there is nothing to erase is safe, so we don't have to handle this in any
112 // particular way.
113 factory_ptrs_.erase(frame_id);
114 }
115
116 bool AudioIPCFactory::UsingMojoFactories() const {
117 return audio_message_filter_ == nullptr;
118 }
119
120 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/audio_ipc_factory.h ('k') | content/renderer/media/audio_ipc_factory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698