| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/media/media_stream_dispatcher.h" | 5 #include "content/renderer/media/media_stream_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "content/common/media/media_stream_messages.h" | 10 #include "content/common/media/media_stream_messages.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; | 61 base::WeakPtr<MediaStreamDispatcherEventHandler> handler; |
| 62 StreamDeviceInfoArray audio_array; | 62 StreamDeviceInfoArray audio_array; |
| 63 StreamDeviceInfoArray video_array; | 63 StreamDeviceInfoArray video_array; |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 MediaStreamDispatcher::MediaStreamDispatcher(RenderFrame* render_frame) | 66 MediaStreamDispatcher::MediaStreamDispatcher(RenderFrame* render_frame) |
| 67 : RenderFrameObserver(render_frame), | 67 : RenderFrameObserver(render_frame), |
| 68 next_ipc_id_(0) { | 68 next_ipc_id_(0) { |
| 69 } | 69 } |
| 70 | 70 |
| 71 MediaStreamDispatcher::~MediaStreamDispatcher() { | 71 MediaStreamDispatcher::~MediaStreamDispatcher() {} |
| 72 DCHECK(device_change_subscribers_.empty()); | |
| 73 } | |
| 74 | 72 |
| 75 void MediaStreamDispatcher::GenerateStream( | 73 void MediaStreamDispatcher::GenerateStream( |
| 76 int request_id, | 74 int request_id, |
| 77 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 75 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
| 78 const StreamControls& controls, | 76 const StreamControls& controls, |
| 79 const url::Origin& security_origin) { | 77 const url::Origin& security_origin) { |
| 80 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
| 81 DVLOG(1) << "MediaStreamDispatcher::GenerateStream(" << request_id << ")"; | 79 DVLOG(1) << "MediaStreamDispatcher::GenerateStream(" << request_id << ")"; |
| 82 | 80 |
| 83 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); | 81 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 } | 124 } |
| 127 } | 125 } |
| 128 ++stream_it; | 126 ++stream_it; |
| 129 } | 127 } |
| 130 DCHECK(device_found); | 128 DCHECK(device_found); |
| 131 | 129 |
| 132 Send(new MediaStreamHostMsg_StopStreamDevice(routing_id(), | 130 Send(new MediaStreamHostMsg_StopStreamDevice(routing_id(), |
| 133 device_info.device.id)); | 131 device_info.device.id)); |
| 134 } | 132 } |
| 135 | 133 |
| 136 void MediaStreamDispatcher::EnumerateDevices( | |
| 137 int request_id, | |
| 138 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | |
| 139 MediaStreamType type, | |
| 140 const url::Origin& security_origin) { | |
| 141 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 142 DCHECK(type == MEDIA_DEVICE_AUDIO_CAPTURE || | |
| 143 type == MEDIA_DEVICE_VIDEO_CAPTURE || | |
| 144 type == MEDIA_DEVICE_AUDIO_OUTPUT); | |
| 145 DVLOG(1) << "MediaStreamDispatcher::EnumerateDevices(" | |
| 146 << request_id << ")"; | |
| 147 | |
| 148 for (RequestList::iterator it = requests_.begin(); it != requests_.end(); | |
| 149 ++it) { | |
| 150 DCHECK(!it->IsThisRequest(request_id, event_handler)); | |
| 151 } | |
| 152 | |
| 153 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); | |
| 154 Send(new MediaStreamHostMsg_EnumerateDevices(routing_id(), | |
| 155 next_ipc_id_++, | |
| 156 type, | |
| 157 security_origin)); | |
| 158 } | |
| 159 | |
| 160 void MediaStreamDispatcher::StopEnumerateDevices( | |
| 161 int request_id, | |
| 162 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) { | |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 164 DVLOG(1) << "MediaStreamDispatcher::StopEnumerateDevices(" | |
| 165 << request_id << ")"; | |
| 166 for (RequestList::iterator it = requests_.begin(); it != requests_.end(); | |
| 167 ++it) { | |
| 168 if (it->IsThisRequest(request_id, event_handler)) { | |
| 169 Send(new MediaStreamHostMsg_CancelEnumerateDevices(routing_id(), | |
| 170 it->ipc_request)); | |
| 171 requests_.erase(it); | |
| 172 break; | |
| 173 } | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 void MediaStreamDispatcher::OpenDevice( | 134 void MediaStreamDispatcher::OpenDevice( |
| 178 int request_id, | 135 int request_id, |
| 179 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | 136 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, |
| 180 const std::string& device_id, | 137 const std::string& device_id, |
| 181 MediaStreamType type, | 138 MediaStreamType type, |
| 182 const url::Origin& security_origin) { | 139 const url::Origin& security_origin) { |
| 183 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 184 DVLOG(1) << "MediaStreamDispatcher::OpenDevice(" << request_id << ")"; | 141 DVLOG(1) << "MediaStreamDispatcher::OpenDevice(" << request_id << ")"; |
| 185 | 142 |
| 186 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); | 143 requests_.push_back(Request(event_handler, request_id, next_ipc_id_)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 204 << ", {label = " << label << "}"; | 161 << ", {label = " << label << "}"; |
| 205 | 162 |
| 206 LabelStreamMap::iterator it = label_stream_map_.find(label); | 163 LabelStreamMap::iterator it = label_stream_map_.find(label); |
| 207 if (it == label_stream_map_.end()) | 164 if (it == label_stream_map_.end()) |
| 208 return; | 165 return; |
| 209 label_stream_map_.erase(it); | 166 label_stream_map_.erase(it); |
| 210 | 167 |
| 211 Send(new MediaStreamHostMsg_CloseDevice(routing_id(), label)); | 168 Send(new MediaStreamHostMsg_CloseDevice(routing_id(), label)); |
| 212 } | 169 } |
| 213 | 170 |
| 214 void MediaStreamDispatcher::SubscribeToDeviceChangeNotifications( | |
| 215 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler, | |
| 216 const url::Origin& security_origin) { | |
| 217 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 218 DCHECK(std::find_if( | |
| 219 device_change_subscribers_.begin(), | |
| 220 device_change_subscribers_.end(), | |
| 221 [&event_handler]( | |
| 222 const base::WeakPtr<MediaStreamDispatcherEventHandler>& item) { | |
| 223 return event_handler.get() == item.get(); | |
| 224 }) == device_change_subscribers_.end()); | |
| 225 DVLOG(1) << "MediaStreamDispatcher::SubscribeToDeviceChangeNotifications"; | |
| 226 | |
| 227 if (device_change_subscribers_.empty()) { | |
| 228 Send(new MediaStreamHostMsg_SubscribeToDeviceChangeNotifications( | |
| 229 routing_id(), security_origin)); | |
| 230 } | |
| 231 device_change_subscribers_.push_back(event_handler); | |
| 232 } | |
| 233 | |
| 234 void MediaStreamDispatcher::CancelDeviceChangeNotifications( | |
| 235 const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) { | |
| 236 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 237 DVLOG(1) << "MediaStreamDispatcher::SubscribeToDeviceChangeNotifications"; | |
| 238 auto it = std::find_if( | |
| 239 device_change_subscribers_.begin(), device_change_subscribers_.end(), | |
| 240 [&event_handler]( | |
| 241 const base::WeakPtr<MediaStreamDispatcherEventHandler>& item) { | |
| 242 return event_handler.get() == item.get(); | |
| 243 }); | |
| 244 CHECK(it != device_change_subscribers_.end()); | |
| 245 device_change_subscribers_.erase(it); | |
| 246 | |
| 247 if (device_change_subscribers_.empty()) | |
| 248 Send(new MediaStreamHostMsg_CancelDeviceChangeNotifications(routing_id())); | |
| 249 } | |
| 250 | |
| 251 void MediaStreamDispatcher::OnDestruct() { | 171 void MediaStreamDispatcher::OnDestruct() { |
| 252 // Do not self-destruct. UserMediaClientImpl owns |this|. | 172 // Do not self-destruct. UserMediaClientImpl owns |this|. |
| 253 } | 173 } |
| 254 | 174 |
| 255 bool MediaStreamDispatcher::Send(IPC::Message* message) { | 175 bool MediaStreamDispatcher::Send(IPC::Message* message) { |
| 256 if (!RenderThread::Get()) { | 176 if (!RenderThread::Get()) { |
| 257 delete message; | 177 delete message; |
| 258 return false; | 178 return false; |
| 259 } | 179 } |
| 260 | 180 |
| 261 return RenderThread::Get()->Send(message); | 181 return RenderThread::Get()->Send(message); |
| 262 } | 182 } |
| 263 | 183 |
| 264 bool MediaStreamDispatcher::OnMessageReceived(const IPC::Message& message) { | 184 bool MediaStreamDispatcher::OnMessageReceived(const IPC::Message& message) { |
| 265 bool handled = true; | 185 bool handled = true; |
| 266 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcher, message) | 186 IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcher, message) |
| 267 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, | 187 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, |
| 268 OnStreamGenerated) | 188 OnStreamGenerated) |
| 269 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, | 189 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, |
| 270 OnStreamGenerationFailed) | 190 OnStreamGenerationFailed) |
| 271 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, | 191 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, |
| 272 OnDeviceStopped) | 192 OnDeviceStopped) |
| 273 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, | |
| 274 OnDevicesEnumerated) | |
| 275 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, | 193 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, |
| 276 OnDeviceOpened) | 194 OnDeviceOpened) |
| 277 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpenFailed, | 195 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpenFailed, |
| 278 OnDeviceOpenFailed) | 196 OnDeviceOpenFailed) |
| 279 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesChanged, OnDevicesChanged) | |
| 280 IPC_MESSAGE_UNHANDLED(handled = false) | 197 IPC_MESSAGE_UNHANDLED(handled = false) |
| 281 IPC_END_MESSAGE_MAP() | 198 IPC_END_MESSAGE_MAP() |
| 282 return handled; | 199 return handled; |
| 283 } | 200 } |
| 284 | 201 |
| 285 void MediaStreamDispatcher::OnStreamGenerated( | 202 void MediaStreamDispatcher::OnStreamGenerated( |
| 286 int request_id, | 203 int request_id, |
| 287 const std::string& label, | 204 const std::string& label, |
| 288 const StreamDeviceInfoArray& audio_array, | 205 const StreamDeviceInfoArray& audio_array, |
| 289 const StreamDeviceInfoArray& video_array) { | 206 const StreamDeviceInfoArray& video_array) { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 // iterator from |label_stream_map_| (crbug.com/616884). Future work needs to | 275 // iterator from |label_stream_map_| (crbug.com/616884). Future work needs to |
| 359 // be done to resolve this re-entrancy issue. | 276 // be done to resolve this re-entrancy issue. |
| 360 it = label_stream_map_.find(label); | 277 it = label_stream_map_.find(label); |
| 361 if (it == label_stream_map_.end()) | 278 if (it == label_stream_map_.end()) |
| 362 return; | 279 return; |
| 363 stream = &it->second; | 280 stream = &it->second; |
| 364 if (stream->audio_array.empty() && stream->video_array.empty()) | 281 if (stream->audio_array.empty() && stream->video_array.empty()) |
| 365 label_stream_map_.erase(it); | 282 label_stream_map_.erase(it); |
| 366 } | 283 } |
| 367 | 284 |
| 368 void MediaStreamDispatcher::OnDevicesEnumerated( | |
| 369 int request_id, | |
| 370 const StreamDeviceInfoArray& device_array) { | |
| 371 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 372 DCHECK_GE(request_id, 0); | |
| 373 | |
| 374 for (RequestList::iterator it = requests_.begin(); it != requests_.end(); | |
| 375 ++it) { | |
| 376 if (it->ipc_request == request_id && it->handler.get()) { | |
| 377 it->handler->OnDevicesEnumerated(it->request_id, device_array); | |
| 378 break; | |
| 379 } | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 void MediaStreamDispatcher::OnDeviceOpened( | 285 void MediaStreamDispatcher::OnDeviceOpened( |
| 384 int request_id, | 286 int request_id, |
| 385 const std::string& label, | 287 const std::string& label, |
| 386 const StreamDeviceInfo& device_info) { | 288 const StreamDeviceInfo& device_info) { |
| 387 DCHECK(thread_checker_.CalledOnValidThread()); | 289 DCHECK(thread_checker_.CalledOnValidThread()); |
| 388 for (RequestList::iterator it = requests_.begin(); | 290 for (RequestList::iterator it = requests_.begin(); |
| 389 it != requests_.end(); ++it) { | 291 it != requests_.end(); ++it) { |
| 390 Request& request = *it; | 292 Request& request = *it; |
| 391 if (request.ipc_request == request_id) { | 293 if (request.ipc_request == request_id) { |
| 392 Stream new_stream; | 294 Stream new_stream; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 420 request.handler->OnDeviceOpenFailed(request.request_id); | 322 request.handler->OnDeviceOpenFailed(request.request_id); |
| 421 DVLOG(1) << "MediaStreamDispatcher::OnDeviceOpenFailed(" | 323 DVLOG(1) << "MediaStreamDispatcher::OnDeviceOpenFailed(" |
| 422 << request.request_id << ")\n"; | 324 << request.request_id << ")\n"; |
| 423 } | 325 } |
| 424 requests_.erase(it); | 326 requests_.erase(it); |
| 425 break; | 327 break; |
| 426 } | 328 } |
| 427 } | 329 } |
| 428 } | 330 } |
| 429 | 331 |
| 430 void MediaStreamDispatcher::OnDevicesChanged() { | |
| 431 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 432 for (auto event_handler : device_change_subscribers_) { | |
| 433 DCHECK(event_handler); | |
| 434 event_handler->OnDevicesChanged(); | |
| 435 } | |
| 436 } | |
| 437 | |
| 438 int MediaStreamDispatcher::audio_session_id(const std::string& label, | 332 int MediaStreamDispatcher::audio_session_id(const std::string& label, |
| 439 int index) { | 333 int index) { |
| 440 DCHECK(thread_checker_.CalledOnValidThread()); | 334 DCHECK(thread_checker_.CalledOnValidThread()); |
| 441 LabelStreamMap::iterator it = label_stream_map_.find(label); | 335 LabelStreamMap::iterator it = label_stream_map_.find(label); |
| 442 if (it == label_stream_map_.end() || | 336 if (it == label_stream_map_.end() || |
| 443 it->second.audio_array.size() <= static_cast<size_t>(index)) { | 337 it->second.audio_array.size() <= static_cast<size_t>(index)) { |
| 444 return StreamDeviceInfo::kNoId; | 338 return StreamDeviceInfo::kNoId; |
| 445 } | 339 } |
| 446 return it->second.audio_array[index].session_id; | 340 return it->second.audio_array[index].session_id; |
| 447 } | 341 } |
| 448 | 342 |
| 449 bool MediaStreamDispatcher::IsStream(const std::string& label) { | 343 bool MediaStreamDispatcher::IsStream(const std::string& label) { |
| 450 DCHECK(thread_checker_.CalledOnValidThread()); | 344 DCHECK(thread_checker_.CalledOnValidThread()); |
| 451 return label_stream_map_.find(label) != label_stream_map_.end(); | 345 return label_stream_map_.find(label) != label_stream_map_.end(); |
| 452 } | 346 } |
| 453 | 347 |
| 454 int MediaStreamDispatcher::video_session_id(const std::string& label, | 348 int MediaStreamDispatcher::video_session_id(const std::string& label, |
| 455 int index) { | 349 int index) { |
| 456 DCHECK(thread_checker_.CalledOnValidThread()); | 350 DCHECK(thread_checker_.CalledOnValidThread()); |
| 457 LabelStreamMap::iterator it = label_stream_map_.find(label); | 351 LabelStreamMap::iterator it = label_stream_map_.find(label); |
| 458 if (it == label_stream_map_.end() || | 352 if (it == label_stream_map_.end() || |
| 459 it->second.video_array.size() <= static_cast<size_t>(index)) { | 353 it->second.video_array.size() <= static_cast<size_t>(index)) { |
| 460 return StreamDeviceInfo::kNoId; | 354 return StreamDeviceInfo::kNoId; |
| 461 } | 355 } |
| 462 return it->second.video_array[index].session_id; | 356 return it->second.video_array[index].session_id; |
| 463 } | 357 } |
| 464 | 358 |
| 465 } // namespace content | 359 } // namespace content |
| OLD | NEW |