2 * Copyright (C) 2015 Ericsson AB. All rights reserved.
3 * Copyright (C) 2016-2018 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * 3. Neither the name of Ericsson nor the names of its contributors
16 * may be used to endorse or promote products derived from this
17 * software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #if ENABLE(MEDIA_STREAM)
36 #include "ActiveDOMObject.h"
37 #include "EventNames.h"
38 #include "EventTarget.h"
39 #include "ExceptionOr.h"
41 #include "MediaTrackConstraints.h"
42 #include "RealtimeMediaSourceCenter.h"
44 #include "UserMediaClient.h"
45 #include <wtf/WeakPtr.h>
50 class MediaDeviceInfo;
52 class UserGestureToken;
54 struct MediaTrackSupportedConstraints;
56 template<typename IDLType> class DOMPromiseDeferred;
58 class MediaDevices final : public RefCounted<MediaDevices>, public ActiveDOMObject, public EventTargetWithInlineData, public CanMakeWeakPtr<MediaDevices> {
59 WTF_MAKE_ISO_ALLOCATED(MediaDevices);
61 static Ref<MediaDevices> create(Document&);
65 Document* document() const;
67 using Promise = DOMPromiseDeferred<IDLInterface<MediaStream>>;
68 using EnumerateDevicesPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<MediaDeviceInfo>>>;
70 enum class DisplayCaptureSurfaceType {
77 struct StreamConstraints {
78 Variant<bool, MediaTrackConstraints> video;
79 Variant<bool, MediaTrackConstraints> audio;
81 void getUserMedia(const StreamConstraints&, Promise&&);
82 void getDisplayMedia(const StreamConstraints&, Promise&&);
83 void enumerateDevices(EnumerateDevicesPromise&&);
84 MediaTrackSupportedConstraints getSupportedConstraints();
86 using RefCounted<MediaDevices>::ref;
87 using RefCounted<MediaDevices>::deref;
90 explicit MediaDevices(Document&);
92 void scheduledEventTimerFired();
93 bool addEventListener(const AtomString& eventType, Ref<EventListener>&&, const AddEventListenerOptions&) override;
95 void refreshDevices(const Vector<CaptureDevice>&);
96 void listenForDeviceChanges();
98 friend class JSMediaDevicesOwner;
101 const char* activeDOMObjectName() const final;
103 bool hasPendingActivity() const final;
105 // EventTargetWithInlineData.
106 EventTargetInterface eventTargetInterface() const final { return MediaDevicesEventTargetInterfaceType; }
107 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
108 void refEventTarget() final { ref(); }
109 void derefEventTarget() final { deref(); }
111 enum class GestureAllowedRequest {
116 bool computeUserGesturePriviledge(GestureAllowedRequest);
118 Timer m_scheduledEventTimer;
119 UserMediaClient::DeviceChangeObserverToken m_deviceChangeToken;
120 const EventNames& m_eventNames; // Need to cache this so we can use it from GC threads.
121 bool m_listeningForDeviceChanges { false };
123 Vector<Ref<MediaDeviceInfo>> m_devices;
124 bool m_canAccessCamera { false };
125 bool m_canAccessMicrophone { false };
127 OptionSet<GestureAllowedRequest> m_requestTypesForCurrentGesture;
128 UserGestureToken* m_currentGestureToken { nullptr };
131 } // namespace WebCore
133 #endif // ENABLE(MEDIA_STREAM)