Skip to content

Commit

Permalink
Merge branch 'main' of github.com:dzianis-dashkevich/web-media-box in…
Browse files Browse the repository at this point in the history
…to dash-parser-parent-node
  • Loading branch information
Dzianis Dashkevich committed Oct 20, 2023
2 parents 14822bf + 72b3682 commit 05352b9
Show file tree
Hide file tree
Showing 21 changed files with 758 additions and 53 deletions.
44 changes: 44 additions & 0 deletions packages/web-media-box/src/dash-parser/consts/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// All fields we want to retiive from specific Nodes
// Note not all are required.

// TODO: This file will be removed, but keeping so I can copy attributes to the
// node processors.

export interface Attribute {
name: string,
required: boolean,
default?: unknown
}

export const BaseURLAttributes: Array<Attribute> = [
{ name: 'id', required: true },
];

export const SegmentTemplateAttributes: Array<Attribute> = [
{ name: 'media', required: false },
{ name: 'index', required: false },
{ name: 'initialization', required: false },
{ name: 'bitstreamSwitching', required: false },
];

export const UTCTimingAttributes: Array<Attribute> = [
{ name: 'schemeIdUri', required: true },
{ name: 'value', required: false },
];

export const EventStreamAttributes: Array<Attribute> = [
{ name: 'schemeIdUri', required: true },
{ name: 'value', required: false },
{ name: 'xlink:href', required: false },
{ name: 'xlink:actuate', required: false, default: `onRequest` },
{ name: 'timescale', required: false },
{ name: 'presentationTimeOffset', required: false, default: 0 },
];

export const EventAttributes: Array<Attribute> = [
{ name: 'presentationTime', required: false, default: 0 },
{ name: 'id', required: false },
{ name: 'duration', required: false },
{ name: 'contentEncoding', required: false },
{ name: 'messageData', required: false },
];
8 changes: 8 additions & 0 deletions packages/web-media-box/src/dash-parser/consts/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,11 @@ export const PERIOD = 'Period';
export const ADAPTATION_SET = 'AdaptationSet';
export const BASE_URL = 'BaseURL';
export const REPRESENTATION = 'Representation';
export const SEGMENT_TEMPLATE = 'SegmentTemplate';
export const UTC_TIMING = 'UTCTiming';
export const EVENT_STREAM = 'EventStream';
export const EVENT = 'Event';

