1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_dom_MediaDevices_h
6 #define mozilla_dom_MediaDevices_h
8 #include "MediaEventSource.h"
9 #include "js/RootingAPI.h"
10 #include "mozilla/AlreadyAddRefed.h"
11 #include "mozilla/DOMEventTargetHelper.h"
12 #include "mozilla/UseCounter.h"
13 #include "mozilla/dom/BindingDeclarations.h"
14 #include "mozilla/dom/MediaDeviceInfoBinding.h"
17 #include "nsISupports.h"
18 #include "nsTHashSet.h"
20 class AudioDeviceInfo
;
24 class LocalMediaDevice
;
28 template <typename ResolveValueT
, typename RejectValueT
, bool IsExclusive
>
39 struct MediaStreamConstraints
;
40 struct DisplayMediaStreamConstraints
;
41 struct MediaTrackSupportedConstraints
;
42 struct AudioOutputOptions
;
44 class MediaDevices final
: public DOMEventTargetHelper
{
47 MozPromise
<RefPtr
<DOMMediaStream
>, RefPtr
<MediaMgrError
>, true>;
48 using SinkInfoPromise
= MozPromise
<RefPtr
<AudioDeviceInfo
>, nsresult
, true>;
50 explicit MediaDevices(nsPIDOMWindowInner
* aWindow
);
52 NS_DECL_ISUPPORTS_INHERITED
53 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaDevices
, DOMEventTargetHelper
)
55 JSObject
* WrapObject(JSContext
* cx
,
56 JS::Handle
<JSObject
*> aGivenProto
) override
;
58 // No code needed, as MediaTrackSupportedConstraints members default to true.
59 void GetSupportedConstraints(MediaTrackSupportedConstraints
& aResult
){};
61 already_AddRefed
<Promise
> GetUserMedia(
62 const MediaStreamConstraints
& aConstraints
, CallerType aCallerType
,
65 RefPtr
<StreamPromise
> GetUserMedia(nsPIDOMWindowInner
* aWindow
,
66 const MediaStreamConstraints
& aConstraints
,
67 CallerType aCallerType
);
69 already_AddRefed
<Promise
> EnumerateDevices(ErrorResult
& aRv
);
71 already_AddRefed
<Promise
> GetDisplayMedia(
72 const DisplayMediaStreamConstraints
& aConstraints
, CallerType aCallerType
,
75 already_AddRefed
<Promise
> SelectAudioOutput(
76 const AudioOutputOptions
& aOptions
, CallerType aCallerType
,
79 // Get the sink that corresponds to the given device id.
80 // The returned promise will be resolved with the device
81 // information if the aDeviceId matches a device that would be exposed by
82 // enumerateDevices().
83 // The promise will be rejected with NS_ERROR_NOT_AVAILABLE if aDeviceId
84 // does not match any exposed device.
85 RefPtr
<SinkInfoPromise
> GetSinkDevice(const nsString
& aDeviceId
);
87 // Called when MediaManager encountered a change in its device lists.
88 void OnDeviceChange();
90 void SetupDeviceChangeListener();
92 mozilla::dom::EventHandlerNonNull
* GetOndevicechange();
93 void SetOndevicechange(mozilla::dom::EventHandlerNonNull
* aCallback
);
95 void EventListenerAdded(nsAtom
* aType
) override
;
96 using DOMEventTargetHelper::EventListenerAdded
;
98 void BackgroundStateChanged() { MaybeResumeDeviceExposure(); }
99 void WindowResumed() { MaybeResumeDeviceExposure(); }
100 void BrowserWindowBecameActive() { MaybeResumeDeviceExposure(); }
103 using MediaDeviceSet
= nsTArray
<RefPtr
<MediaDevice
>>;
104 using MediaDeviceSetRefCnt
= media::Refcountable
<MediaDeviceSet
>;
105 using LocalMediaDeviceSet
= nsTArray
<RefPtr
<LocalMediaDevice
>>;
107 virtual ~MediaDevices();
108 void MaybeResumeDeviceExposure();
109 void ResumeEnumerateDevices(
110 nsTArray
<RefPtr
<Promise
>>&& aPromises
,
111 RefPtr
<const MediaDeviceSetRefCnt
> aExposedDevices
) const;
112 RefPtr
<MediaDeviceSetRefCnt
> FilterExposedDevices(
113 const MediaDeviceSet
& aDevices
) const;
114 bool CanExposeInfo(MediaDeviceKind aKind
) const;
115 bool ShouldQueueDeviceChange(const MediaDeviceSet
& aExposedDevices
) const;
116 void ResolveEnumerateDevicesPromise(
117 Promise
* aPromise
, const LocalMediaDeviceSet
& aDevices
) const;
119 nsTHashSet
<nsString
> mExplicitlyGrantedAudioOutputRawIds
;
120 nsTArray
<RefPtr
<Promise
>> mPendingEnumerateDevicesPromises
;
121 // Set only once, if and when required.
122 mutable nsString mDefaultOutputLabel
;
124 // Connect/Disconnect on main thread only
125 MediaEventListener mDeviceChangeListener
;
126 // Ordered set of the system physical devices when devicechange event
127 // decisions were last performed.
128 RefPtr
<const MediaDeviceSetRefCnt
> mLastPhysicalDevices
;
129 bool mIsDeviceChangeListenerSetUp
= false;
130 bool mHaveUnprocessedDeviceListChange
= false;
131 bool mCanExposeMicrophoneInfo
= false;
132 bool mCanExposeCameraInfo
= false;
134 void RecordAccessTelemetry(const UseCounter counter
) const;
138 } // namespace mozilla
140 #endif // mozilla_dom_MediaDevices_h