1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MediaCapabilities.h"
#include "ContentType.h"
#include "Document.h"
#include "EventLoop.h"
#include "JSDOMPromiseDeferred.h"
#include "JSMediaCapabilitiesDecodingInfo.h"
#include "JSMediaCapabilitiesEncodingInfo.h"
#include "Logging.h"
#include "MediaCapabilitiesDecodingInfo.h"
#include "MediaCapabilitiesEncodingInfo.h"
#include "MediaCapabilitiesLogging.h"
#include "MediaDecodingConfiguration.h"
#include "MediaEncodingConfiguration.h"
#include "MediaEngineConfigurationFactory.h"
#include "Page.h"
#include "Settings.h"
#include "WebRTCProvider.h"
#include <wtf/Logger.h>
#include <wtf/SortedArrayMap.h>
namespace WebCore {
static bool isValidMediaMIMEType(const ContentType& contentType)
{
// A "bucket" MIME types is one whose container type does not uniquely specify a codec.
// See: https://tools.ietf.org/html/rfc6381
static constexpr ComparableASCIILiteral bucketMIMETypeArray[] = {
"application/mp21"_s,
"application/mp4"_s,
"audio/3gpp"_s,
"audio/3gpp2"_s,
"audio/mp4"_s,
"audio/ogg"_s,
"audio/vnd.apple.mpegurl"_s,
"audio/webm"_s,
"video/3gpp"_s,
"video/3gpp2"_s,
"video/mp4"_s,
"video/ogg"_s,
"video/quicktime"_s,
"video/vnd.apple.mpegurl"_s,
"video/webm"_s,
};
static constexpr SortedArraySet bucketMIMETypes { bucketMIMETypeArray };
// 2.1.4. MIME types
// https://wicg.github.io/media-capabilities/#valid-media-mime-type
// A valid media MIME type is a string that is a valid MIME type per [mimesniff]. If the MIME type does
// not imply a codec, the string MUST also have one and only one parameter that is named codecs with a
// value describing a single media codec. Otherwise, it MUST contain no parameters.
if (contentType.isEmpty())
return false;
auto codecs = contentType.codecs();
// FIXME: The spec requires that the "codecs" parameter is the only parameter present.
if (bucketMIMETypes.contains(contentType.containerType()))
return codecs.size() == 1;
return !codecs.size();
}
static bool isValidVideoMIMEType(const ContentType& contentType)
{
// 2.1.4 MIME Types
// https://wicg.github.io/media-capabilities/#valid-video-mime-type
// A valid video MIME type is a string that is a valid media MIME type and for which the type per [RFC7231]
// is either video or application.
if (!isValidMediaMIMEType(contentType))
return false;
auto containerType = contentType.containerType();
if (!startsWithLettersIgnoringASCIICase(containerType, "video/"_s) && !startsWithLettersIgnoringASCIICase(containerType, "application/"_s))
return false;
return true;
}
static bool isValidAudioMIMEType(const ContentType& contentType)
{
// 2.1.4 MIME Types
// https://wicg.github.io/media-capabilities/#valid-audio-mime-type
// A valid audio MIME type is a string that is a valid media MIME type and for which the type per [RFC7231]
// is either audio or application.
if (!isValidMediaMIMEType(contentType))
return false;
auto containerType = contentType.containerType();
if (!startsWithLettersIgnoringASCIICase(containerType, "audio/"_s) && !startsWithLettersIgnoringASCIICase(containerType, "application/"_s))
return false;
return true;
}
static bool isValidVideoConfiguration(const VideoConfiguration& configuration)
{
// 2.1.5. VideoConfiguration
// https://wicg.github.io/media-capabilities/#valid-video-configuration
// 1. If configuration’s contentType is not a valid video MIME type, return false and abort these steps.
if (!isValidVideoMIMEType(ContentType(configuration.contentType)))
return false;
// 2. If none of the following is true, return false and abort these steps:
// o. Applying the rules for parsing floating-point number values to configuration’s framerate
// results in a number that is finite and greater than 0.
if (!std::isfinite(configuration.framerate) || configuration.framerate <= 0)
return false;
// 3. Return true.
return true;
}
static bool isValidAudioConfiguration(const AudioConfiguration& configuration)
{
// 2.1.6. AudioConfiguration
// https://wicg.github.io/media-capabilities/#audioconfiguration
// 1. If configuration’s contentType is not a valid audio MIME type, return false and abort these steps.
if (!isValidAudioMIMEType(ContentType(configuration.contentType)))
return false;
// 2. Return true.
return true;
}
static bool isValidMediaConfiguration(const MediaConfiguration& configuration)
{
// 2.1.1. MediaConfiguration
// https://wicg.github.io/media-capabilities/#mediaconfiguration
// For a MediaConfiguration to be a valid MediaConfiguration, audio or video MUST be present.
if (!configuration.video && !configuration.audio)
return false;
if (configuration.video && !isValidVideoConfiguration(configuration.video.value()))
return false;
if (configuration.audio && !isValidAudioConfiguration(configuration.audio.value()))
return false;
return true;
}
static void gatherDecodingInfo(Document& document, MediaDecodingConfiguration&& configuration, MediaEngineConfigurationFactory::DecodingConfigurationCallback&& callback)
{
RELEASE_LOG_INFO(Media, "Gathering decoding MediaCapabilities");
MediaEngineConfigurationFactory::DecodingConfigurationCallback decodingCallback = [callback = WTFMove(callback)](MediaCapabilitiesDecodingInfo&& result) mutable {
RELEASE_LOG_INFO(Media, "Finished gathering decoding MediaCapabilities");
callback(WTFMove(result));
};
if (!document.settings().mediaCapabilitiesExtensionsEnabled() && configuration.video)
configuration.video.value().alphaChannel.reset();
configuration.allowedMediaContainerTypes = document.settings().allowedMediaContainerTypes();
configuration.allowedMediaCodecTypes = document.settings().allowedMediaCodecTypes();
#if ENABLE(VP9)
configuration.canExposeVP9 = document.settings().vp9DecoderEnabled();
#endif
#if ENABLE(WEB_RTC)
if (configuration.type == MediaDecodingType::WebRTC) {
if (auto* page = document.page())
page->webRTCProvider().createDecodingConfiguration(WTFMove(configuration), WTFMove(decodingCallback));
return;
}
#endif
MediaEngineConfigurationFactory::createDecodingConfiguration(WTFMove(configuration), WTFMove(decodingCallback));
}
static void gatherEncodingInfo(Document& document, MediaEncodingConfiguration&& configuration, MediaEngineConfigurationFactory::EncodingConfigurationCallback&& callback)
{
RELEASE_LOG_INFO(Media, "Gathering encoding MediaCapabilities");
MediaEngineConfigurationFactory::EncodingConfigurationCallback encodingCallback = [callback = WTFMove(callback)](auto&& result) mutable {
RELEASE_LOG_INFO(Media, "Finished gathering encoding MediaCapabilities");
callback(WTFMove(result));
};
#if ENABLE(WEB_RTC)
if (configuration.type == MediaEncodingType::WebRTC) {
if (auto* page = document.page())
page->webRTCProvider().createEncodingConfiguration(WTFMove(configuration), WTFMove(encodingCallback));
return;
}
#else
UNUSED_PARAM(document);
#endif
MediaEngineConfigurationFactory::createEncodingConfiguration(WTFMove(configuration), WTFMove(encodingCallback));
}
void MediaCapabilities::decodingInfo(ScriptExecutionContext& context, MediaDecodingConfiguration&& configuration, Ref<DeferredPromise>&& promise)
{
// 2.4 Media Capabilities Interface
// https://wicg.github.io/media-capabilities/#media-capabilities-interface
// 1. If configuration is not a valid MediaConfiguration, return a Promise rejected with a TypeError.
// 2. If configuration.video is present and is not a valid video configuration, return a Promise rejected with a TypeError.
// 2.2.3 If configuration is of type MediaDecodingConfiguration, run the following substeps:
// 2.2.3.1. If the user agent is able to decode the media represented by
// configuration, set supported to true. Otherwise set it to false.
// 2.2.3.2. If the user agent is able to decode the media represented by
// configuration at a pace that allows a smooth playback, set smooth to
// true. Otherwise set it to false.
// 2.2.3.3. If the user agent is able to decode the media represented by
// configuration in a power efficient manner, set powerEfficient to
// true. Otherwise set it to false. The user agent SHOULD NOT take into
// consideration the current power source in order to determine the
// decoding power efficiency unless the device’s power source has side
// effects such as enabling different decoding modules.
// 3. If configuration.audio is present and is not a valid audio configuration, return a Promise rejected with a TypeError.
if (!isValidMediaConfiguration(configuration)) {
RELEASE_LOG_INFO(Media, "Invalid decoding media configuration");
promise->reject(ExceptionCode::TypeError);
return;
}
// 4. Let p be a new promise.
// 5. In parallel, run the create a MediaCapabilitiesInfo algorithm with configuration and resolve p with its result.
// 6. Return p.
MediaEngineConfigurationFactory::DecodingConfigurationCallback callback = [promise = WTFMove(promise), context = Ref { context }](auto info) mutable {
context->eventLoop().queueTask(TaskSource::MediaElement, [promise = WTFMove(promise), info = WTFMove(info)] () mutable {
promise->resolve<IDLDictionary<MediaCapabilitiesDecodingInfo>>(WTFMove(info));
});
};
if (RefPtr document = dynamicDowncast<Document>(context)) {
gatherDecodingInfo(*document, WTFMove(configuration), WTFMove(callback));
return;
}
m_decodingTasks.add(++m_nextTaskIdentifier, WTFMove(callback));
context.postTaskToResponsibleDocument([configuration = WTFMove(configuration).isolatedCopy(), contextIdentifier = context.identifier(), weakThis = WeakPtr { this }, taskIdentifier = m_nextTaskIdentifier](auto& document) mutable {
gatherDecodingInfo(document, WTFMove(configuration), [contextIdentifier, weakThis = WTFMove(weakThis), taskIdentifier](MediaCapabilitiesDecodingInfo&& result) mutable {
ScriptExecutionContext::postTaskTo(contextIdentifier, [weakThis = WTFMove(weakThis), taskIdentifier, result = WTFMove(result).isolatedCopy()](auto&) mutable {
if (!weakThis)
return;
if (auto callback = weakThis->m_decodingTasks.take(taskIdentifier))
callback(WTFMove(result));
});
});
});
}
void MediaCapabilities::encodingInfo(ScriptExecutionContext& context, MediaEncodingConfiguration&& configuration, Ref<DeferredPromise>&& promise)
{
// 2.4 Media Capabilities Interface
// https://wicg.github.io/media-capabilities/#media-capabilities-interface
// 1. If configuration is not a valid MediaConfiguration, return a Promise rejected with a TypeError.
// 2. If configuration.video is present and is not a valid video configuration, return a Promise rejected with a TypeError.
// 3. If configuration.audio is present and is not a valid audio configuration, return a Promise rejected with a TypeError.
// 2.2.4. If configuration is of type MediaEncodingConfiguration, run the following substeps:
// 2.2.4.1. If the user agent is able to encode the media
// represented by configuration, set supported to true. Otherwise
// set it to false.
// 2.2.4.2. If the user agent is able to encode the media
// represented by configuration at a pace that allows encoding
// frames at the same pace as they are sent to the encoder, set
// smooth to true. Otherwise set it to false.
// 2.2.4.3. If the user agent is able to encode the media
// represented by configuration in a power efficient manner, set
// powerEfficient to true. Otherwise set it to false. The user agent
// SHOULD NOT take into consideration the current power source in
// order to determine the encoding power efficiency unless the
// device’s power source has side effects such as enabling different
// encoding modules.
if (!isValidMediaConfiguration(configuration)) {
RELEASE_LOG_INFO(Media, "Invalid encoding media configuration");
promise->reject(ExceptionCode::TypeError);
return;
}
// 4. Let p be a new promise.
// 5. In parallel, run the create a MediaCapabilitiesInfo algorithm with configuration and resolve p with its result.
// 6. Return p.
MediaEngineConfigurationFactory::EncodingConfigurationCallback callback = [promise = WTFMove(promise), context = Ref { context }](auto info) mutable {
context->eventLoop().queueTask(TaskSource::MediaElement, [promise = WTFMove(promise), info = WTFMove(info)] () mutable {
promise->resolve<IDLDictionary<MediaCapabilitiesEncodingInfo>>(WTFMove(info));
});
};
if (RefPtr document = dynamicDowncast<Document>(context)) {
gatherEncodingInfo(*document, WTFMove(configuration), WTFMove(callback));
return;
}
m_encodingTasks.add(++m_nextTaskIdentifier, WTFMove(callback));
context.postTaskToResponsibleDocument([configuration = WTFMove(configuration).isolatedCopy(), contextIdentifier = context.identifier(), weakThis = WeakPtr { this }, taskIdentifier = m_nextTaskIdentifier](auto& document) mutable {
gatherEncodingInfo(document, WTFMove(configuration), [contextIdentifier, weakThis = WTFMove(weakThis), taskIdentifier](auto&& result) mutable {
ScriptExecutionContext::postTaskTo(contextIdentifier, [weakThis = WTFMove(weakThis), taskIdentifier, result = WTFMove(result).isolatedCopy()](auto&) mutable {
if (!weakThis)
return;
if (auto callback = weakThis->m_encodingTasks.take(taskIdentifier))
callback(WTFMove(result));
});
});
});
}
}
|