Skip to content

Commit

Permalink
Implement MediaKeys.getMetrics()
Browse files Browse the repository at this point in the history
  • Loading branch information
sideb0ard committed Feb 5, 2025
1 parent 5643e1e commit 7298bc4
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 3 deletions.
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 void GetMetrics(std::string &metrics_results) {}
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)
};

} // namespace blink
Expand Down
12 changes: 9 additions & 3 deletions third_party/blink/renderer/bindings/idl_in_modules.gni
Original file line number Diff line number Diff line change
Expand Up @@ -1295,13 +1295,19 @@ if (target_os != "android") {
"abspath")
}

# SourceBufferWriteHead
# An extension to the SourceBuffer interface that allows web apps to check the
# highest presentation timestamp written to the Renderer.
if (is_cobalt && use_starboard_media) {
# SourceBufferWriteHead
# An extension to the SourceBuffer interface that allows web apps to check the
# highest presentation timestamp written to the Renderer.
static_idl_files_in_modules += get_path_info(
[ "//third_party/blink/renderer/modules/mediasource/source_buffer_write_head.idl" ],
"abspath")

# MediaKeys
# An extension to MediaKeys, getMetrics, which is used in YTS golden ATV tests.
static_idl_files_in_modules += get_path_info(
[ "//third_party/blink/renderer/modules/encryptedmedia/media_keys_extensions.idl" ],
"abspath")
}

# Statically-defined (not runtime-generated) IDL files in 'modules' component.
Expand Down
11 changes: 11 additions & 0 deletions third_party/blink/renderer/modules/encryptedmedia/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# found in the LICENSE file.

import("//third_party/blink/renderer/modules/modules.gni")
if (is_cobalt) {
import("//starboard/build/buildflags.gni")
}

blink_modules_sources("encryptedmedia") {
sources = [
Expand Down Expand Up @@ -34,4 +37,12 @@ blink_modules_sources("encryptedmedia") {
"navigator_request_media_key_system_access.h",
]
deps = [ "//services/metrics/public/cpp:ukm_builders" ]

if (is_cobalt && use_starboard_media) {
sources += [
"media_keys_get_metrics.cc",
"media_keys_get_metrics.h",
]
deps += [ "//starboard($starboard_toolchain)" ]
}
}
12 changes: 12 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,18 @@ ScriptPromise MediaKeys::getStatusForPolicy(
return promise;
}

#if BUILDFLAG(USE_STARBOARD_MEDIA)
NotShared<DOMUint8Array> MediaKeys::getMetrics(ExceptionState& exception_state) {
std::string metrics;
cdm_->GetMetrics(metrics);

const unsigned char* unsigned_buffer =
reinterpret_cast<const unsigned char*>(metrics.c_str());

return NotShared<DOMUint8Array>(DOMUint8Array::Create(unsigned_buffer, metrics.size()));
}
#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 @@ -43,6 +43,11 @@
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

#if BUILDFLAG(USE_STARBOARD_MEDIA)
#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

namespace blink {

class ExceptionState;
Expand Down Expand Up @@ -80,6 +85,10 @@ class MediaKeys : public ScriptWrappable,
const MediaKeysPolicy*,
ExceptionState&);

#if BUILDFLAG(USE_STARBOARD_MEDIA)
NotShared<DOMUint8Array> 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
@@ -0,0 +1,14 @@
// Copyright 2025 The Cobalt Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

[
Exposed=Window,
ActiveScriptWrappable,
SecureContext,
ImplementedAs=MediaKeysGetMetrics
] partial interface MediaKeys {
[RaisesException] Uint8Array getMetrics();
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 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/encryptedmedia/media_keys_get_metrics.h"

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

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

namespace blink {

// static
NotShared<DOMUint8Array> 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_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_

#include "third_party/blink/renderer/core/typed_arrays/array_buffer_view_helpers.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"

namespace blink {

class MediaKeys;

class MediaKeysGetMetrics {
STATIC_ONLY(MediaKeysGetMetrics);

public:
static NotShared<DOMUint8Array> getMetrics(MediaKeys&, ExceptionState&);
};

} // namespace blink

#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_ENCRYPTEDMEDIA_MEDIA_KEYS_GET_METRICS_H_
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,26 @@ void WebContentDecryptionModuleImpl::GetStatusForPolicy(
kGetStatusForPolicyUMAName));
}

#if BUILDFLAG(USE_STARBOARD_MEDIA)
void WebContentDecryptionModuleImpl::GetMetrics(std::string &metrics_results) {
auto cdm_context_ref = adapter_->GetCdmContextRef();
if (!cdm_context_ref) {
NOTREACHED();
return;
}
auto* cdm_context = cdm_context_ref->GetCdmContext();
DCHECK(cdm_context);

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

int metrics_size = -1;
const void* metrics = SbDrmGetMetrics(sbdrm, &metrics_size);
DCHECK(metrics_size > 0);
metrics_results.assign(static_cast<const char*>(metrics), metrics_size);
}
#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)
void GetMetrics(std::string &metrics_results) override;
#endif // BUILDFLAG(USE_STARBOARD_MEDIA)

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

Expand Down

0 comments on commit 7298bc4

Please sign in to comment.