OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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_output_client.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/files/file.h" | |
10 #include "base/message_loop/message_loop.h" | |
11 #include "base/single_thread_task_runner.h" | |
12 #include "base/time/time.h" | |
13 #include "content/common/media/audio_messages.h" | |
14 #include "content/public/common/service_registry.h" | |
15 #include "content/renderer/media/audio_message_filter.h" | |
16 #include "content/renderer/media/webrtc_logging.h" | |
17 #include "media/audio/audio_parameters.h" | |
18 #include "media/mojo/common/media_type_converters.h" | |
19 #include "mojo/edk/embedder/embedder.h" | |
20 #include "mojo/public/c/system/buffer.h" | |
21 #include "mojo/public/cpp/bindings/binding.h" | |
22 #include "mojo/public/cpp/system/handle.h" | |
23 | |
24 namespace content { | |
25 | |
26 namespace { | |
27 | |
28 int FindAudioOutputStreamPtrID( | |
29 const AudioOutputClient::ScopedAudioOutputStreamPtrMap& stream_ids, | |
30 media::interfaces::AudioOutputStreamPtr* const key) { | |
31 for (auto& it : stream_ids) { | |
32 if (it.second.get() == key) { | |
mcasas
2016/04/29 18:29:01
No {} for one-line bodies
rchtara
2016/05/23 16:38:18
Done.
| |
33 return it.first; | |
34 } | |
35 } | |
mcasas
2016/04/29 18:29:00
Nit: Consider std::find_if()
rchtara
2016/05/23 16:38:18
Tried to do that:
bool f(const std::pair<int,
| |
36 return -1; | |
mcasas
2016/04/29 18:29:00
Consider making |kStreamIDNotSet| not
public (and
rchtara
2016/05/23 16:38:18
Done.
| |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 AudioOutputClient::AudioOutputClient( | |
42 ServiceRegistry* service_registry, | |
43 scoped_refptr<AudioMessageFilter> audio_message_filter) | |
44 : audio_message_filter_(audio_message_filter), | |
45 main_thread_task_runner_(base::MessageLoop::current()->task_runner()) { | |
46 if (service_registry) { | |
47 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); | |
48 service_.set_connection_error_handler(base::Bind( | |
49 &AudioOutputClient::OnConnectionError, base::Unretained(this))); | |
mcasas
2016/04/29 18:29:01
base::Unretained should be used sparingly.
Since h
| |
50 } else { | |
51 service_ = 0; | |
mcasas
2016/04/29 18:29:01
No need to do this (and is semantically wrong),
in
rchtara
2016/05/23 16:38:18
Done.
| |
52 } | |
53 } | |
54 | |
55 AudioOutputClient::~AudioOutputClient() {} | |
56 | |
57 void AudioOutputClient::OnConnectionError() { | |
58 LOG(ERROR) << "Mojo client connection error"; | |
mcasas
2016/04/29 18:29:01
s/LOG/DLOG/
(I think there's a presubmit warning f
rchtara
2016/05/23 16:38:18
Done.
| |
59 } | |
60 | |
61 void AudioOutputClient::OnStreamError(int stream_id) { | |
62 audio_message_filter_->io_task_runner()->PostTask( | |
63 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, | |
64 base::Unretained(this), stream_id)); | |
mcasas
2016/04/29 18:29:01
base::Unretained() Should not be used. If this
cla
rchtara
2016/05/23 16:38:18
I failed to use WeakPtrFactory. So I will made thi
| |
65 } | |
66 | |
67 void AudioOutputClient::CreateStream(int stream_id, | |
68 int render_frame_id, | |
69 const media::AudioParameters& params) { | |
70 main_thread_task_runner_->PostTask( | |
mcasas
2016/04/29 18:29:01
Consider merging with CreateStreamOnMainThread():
rchtara
2016/05/23 16:38:18
Done.
| |
71 FROM_HERE, | |
72 base::Bind(&AudioOutputClient::CreateStreamOnMainThread, | |
73 base::Unretained(this), stream_id, render_frame_id, params)); | |
74 } | |
75 | |
76 void AudioOutputClient::CreateStreamOnMainThread( | |
77 int stream_id, | |
78 int render_frame_id, | |
79 const media::AudioParameters& params) { | |
80 media::AudioParameters param; | |
81 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
82 if (service_) { | |
83 service_->CreateStream( | |
84 stream_id, render_frame_id, | |
85 mojo::ConvertTo<media::interfaces::AudioOutputStreamParametersPtr>( | |
86 params), | |
87 base::Bind(&AudioOutputClient::CreateStreamCallback, | |
88 base::Unretained(this))); | |
89 } | |
90 } | |
91 | |
92 void AudioOutputClient::CreateStreamCallback( | |
93 int stream_id, | |
94 media::interfaces::AudioOutputStreamPtr stream, | |
95 mojo::ScopedSharedBufferHandle shared_buffer, | |
96 mojo::ScopedHandle socket_descriptor) { | |
97 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
98 | |
99 if (!stream.is_bound()) { | |
100 audio_message_filter_->io_task_runner()->PostTask( | |
101 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, | |
102 base::Unretained(this), stream_id)); | |
103 return; | |
104 } | |
105 | |
106 stream.set_connection_error_handler(base::Bind( | |
107 &AudioOutputClient::OnStreamError, base::Unretained(this), stream_id)); | |
108 | |
109 base::SharedMemoryHandle shared_memory_handle; | |
110 size_t length; | |
111 | |
112 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( | |
113 shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); | |
114 | |
115 if (pass_shared_memory_result != MOJO_RESULT_OK) { | |
116 LOG(ERROR) << "Failed to pass shared memory. Closing: " | |
mcasas
2016/04/29 18:29:00
s/LOG/DLOG/
rchtara
2016/05/23 16:38:18
Done.
| |
117 << pass_shared_memory_result; | |
118 return; | |
119 } | |
120 | |
121 mojo::edk::ScopedPlatformHandle platform_handle; | |
122 | |
123 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( | |
124 socket_descriptor.release().value(), &platform_handle); | |
125 | |
126 if (pass_platform_handle_result != MOJO_RESULT_OK) { | |
127 LOG(ERROR) << "Failed to pass transit descriptor. Closing: " | |
128 << pass_platform_handle_result; | |
129 return; | |
130 } | |
131 | |
132 base::SyncSocket::TransitDescriptor descriptor; | |
133 | |
134 #if defined(OS_WIN) | |
135 descriptor = platform_handle.release().handle; | |
136 #else | |
137 descriptor.fd = platform_handle.release().handle; | |
138 #endif | |
139 streams_[stream_id] = make_scoped_ptr( | |
140 new media::interfaces::AudioOutputStreamPtr(std::move(stream))); | |
141 | |
142 audio_message_filter_->io_task_runner()->PostTask( | |
143 FROM_HERE, base::Bind(&AudioOutputClient::CreateStreamOnIOThread, | |
144 base::Unretained(this), streams_[stream_id].get(), | |
145 shared_memory_handle, descriptor, length)); | |
146 } | |
147 | |
148 void AudioOutputClient::CreateStreamOnIOThread( | |
149 media::interfaces::AudioOutputStreamPtr* const stream, | |
150 base::SharedMemoryHandle handle, | |
151 base::SyncSocket::TransitDescriptor socket_descriptor, | |
152 uint32_t length) { | |
153 DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread()); | |
154 audio_message_filter_->OnStreamCreated( | |
155 FindAudioOutputStreamPtrID(streams_, stream), handle, socket_descriptor, | |
156 length); | |
157 } | |
158 | |
159 void AudioOutputClient::CloseStream(int stream_id) { | |
160 main_thread_task_runner_->PostTask( | |
161 FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread, | |
162 base::Unretained(this), stream_id)); | |
163 } | |
164 | |
165 void AudioOutputClient::CloseStreamOnMainThread(int stream_id) { | |
166 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); | |
167 streams_[stream_id]->get()->Close(); | |
mcasas
2016/04/29 18:29:01
Remove get()->, a unique_ptr behaves like a
pointe
rchtara
2016/05/23 16:38:18
Done.
| |
168 } | |
169 | |
170 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { | |
171 DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread()); | |
172 audio_message_filter_->OnStreamStateChanged( | |
173 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); | |
174 } | |
175 | |
176 } // namespace content | |
OLD | NEW |