1/*
2 * Copyright (C) 2015 Ericsson AB. All rights reserved.
3 * Copyright (C) 2015-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#include "config.h"
33#include "MediaDevices.h"
34
35#if ENABLE(MEDIA_STREAM)
36
37#include "Document.h"
38#include "Event.h"
39#include "EventNames.h"
40#include "MediaDevicesRequest.h"
41#include "MediaTrackSupportedConstraints.h"
42#include "RealtimeMediaSourceSettings.h"
43#include "RuntimeEnabledFeatures.h"
44#include "UserMediaController.h"
45#include "UserMediaRequest.h"
46#include <wtf/IsoMallocInlines.h>
47#include <wtf/RandomNumber.h>
48
49namespace WebCore {
50
51WTF_MAKE_ISO_ALLOCATED_IMPL(MediaDevices);
52
53inline MediaDevices::MediaDevices(Document& document)
54 : ActiveDOMObject(document)
55 , m_scheduledEventTimer(*this, &MediaDevices::scheduledEventTimerFired)
56 , m_eventNames(eventNames())
57{
58 suspendIfNeeded();
59
60 static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Monitor) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor), "MediaDevices::DisplayCaptureSurfaceType::Monitor is not equal to RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor as expected");
61 static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Window) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Window), "MediaDevices::DisplayCaptureSurfaceType::Window is not RealtimeMediaSourceSettings::DisplaySurfaceType::Window as expected");
62 static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Application) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Application), "MediaDevices::DisplayCaptureSurfaceType::Application is not RealtimeMediaSourceSettings::DisplaySurfaceType::Application as expected");
63 static_assert(static_cast<size_t>(MediaDevices::DisplayCaptureSurfaceType::Browser) == static_cast<size_t>(RealtimeMediaSourceSettings::DisplaySurfaceType::Browser), "MediaDevices::DisplayCaptureSurfaceType::Browser is not RealtimeMediaSourceSettings::DisplaySurfaceType::Browser as expected");
64}
65
66MediaDevices::~MediaDevices() = default;
67
68void MediaDevices::stop()
69{
70 if (m_deviceChangeToken) {
71 auto* document = this->document();
72 auto* controller = document ? UserMediaController::from(document->page()) : nullptr;
73 if (document && controller)
74 controller->removeDeviceChangeObserver(m_deviceChangeToken);
75 }
76}
77
78Ref<MediaDevices> MediaDevices::create(Document& document)
79{
80 return adoptRef(*new MediaDevices(document));
81}
82
83Document* MediaDevices::document() const
84{
85 return downcast<Document>(scriptExecutionContext());
86}
87
88static MediaConstraints createMediaConstraints(const Variant<bool, MediaTrackConstraints>& constraints)
89{
90 return WTF::switchOn(constraints,
91 [&] (bool isValid) {
92 MediaConstraints constraints;
93 constraints.isValid = isValid;
94 return constraints;
95 },
96 [&] (const MediaTrackConstraints& trackConstraints) {
97 return createMediaConstraints(trackConstraints);
98 }
99 );
100}
101
102void MediaDevices::getUserMedia(const StreamConstraints& constraints, Promise&& promise) const
103{
104 auto* document = this->document();
105 if (!document)
106 return;
107
108 auto audioConstraints = createMediaConstraints(constraints.audio);
109 auto videoConstraints = createMediaConstraints(constraints.video);
110 if (videoConstraints.isValid)
111 videoConstraints.setDefaultVideoConstraints();
112
113 auto request = UserMediaRequest::create(*document, { MediaStreamRequest::Type::UserMedia, WTFMove(audioConstraints), WTFMove(videoConstraints) }, WTFMove(promise));
114 request->start();
115}
116
117void MediaDevices::getDisplayMedia(const StreamConstraints& constraints, Promise&& promise) const
118{
119 auto* document = this->document();
120 if (!document)
121 return;
122
123 if (!m_disableGetDisplayMediaUserGestureConstraint && !UserGestureIndicator::processingUserGesture()) {
124 promise.reject(Exception { InvalidAccessError, "getDisplayMedia must be called from a user gesture handler."_s });
125 return;
126 }
127
128 auto request = UserMediaRequest::create(*document, { MediaStreamRequest::Type::DisplayMedia, { }, createMediaConstraints(constraints.video) }, WTFMove(promise));
129 request->start();
130}
131
132void MediaDevices::enumerateDevices(EnumerateDevicesPromise&& promise) const
133{
134 auto* document = this->document();
135 if (!document)
136 return;
137 MediaDevicesRequest::create(*document, WTFMove(promise))->start();
138}
139
140MediaTrackSupportedConstraints MediaDevices::getSupportedConstraints()
141{
142 auto& supported = RealtimeMediaSourceCenter::singleton().supportedConstraints();
143 MediaTrackSupportedConstraints result;
144 result.width = supported.supportsWidth();
145 result.height = supported.supportsHeight();
146 result.aspectRatio = supported.supportsAspectRatio();
147 result.frameRate = supported.supportsFrameRate();
148 result.facingMode = supported.supportsFacingMode();
149 result.volume = supported.supportsVolume();
150 result.sampleRate = supported.supportsSampleRate();
151 result.sampleSize = supported.supportsSampleSize();
152 result.echoCancellation = supported.supportsEchoCancellation();
153 result.deviceId = supported.supportsDeviceId();
154 result.groupId = supported.supportsGroupId();
155
156 return result;
157}
158
159void MediaDevices::scheduledEventTimerFired()
160{
161 if (scriptExecutionContext())
162 dispatchEvent(Event::create(eventNames().devicechangeEvent, Event::CanBubble::No, Event::IsCancelable::No));
163}
164
165bool MediaDevices::hasPendingActivity() const
166{
167 return !isContextStopped() && hasEventListeners(m_eventNames.devicechangeEvent);
168}
169
170const char* MediaDevices::activeDOMObjectName() const
171{
172 return "MediaDevices";
173}
174
175bool MediaDevices::canSuspendForDocumentSuspension() const
176{
177 return true;
178}
179
180bool MediaDevices::addEventListener(const AtomString& eventType, Ref<EventListener>&& listener, const AddEventListenerOptions& options)
181{
182 if (!m_listeningForDeviceChanges && eventType == eventNames().devicechangeEvent) {
183 auto* document = this->document();
184 auto* controller = document ? UserMediaController::from(document->page()) : nullptr;
185 if (document && controller) {
186 m_listeningForDeviceChanges = true;
187
188 m_deviceChangeToken = controller->addDeviceChangeObserver([weakThis = makeWeakPtr(*this), this]() {
189
190 if (!weakThis || m_scheduledEventTimer.isActive())
191 return;
192
193 auto* document = this->document();
194 auto* controller = document ? UserMediaController::from(document->page()) : nullptr;
195 if (!controller)
196 return;
197
198 bool canAccessMicrophone = controller->canCallGetUserMedia(*document, { UserMediaController::CaptureType::Microphone }) == UserMediaController::GetUserMediaAccess::CanCall;
199 bool canAccessCamera = controller->canCallGetUserMedia(*document, { UserMediaController::CaptureType::Camera }) == UserMediaController::GetUserMediaAccess::CanCall;
200 if (!canAccessMicrophone && !canAccessCamera)
201 return;
202
203 m_scheduledEventTimer.startOneShot(Seconds(randomNumber() / 2));
204 });
205 }
206 }
207
208 return EventTargetWithInlineData::addEventListener(eventType, WTFMove(listener), options);
209}
210
211} // namespace WebCore
212
213#endif // ENABLE(MEDIA_STREAM)
214