@@ -57,7 +57,7 @@ inline MediaDevices::MediaDevices(Document& document)
5757 : ActiveDOMObject(document)
5858 , m_scheduledEventTimer(*this , &MediaDevices::scheduledEventTimerFired)
5959 , m_eventNames(eventNames())
60- , m_idHashSalt (createCanonicalUUIDString())
60+ , m_groupIdHashSalt (createCanonicalUUIDString())
6161{
6262 suspendIfNeeded ();
6363
@@ -76,7 +76,6 @@ void MediaDevices::stop()
7676 if (controller)
7777 controller->removeDeviceChangeObserver (m_deviceChangeToken);
7878 }
79- m_devices.clear ();
8079 m_scheduledEventTimer.stop ();
8180}
8281
@@ -192,15 +191,16 @@ static inline MediaDeviceInfo::Kind toMediaDeviceInfoKind(CaptureDevice::DeviceT
192191 return MediaDeviceInfo::Kind::Audioinput;
193192}
194193
195- void MediaDevices::refreshDevices (const Vector<CaptureDevice>& newDevices)
194+ void MediaDevices::exposeDevices (const Vector<CaptureDevice>& newDevices, const String& deviceIDHashSalt, EnumerateDevicesPromise&& promise )
196195{
197- auto * document = this ->document ();
198- if (!document)
196+ if (isContextStopped ())
199197 return ;
200198
201- bool canAccessCamera = checkCameraAccess (*document);
202- bool canAccessMicrophone = checkMicrophoneAccess (*document);
203- bool canAccessSpeaker = checkSpeakerAccess (*document);
199+ auto & document = *this ->document ();
200+
201+ bool canAccessCamera = checkCameraAccess (document);
202+ bool canAccessMicrophone = checkMicrophoneAccess (document);
203+ bool canAccessSpeaker = checkSpeakerAccess (document);
204204
205205 Vector<Ref<MediaDeviceInfo>> devices;
206206 for (auto & newDevice : newDevices) {
@@ -211,19 +211,12 @@ void MediaDevices::refreshDevices(const Vector<CaptureDevice>& newDevices)
211211 if (!canAccessSpeaker && newDevice.type () == CaptureDevice::DeviceType::Speaker)
212212 continue ;
213213
214- auto deviceKind = toMediaDeviceInfoKind (newDevice.type ());
215- auto index = m_devices.findMatching ([deviceKind, &newDevice](auto & oldDevice) {
216- return oldDevice->deviceId () == newDevice.persistentId () && oldDevice->kind () == deviceKind;
217- });
218- if (index != notFound) {
219- devices.append (m_devices[index].copyRef ());
220- continue ;
221- }
214+ auto deviceId = RealtimeMediaSourceCenter::singleton ().hashStringWithSalt (newDevice.persistentId (), deviceIDHashSalt);
215+ auto groupId = RealtimeMediaSourceCenter::singleton ().hashStringWithSalt (newDevice.groupId (), m_groupIdHashSalt);
222216
223- auto groupId = RealtimeMediaSourceCenter::singleton ().hashStringWithSalt (newDevice.groupId (), m_idHashSalt);
224- devices.append (MediaDeviceInfo::create (newDevice.label (), newDevice.persistentId (), WTFMove (groupId), deviceKind));
217+ devices.append (MediaDeviceInfo::create (newDevice.label (), WTFMove (deviceId), WTFMove (groupId), toMediaDeviceInfoKind (newDevice.type ())));
225218 }
226- m_devices = WTFMove (devices);
219+ promise. resolve (devices);
227220}
228221
229222void MediaDevices::enumerateDevices (EnumerateDevicesPromise&& promise)
@@ -245,12 +238,9 @@ void MediaDevices::enumerateDevices(EnumerateDevicesPromise&& promise)
245238 }
246239
247240 controller->enumerateMediaDevices (*document, [this , weakThis = makeWeakPtr (this ), promise = WTFMove (promise)](const auto & newDevices, const auto & deviceIDHashSalt) mutable {
248- if (!weakThis || isContextStopped () )
241+ if (!weakThis)
249242 return ;
250-
251- this ->document ()->setDeviceIDHashSalt (deviceIDHashSalt);
252- refreshDevices (newDevices);
253- promise.resolve (m_devices);
243+ exposeDevices (newDevices, deviceIDHashSalt, WTFMove (promise));
254244 });
255245}
256246
0 commit comments