Skip to content

Module: src/core/Data

The Data component is responsible for managing, parsing, and validating the media metadata used by the player. It supports single media items as well as complex playlist structures, including multiple quality levels, encodings, subtitle tracks, and overlays. It exposes an API for dynamic switching of variants or media entries, integrates MIME-type and capability checks, and handles fallback scenarios for unplayable or malformed data. This component ensures that only valid and playable streams are used, while offering flexibility through configuration options such as lenient parsing or skipping invalid entries. Additionally, it dispatches lifecycle events to signal when media is ready, parsed, or in case of errors. Note: this component is mandatory and required for normal player operations, so it cannot be switched off.

Version
1.0.1
Author
Frank Kudermann - alphanull
License
MIT
Requires
object
AsyncTask
DataError
Source
Data.js, line 20

Constructor

new Data(player, parent, optionsopt)

Creates an instance of the Data component.

Parameters:
Name Type Attributes Description
player Player

Reference to the VisionPlayer instance.

parent Player

Reference to the parent instance.

options Object optional

Additional options.

Parameters:
Name Type Attributes Description
apiKey symbol optional

Token for extended access to the player API.

Throws

If trying to disable this component.

Type Error

Members

Contains configuration options for how the media is parsed and validated.

Properties
Name Type Attributes Default Description
skipInvalidItems boolean optional false

Ignore (skip) invalid media items rather than throwing an error.

skipInvalidRepresentations boolean optional true

Ignore invalid representations instead of throwing errors for them.

skipEmptyData boolean optional false

Ignore empty media data (eg is null or undefined) and do not throw an error. Useful if you want to assign mediaData not immediatly on player instantiation.

disablePlayCheck boolean optional false

Skip any play checks and trust the source to be playable.

lenientPlayCheck boolean optional false

Check only file extensions, but do not use canPlay.

lenientPlayCheckBlob boolean optional true

Assume blob: URLs are valid without checking.

preferredQuality number | string | boolean optional

Quality setting that should be preferred when loading new media, or false to not set such a preference and use autoselect instead.

preferredLanguage string | boolean optional

Language that should be preferred when loading new media, true to use the player locale as preferred default or false to not set any preference at all.

Type
Object
Source
Data.js, line 34

Reference to the player instance.

Type
Player
Source
Data.js, line 49

Contains the players' mediaData.

Properties
Name Type Attributes Description
media Array<Data~mediaItem>

Array of media items.

currentMediaIndex number

Index of the currently active media item.

title string | Object<string, string> optional

Title of the playlist.

titleSecondary string | Object<string, string> optional

Secondary title of the playlist.

Type
Object
Source
Data.js, line 59

Reference to the root DOM element.

Type
HTMLElement
Source
Data.js, line 70

Secret key only known to the player instance and initialized components. Used to be able to restrict access to API methods in conjunction with secure mode.

Type
symbol
Source
Data.js, line 77

Reference to the Async Task instance. Used to handle async tasks, which can be cancelled, resolved or rejected.

Type
AsyncTask
Source
Data.js, line 83

Timeout ID for data error publishing. Used to prevent race conditions when component is destroyed.

Type
number
Source
Data.js, line 89

The previous mediaData argument. Used for handling repeated calls to setMediaData. If two calls have the same mediaData, both will get the same promise. If the new call is different, the old one is cancelled.

Source
Data.js, line 95

Methods

private #getMediaData(selectoropt) Data~mediaItem|Object|number|undefined

Depending on the selector, returns a specific media item (selector is a number representing the index in the media playlist), the entire data object (selector = 'all'), the current media item (selector = 'current') or the currently active index (selector = 'index').

Parameters:
Name Type Attributes Description
selector number | 'all' | 'index' optional

Either a media based on numerical index, 'all' for all data, or 'index' for the current media index.

Returns

Returns one media item, the entire data object, the current media item, or the current numerical index, depending on the selector.

Type Data~mediaItem | Object | number | undefined
Source
Data.js, line 131

private #setMediaData(mediaData, indexopt) → Promise

Assigns media data to the player instance. mediaData can be a valid data object or a string, in this case the player will either

  • try to to load it as a media resource directly (if the extension matches a known type) or
  • try to load it as a mediaData object in JSON format.
