Skip to content

Commit c06475c

Browse files
committed
extend json parsing to different formats, improve the rendering of logo
1 parent d9ff422 commit c06475c

2 files changed

Lines changed: 47 additions & 38 deletions

File tree

packages/mux-video/src/index.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getChapters,
2828
toPlaybackIdFromSrc,
2929
toPlaybackIdParts,
30+
fetchMetadata,
3031
// isMuxVideoSrc,
3132
} from '@mux/playback-core';
3233
import type {
@@ -70,6 +71,7 @@ export const Attributes = {
7071
ASSET_START_TIME: 'asset-start-time',
7172
ASSET_END_TIME: 'asset-end-time',
7273
METADATA_URL: 'metadata-url',
74+
METADATA: 'metadata',
7375
PLAYBACK_ID: 'playback-id',
7476
PLAYER_SOFTWARE_NAME: 'player-software-name',
7577
PLAYER_SOFTWARE_VERSION: 'player-software-version',
@@ -113,10 +115,15 @@ class MuxVideoBaseElement extends CustomVideoElement implements Partial<MuxMedia
113115
#errorTranslator?: (errorEvent: any) => any;
114116
#logo: string = '';
115117

118+
static getLogoHTML(logoValue: string | null): string {
119+
if (!logoValue || logoValue === 'false') return '';
120+
return logoValue === 'default' ? muxLogo : `<img class="logo" part="logo" src="${logoValue}" />`;
121+
}
122+
116123
static getTemplateHTML(attrs: Record<string, string> = {}) {
117124
const template = super.getTemplateHTML(attrs);
118125

119-
const logoHTML = this.prototype.getLogoHTML(attrs[Attributes.LOGO] ?? null);
126+
const logoHTML = this.getLogoHTML(attrs[Attributes.LOGO] ?? null);
120127
return `
121128
${template}
122129
<style>
@@ -743,7 +750,6 @@ class MuxVideoBaseElement extends CustomVideoElement implements Partial<MuxMedia
743750
}
744751

745752
set logo(val) {
746-
console.log('must set logo');
747753
if (val) {
748754
this.setAttribute(Attributes.LOGO, val);
749755
} else {
@@ -825,22 +831,7 @@ class MuxVideoBaseElement extends CustomVideoElement implements Partial<MuxMedia
825831
}
826832
case Attributes.METADATA_URL:
827833
if (newValue) {
828-
fetch(newValue)
829-
.then((resp) => resp.json())
830-
.then((json) => {
831-
this.metadata = json;
832-
const metadata = json?.[0]?.metadata ?? [];
833-
const planMeta = metadata.find((m: { value: string }) => m.value === 'mux-free-plan');
834-
835-
if (planMeta) {
836-
const event = new Event('setdefaultlogo', {
837-
bubbles: true,
838-
composed: true,
839-
});
840-
this.dispatchEvent(event);
841-
}
842-
})
843-
.catch(() => console.error(`Unable to load or parse metadata JSON from metadata-url ${newValue}!`));
834+
fetchMetadata(this as HTMLMediaElement, newValue);
844835
}
845836
break;
846837
case Attributes.STREAM_TYPE:
@@ -866,19 +857,12 @@ class MuxVideoBaseElement extends CustomVideoElement implements Partial<MuxMedia
866857
}
867858
}
868859

869-
getLogoHTML(logoValue: string | null = null): string {
870-
const logo = logoValue ?? this.#logo ?? this.logo;
871-
if (!logo || logo === 'false') return '';
872-
873-
return logo === 'default' ? muxLogo : `<img class="logo" part="logo" src="${logo}" />`;
874-
}
875-
876860
updateLogo() {
877861
if (!this.shadowRoot) return;
878862
const slotLogo = this.shadowRoot.querySelector('slot[name="logo"]');
879863
if (!slotLogo) return;
880864

881-
const logoHTML = this.getLogoHTML();
865+
const logoHTML = (this.constructor as typeof MuxVideoElement).getLogoHTML(!!this.#logo ? this.#logo : this.logo);
882866
(slotLogo as HTMLElement).innerHTML = logoHTML;
883867
}
884868

packages/playback-core/src/index.ts

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ export const getError = (mediaEl: HTMLMediaElement) => {
427427
return muxMediaState.get(mediaEl)?.error;
428428
};
429429

430+
export const getMetadata = (mediaEl: HTMLMediaElement) => {
431+
return muxMediaState.get(mediaEl)?.metadata;
432+
};
433+
430434
export const getStreamType = (mediaEl: HTMLMediaElement) => {
431435
return muxMediaState.get(mediaEl)?.streamType ?? StreamTypes.UNKNOWN;
432436
};
@@ -1515,24 +1519,22 @@ const getErrorFromHlsErrorData = (
15151519
export const fetchMetadata = (el: HTMLMediaElement, metadataUrl: string) => {
15161520
fetch(metadataUrl)
15171521
.then((resp) => {
1518-
if (!resp.ok) {
1519-
throw resp;
1520-
}
1522+
if (!resp.ok) throw resp;
15211523
return resp.json();
15221524
})
15231525
.then((json) => {
1526+
const metadata = extractMetadata(json);
15241527
if ('metadata' in el) {
1525-
(el as any).metadata = json;
1528+
(el as any).metadata = {
1529+
...(el as any).metadata,
1530+
...metadata,
1531+
};
15261532
}
1527-
const metadata = json?.[0]?.metadata ?? [];
1528-
const planMeta = metadata.find((m: { value: string }) => m.value === 'mux-free-plan');
15291533

1530-
const event = new Event('setdefaultlogo', {
1531-
bubbles: true,
1532-
composed: true,
1533-
});
1534-
1535-
if (planMeta) el.dispatchEvent(event);
1534+
if (metadata['com.mux.video.branding'] === 'mux-free-plan') {
1535+
const event = new Event('setdefaultlogo', { bubbles: true, composed: true });
1536+
el.dispatchEvent(event);
1537+
}
15361538
})
15371539
.catch((e) => {
15381540
el.mux?.emit('error', {
@@ -1542,3 +1544,26 @@ export const fetchMetadata = (el: HTMLMediaElement, metadataUrl: string) => {
15421544
});
15431545
});
15441546
};
1547+
1548+
const extractMetadata = (json: any): Record<string, string> => {
1549+
const metadata: Record<string, string> = {};
1550+
1551+
if (Array.isArray(json) && json.length > 0) {
1552+
const first = json[0];
1553+
if (Array.isArray(first.metadata)) {
1554+
for (const item of first.metadata) {
1555+
if (item.key && item.value) {
1556+
metadata[item.key] = item.value;
1557+
}
1558+
}
1559+
} else {
1560+
for (const [key, value] of Object.entries(first)) {
1561+
if (typeof value === 'string' || typeof value === 'number') {
1562+
metadata[key] = String(value);
1563+
}
1564+
}
1565+
}
1566+
}
1567+
1568+
return metadata;
1569+
};

0 commit comments

Comments
 (0)