1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/mediastream/media_stream_device_observer.h"
#include <stddef.h>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "third_party/blink/public/platform/interface_registry.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/modules/mediastream/user_media_processor.h"
namespace blink {
namespace {
bool RemoveStreamDeviceFromArray(const MediaStreamDevice& device,
MediaStreamDevices* devices) {
for (auto device_it = devices->begin(); device_it != devices->end();
++device_it) {
if (device_it->IsSameDevice(device)) {
devices->erase(device_it);
return true;
}
}
return false;
}
} // namespace
MediaStreamDeviceObserver::MediaStreamDeviceObserver(LocalFrame* frame) {
// There is no frame on unit tests.
if (frame) {
frame->GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
&MediaStreamDeviceObserver::BindMediaStreamDeviceObserverReceiver,
weak_factory_.GetWeakPtr()));
}
}
MediaStreamDeviceObserver::~MediaStreamDeviceObserver() = default;
MediaStreamDevices MediaStreamDeviceObserver::GetNonScreenCaptureDevices() {
MediaStreamDevices video_devices;
for (const auto& stream_it : label_stream_map_) {
for (const auto& stream : stream_it.value) {
for (const auto& video_device : stream.video_devices) {
if (!IsScreenCaptureMediaType(video_device.type))
video_devices.push_back(video_device);
}
}
}
return video_devices;
}
void MediaStreamDeviceObserver::OnDeviceStopped(
const String& label,
const MediaStreamDevice& device) {
DVLOG(1) << __func__ << " label=" << label << " device_id=" << device.id;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
// This can happen if a user stops a device from JS at the same
// time as the underlying media device is unplugged from the system.
return;
}
for (Stream& stream : it->value) {
if (IsAudioInputMediaType(device.type)) {
RemoveStreamDeviceFromArray(device, &stream.audio_devices);
} else {
RemoveStreamDeviceFromArray(device, &stream.video_devices);
}
if (stream.on_device_stopped_cb) {
// Running `stream.on_device_stopped_cb` can destroy `this`. Use a weak
// pointer to detect that condition, and stop processing if it happens.
base::WeakPtr<MediaStreamDeviceObserver> weak_this =
weak_factory_.GetWeakPtr();
stream.on_device_stopped_cb.Run(device);
if (!weak_this) {
return;
}
}
}
// |it| could have already been invalidated in the function call above. So we
// need to check if |label| is still in |label_stream_map_| again.
// Note: this is a quick fix to the crash caused by erasing the invalidated
// iterator from |label_stream_map_| (https://crbug.com/616884). Future work
// needs to be done to resolve this re-entrancy issue.
it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
return;
}
auto to_remove = std::ranges::remove_if(it->value, [](const auto& t) {
return t.audio_devices.empty() && t.video_devices.empty();
});
it->value.erase(to_remove.begin(), to_remove.end());
if (it->value.empty())
label_stream_map_.erase(it);
}
void MediaStreamDeviceObserver::OnDeviceChanged(
const String& label,
const MediaStreamDevice& old_device,
const MediaStreamDevice& new_device) {
DVLOG(1) << __func__ << " old_device_id=" << old_device.id
<< " new_device_id=" << new_device.id;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
// This can happen if a user stops a device from JS at the same
// time as the underlying media device is unplugged from the system.
return;
}
// OnDeviceChanged cannot only happen in combination with getAllScreensMedia,
// which is the only API that handles multiple streams at once.
DCHECK_EQ(1u, it->value.size());
Stream* stream = &it->value[0];
if (stream->on_device_changed_cb) {
// Running `stream->on_device_changed_cb` can destroy `this`. Use a weak
// pointer to detect that condition, and stop processing if it happens.
base::WeakPtr<MediaStreamDeviceObserver> weak_this =
weak_factory_.GetWeakPtr();
stream->on_device_changed_cb.Run(old_device, new_device);
if (!weak_this) {
return;
}
}
// Update device list only for device changing. Removing device will be
// handled in its own callback.
if (old_device.type != mojom::MediaStreamType::NO_SERVICE &&
new_device.type != mojom::MediaStreamType::NO_SERVICE) {
if (RemoveStreamDeviceFromArray(old_device, &stream->audio_devices) ||
RemoveStreamDeviceFromArray(old_device, &stream->video_devices)) {
if (IsAudioInputMediaType(new_device.type))
stream->audio_devices.push_back(new_device);
else
stream->video_devices.push_back(new_device);
}
}
}
void MediaStreamDeviceObserver::OnDeviceRequestStateChange(
const String& label,
const MediaStreamDevice& device,
const mojom::blink::MediaStreamStateChange new_state) {
DVLOG(1) << __func__ << " label=" << label << " device_id=" << device.id;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
// This can happen if a user stops a device from JS at the same
// time as the underlying media device is unplugged from the system.
return;
}
for (Stream& stream : it->value) {
if (stream.ContainsDevice(device) &&
stream.on_device_request_state_change_cb) {
stream.on_device_request_state_change_cb.Run(device, new_state);
break;
}
}
}
void MediaStreamDeviceObserver::OnDeviceCaptureConfigurationChange(
const String& label,
const MediaStreamDevice& device) {
DVLOG(1) << __func__ << " label=" << label << " device_id=" << device.id;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
// This can happen if a user stops a device from JS at the same
// time as the underlying media device is unplugged from the system.
return;
}
for (Stream& stream : it->value) {
if (stream.ContainsDevice(device) &&
stream.on_device_capture_configuration_change_cb) {
stream.on_device_capture_configuration_change_cb.Run(device);
break;
}
}
}
void MediaStreamDeviceObserver::OnDeviceCaptureHandleChange(
const String& label,
const MediaStreamDevice& device) {
DVLOG(1) << __func__ << " label=" << label << " device_id=" << device.id;
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
// This can happen if a user stops a device from JS at the same
// time as the underlying media device is unplugged from the system.
return;
}
// OnDeviceCaptureHandleChange cannot only happen in combination with
// getAllScreensMedia, which is the only API that handles multiple streams
// at once.
DCHECK_EQ(1u, it->value.size());
Stream* stream = &it->value[0];
if (stream->on_device_capture_handle_change_cb) {
stream->on_device_capture_handle_change_cb.Run(device);
}
}
void MediaStreamDeviceObserver::OnZoomLevelChange(
const String& label,
const MediaStreamDevice& device,
int zoom_level) {
DVLOG(1) << __func__ << " label=" << label << " device_id=" << device.id;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
CHECK_GT(zoom_level, 0);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end()) {
return;
}
Vector<Stream>& streams = it->value;
if (streams.size() != 1u) {
return;
}
Stream* stream = &streams[0];
if (!stream) {
return;
}
if (stream->on_zoom_level_change_cb) {
stream->on_zoom_level_change_cb.Run(device, zoom_level);
}
#endif
}
void MediaStreamDeviceObserver::BindMediaStreamDeviceObserverReceiver(
mojo::PendingReceiver<mojom::blink::MediaStreamDeviceObserver> receiver) {
receiver_.reset();
receiver_.Bind(std::move(receiver));
}
void MediaStreamDeviceObserver::AddStreams(
const String& label,
const mojom::blink::StreamDevicesSet& stream_devices_set,
const WebMediaStreamDeviceObserver::StreamCallbacks& stream_callbacks) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
Vector<Stream> streams;
for (const mojom::blink::StreamDevicesPtr& stream_devices_ptr :
stream_devices_set.stream_devices) {
const mojom::blink::StreamDevices& stream_devices = *stream_devices_ptr;
Stream stream;
stream.on_device_stopped_cb = stream_callbacks.on_device_stopped_cb;
stream.on_device_changed_cb = stream_callbacks.on_device_changed_cb;
stream.on_device_request_state_change_cb =
stream_callbacks.on_device_request_state_change_cb;
stream.on_device_capture_configuration_change_cb =
stream_callbacks.on_device_capture_configuration_change_cb;
stream.on_device_capture_handle_change_cb =
stream_callbacks.on_device_capture_handle_change_cb;
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
stream.on_zoom_level_change_cb = stream_callbacks.on_zoom_level_change_cb;
#endif
if (stream_devices.audio_device.has_value()) {
stream.audio_devices.push_back(stream_devices.audio_device.value());
}
if (stream_devices.video_device.has_value()) {
stream.video_devices.push_back(stream_devices.video_device.value());
}
streams.emplace_back(std::move(stream));
}
label_stream_map_.Set(label, streams);
}
void MediaStreamDeviceObserver::AddStream(const String& label,
const MediaStreamDevice& device) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
Stream stream;
if (IsAudioInputMediaType(device.type)) {
stream.audio_devices.push_back(device);
} else if (IsVideoInputMediaType(device.type)) {
stream.video_devices.push_back(device);
} else {
NOTREACHED();
}
label_stream_map_.Set(label, Vector<Stream>{std::move(stream)});
}
bool MediaStreamDeviceObserver::RemoveStreams(const String& label) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end())
return false;
label_stream_map_.erase(it);
return true;
}
void MediaStreamDeviceObserver::RemoveStreamDevice(
const MediaStreamDevice& device) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
// Remove |device| from all streams in |label_stream_map_|.
bool device_found = false;
Vector<String> streams_to_remove;
for (auto& entry : label_stream_map_) {
for (auto& stream : entry.value) {
if (RemoveStreamDeviceFromArray(device, &stream.audio_devices) ||
RemoveStreamDeviceFromArray(device, &stream.video_devices)) {
device_found = true;
}
}
auto to_remove = std::ranges::remove_if(entry.value, [](const auto& t) {
return t.audio_devices.empty() && t.video_devices.empty();
});
entry.value.erase(to_remove.begin(), to_remove.end());
if (device_found && entry.value.size() == 0) {
streams_to_remove.push_back(entry.key);
}
}
for (const String& label : streams_to_remove) {
label_stream_map_.erase(label);
}
}
base::UnguessableToken MediaStreamDeviceObserver::GetAudioSessionId(
const String& label) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end() || it->value.empty() ||
it->value[0].audio_devices.empty())
return base::UnguessableToken();
// It is assumed that all devices belong to the same request and
// therefore have the same session id.
return it->value[0].audio_devices[0].session_id();
}
base::UnguessableToken MediaStreamDeviceObserver::GetVideoSessionId(
const String& label) {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
auto it = label_stream_map_.find(label);
if (it == label_stream_map_.end() || it->value.empty() ||
it->value[0].video_devices.empty())
return base::UnguessableToken();
// It is assumed that all devices belong to the same request and
// therefore have the same session id.
return it->value[0].video_devices[0].session_id();
}
bool MediaStreamDeviceObserver::Stream::ContainsDevice(
const MediaStreamDevice& device) const {
for (blink::MediaStreamDevice stream_device : audio_devices) {
if (device.IsSameDevice(stream_device)) {
return true;
}
}
for (blink::MediaStreamDevice stream_device : video_devices) {
if (device.IsSameDevice(stream_device)) {
return true;
}
}
return false;
}
} // namespace blink
|