Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MediaKeys.getMetrics() #4837

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class BLINK_PLATFORM_EXPORT WebContentDecryptionModule {

virtual void GetStatusForPolicy(const WebString& min_hdcp_version,
WebContentDecryptionModuleResult) = 0;

#if BUILDFLAG(USE_STARBOARD_MEDIA)
virtual bool GetMetrics(std::string& metrics_results) = 0;
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)
};

} // namespace blink
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/bindings/idl_in_modules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ if (is_cobalt) {
"//third_party/blink/renderer/modules/cobalt/window_h_5_vcc.idl",
"//third_party/blink/renderer/modules/cobalt/h5vcc_system/h_5_vcc_system.idl",
"//third_party/blink/renderer/modules/cobalt/h5vcc_runtime/h_5_vcc_runtime.idl",
"//third_party/blink/renderer/modules/cobalt/encryptedmedia/media_keys_extensions.idl",
],
"abspath")
}
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/modules/cobalt/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ blink_modules_sources("h_5_vcc") {
]
deps = [
"//third_party/blink/renderer/modules/cobalt/crash_annotator",
"//third_party/blink/renderer/modules/cobalt/encryptedmedia",
"//third_party/blink/renderer/modules/cobalt/h5vcc_runtime",
"//third_party/blink/renderer/modules/cobalt/h5vcc_system",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2025 The Cobalt Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//third_party/blink/renderer/modules/modules.gni")

blink_modules_sources("encryptedmedia") {
sources = [
"media_keys_get_metrics.cc",
"media_keys_get_metrics.h",
]
deps =
[ "//third_party/blink/renderer/modules/encryptedmedia:encryptedmedia" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// https://w3c.github.io/encrypted-media/#mediakeys-interface

[
Exposed=Window,
ActiveScriptWrappable,
SecureContext,
ImplementedAs=MediaKeysGetMetrics
] partial interface MediaKeys {
[RaisesException] DOMString getMetrics();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "third_party/blink/renderer/modules/cobalt/encryptedmedia/media_keys_get_metrics.h"

#include "third_party/blink/renderer/modules/encryptedmedia/media_keys.h"

#if !BUILDFLAG(USE_STARBOARD_MEDIA)
#error "This file only works with Starboard media"
#endif // !BUILDFLAG(USE_STARBOARD_MEDIA)

namespace blink {

// static
WebString MediaKeysGetMetrics::getMetrics(MediaKeys& media_keys,
ExceptionState& exception_state) {
return media_keys.getMetrics(exception_state);
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_COBALT_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_COBALT_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_

#include "build/build_config.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

namespace blink {

class MediaKeys;

class MediaKeysGetMetrics {
STATIC_ONLY(MediaKeysGetMetrics);

public:
static WebString getMetrics(MediaKeys&, ExceptionState&);
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_COBALT_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_
10 changes: 10 additions & 0 deletions third_party/blink/renderer/modules/encryptedmedia/media_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ ScriptPromise MediaKeys::getStatusForPolicy(
return promise;
}

#if BUILDFLAG(USE_STARBOARD_MEDIA)
WebString MediaKeys::getMetrics(ExceptionState& exception_state) {
std::string metrics;
if (cdm_->GetMetrics(metrics)) {
return WebString::FromUTF8(metrics);
}
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError, "CDM returned empty GetMetrics()");
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

void MediaKeys::GetStatusForPolicyTask(const String& min_hdcp_version,
ContentDecryptionModuleResult* result) {
DVLOG(MEDIA_KEYS_LOG_LEVEL) << __func__ << ": " << min_hdcp_version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class MediaKeys : public ScriptWrappable,
const MediaKeysPolicy*,
ExceptionState&);

#if BUILDFLAG(USE_STARBOARD_MEDIA)
WebString getMetrics(ExceptionState&);
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

// Indicates that the provided HTMLMediaElement wants to use this object.
// Returns true if no other HTMLMediaElement currently references this
// object, false otherwise. If true, will take a weak reference to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ void WebContentDecryptionModuleImpl::GetStatusForPolicy(
kGetStatusForPolicyUMAName));
}

#if BUILDFLAG(USE_STARBOARD_MEDIA)
bool WebContentDecryptionModuleImpl::GetMetrics(std::string& metrics_results) {
auto cdm_context_ref = adapter_->GetCdmContextRef();
DCHECK(cdm_context_ref);

auto* cdm_context = cdm_context_ref->GetCdmContext();
DCHECK(cdm_context);

auto sb_drm = cdm_context->GetSbDrmSystem();
DCHECK(SbDrmSystemIsValid(sb_drm));

int size = 0;
const uint8_t* raw_metrics =
static_cast<const uint8_t*>(SbDrmGetMetrics(sb_drm, &size));
if (!raw_metrics || size < 0) {
return false;
}

metrics_results.assign(raw_metrics, raw_metrics + size);
return true;
}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

std::unique_ptr<media::CdmContextRef>
WebContentDecryptionModuleImpl::GetCdmContextRef() {
return adapter_->GetCdmContextRef();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ class PLATFORM_EXPORT WebContentDecryptionModuleImpl
void GetStatusForPolicy(const WebString& min_hdcp_version_string,
WebContentDecryptionModuleResult result) override;

#if BUILDFLAG(USE_STARBOARD_MEDIA)
bool GetMetrics(std::string& metrics) override;
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

std::unique_ptr<media::CdmContextRef> GetCdmContextRef();
media::CdmConfig GetCdmConfig() const;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2025 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "third_party/blink/renderer/platform/media/web_content_decryption_module_impl.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "media/starboard/starboard_cdm_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/media/cdm_session_adapter.h"

namespace blink {

const char* kKeyType = "com.widevine.alpha";

class WebContentDecryptionModuleImplTest : public testing::Test {
public:
void SetUp() override {}

void OnWebCdmCreated(WebContentDecryptionModule* cdm,
const std::string& error_message) {
cdm_ = cdm;
}

protected:
media::CdmConfig cdm_config_{kKeyType, false, false, false};
std::unique_ptr<media::StarboardCdmFactory> cdm_factory_;
scoped_refptr<CdmSessionAdapter> adapter_;
WebContentDecryptionModule* cdm_{nullptr};

base::WeakPtrFactory<WebContentDecryptionModuleImplTest> weak_factory_{this};
};

TEST_F(WebContentDecryptionModuleImplTest, IsValid) {
EXPECT_NE(cdm_factory_, nullptr);

base::test::TaskEnvironment task_environment_;
cdm_factory_ = std::make_unique<media::StarboardCdmFactory>();
adapter_ = new CdmSessionAdapter();

adapter_->CreateCdm(
cdm_factory_.get(), cdm_config_,
base::BindOnce(&WebContentDecryptionModuleImplTest::OnWebCdmCreated,
weak_factory_.GetWeakPtr()));
Comment on lines +57 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro-nit: We could use here more GTest style:

     // l.34...
     MOCK_METHOD2(OnWebCdmCreated, void(WebContentDecryptionModule*,
                                   const std::string&);

  
   EXPECT_CALL(this, OnWebCdmCreated(::testing:: NotNull(), _)).Times(1);

   adapter_->CreateCdm(
       cdm_factory_.get(), cdm_config_,
       base::BindOnce(&WebContentDecryptionModuleImplTest::OnWebCdmCreated,
                      weak_factory_.GetWeakPtr());
   base::RunLoop().RunUntilIdle();

   // No need for EXPECT_NE(cdm_, nullptr);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the suggestion, I'll amend this unittest in follow-up PR for b/395905524

base::RunLoop().RunUntilIdle();

EXPECT_NE(cdm_, nullptr);
}

} // namespace blink
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>MediaKeys.getMetrics</title>
<script src="encrypted-media-utils.js"></script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
</head>
<body>
<video></video>
<script>
async_test(function(test)
{
const opts = [{
initDataTypes: ['cenc', 'sinf', 'keyids'],
encryptionScheme: 'cbcs-1-9',
videoCapabilities: [{
contentType: 'video/mp4; codecs="avc1.640028"'
}],
audioCapabilities: [{
contentType: 'audio/mp4; codecs="mp4a.40.2"'
}],
}];

opts[0].videoCapabilities[0].robustness = 'SW_SECURE_DECODE';
opts[0].audioCapabilities[0].robustness = 'SW_SECURE_CRYPTO';

return navigator.requestMediaKeySystemAccess('com.widevine.alpha', opts)
.then(function(keySystemAccess) {
return keySystemAccess.createMediaKeys();
})
.then(function (mediaKeys) {
assert_not_equals(mediaKeys, null);
const blob = mediaKeys.getMetrics();
assert_true(blob.length > 0);
test.done();
}).catch(function(error) {
forceTestFailureFromPromise(test, error);
});
}, 'GetMetrics from MediaKeys.');
</script>
</body>
</html>
Loading