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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_MEDIA_EFFECTS_MEDIA_DEVICE_INFO_H_
#define COMPONENTS_MEDIA_EFFECTS_MEDIA_DEVICE_INFO_H_
#include "base/auto_reset.h"
#include "base/system/system_monitor.h"
#include "media/audio/audio_device_description.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/audio/public/mojom/system_info.mojom.h"
#include "services/video_capture/public/mojom/video_source_provider.mojom.h"
namespace base {
template <typename T>
class NoDestructor;
}
namespace media_effects {
// Get the id of the real default device if present in the passed `infos`,
// otherwise return nullopt.
std::optional<std::string> GetRealDefaultDeviceId(
const std::vector<media::AudioDeviceDescription>& infos);
// Get the id of the real communications device if present in the passed
// `infos`, otherwise return nullopt. Only relevant on Windows.
std::optional<std::string> GetRealCommunicationsDeviceId(
const std::vector<media::AudioDeviceDescription>& infos);
// Returns a list of the real mics names by excluding virtual devices such as
// default.
std::vector<std::string> GetRealAudioDeviceNames(
const std::vector<media::AudioDeviceDescription>& infos);
// Returns a list of the cameras names.
std::vector<std::string> GetRealVideoDeviceNames(
const std::vector<media::VideoCaptureDeviceInfo>& infos);
// This class manages a cache of device infos for currently connected audio and
// video capture devices. It is similar to `MediaCaptureDevicesImpl` from
// content, but it holds the media::types instead of blink::MediaStreamDevice.
// It allows usages within chrome to access the full device information.
class MediaDeviceInfo : public base::SystemMonitor::DevicesChangedObserver {
public:
class Observer : public base::CheckedObserver {
public:
// If the cache hasn't yet received the device list from the service,
// `device_infos` will be nullopt.
virtual void OnAudioDevicesChanged(
const std::optional<std::vector<media::AudioDeviceDescription>>&
device_infos) {}
// If the cache hasn't yet received the device list from the service,
// `device_infos` will be nullopt.
virtual void OnVideoDevicesChanged(
const std::optional<std::vector<media::VideoCaptureDeviceInfo>>&
device_infos) {}
};
~MediaDeviceInfo() override;
static MediaDeviceInfo* GetInstance();
// Create a new instance and use it to override the singleton for testing.
// This allows tests to be hermetic by creating a new cache in SetUp.
static std::pair<std::unique_ptr<MediaDeviceInfo>,
base::AutoReset<MediaDeviceInfo*>>
OverrideInstanceForTesting();
// Get the cached device infos synchronously. If the cache hasn't yet received
// the device list from the service, this will return nullopt.
std::optional<media::AudioDeviceDescription> GetAudioDeviceInfoForId(
const std::string& device_id) const;
std::optional<media::VideoCaptureDeviceInfo> GetVideoDeviceInfoForId(
const std::string& device_id) const;
const std::optional<std::vector<media::AudioDeviceDescription>>&
GetAudioDeviceInfos() const;
const std::optional<std::vector<media::VideoCaptureDeviceInfo>>&
GetVideoDeviceInfos() const;
// Used to get mic format info (e.g. sample rate).
void GetAudioInputStreamParameters(
const std::string& device_id,
audio::mojom::SystemInfo::GetInputStreamParametersCallback callback);
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
private:
// Allow `NoDestructor` to use the private constructor.
friend class base::NoDestructor<MediaDeviceInfo>;
MediaDeviceInfo();
// base::SystemMonitor::DevicesChangedObserver implementation
void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
void OnAudioDeviceDescriptionsReceived(
const std::vector<media::AudioDeviceDescription> device_infos);
void OnVideoDeviceInfosReceived(
const std::vector<media::VideoCaptureDeviceInfo>& device_infos);
void NotifyAudioDevicesChanged();
void NotifyVideoDevicesChanged();
// Mojo remotes for getting device infos.
mojo::Remote<video_capture::mojom::VideoSourceProvider>
video_source_provider_;
mojo::Remote<audio::mojom::SystemInfo> audio_system_info_;
// Cached vector of device infos. This is updated on every device change.
std::optional<std::vector<media::VideoCaptureDeviceInfo>> video_device_infos_;
// Cached vector of device infos. This is updated on every device change.
std::optional<std::vector<media::AudioDeviceDescription>> audio_device_infos_;
base::ObserverList<Observer> observers_;
};
} // namespace media_effects
#endif // COMPONENTS_MEDIA_EFFECTS_MEDIA_DEVICE_INFO_H_
|