Parameters:
Name Type Attributes Default Description
mediaData Data~mediaItem | string

Can be either a string representing an url or a media object.

index number optional 0

The index of the media item to switch to.

Fires
data/parsed
data/nomedia
Returns

A promise that resolves with the loaded and parsed data or rejects when loading or parsing failed.

Type Promise
Source
Data.js, line 154

private #setMediaIndex(index, optionsopt) → Promise

Switches playback to another media item, with index representing the position of the media to switch to in the internal playlist. Additional options can influence switching behavior in the Media component, like trying to restore the previous seek position (rememberState) or controlling if and how the media is played after switching (ignoreAutoplay, play).

Parameters:
Name Type Attributes Description
index number

The index of the media item to switch to.

options Media~mediaLoadOptions optional

Optional config to set switch behavior.

Fires
data/ready
Returns

A promise that resolves with the loaded metadata of the switched item data or rejects when loading failed.

Type Promise
Source
Data.js, line 243

private #getPreferredMetaData(optionsopt, mediaItemopt) → Media~metaData

Returns the best matching media variant, considering the user's language and quality preferences. Falls back to the closest possible match if an exact match isn't found. In this case, language preferences have priority over quality preferences.

Parameters:
Name Type Attributes Description
options Object optional

Optional preferences.

Parameters:
Name Type Attributes Description
preferredLanguage string optional

The language which is preferred.

preferredQuality string optional

The quality setting which is preferred.

mediaItem Data~mediaItem optional

Media item to search for, defaults to current active item.

Returns

Metadata for a matching media object, or 'false' if nothing suitable was found.

Type Media~metaData
Source
Data.js, line 284

async, private #loadMediaData(url) → Promise<Data~mediaItem>

Loads media data definition from a given URL into the player.

Parameters:
Name Type Description
url string

The URL pointing to a JSON file describing the media data.

Returns

A promise that resolves to the loaded media data.

Type Promise<Data~mediaItem>
Source
Data.js, line 376

async, private #parseMediaDataItem(mediaItem) → Promise<Data~mediaItem>

Parses a single media item (either as URL or object) and returns a normalized media data structure. This includes resolving variants & representations, validating MIME types, applying quality/height heuristics, and optionally loading remote JSON if the input is a URL pointing to a JSON file. This method is used internally by #setMediaData() and supports both individual items and full playlists.

Parameters:
Name Type Description
mediaItem string | Data~mediaItem

The media data to parse, either as object or URL string.

Returns

A Promise resolving to a valid mediaItem structure.

Type Promise<Data~mediaItem>
Throws

If parsing fails or no playable source is found.

Type Error
Source
Data.js, line 400

private #addPlayableMetaData(metaDataArg, pushArrayopt, mediaItemopt) → Media~metaData|false

This helper function searches a source object for a playable source, i.e. A source which has a mime type which at least results in a 'maybe' using the engines 'canPlay' test method. Representations which return a 'probably' are preferred. If no mime type is provided, the method tries to guess it from the source path file ext.

Parameters:
Name Type Attributes Description
metaDataArg Media~metaData

The metaData object to search.

pushArray Array<Media~metaData> optional

If provided, the playable metaData item is pushed there on success.

mediaItem Data~mediaItem optional

If provided, the detected mediaType is assigned to it.

Returns

The playable metaData if found, otherwise false.

Type Media~metaData | false
Throws

Throws various Errors when parsing fails.

Type Error
Source
Data.js, line 544

private #dataError(messageOrKey, erroropt)

This method should be called when a 'data error' occurs. In contrast to a 'media error' which usually indicates problems with playing back certain media (for example, due to network problems or decoding errors), a data error usually means that the player - or some component - wasn't able to correctly and completely parse the data it was provided. A typical case would be some essential things missing from the media data, like no src. Also publishes an appropriate event, so other components can for example display an error message to the user.

Parameters:
Name Type Attributes Description
messageOrKey string

The message text or translate key.

error Error optional

Optional error object.

Fires
data/error
Source
Data.js, line 646

destroy()

