Bug 1672751 - record HLS playback info on Android. r=alwu

Differential Revision: https://phabricator.services.mozilla.com/D204936
This commit is contained in:
John Lin 2024-03-23 05:16:33 +00:00
parent 2927836b40
commit 4567c07891
5 changed files with 114 additions and 16 deletions

View File

@ -6,6 +6,7 @@
#include "DecoderTraits.h"
#include "MediaContainerType.h"
#include "mozilla/glean/GleanMetrics.h"
#include "mozilla/Preferences.h"
#include "OggDecoder.h"
@ -127,11 +128,11 @@ static CanPlayStatus CanHandleCodecsType(
static CanPlayStatus CanHandleMediaType(
const MediaContainerType& aType, DecoderDoctorDiagnostics* aDiagnostics) {
if (DecoderTraits::IsHttpLiveStreamingType(aType)) {
Telemetry::Accumulate(Telemetry::MEDIA_HLS_CANPLAY_REQUESTED, true);
glean::hls::canplay_requested.Add();
}
#ifdef MOZ_ANDROID_HLS_SUPPORT
if (HLSDecoder::IsSupportedType(aType)) {
Telemetry::Accumulate(Telemetry::MEDIA_HLS_CANPLAY_SUPPORTED, true);
glean::hls::canplay_supported.Add();
return CANPLAY_MAYBE;
}
#endif

View File

@ -18,9 +18,11 @@
#include "mozilla/java/GeckoHLSResourceWrapperNatives.h"
#include "nsContentUtils.h"
#include "nsIChannel.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsThreadUtils.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/glean/GleanMetrics.h"
#include "mozilla/NullPrincipal.h"
#include "mozilla/StaticPrefs_media.h"
@ -169,7 +171,8 @@ nsresult HLSDecoder::Load(nsIChannel* aChannel) {
mChannel = aChannel;
nsCString spec;
Unused << mURI->GetSpec(spec);
;
mUsageRecorded = false;
HLSResourceCallbacksSupport::Init();
mJavaCallbacks = java::GeckoHLSResourceWrapper::Callbacks::New();
mCallbackSupport = new HLSResourceCallbacksSupport(this);
@ -253,13 +256,36 @@ void HLSDecoder::NotifyDataArrived() {
void HLSDecoder::NotifyLoad(nsCString aMediaUrl) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
UpdateCurrentPrincipal(aMediaUrl);
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aMediaUrl.Data());
NS_ENSURE_SUCCESS_VOID(rv);
RecordMediaUsage(uri);
UpdateCurrentPrincipal(uri);
}
void HLSDecoder::RecordMediaUsage(nsIURI* aMediaUri) {
if (mUsageRecorded) {
return;
}
nsresult rv;
nsCOMPtr<nsIURL> url = do_QueryInterface(aMediaUri, &rv);
NS_ENSURE_SUCCESS_VOID(rv);
// TODO: get hostname. See bug 1887053.
nsAutoCString mediaExt;
Unused << url->GetFileExtension(mediaExt);
glean::hls::MediaLoadExtra extra = {.mediaExtension = Some(mediaExt.get())};
glean::hls::media_load.Record(Some(extra));
mUsageRecorded = true;
}
// Should be called when the decoder loads media from a URL to ensure the
// principal of the media element is appropriately set for CORS.
void HLSDecoder::UpdateCurrentPrincipal(nsCString aMediaUrl) {
nsCOMPtr<nsIPrincipal> principal = GetContentPrincipal(aMediaUrl);
void HLSDecoder::UpdateCurrentPrincipal(nsIURI* aMediaUri) {
nsCOMPtr<nsIPrincipal> principal = GetContentPrincipal(aMediaUri);
MOZ_DIAGNOSTIC_ASSERT(principal);
// Check the subsumption of old and new principals. Should be either
@ -280,12 +306,8 @@ void HLSDecoder::UpdateCurrentPrincipal(nsCString aMediaUrl) {
}
already_AddRefed<nsIPrincipal> HLSDecoder::GetContentPrincipal(
nsCString aMediaUrl) {
nsCOMPtr<nsIURI> uri;
nsresult rv = NS_NewURI(getter_AddRefs(uri), aMediaUrl.Data());
NS_ENSURE_SUCCESS(rv, nullptr);
nsIURI* aMediaUri) {
RefPtr<dom::HTMLMediaElement> element = GetOwner()->GetMediaElement();
NS_ENSURE_SUCCESS(rv, nullptr);
nsSecurityFlags securityFlags =
element->ShouldCheckAllowOrigin()
? nsILoadInfo::SEC_REQUIRE_CORS_INHERITS_SEC_CONTEXT
@ -294,9 +316,9 @@ already_AddRefed<nsIPrincipal> HLSDecoder::GetContentPrincipal(
securityFlags |= nsILoadInfo::SEC_COOKIES_INCLUDE;
}
nsCOMPtr<nsIChannel> channel;
rv = NS_NewChannel(getter_AddRefs(channel), uri,
static_cast<dom::Element*>(element), securityFlags,
nsIContentPolicy::TYPE_INTERNAL_VIDEO);
nsresult rv = NS_NewChannel(
getter_AddRefs(channel), aMediaUri, static_cast<dom::Element*>(element),
securityFlags, nsIContentPolicy::TYPE_INTERNAL_VIDEO);
NS_ENSURE_SUCCESS(rv, nullptr);
nsCOMPtr<nsIPrincipal> principal;
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();

View File

@ -61,8 +61,9 @@ class HLSDecoder final : public MediaDecoder {
return true;
}
void UpdateCurrentPrincipal(nsCString aMediaUrl);
already_AddRefed<nsIPrincipal> GetContentPrincipal(nsCString aMediaUrl);
void UpdateCurrentPrincipal(nsIURI* aMediaUri);
already_AddRefed<nsIPrincipal> GetContentPrincipal(nsIURI* aMediaUri);
void RecordMediaUsage(nsIURI* aMediaUri);
static size_t sAllocatedInstances; // Access only in the main thread.
@ -72,6 +73,9 @@ class HLSDecoder final : public MediaDecoder {
java::GeckoHLSResourceWrapper::Callbacks::GlobalRef mJavaCallbacks;
RefPtr<HLSResourceCallbacksSupport> mCallbackSupport;
nsCOMPtr<nsIPrincipal> mContentPrincipal;
// There can be multiple media files loaded for one HLS content. Use this flag
// to ensure we only record once per content.
bool mUsageRecorded;
};
} // namespace mozilla

View File

@ -0,0 +1,70 @@
# 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/.
# Adding a new metric? We have docs for that!
# https://firefox-source-docs.mozilla.org/toolkit/components/glean/user/new_definitions_file.html
---
$schema: moz://mozilla.org/schemas/glean/metrics/2-0-0
$tags:
- 'Core :: Audio/Video'
hls:
canplay_requested:
type: counter
description: >
Record when a page requests canPlayType for a HLS media type.
metadata:
tags:
- 'Core :: Audio/Video: Playback'
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751#8
data_sensitivity:
- technical
notification_emails:
- media-alerts@mozilla.com
expires: 132
canplay_supported:
type: counter
description: >
Record when a canPlayType request supports HLS.
metadata:
tags:
- 'Core :: Audio/Video: Playback'
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751#8
data_sensitivity:
- technical
notification_emails:
- media-alerts@mozilla.com
expires: 132
media_load:
type: event
description: >
Record the information about the HLS playback on Android using ExoPlayer.
The value of this event contains the media format.
metadata:
tags:
- 'Core :: Audio/Video: Playback'
bugs:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751
data_reviews:
- https://bugzilla.mozilla.org/show_bug.cgi?id=1672751#8
data_sensitivity:
- technical
notification_emails:
- media-alerts@mozilla.com
extra_keys:
media_extension:
description: >
The extension in the media file name, could be 'ts' (for MPEG-TS), 'mp4',
'aac', 'mp3', ...
type: string
expires: 132

View File

@ -18,6 +18,7 @@ gecko_metrics = [
"browser/base/content/metrics.yaml",
"docshell/base/metrics.yaml",
"dom/base/use_counter_metrics.yaml",
"dom/media/hls/metrics.yaml",
"dom/media/metrics.yaml",
"dom/media/webrtc/metrics.yaml",
"dom/metrics.yaml",