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/base/audio_parameters.h" | |
18 #include "mojo/edk/embedder/embedder.h" | |
19 #include "mojo/public/c/system/buffer.h" | |
20 #include "mojo/public/cpp/bindings/binding.h" | |
21 #include "mojo/public/cpp/system/handle.h" | |
22 | |
23 namespace content { | |
24 | |
25 namespace { | |
26 // Mojo connection error handler. | |
27 void OnConnectionError() { | |
28 DLOG(ERROR) << "Mojo client connection error"; | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 AudioOutputClient::AudioOutputClient( | |
34 ServiceRegistry* service_registry, | |
35 const scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) | |
36 : thread_task_runner_(thread_task_runner) { | |
37 if (!service_registry) | |
38 return; | |
39 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); | |
40 service_.set_connection_error_handler(base::Bind(&OnConnectionError)); | |
41 } | |
42 | |
43 AudioOutputClient::~AudioOutputClient() {} | |
44 | |
45 void AudioOutputClient::OnStreamError(int stream_id) { | |
46 if (!service_.is_bound()) | |
47 return; | |
48 AudioMessageFilter::Get()->io_task_runner()->PostTask( | |
49 FROM_HERE, | |
50 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); | |
51 } | |
52 | |
53 void AudioOutputClient::CreateStream( | |
54 int stream_id, | |
55 const media::AudioParameters& params, | |
56 const media::mojom::AudioOutput::CreateStreamCallback& callback) { | |
57 if (!thread_task_runner_->BelongsToCurrentThread()) { | |
58 thread_task_runner_->PostTask( | |
59 FROM_HERE, base::Bind(&AudioOutputClient::CreateStream, this, stream_id, | |
60 params, callback)); | |
61 return; | |
62 } | |
63 DCHECK(thread_task_runner_->BelongsToCurrentThread()); | |
Henrik Grunell
2016/05/20 13:35:41
No need since there is a conditional post above.
rchtara
2016/05/27 15:24:38
Done.
| |
64 if (service_.is_bound()) { | |
65 service_->CreateStream(stream_id, params, callback); | |
66 } | |
67 } | |
68 | |
69 void AudioOutputClient::CreateStreamCallback( | |
70 int stream_id, | |
71 media::mojom::AudioOutputStreamPtr stream, | |
72 mojo::ScopedSharedBufferHandle shared_buffer, | |
73 mojo::ScopedHandle socket_descriptor) { | |
74 DCHECK(thread_task_runner_->BelongsToCurrentThread()); | |
75 if (!AudioMessageFilter::Get()) | |
76 return; | |
77 if (!stream.is_bound()) { | |
78 AudioMessageFilter::Get()->io_task_runner()->PostTask( | |
Henrik Grunell
2016/05/20 13:35:41
Pass the io task runner in the constructor instead
rchtara
2016/05/27 15:24:38
Done.
| |
79 FROM_HERE, | |
80 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); | |
81 return; | |
82 } | |
83 | |
84 stream.set_connection_error_handler( | |
85 base::Bind(&AudioOutputClient::OnStreamError, this, stream_id)); | |
86 | |
87 base::SharedMemoryHandle shared_memory_handle; | |
88 size_t length; | |
89 | |
90 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( | |
91 shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); | |
92 | |
93 if (pass_shared_memory_result != MOJO_RESULT_OK) { | |
94 DLOG(ERROR) << "Failed to pass shared memory. Closing: " | |
95 << pass_shared_memory_result; | |
96 AudioMessageFilter::Get()->io_task_runner()->PostTask( | |
97 FROM_HERE, | |
98 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); | |
99 return; | |
100 } | |
101 | |
102 mojo::edk::ScopedPlatformHandle platform_handle; | |
103 | |
104 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( | |
105 socket_descriptor.release().value(), &platform_handle); | |
106 | |
107 if (pass_platform_handle_result != MOJO_RESULT_OK) { | |
108 DLOG(ERROR) << "Failed to pass transit descriptor. Closing: " | |
109 << pass_platform_handle_result; | |
110 AudioMessageFilter::Get()->io_task_runner()->PostTask( | |
111 FROM_HERE, | |
112 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); | |
113 return; | |
114 } | |
115 | |
116 base::SyncSocket::TransitDescriptor descriptor; | |
117 | |
118 #if defined(OS_WIN) | |
119 descriptor = platform_handle.release().handle; | |
120 #else | |
121 descriptor.fd = platform_handle.release().handle; | |
122 #endif | |
123 | |
124 auto result = streams_.insert(std::make_pair(stream_id, std::move(stream))); | |
125 AudioMessageFilter::Get()->io_task_runner()->PostTask( | |
126 FROM_HERE, | |
127 base::Bind(&AudioOutputClient::CreateStreamOnIOThread, this, result.first, | |
128 shared_memory_handle, descriptor, length)); | |
129 } | |
130 | |
131 void AudioOutputClient::CreateStreamOnIOThread( | |
132 AudioOutputClient::AudioOutputStreamPtrMap::iterator stream, | |
133 base::SharedMemoryHandle handle, | |
134 base::SyncSocket::TransitDescriptor socket_descriptor, | |
135 uint32_t length) { | |
136 if (!AudioMessageFilter::Get()) | |
137 return; | |
138 DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); | |
139 AudioMessageFilter::Get()->OnStreamCreated(stream->first, handle, | |
140 socket_descriptor, length); | |
141 } | |
142 | |
143 void AudioOutputClient::CloseStream(int stream_id) { | |
144 if (!thread_task_runner_->BelongsToCurrentThread()) { | |
145 thread_task_runner_->PostTask( | |
146 FROM_HERE, | |
147 base::Bind(&AudioOutputClient::CloseStream, this, stream_id)); | |
148 return; | |
149 } | |
150 | |
151 DCHECK(thread_task_runner_->BelongsToCurrentThread()); | |
152 if (service_.is_bound()) { | |
153 if (streams_.find(stream_id) != streams_.end() && | |
154 streams_[stream_id].is_bound()) { | |
155 streams_[stream_id]->Close(); | |
156 streams_.erase(stream_id); | |
157 } else if (AudioMessageFilter::Get()) { | |
158 AudioMessageFilter::Get()->CloseStream(stream_id); | |
159 } | |
160 } | |
161 } | |
162 | |
163 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { | |
164 DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); | |
165 if (!AudioMessageFilter::Get()) | |
166 return; | |
167 AudioMessageFilter::Get()->OnStreamStateChanged( | |
168 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); | |
169 } | |
170 | |
171 } // namespace content | |
OLD | NEW |