This is the cleanup method which should be called when removing the player. It is strongly recommended to do so to prevent memory leaks and possible other unwanted side effects.

Source
Data.js, line 671

Type Definitions

The mediaItem is a representation of a single media data item.

Properties
Name Type Attributes Description
title string | Object<string, string> optional

Title of the media item (can be multilingual).

titleSecondary string | Object<string, string> optional

Secondary title of the media player in multiple languages.

variants Array<Data~mediaItem_variants>

List of available video variants.

text Array<Data~mediaItem_text> optional

List of subtitle tracks.

overlays Array<Data~mediaItem_overlay> optional

List of overlays displayed in the player.

chapters Array<Data~mediaItem_chapter> optional

List of video chapters.

thumbnails Data~mediaItem_thumbnail optional

The thumbnail representation of this media item.

Type
Object
Source
Data.js, line 681

The variants item is a representation of a single variant data item.

Properties
Name Type Attributes Description
language string

Language of the variant.

default boolean optional

Whether this variant is marked as default.

representations Array<Object>

Available representations for this variant.

Properties
Name Type Attributes Description
height number optional

Video height in pixels.

width number optional

Video width in pixels (optional).

quality number | string optional

Quality designation (if available).

encodings Array<Object> optional

List of encodings available for this resolution.

Properties
Name Type Attributes Description
mimeType string optional

The MIME type of the video encoding.

src string optional

Source URL of the encoded video.

Type
Object
Source
Data.js, line 693

The overlaysItem is a representation of a single overlay data item.

Properties
Name Type Attributes Description
type string

Type of overlay (e.g., 'poster', 'image').

src string

Source URL of the overlay.

className string optional

Optional CSS class for styling the overlay.

alt string optional

Alternative text for the overlay image.

placement string optional

Placement of the overlay on the screen.

margin number optional

Margin around the overlay.

cueIn number optional

Timestamp (in seconds) when the overlay appears.

cueOut number optional

Timestamp (in seconds) when the overlay disappears.

Type
Object
Source
Data.js, line 707

The textItem is a representation of a single text data item.

Properties
Name Type Attributes Default Description
type string optional 'subtitles'

Type of subtitle track (e.g., 'subtitles', 'captions' etc).

language string

Language of the subtitle track.

src string

Source URL of the subtitle file.

default boolean optional

Whether this is the default subtitle track.

Type
Object
Source
Data.js, line 720

The chapterItem is a representation of a single chapter data item.

Properties
Name Type Description
title string | Object<string, string>

Chapter title (can be multilingual).

start number

Start time of the chapter in seconds.

Type
Object
Source
Data.js, line 729

The thumbnailsItem is a representation of a single thumbnail data item.

Properties
Name Type Attributes Description
src string | Object

Thumbnail image source(s), either direct src or object for multiple languages.

gridX number

Number of thumbnail columns.

gridY number

Number of thumbnail rows.

timeDelta number

Time difference between thumbnails.

timeDeltaHigh number optional

High-resolution thumbnail time difference.

Type
Object
Source
Data.js, line 736

Events

data/parsed

Fired when the player data was parsed and is available for further consumption.

Parameters:
Name Type Description
data Object

Reference to the data store.

Parameters:
Name Type Attributes Description
media Array<Data~mediaItem>

Array of media items.

currentMediaIndex number

Index of the currently active media item.

title string | Object<string, string> optional

Title of the playlist.

titleSecondary string | Object<string, string> optional

Secondary title of the playlist.

Listeners
Playlist#onDataParsed
Source
Data.js, line 746

data/ready

data/source

Fired when a media source has been selected, but before the actual load.

Parameters:
Name Type Description
metaData Media~metaData

The new media metadata.

Listeners
Language#onDataSource
Quality#onDataSource
Source
Data.js, line 762

data/error

Fired when a data related error occurs (for example a parsing error due to wrong media data format).

Parameters:
Name Type Description
msgObj Object

The data error message object.

Parameters:
Name Type Description
code string

The error code.

message string

The error message.

messageSecondary string

Optional error message from original error.

Listeners
Notifications#onDataError
Source
Data.js, line 768

data/nomedia