From 1a3d663cc56a54b8517eb5f82e7892d37db3082e Mon Sep 17 00:00:00 2001 From: absidue <48293849+absidue@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:43:25 +0100 Subject: [PATCH] perf: Optimise DASH manifest generation (#870) --- src/utils/DashManifest.tsx | 54 +++++++++++++++++++------------------- src/utils/DashUtils.ts | 32 +++++++--------------- 2 files changed, 37 insertions(+), 49 deletions(-) diff --git a/src/utils/DashManifest.tsx b/src/utils/DashManifest.tsx index 659f8e47..3cea53fa 100644 --- a/src/utils/DashManifest.tsx +++ b/src/utils/DashManifest.tsx @@ -32,13 +32,13 @@ async function OTFPostLiveDvrSegmentInfo({ info }: { info: FSegmentInfo }) { const template = await info.getSegmentTemplate(); - return - + { template.timeline.map((segment_duration) => ( )) } - - ; + + ; } function SegmentInfo({ info }: { info: FSegmentInfo }) { @@ -56,12 +56,12 @@ function SegmentInfo({ info }: { info: FSegmentInfo }) { return ; } return <> - + {info.base_url} - - + + - + ; } @@ -87,7 +87,7 @@ async function DashManifest({ // XXX: DASH spec: https://standards.iso.org/ittf/PubliclyAvailableStandards/c083314_ISO_IEC%2023009-1_2022(en).zip - return { audio_sets.map((set, index) => ( - @@ -140,7 +140,7 @@ async function DashManifest({ > { rep.channels && - @@ -149,12 +149,12 @@ async function DashManifest({ )) } - + )) } { video_sets.map((set, index) => ( - { set.color_info.primaries && - } { set.color_info.transfer_characteristics && - } { set.color_info.matrix_coefficients && - @@ -199,12 +199,12 @@ async function DashManifest({ )) } - + )) } { image_sets.map(async (set, index) => { - return - - )) } - ; + ; }) } { text_sets.map((set, index) => { - return - + {set.representation.base_url} - + - ; + ; }) } - ; + ; } export function toDash( diff --git a/src/utils/DashUtils.ts b/src/utils/DashUtils.ts index 869b2ba0..744147a2 100644 --- a/src/utils/DashUtils.ts +++ b/src/utils/DashUtils.ts @@ -33,11 +33,7 @@ function escapeXMLString(str: string) { } function normalizeTag(tag: string) { - if (tag === 'mpd') return 'MPD'; - if (tag === 'base-url') return 'BaseURL'; - - const sections = tag.split('-'); - return sections.map((section) => section.charAt(0).toUpperCase() + section.slice(1)).join(''); + return tag.charAt(0).toUpperCase() + tag.slice(1); } export function createElement( @@ -45,7 +41,7 @@ export function createElement( props: { [key: string] : unknown } | null | undefined, ...children: DashChild[] ): DashNode | Promise { - const normalizedChildren = children.flat().map((child) => typeof child === 'string' ? createTextElement(child) : child); + const normalizedChildren = children.flat(); if (typeof tagNameOrFunction === 'function') { return tagNameOrFunction({ ...props, children: normalizedChildren }); @@ -60,26 +56,18 @@ export function createElement( }; } -export function createTextElement(text: string): DashNode { - return { - type: 'TEXT_ELEMENT', - props: { nodeValue: text } - }; -} - -export async function renderElementToString(element: DashNode): Promise { - if (element.type === 'TEXT_ELEMENT') - return escapeXMLString(typeof element.props.nodeValue === 'string' ? element.props.nodeValue : ''); +export async function renderElementToString(element: DashNode | string): Promise { + if (typeof element === 'string') + return escapeXMLString(element); let dom = `<${element.type}`; if (element.props) { - const properties = Object.keys(element.props) - .filter((key) => ![ 'children', 'nodeValue' ].includes(key) && element.props[key] !== undefined) - .map((name) => `${name}="${escapeXMLString(`${element.props[name]}`)}"`); - - if (properties.length > 0) - dom += ` ${properties.join(' ')}`; + for (const key of Object.keys(element.props)) { + if (key !== 'children' && element.props[key] !== undefined) { + dom += ` ${key}="${escapeXMLString(`${element.props[key]}`)}"`; + } + } } if (element.props.children) {