export const ACCESSIBILITY = 'Accessibility';
export const ROLE = 'Role';
export const AUDIO_CHANNEL_CONFIGURATION = 'AudioChannelConfiguration';
29 changes: 24 additions & 5 deletions packages/web-media-box/src/dash-parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ import type {
import { noop } from '../utils/fn.ts';
import { ParsedManifest } from '@/dash-parser/types/parsedManifest';
import type { TagProcessor } from '@/dash-parser/tags/base.ts';
import { ADAPTATION_SET, MPD, PERIOD, REPRESENTATION } from '@/dash-parser/consts/tags.ts';
import { AdaptationSet, Mpd, Period, Representation } from '@/dash-parser/tags/base.ts';
import {
ADAPTATION_SET,
MPD,
PERIOD,
REPRESENTATION,
UTC_TIMING,
EVENT_STREAM,
EVENT,
BASE_URL
} from '@/dash-parser/consts/tags.ts';
import {
AdaptationSet,
Mpd,
Period,
Representation,
UTCTiming
} from '@/dash-parser/tags/base.ts';
import { TagInfo } from '@/dash-parser/stateMachine.ts';
import { ignoreTagWarn, unsupportedTagWarn } from '@/dash-parser/utils/warn.ts';
import createStateMachine from '@/dash-parser/stateMachine.ts';
Expand Down Expand Up @@ -41,12 +56,15 @@ class Parser {
options.transformTagAttributes || ((tagKey, tagAttributes): Record<string, string> => tagAttributes);

this.parsedManifest = {
type: 'static',
segments: [],
representations: [],
type: 'static', //default value, could be updated after parsing
custom: {},
};

this.sharedState = {};
this.sharedState = {
mpdAttributes: {},
adaptationSetAttributes: {}
};

this.pendingProcessors = new PendingProcessors();

Expand All @@ -55,6 +73,7 @@ class Parser {
[PERIOD]: new Period(this.warnCallback),
[ADAPTATION_SET]: new AdaptationSet(this.warnCallback),
[REPRESENTATION]: new Representation(this.warnCallback),
[UTC_TIMING]: new UTCTiming(this.warnCallback)
};
}

Expand Down
250 changes: 250 additions & 0 deletions packages/web-media-box/src/dash-parser/parseAttributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
import { parseDivisionValue } from './utils/string';
import { parseDuration, parseDate } from './utils/time';

type Parsers = {
[key: string]: (value: string) => string | number;
}

const parsers: Parsers = {
/**
* Specifies the duration of the entire Media Presentation. Format is a duration string
* as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The duration in seconds
*/
mediaPresentationDuration(value: string): number {
return parseDuration(value);
},

/**
* Specifies the Segment availability start time for all Segments referred to in this
* MPD. For a dynamic manifest, it specifies the anchor for the earliest availability
* time. Format is a date string as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The date as seconds from unix epoch
*/
availabilityStartTime(value: string): number {
return parseDate(value) / 1000;
},

/**
* Specifies the smallest period between potential changes to the MPD. Format is a
* duration string as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The duration in seconds
*/
minimumUpdatePeriod(value: string): number {
return parseDuration(value);
},

/**
* Specifies the suggested presentation delay. Format is a
* duration string as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The duration in seconds
*/
suggestedPresentationDelay(value: string): number {
return parseDuration(value);
},

/**
* specifices the type of mpd. Can be either "static" or "dynamic"
*
* @param value value of attribute as a string
* @return The type as a string
*/
type(value: string): string {
return value;
},

/**
* Specifies the duration of the smallest time shifting buffer for any Representation
* in the MPD. Format is a duration string as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The duration in seconds
*/
timeShiftBufferDepth(value: string): number {
return parseDuration(value);
},

/**
* Specifies the PeriodStart time of the Period relative to the availabilityStarttime.
* Format is a duration string as specified in ISO 8601
*
* @param value value of attribute as a string
* @return The duration in seconds
*/
start(value: string): number {
return parseDuration(value);
},

/**
* Specifies the width of the visual presentation
*
* @param value value of attribute as a string
* @return The parsed width
*/
width(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the height of the visual presentation
*
* @param value value of attribute as a string
* @return The parsed height
*/
height(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the bitrate of the representation
*
* @param value value of attribute as a string
* @return The parsed bandwidth
*/
bandwidth(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the frame rate of the representation
*
* @param value value of attribute as a string
* @return The parsed frame rate
*/
frameRate(value: string): number {
return parseDivisionValue(value);
},

/**
* Specifies the number of the first Media Segment in this Representation in the Period
*
* @param value value of attribute as a string
* @return The parsed number
*/
startNumber(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the timescale in units per seconds
*
* @param value of attribute as a string
* @return The parsed timescale
*/
timescale(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the presentationTimeOffset.
*
* @param value value of the attribute as a string
* @return The parsed presentationTimeOffset
*/
presentationTimeOffset(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the constant approximate Segment duration
* NOTE: The <Period> element also contains an @duration attribute. This duration
* specifies the duration of the Period. This attribute is currently not
* supported by the rest of the parser, however we still check for it to prevent
* errors.
*
* @param value value of attribute as a string
* @return The parsed duration
*/
duration(value: string): number {
const parsedValue = parseInt(value, 10);

if (isNaN(parsedValue)) {
return parseDuration(value);
}

return parsedValue;
},

/**
* Specifies the Segment duration, in units of the value of the @timescale.
*
* @param value value of attribute as a string
* @return The parsed duration
*/
d(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the MPD start time, in @timescale units, the first Segment in the series
* starts relative to the beginning of the Period
*
* @param value value of attribute as a string
* @return The parsed time
*/
t(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the repeat count of the number of following contiguous Segments with the
* same duration expressed by the value of @d
*
* @param value value of attribute as a string
* @return The parsed number
*/
r(value: string): number {
return parseInt(value, 10);
},

/**
* Specifies the presentationTime.
*
* @param value value of the attribute as a string
* @return The parsed presentationTime
*/
presentationTime(value: string): number {
return parseInt(value, 10);
},

/**
* Default parser for all other attributes. Acts as a no-op and just returns the value
* as a string
*
* @param value value of attribute as a string
* @return Unparsed value
*/
DEFAULT(value: string): string {
return value;
}
};

/**
* Gets all the attributes and values of the provided node, parses attributes with known
* types, and returns an object with attribute names mapped to values.
*
* @param attributes The key/value pairs of attributes to parse
* @return Object with all attributes parsed
*/
export const parseAttributes = (attributes: Record<string, unknown>): Record<string, unknown> => {
interface Attrs {
[key: string]: unknown
};

const newAttributes: Attrs = {};

for (const att in attributes) {
const parseFn: (value: string) => string | number = parsers[att] || parsers.DEFAULT;
newAttributes[att] = parseFn(attributes[att] as string);
}

return newAttributes;
};
Loading

0 comments on commit 05352b9

Please sign in to comment.