media_capture_devices_dispatcher.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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
  3. // found in the LICENSE-CHROMIUM file.
  4. #include "shell/browser/media/media_capture_devices_dispatcher.h"
  5. #include "components/webrtc/media_stream_devices_util.h"
  6. #include "content/public/browser/browser_thread.h"
  7. #include "content/public/browser/media_capture_devices.h"
  8. using content::BrowserThread;
  9. namespace electron {
  10. MediaCaptureDevicesDispatcher* MediaCaptureDevicesDispatcher::GetInstance() {
  11. static base::NoDestructor<MediaCaptureDevicesDispatcher> instance;
  12. return instance.get();
  13. }
  14. MediaCaptureDevicesDispatcher::MediaCaptureDevicesDispatcher() {
  15. // MediaCaptureDevicesDispatcher is a singleton. It should be created on
  16. // UI thread.
  17. DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  18. }
  19. MediaCaptureDevicesDispatcher::~MediaCaptureDevicesDispatcher() = default;
  20. const std::optional<blink::MediaStreamDevice>
  21. MediaCaptureDevicesDispatcher::GetPreferredAudioDeviceForBrowserContext(
  22. content::BrowserContext* browser_context,
  23. const std::vector<std::string>& eligible_audio_device_ids) const {
  24. auto audio_devices = GetAudioCaptureDevices();
  25. if (!eligible_audio_device_ids.empty()) {
  26. audio_devices =
  27. webrtc::FilterMediaDevices(audio_devices, eligible_audio_device_ids);
  28. }
  29. if (audio_devices.empty())
  30. return std::nullopt;
  31. return audio_devices.front();
  32. }
  33. const std::optional<blink::MediaStreamDevice>
  34. MediaCaptureDevicesDispatcher::GetPreferredVideoDeviceForBrowserContext(
  35. content::BrowserContext* browser_context,
  36. const std::vector<std::string>& eligible_video_device_ids) const {
  37. auto video_devices = GetVideoCaptureDevices();
  38. if (!eligible_video_device_ids.empty()) {
  39. video_devices =
  40. webrtc::FilterMediaDevices(video_devices, eligible_video_device_ids);
  41. }
  42. if (video_devices.empty())
  43. return std::nullopt;
  44. return video_devices.front();
  45. }
  46. } // namespace electron