Skip to content

Commit

Permalink
Merge pull request #7 from THEOplayer/feature/nielsen_dash
Browse files Browse the repository at this point in the history
Feature/nielsen dash
  • Loading branch information
tvanlaerhoven authored Mar 27, 2024
2 parents 57ab883 + 044dd06 commit 58ed70b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-buttons-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theoplayer/nielsen-connector-web": patch
---

Added support for DASH emsg cue payloads.
56 changes: 49 additions & 7 deletions nielsen/src/integration/NielsenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { loadNielsenLibrary } from '../nielsen/NOLBUNDLE';
import { AdMetadata, ContentMetadata, NielsenOptions } from '../nielsen/Types';
import { getAdType } from '../utils/Util';

const EMSG_PRIV_SUFFIX = "PRIV{";
const EMSG_PAYLOAD_SUFFIX = "payload=";

export class NielsenHandler {
private player: ChromelessPlayer;

Expand All @@ -12,6 +15,8 @@ export class NielsenHandler {

private duration: number = NaN;

private decoder = new TextDecoder('utf-8');

constructor(player: ChromelessPlayer, appId: string, instanceName: string, options?: NielsenOptions) {
this.player = player;
this.nSdkInstance = loadNielsenLibrary(appId, instanceName, options);
Expand Down Expand Up @@ -91,8 +96,8 @@ export class NielsenHandler {
private onAddTrack = (event: AddTrackEvent) => {
if (event.track.kind === 'metadata') {
const track = event.track as TextTrack;
if (track.type === 'id3') {
// || track.type === 'emsg') {
if (track.type === 'id3' || track.type === 'emsg') {
// Make sure we get cues.
if (track.mode === 'disabled') {
track.mode = 'hidden';
}
Expand All @@ -103,15 +108,52 @@ export class NielsenHandler {

private onEnterCue = (event: TextTrackEnterCueEvent) => {
const { cue } = event;
if (cue.track.type === 'id3') {
if (cue.content.id === 'PRIV' && cue.content.ownerIdentifier.indexOf('www.nielsen.com') !== -1) {
this.nSdkInstance.ggPM('sendID3', cue.content.ownerIdentifier);
if (cue.content) {
if (cue.track.type === 'id3') {
this.handleNielsenId3Payload(cue.content);
} else if (cue.track.type === 'emsg') {
this.handleNielsenEmsgPayload(cue.content);
}
} else {
// TODO emsg is not supported for now
}
};

private handleNielsenId3Payload = (content: any) => {
if (content.id === 'PRIV' && content.ownerIdentifier.indexOf('www.nielsen.com') !== -1) {
this.nSdkInstance.ggPM('sendID3', content.ownerIdentifier);
}
}

private handleNielsenEmsgPayload = (content: any) => {
const cueContentText = this.decoder.decode(content);
if (cueContentText.startsWith('type=nielsen_tag')) {
// extract payload
const base64Index = cueContentText.indexOf(EMSG_PAYLOAD_SUFFIX);
try {
if (base64Index !== -1) {
const base64Payload = cueContentText.substring(base64Index + EMSG_PAYLOAD_SUFFIX.length);

// sanitise base64payload before decoding, remove null and %-encoded chars.
// eslint-disable-next-line no-control-regex
const sanitizedBase64Payload = base64Payload.replace(/\x00|%[0-9A-Fa-f]{2}/g, '');
const payload = atob(sanitizedBase64Payload);

// sanitise payload before submitting:
// - only allow printable characters within ASCII 32 to 126 range.
// - no character beyond the last digit.
// - drop everything before ID3 PRIV{
let sanitizedPayload = payload.replace(/[^ -~]|\D+$/g, '');
const privIndex = sanitizedPayload.indexOf(EMSG_PRIV_SUFFIX);
sanitizedPayload = privIndex !== -1 ? sanitizedPayload.substring(privIndex + EMSG_PRIV_SUFFIX.length) : sanitizedPayload;

// send payload. Note that there is no separate method for sending emsg content.
this.nSdkInstance.ggPM('sendID3', sanitizedPayload);
}
} catch (error) {
console.error("NielsenConnector", "Failed to parse Nielsen payload", error);
}
}
}

private onAdBegin = () => {
const currentAd = this.player.ads!.currentAds.filter((ad: Ad) => ad.type === 'linear');
const type = getAdType(this.player.ads!.currentAdBreak!);
Expand Down
2 changes: 1 addition & 1 deletion nielsen/test/pages/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Connector test page</title>
<link rel="stylesheet" type="text/css" href="/node_modules/theoplayer/ui.css" />
<script src="/node_modules/theoplayer/THEOplayer.js"></script>
<script src="/dist/THEOplayerNielsenConnector.umd.js"></script>
<script src="/dist/nielsen-connector.umd.js"></script>
</head>
<body>
<div id="THEOplayer" class="theoplayer-container video-js theoplayer-skin"></div>
Expand Down

0 comments on commit 58ed70b

Please sign in to comment.