1 | /* |
2 | * Copyright (C) 2015 Ericsson AB. All rights reserved. |
3 | * Copyright (C) 2016-2018 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
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 |
14 | * distribution. |
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. |
18 | * |
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. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #if ENABLE(MEDIA_STREAM) |
35 | |
36 | #include "ActiveDOMObject.h" |
37 | #include "EventNames.h" |
38 | #include "EventTarget.h" |
39 | #include "ExceptionOr.h" |
40 | #include "JSDOMPromiseDeferred.h" |
41 | #include "MediaTrackConstraints.h" |
42 | #include "RealtimeMediaSourceCenter.h" |
43 | #include "Timer.h" |
44 | #include "UserMediaClient.h" |
45 | #include <wtf/WeakPtr.h> |
46 | |
47 | namespace WebCore { |
48 | |
49 | class Document; |
50 | class MediaDeviceInfo; |
51 | class MediaStream; |
52 | |
53 | struct MediaTrackSupportedConstraints; |
54 | |
55 | class MediaDevices final : public RefCounted<MediaDevices>, public ActiveDOMObject, public EventTargetWithInlineData, public CanMakeWeakPtr<MediaDevices> { |
56 | WTF_MAKE_ISO_ALLOCATED(MediaDevices); |
57 | public: |
58 | static Ref<MediaDevices> create(Document&); |
59 | |
60 | ~MediaDevices(); |
61 | |
62 | Document* document() const; |
63 | |
64 | using Promise = DOMPromiseDeferred<IDLInterface<MediaStream>>; |
65 | using EnumerateDevicesPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<MediaDeviceInfo>>>; |
66 | |
67 | enum class DisplayCaptureSurfaceType { |
68 | Monitor, |
69 | Window, |
70 | Application, |
71 | Browser, |
72 | }; |
73 | |
74 | struct StreamConstraints { |
75 | Variant<bool, MediaTrackConstraints> video; |
76 | Variant<bool, MediaTrackConstraints> audio; |
77 | }; |
78 | void getUserMedia(const StreamConstraints&, Promise&&) const; |
79 | void getDisplayMedia(const StreamConstraints&, Promise&&) const; |
80 | void enumerateDevices(EnumerateDevicesPromise&&) const; |
81 | MediaTrackSupportedConstraints getSupportedConstraints(); |
82 | |
83 | using RefCounted<MediaDevices>::ref; |
84 | using RefCounted<MediaDevices>::deref; |
85 | |
86 | void setDisableGetDisplayMediaUserGestureConstraint(bool value) { m_disableGetDisplayMediaUserGestureConstraint = value; } |
87 | |
88 | private: |
89 | explicit MediaDevices(Document&); |
90 | |
91 | void scheduledEventTimerFired(); |
92 | bool addEventListener(const AtomicString& eventType, Ref<EventListener>&&, const AddEventListenerOptions&) override; |
93 | |
94 | friend class JSMediaDevicesOwner; |
95 | |
96 | // ActiveDOMObject |
97 | const char* activeDOMObjectName() const final; |
98 | bool canSuspendForDocumentSuspension() const final; |
99 | void stop() final; |
100 | bool hasPendingActivity() const final; |
101 | |
102 | // EventTargetWithInlineData. |
103 | EventTargetInterface eventTargetInterface() const final { return MediaDevicesEventTargetInterfaceType; } |
104 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
105 | void refEventTarget() final { ref(); } |
106 | void derefEventTarget() final { deref(); } |
107 | |
108 | Timer m_scheduledEventTimer; |
109 | UserMediaClient::DeviceChangeObserverToken m_deviceChangeToken; |
110 | const EventNames& m_eventNames; // Need to cache this so we can use it from GC threads. |
111 | bool m_listeningForDeviceChanges { false }; |
112 | bool m_disableGetDisplayMediaUserGestureConstraint { false }; |
113 | }; |
114 | |
115 | } // namespace WebCore |
116 | |
117 | #endif // ENABLE(MEDIA_STREAM) |
118 | |