2014-11-18 12:13:02 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/MediaKeySystemAccess.h"
|
|
|
|
#include "mozilla/dom/MediaKeySystemAccessBinding.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "nsContentTypeParser.h"
|
|
|
|
#ifdef MOZ_FMP4
|
|
|
|
#include "MP4Decoder.h"
|
|
|
|
#endif
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "mozilla/WindowsVersion.h"
|
|
|
|
#endif
|
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "mozIGeckoMediaPluginService.h"
|
|
|
|
#include "VideoUtils.h"
|
2015-02-13 22:52:42 +03:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsIObserverService.h"
|
2015-02-20 04:37:55 +03:00
|
|
|
#include "mozilla/EMEUtils.h"
|
2014-11-18 12:13:02 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaKeySystemAccess,
|
|
|
|
mParent)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaKeySystemAccess)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaKeySystemAccess)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeySystemAccess)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
MediaKeySystemAccess::MediaKeySystemAccess(nsPIDOMWindow* aParent,
|
|
|
|
const nsAString& aKeySystem)
|
|
|
|
: mParent(aParent)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaKeySystemAccess::~MediaKeySystemAccess()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaKeySystemAccess::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return MediaKeySystemAccessBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow*
|
|
|
|
MediaKeySystemAccess::GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySystemAccess::GetKeySystem(nsString& aRetVal) const
|
|
|
|
{
|
|
|
|
aRetVal = mKeySystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeySystemAccess::CreateMediaKeys(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaKeys> keys(new MediaKeys(mParent, mKeySystem));
|
|
|
|
return keys->Init(aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
HaveGMPFor(mozIGeckoMediaPluginService* aGMPService,
|
|
|
|
const nsCString& aKeySystem,
|
|
|
|
const nsCString& aAPI,
|
|
|
|
const nsCString& aTag = EmptyCString())
|
|
|
|
{
|
|
|
|
nsTArray<nsCString> tags;
|
|
|
|
tags.AppendElement(aKeySystem);
|
|
|
|
if (!aTag.IsEmpty()) {
|
|
|
|
tags.AppendElement(aTag);
|
|
|
|
}
|
|
|
|
bool hasPlugin = false;
|
|
|
|
if (NS_FAILED(aGMPService->HasPluginForAPI(aAPI,
|
|
|
|
&tags,
|
|
|
|
&hasPlugin))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return hasPlugin;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:38:08 +03:00
|
|
|
static MediaKeySystemStatus
|
|
|
|
EnsureMinCDMVersion(mozIGeckoMediaPluginService* aGMPService,
|
|
|
|
const nsAString& aKeySystem,
|
|
|
|
int32_t aMinCdmVersion)
|
|
|
|
{
|
|
|
|
if (aMinCdmVersion == NO_CDM_VERSION) {
|
|
|
|
return MediaKeySystemStatus::Available;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsCString> tags;
|
|
|
|
tags.AppendElement(NS_ConvertUTF16toUTF8(aKeySystem));
|
|
|
|
nsAutoCString versionStr;
|
|
|
|
if (NS_FAILED(aGMPService->GetPluginVersionForAPI(NS_LITERAL_CSTRING(GMP_API_DECRYPTOR),
|
|
|
|
&tags,
|
|
|
|
versionStr))) {
|
|
|
|
return MediaKeySystemStatus::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
int32_t version = versionStr.ToInteger(&rv);
|
|
|
|
if (NS_FAILED(rv) || version < 0 || aMinCdmVersion > version) {
|
|
|
|
return MediaKeySystemStatus::Cdm_insufficient_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
return MediaKeySystemStatus::Available;
|
|
|
|
}
|
|
|
|
|
2014-11-18 12:13:02 +03:00
|
|
|
/* static */
|
2015-02-13 22:52:42 +03:00
|
|
|
MediaKeySystemStatus
|
2015-02-20 04:38:08 +03:00
|
|
|
MediaKeySystemAccess::GetKeySystemStatus(const nsAString& aKeySystem,
|
|
|
|
int32_t aMinCdmVersion)
|
2014-11-18 12:13:02 +03:00
|
|
|
{
|
2015-02-13 22:52:42 +03:00
|
|
|
MOZ_ASSERT(Preferences::GetBool("media.eme.enabled", false));
|
2014-11-18 12:13:02 +03:00
|
|
|
nsCOMPtr<mozIGeckoMediaPluginService> mps =
|
|
|
|
do_GetService("@mozilla.org/gecko-media-plugin-service;1");
|
|
|
|
if (NS_WARN_IF(!mps)) {
|
2015-02-13 22:52:42 +03:00
|
|
|
return MediaKeySystemStatus::Error;
|
2014-11-18 12:13:02 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:52:42 +03:00
|
|
|
if (aKeySystem.EqualsLiteral("org.w3.clearkey")) {
|
|
|
|
if (!Preferences::GetBool("media.eme.clearkey.enabled", true)) {
|
|
|
|
return MediaKeySystemStatus::Cdm_disabled;
|
|
|
|
}
|
|
|
|
if (!HaveGMPFor(mps,
|
|
|
|
NS_LITERAL_CSTRING("org.w3.clearkey"),
|
|
|
|
NS_LITERAL_CSTRING(GMP_API_DECRYPTOR))) {
|
|
|
|
return MediaKeySystemStatus::Cdm_not_installed;
|
|
|
|
}
|
2015-02-20 04:38:08 +03:00
|
|
|
return EnsureMinCDMVersion(mps, aKeySystem, aMinCdmVersion);
|
2014-11-18 12:13:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
2015-02-04 23:36:44 +03:00
|
|
|
if ((aKeySystem.EqualsLiteral("com.adobe.access") ||
|
2015-02-13 22:52:42 +03:00
|
|
|
aKeySystem.EqualsLiteral("com.adobe.primetime"))) {
|
|
|
|
// Win Vista and later only.
|
|
|
|
if (!IsVistaOrLater()) {
|
|
|
|
return MediaKeySystemStatus::Cdm_not_supported;
|
|
|
|
}
|
2015-02-17 20:50:16 +03:00
|
|
|
if (!Preferences::GetBool("media.gmp-eme-adobe.enabled", false)) {
|
2015-02-13 22:52:42 +03:00
|
|
|
return MediaKeySystemStatus::Cdm_disabled;
|
|
|
|
}
|
|
|
|
if (!HaveGMPFor(mps,
|
|
|
|
NS_ConvertUTF16toUTF8(aKeySystem),
|
|
|
|
NS_LITERAL_CSTRING(GMP_API_DECRYPTOR))) {
|
|
|
|
return MediaKeySystemStatus::Cdm_not_installed;
|
|
|
|
}
|
2015-02-20 04:38:08 +03:00
|
|
|
return EnsureMinCDMVersion(mps, aKeySystem, aMinCdmVersion);
|
2014-11-18 12:13:02 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 22:52:42 +03:00
|
|
|
return MediaKeySystemStatus::Cdm_not_supported;
|
2014-11-18 12:13:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsPlayableWithGMP(mozIGeckoMediaPluginService* aGMPS,
|
|
|
|
const nsAString& aKeySystem,
|
|
|
|
const nsAString& aContentType)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_FMP4
|
|
|
|
nsContentTypeParser parser(aContentType);
|
|
|
|
nsAutoString mimeType;
|
|
|
|
nsresult rv = parser.GetType(mimeType);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mimeType.EqualsLiteral("audio/mp4") &&
|
|
|
|
!mimeType.EqualsLiteral("audio/x-m4a") &&
|
|
|
|
!mimeType.EqualsLiteral("video/mp4")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString codecs;
|
|
|
|
parser.GetParameter("codecs", codecs);
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 mimeTypeUTF8(mimeType);
|
|
|
|
bool hasAAC = false;
|
|
|
|
bool hasH264 = false;
|
|
|
|
bool hasMP3 = false;
|
|
|
|
if (!MP4Decoder::CanHandleMediaType(mimeTypeUTF8,
|
|
|
|
codecs,
|
|
|
|
hasAAC,
|
|
|
|
hasH264,
|
|
|
|
hasMP3) ||
|
|
|
|
hasMP3) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (!hasAAC || !HaveGMPFor(aGMPS,
|
|
|
|
NS_ConvertUTF16toUTF8(aKeySystem),
|
2014-12-09 23:35:26 +03:00
|
|
|
NS_LITERAL_CSTRING(GMP_API_DECRYPTOR),
|
2014-11-18 12:13:02 +03:00
|
|
|
NS_LITERAL_CSTRING("aac"))) &&
|
|
|
|
(!hasH264 || !HaveGMPFor(aGMPS,
|
|
|
|
NS_ConvertUTF16toUTF8(aKeySystem),
|
2014-12-09 23:35:26 +03:00
|
|
|
NS_LITERAL_CSTRING(GMP_API_DECRYPTOR),
|
2014-11-18 12:13:02 +03:00
|
|
|
NS_LITERAL_CSTRING("h264")));
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
MediaKeySystemAccess::IsSupported(const nsAString& aKeySystem,
|
|
|
|
const Sequence<MediaKeySystemOptions>& aOptions)
|
|
|
|
{
|
|
|
|
nsCOMPtr<mozIGeckoMediaPluginService> mps =
|
|
|
|
do_GetService("@mozilla.org/gecko-media-plugin-service;1");
|
|
|
|
if (NS_WARN_IF(!mps)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < aOptions.Length(); i++) {
|
|
|
|
const MediaKeySystemOptions& options = aOptions[i];
|
|
|
|
if (!options.mInitDataType.EqualsLiteral("cenc")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!options.mAudioCapability.IsEmpty() ||
|
|
|
|
!options.mVideoCapability.IsEmpty()) {
|
|
|
|
// Don't support any capabilites until we know we have a CDM with
|
|
|
|
// capabilities...
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!options.mAudioType.IsEmpty() &&
|
|
|
|
!IsPlayableWithGMP(mps, aKeySystem, options.mAudioType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!options.mVideoType.IsEmpty() &&
|
|
|
|
!IsPlayableWithGMP(mps, aKeySystem, options.mVideoType)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our sandbox provides an origin specific unique identifier, and the
|
|
|
|
// ability to persist data. We don't yet have a way to turn those off
|
|
|
|
// and on for specific GMPs/CDMs, so we don't check the uniqueidentifier
|
|
|
|
// and stateful attributes here.
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:52:42 +03:00
|
|
|
/* static */
|
|
|
|
void
|
2015-02-17 00:25:11 +03:00
|
|
|
MediaKeySystemAccess::NotifyObservers(nsIDOMWindow* aWindow,
|
|
|
|
const nsAString& aKeySystem,
|
2015-02-13 22:52:42 +03:00
|
|
|
MediaKeySystemStatus aStatus)
|
|
|
|
{
|
|
|
|
RequestMediaKeySystemAccessNotification data;
|
|
|
|
data.mKeySystem = aKeySystem;
|
|
|
|
data.mStatus = aStatus;
|
|
|
|
nsAutoString json;
|
|
|
|
data.ToJSON(json);
|
|
|
|
nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
|
|
|
|
if (obs) {
|
2015-02-17 00:25:11 +03:00
|
|
|
obs->NotifyObservers(aWindow, "mediakeys-request", json.get());
|
2015-02-13 22:52:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 12:13:02 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|