1 | /* |
2 | * Copyright (C) 2011 Ericsson AB. All rights reserved. |
3 | * Copyright (C) 2012 Google Inc. All rights reserved. |
4 | * Copyright (C) 2013-2018 Apple Inc. All rights reserved. |
5 | * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies). |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions |
9 | * are met: |
10 | * |
11 | * 1. Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer |
15 | * in the documentation and/or other materials provided with the |
16 | * distribution. |
17 | * 3. Neither the name of Ericsson nor the names of its contributors |
18 | * may be used to endorse or promote products derived from this |
19 | * software without specific prior written permission. |
20 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | */ |
33 | |
34 | #include "config.h" |
35 | #include "UserMediaRequest.h" |
36 | |
37 | #if ENABLE(MEDIA_STREAM) |
38 | |
39 | #include "Document.h" |
40 | #include "Frame.h" |
41 | #include "JSMediaStream.h" |
42 | #include "JSOverconstrainedError.h" |
43 | #include "Logging.h" |
44 | #include "MediaConstraints.h" |
45 | #include "PlatformMediaSessionManager.h" |
46 | #include "RealtimeMediaSourceCenter.h" |
47 | #include "SchemeRegistry.h" |
48 | #include "Settings.h" |
49 | #include "UserMediaController.h" |
50 | #include <wtf/Scope.h> |
51 | |
52 | namespace WebCore { |
53 | |
54 | Ref<UserMediaRequest> UserMediaRequest::create(Document& document, MediaStreamRequest&& request, DOMPromiseDeferred<IDLInterface<MediaStream>>&& promise) |
55 | { |
56 | auto result = adoptRef(*new UserMediaRequest(document, WTFMove(request), WTFMove(promise))); |
57 | result->suspendIfNeeded(); |
58 | return result; |
59 | } |
60 | |
61 | UserMediaRequest::UserMediaRequest(Document& document, MediaStreamRequest&& request, DOMPromiseDeferred<IDLInterface<MediaStream>>&& promise) |
62 | : ActiveDOMObject(document) |
63 | , m_promise(WTFMove(promise)) |
64 | , m_request(WTFMove(request)) |
65 | { |
66 | } |
67 | |
68 | UserMediaRequest::~UserMediaRequest() = default; |
69 | |
70 | SecurityOrigin* UserMediaRequest::userMediaDocumentOrigin() const |
71 | { |
72 | if (!m_scriptExecutionContext) |
73 | return nullptr; |
74 | return m_scriptExecutionContext->securityOrigin(); |
75 | } |
76 | |
77 | SecurityOrigin* UserMediaRequest::topLevelDocumentOrigin() const |
78 | { |
79 | if (!m_scriptExecutionContext) |
80 | return nullptr; |
81 | return &m_scriptExecutionContext->topOrigin(); |
82 | } |
83 | |
84 | static bool hasInvalidGetDisplayMediaConstraint(const MediaConstraints& constraints) |
85 | { |
86 | // https://w3c.github.io/mediacapture-screen-share/#navigator-additions |
87 | // 1. Let constraints be the method's first argument. |
88 | // 2. For each member present in constraints whose value, value, is a dictionary, run the following steps: |
89 | // 1. If value contains a member named advanced, return a promise rejected with a newly created TypeError. |
90 | // 2. If value contains a member which in turn is a dictionary containing a member named either min or |
91 | // exact, return a promise rejected with a newly created TypeError. |
92 | if (!constraints.isValid) |
93 | return false; |
94 | |
95 | if (!constraints.advancedConstraints.isEmpty()) |
96 | return true; |
97 | |
98 | bool invalid = false; |
99 | constraints.mandatoryConstraints.filter([&invalid] (const MediaConstraint& constraint) mutable { |
100 | switch (constraint.constraintType()) { |
101 | case MediaConstraintType::Width: |
102 | case MediaConstraintType::Height: { |
103 | auto& intConstraint = downcast<IntConstraint>(constraint); |
104 | int value; |
105 | invalid = intConstraint.getExact(value) || intConstraint.getMin(value); |
106 | break; |
107 | } |
108 | |
109 | case MediaConstraintType::AspectRatio: |
110 | case MediaConstraintType::FrameRate: { |
111 | auto& doubleConstraint = downcast<DoubleConstraint>(constraint); |
112 | double value; |
113 | invalid = doubleConstraint.getExact(value) || doubleConstraint.getMin(value); |
114 | break; |
115 | } |
116 | |
117 | case MediaConstraintType::DisplaySurface: |
118 | case MediaConstraintType::LogicalSurface: { |
119 | auto& boolConstraint = downcast<BooleanConstraint>(constraint); |
120 | bool value; |
121 | invalid = boolConstraint.getExact(value); |
122 | break; |
123 | } |
124 | |
125 | case MediaConstraintType::FacingMode: |
126 | case MediaConstraintType::DeviceId: |
127 | case MediaConstraintType::GroupId: { |
128 | auto& stringConstraint = downcast<StringConstraint>(constraint); |
129 | Vector<String> values; |
130 | invalid = stringConstraint.getExact(values); |
131 | break; |
132 | } |
133 | |
134 | case MediaConstraintType::SampleRate: |
135 | case MediaConstraintType::SampleSize: |
136 | case MediaConstraintType::Volume: |
137 | case MediaConstraintType::EchoCancellation: |
138 | // Ignored. |
139 | break; |
140 | |
141 | case MediaConstraintType::Unknown: |
142 | ASSERT_NOT_REACHED(); |
143 | break; |
144 | } |
145 | |
146 | return invalid; |
147 | }); |
148 | |
149 | return invalid; |
150 | } |
151 | |
152 | void UserMediaRequest::start() |
153 | { |
154 | ASSERT(m_scriptExecutionContext); |
155 | if (!m_scriptExecutionContext) { |
156 | deny(MediaAccessDenialReason::UserMediaDisabled); |
157 | return; |
158 | } |
159 | |
160 | if (m_request.type == MediaStreamRequest::Type::DisplayMedia) { |
161 | if (hasInvalidGetDisplayMediaConstraint(m_request.videoConstraints)) { |
162 | deny(MediaAccessDenialReason::IllegalConstraint); |
163 | return; |
164 | } |
165 | } |
166 | |
167 | // https://w3c.github.io/mediacapture-main/getusermedia.html#dom-mediadevices-getusermedia() |
168 | // 1. Let constraints be the method's first argument. |
169 | // 2. Let requestedMediaTypes be the set of media types in constraints with either a dictionary |
170 | // value or a value of "true". |
171 | // 3. If requestedMediaTypes is the empty set, return a promise rejected with a TypeError. The word |
172 | // "optional" occurs in the WebIDL due to WebIDL rules, but the argument must be supplied in order |
173 | // for the call to succeed. |
174 | if (!m_request.audioConstraints.isValid && !m_request.videoConstraints.isValid) { |
175 | deny(MediaAccessDenialReason::NoConstraints); |
176 | return; |
177 | } |
178 | |
179 | // 4. If the current settings object's responsible document is NOT allowed to use the feature indicated by |
180 | // attribute name allowusermedia, return a promise rejected with a DOMException object whose name |
181 | // attribute has the value SecurityError. |
182 | auto& document = downcast<Document>(*m_scriptExecutionContext); |
183 | auto* controller = UserMediaController::from(document.page()); |
184 | if (!controller) { |
185 | deny(MediaAccessDenialReason::UserMediaDisabled); |
186 | return; |
187 | } |
188 | |
189 | // 6.3 Optionally, e.g., based on a previously-established user preference, for security reasons, |
190 | // or due to platform limitations, jump to the step labeled Permission Failure below. |
191 | // ... |
192 | // 6.10 Permission Failure: Reject p with a new DOMException object whose name attribute has |
193 | // the value NotAllowedError. |
194 | |
195 | OptionSet<UserMediaController::CaptureType> types; |
196 | UserMediaController::BlockedCaller caller; |
197 | if (m_request.type == MediaStreamRequest::Type::DisplayMedia) { |
198 | types.add(UserMediaController::CaptureType::Display); |
199 | caller = UserMediaController::BlockedCaller::GetDisplayMedia; |
200 | } else { |
201 | if (m_request.audioConstraints.isValid) |
202 | types.add(UserMediaController::CaptureType::Microphone); |
203 | if (m_request.videoConstraints.isValid) |
204 | types.add(UserMediaController::CaptureType::Camera); |
205 | caller = UserMediaController::BlockedCaller::GetUserMedia; |
206 | } |
207 | auto access = controller->canCallGetUserMedia(document, types); |
208 | if (access != UserMediaController::GetUserMediaAccess::CanCall) { |
209 | deny(MediaAccessDenialReason::PermissionDenied); |
210 | controller->logGetUserMediaDenial(document, access, caller); |
211 | return; |
212 | } |
213 | |
214 | PlatformMediaSessionManager::sharedManager().prepareToSendUserMediaPermissionRequest(); |
215 | controller->requestUserMediaAccess(*this); |
216 | } |
217 | |
218 | void UserMediaRequest::allow(CaptureDevice&& audioDevice, CaptureDevice&& videoDevice, String&& deviceIdentifierHashSalt, CompletionHandler<void()>&& completionHandler) |
219 | { |
220 | RELEASE_LOG(MediaStream, "UserMediaRequest::allow %s %s" , audioDevice ? audioDevice.persistentId().utf8().data() : "" , videoDevice ? videoDevice.persistentId().utf8().data() : "" ); |
221 | |
222 | auto callback = [this, protector = makePendingActivity(*this), completionHandler = WTFMove(completionHandler)](RefPtr<MediaStreamPrivate>&& privateStream) mutable { |
223 | auto scopeExit = makeScopeExit([&] { |
224 | completionHandler(); |
225 | }); |
226 | if (!m_scriptExecutionContext) |
227 | return; |
228 | |
229 | if (!privateStream) { |
230 | RELEASE_LOG(MediaStream, "UserMediaRequest::allow failed to create media stream!" ); |
231 | deny(MediaAccessDenialReason::HardwareError); |
232 | return; |
233 | } |
234 | privateStream->monitorOrientation(downcast<Document>(m_scriptExecutionContext)->orientationNotifier()); |
235 | |
236 | auto stream = MediaStream::create(*m_scriptExecutionContext, privateStream.releaseNonNull()); |
237 | if (stream->getTracks().isEmpty()) { |
238 | deny(MediaAccessDenialReason::HardwareError); |
239 | return; |
240 | } |
241 | |
242 | scopeExit.release(); |
243 | m_pendingActivationMediaStream = PendingActivationMediaStream::create(WTFMove(protector), *this, WTFMove(stream), WTFMove(completionHandler)); |
244 | }; |
245 | |
246 | auto& document = downcast<Document>(*scriptExecutionContext()); |
247 | document.setDeviceIDHashSalt(deviceIdentifierHashSalt); |
248 | |
249 | RealtimeMediaSourceCenter::singleton().createMediaStream(WTFMove(callback), WTFMove(deviceIdentifierHashSalt), WTFMove(audioDevice), WTFMove(videoDevice), m_request); |
250 | |
251 | if (!m_scriptExecutionContext) |
252 | return; |
253 | |
254 | #if ENABLE(WEB_RTC) |
255 | if (auto* page = document.page()) |
256 | page->rtcController().disableICECandidateFilteringForDocument(document); |
257 | #endif |
258 | } |
259 | |
260 | void UserMediaRequest::deny(MediaAccessDenialReason reason, const String& message) |
261 | { |
262 | if (!m_scriptExecutionContext) |
263 | return; |
264 | |
265 | ExceptionCode code; |
266 | switch (reason) { |
267 | case MediaAccessDenialReason::IllegalConstraint: |
268 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - invalid constraints" ); |
269 | code = TypeError; |
270 | break; |
271 | case MediaAccessDenialReason::NoConstraints: |
272 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - no constraints" ); |
273 | code = TypeError; |
274 | break; |
275 | case MediaAccessDenialReason::UserMediaDisabled: |
276 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - user media disabled" ); |
277 | code = SecurityError; |
278 | break; |
279 | case MediaAccessDenialReason::NoCaptureDevices: |
280 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - no capture devices" ); |
281 | code = NotFoundError; |
282 | break; |
283 | case MediaAccessDenialReason::InvalidConstraint: |
284 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - invalid constraint - %s" , message.utf8().data()); |
285 | m_promise.rejectType<IDLInterface<OverconstrainedError>>(OverconstrainedError::create(message, "Invalid constraint"_s ).get()); |
286 | return; |
287 | case MediaAccessDenialReason::HardwareError: |
288 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - hardware error" ); |
289 | code = NotReadableError; |
290 | break; |
291 | case MediaAccessDenialReason::OtherFailure: |
292 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - other failure" ); |
293 | code = AbortError; |
294 | break; |
295 | case MediaAccessDenialReason::PermissionDenied: |
296 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - permission denied" ); |
297 | code = NotAllowedError; |
298 | break; |
299 | case MediaAccessDenialReason::InvalidAccess: |
300 | RELEASE_LOG(MediaStream, "UserMediaRequest::deny - invalid access" ); |
301 | code = InvalidAccessError; |
302 | break; |
303 | } |
304 | |
305 | if (!message.isEmpty()) |
306 | m_promise.reject(code, message); |
307 | else |
308 | m_promise.reject(code); |
309 | } |
310 | |
311 | void UserMediaRequest::stop() |
312 | { |
313 | // Protecting 'this' since nulling m_pendingActivationMediaStream might destroy it. |
314 | Ref<UserMediaRequest> protectedThis(*this); |
315 | |
316 | m_pendingActivationMediaStream = nullptr; |
317 | |
318 | auto& document = downcast<Document>(*m_scriptExecutionContext); |
319 | if (auto* controller = UserMediaController::from(document.page())) |
320 | controller->cancelUserMediaAccessRequest(*this); |
321 | } |
322 | |
323 | const char* UserMediaRequest::activeDOMObjectName() const |
324 | { |
325 | return "UserMediaRequest" ; |
326 | } |
327 | |
328 | bool UserMediaRequest::canSuspendForDocumentSuspension() const |
329 | { |
330 | return !hasPendingActivity(); |
331 | } |
332 | |
333 | Document* UserMediaRequest::document() const |
334 | { |
335 | return downcast<Document>(m_scriptExecutionContext); |
336 | } |
337 | |
338 | UserMediaRequest::PendingActivationMediaStream::PendingActivationMediaStream(Ref<PendingActivity<UserMediaRequest>>&& protectingUserMediaRequest, UserMediaRequest& userMediaRequest, Ref<MediaStream>&& stream, CompletionHandler<void()>&& completionHandler) |
339 | : m_protectingUserMediaRequest(WTFMove(protectingUserMediaRequest)) |
340 | , m_userMediaRequest(userMediaRequest) |
341 | , m_mediaStream(WTFMove(stream)) |
342 | , m_completionHandler(WTFMove(completionHandler)) |
343 | { |
344 | m_mediaStream->privateStream().addObserver(*this); |
345 | m_mediaStream->startProducingData(); |
346 | } |
347 | |
348 | UserMediaRequest::PendingActivationMediaStream::~PendingActivationMediaStream() |
349 | { |
350 | m_mediaStream->privateStream().removeObserver(*this); |
351 | m_completionHandler(); |
352 | if (auto* document = m_mediaStream->document()) |
353 | document->updateIsPlayingMedia(); |
354 | } |
355 | |
356 | void UserMediaRequest::PendingActivationMediaStream::characteristicsChanged() |
357 | { |
358 | if (!m_userMediaRequest.m_pendingActivationMediaStream) |
359 | return; |
360 | |
361 | for (auto& track : m_mediaStream->privateStream().tracks()) { |
362 | if (track->source().captureDidFail()) { |
363 | m_userMediaRequest.mediaStreamDidFail(track->source().type()); |
364 | return; |
365 | } |
366 | } |
367 | |
368 | if (m_mediaStream->privateStream().hasVideo() || m_mediaStream->privateStream().hasAudio()) { |
369 | m_userMediaRequest.mediaStreamIsReady(WTFMove(m_mediaStream)); |
370 | return; |
371 | } |
372 | } |
373 | |
374 | void UserMediaRequest::mediaStreamIsReady(Ref<MediaStream>&& stream) |
375 | { |
376 | RELEASE_LOG(MediaStream, "UserMediaRequest::mediaStreamIsReady" ); |
377 | stream->document()->setHasCaptureMediaStreamTrack(); |
378 | m_promise.resolve(WTFMove(stream)); |
379 | m_pendingActivationMediaStream = nullptr; |
380 | } |
381 | |
382 | void UserMediaRequest::mediaStreamDidFail(RealtimeMediaSource::Type type) |
383 | { |
384 | RELEASE_LOG(MediaStream, "UserMediaRequest::mediaStreamDidFail" ); |
385 | const char* typeDescription = "" ; |
386 | switch (type) { |
387 | case RealtimeMediaSource::Type::Audio: |
388 | typeDescription = "audio" ; |
389 | break; |
390 | case RealtimeMediaSource::Type::Video: |
391 | typeDescription = "video" ; |
392 | break; |
393 | case RealtimeMediaSource::Type::None: |
394 | typeDescription = "unknown" ; |
395 | break; |
396 | } |
397 | m_promise.reject(NotReadableError, makeString("Failed starting capture of a "_s , typeDescription, " track"_s )); |
398 | // We are in an observer iterator loop, we do not want to change the observers within this loop. |
399 | callOnMainThread([stream = WTFMove(m_pendingActivationMediaStream)] { }); |
400 | } |
401 | |
402 | } // namespace WebCore |
403 | |
404 | #endif // ENABLE(MEDIA_STREAM) |
405 | |