Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(490)

Side by Side Diff: content/browser/renderer_host/media/media_devices_manager.h

Issue 2350693002: Remove device enumeration, caching and monitoring from MediaStreamManager. (Closed)
Patch Set: latest hta@ comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 file.
4
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_DEVICES_MANAGER_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_DEVICES_MANAGER_H_
7
8 #include <array>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/system_monitor/system_monitor.h"
15 #include "content/common/content_export.h"
16 #include "content/common/media/media_devices.h"
17 #include "media/capture/video/video_capture_device_descriptor.h"
18
19 namespace base {
20 class SingleThreadTaskRunner;
21 }
22
23 namespace media {
24 class AudioManager;
25 }
26
27 namespace content {
28
29 class MediaStreamManager;
30 class VideoCaptureManager;
31
32 // Use MediaDeviceType values to index on this type.
33 using MediaDeviceEnumeration =
34 std::array<MediaDeviceInfoArray, NUM_MEDIA_DEVICE_TYPES>;
35
36 // MediaDevicesManager is responsible for doing media-device enumerations.
37 // In addition it implements caching for enumeration results and device
38 // monitoring in order to keep caches consistent.
39 // All its methods must be called on the IO thread.
40 class CONTENT_EXPORT MediaDevicesManager
41 : public base::SystemMonitor::DevicesChangedObserver {
42 public:
43 // Use MediaDeviceType values to index on this type.
44 using BoolDeviceTypes = std::array<bool, NUM_MEDIA_DEVICE_TYPES>;
45
46 using EnumerationCallback =
47 base::Callback<void(const MediaDeviceEnumeration&)>;
48
49 MediaDevicesManager(
50 media::AudioManager* audio_manager,
51 const scoped_refptr<VideoCaptureManager>& video_capture_manager,
52 MediaStreamManager* media_stream_manager);
53 ~MediaDevicesManager() override;
54
55 // Performs a possibly cached device enumeration for the requested device
56 // types and reports the results to |callback|.
57 // The enumeration results passed to |callback| are guaranteed to be valid
58 // only for the types specified in |requested_types|.
59 // Note that this function is not reentrant, so if |callback| needs to perform
60 // another call to EnumerateDevices, it must do so by posting a task to the
61 // IO thread.
62 void EnumerateDevices(const BoolDeviceTypes& requested_types,
63 const EnumerationCallback& callback);
64
65 // Tries to start device monitoring. If successful, enables caching of
66 // enumeration results for the device types supported by the monitor.
67 void StartMonitoring();
68
69 // Stops device monitoring and disables caching for all device types.
70 void StopMonitoring();
71
72 // Returns true if device monitoring is active, false otherwise.
73 bool IsMonitoringStarted();
74
75 // Implements base::SystemMonitor::DevicesChangedObserver.
76 // This function is only called in response to physical audio/video device
77 // changes.
78 void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
79
80 // TODO(guidou): Remove this function once content::GetMediaDeviceIDForHMAC
81 // is rewritten to receive devices via a callback.
82 // See http://crbug.com/648155.
83 MediaDeviceInfoArray GetCachedDeviceInfo(MediaDeviceType type);
84
85 private:
86 friend class MediaDevicesManagerTest;
87 struct EnumerationRequest;
88
89 // The NO_CACHE policy is such that no previous results are used when
90 // EnumerateDevices is called. The results of a new or in-progress low-level
91 // device enumeration are used.
92 // The SYSTEM_MONITOR policy is such that previous results are re-used,
93 // provided they were produced by a low-level device enumeration issued after
94 // the last call to OnDevicesChanged.
95 enum class CachePolicy {
96 NO_CACHE,
97 SYSTEM_MONITOR,
98 };
99
100 // Manually sets a caching policy for a given device type.
101 void SetCachePolicy(MediaDeviceType type, CachePolicy policy);
102
103 // Helpers to issue low-level device enumerations.
104 void DoEnumerateDevices(MediaDeviceType type);
105 void EnumerateAudioDevices(bool is_input);
106
107 // Callback for VideoCaptureManager::EnumerateDevices.
108 void VideoInputDevicesEnumerated(
109 const media::VideoCaptureDeviceDescriptors& descriptors);
110
111 // Helpers to handle enumeration results.
112 void DevicesEnumerated(MediaDeviceType type,
113 const MediaDeviceInfoArray& snapshot);
114 void UpdateSnapshot(MediaDeviceType type,
115 const MediaDeviceInfoArray& new_snapshot);
116 void ProcessRequests();
117 bool IsEnumerationRequestReady(const EnumerationRequest& request_info);
118
119 // Helpers to handle device-change notification.
120 void HandleDevicesChanged(MediaDeviceType type);
121 void NotifyMediaStreamManager(MediaDeviceType type,
122 const MediaDeviceInfoArray& new_snapshot);
123 void NotifyDeviceChangeSubscribers(MediaDeviceType type,
124 const MediaDeviceInfoArray& snapshot);
125
126 #if defined(OS_MACOSX)
127 void StartMonitoringOnUIThread();
128 #endif
129
130 bool use_fake_devices_;
131 media::AudioManager* const audio_manager_; // not owned
132 scoped_refptr<VideoCaptureManager> video_capture_manager_;
133 MediaStreamManager* const media_stream_manager_; // not owned
134
135 using CachePolicies = std::array<CachePolicy, NUM_MEDIA_DEVICE_TYPES>;
136 CachePolicies cache_policies_;
137
138 class CacheInfo;
139 using CacheInfos = std::vector<CacheInfo>;
140 CacheInfos cache_infos_;
141
142 BoolDeviceTypes has_seen_result_;
143 std::vector<EnumerationRequest> requests_;
144 MediaDeviceEnumeration current_snapshot_;
145 bool monitoring_started_;
146
147 base::WeakPtrFactory<MediaDevicesManager> weak_factory_;
148
149 DISALLOW_COPY_AND_ASSIGN(MediaDevicesManager);
150 };
151
152 } // namespace content
153
154 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_MEDIA_DEVICES_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698