Class: shaka.media.MediaSourceEngine

MediaSourceEngine wraps all operations on MediaSource and SourceBuffers. All asynchronous operations return a Promise, and all operations are internally synchronized and serialized as needed. Operations that can be done in parallel will be done in parallel.

Constructor

new MediaSourceEngine(video, textDisplayernon-null, playerInterfacenon-null, lcevcDecopt, nullable)

MediaSourceEngine wraps all operations on MediaSource and SourceBuffers. All asynchronous operations return a Promise, and all operations are internally synchronized and serialized as needed. Operations that can be done in parallel will be done in parallel.

Parameters:
Name Type Attributes Description
video HTMLMediaElement The video element, whose source is tied to MediaSource during the lifetime of the MediaSourceEngine.
textDisplayer shaka.extern.TextDisplayer The text displayer that will be used with the text engine. MediaSourceEngine takes ownership of the displayer. When MediaSourceEngine is destroyed, it will destroy the displayer.
playerInterface shaka.media.MediaSourceEngine.PlayerInterface Interface for common player methods.
lcevcDec shaka.lcevc.Dec <optional>
<nullable>
Optional - LCEVC Decoder Object
Implements:
Source:

Members

createObjectURL :function(?):string

Internal reference to window.URL.createObjectURL function to avoid compatibility issues with other libraries and frameworks such as React Native. For use in unit tests only, not meant for external use.
Type:
  • function(?):string
Source:

SourceBufferMode_ :string

Type:
  • string
Properties:
Name Value Type Description
SEQUENCE sequence string
SEGMENTS segments string
Source:

attemptTimestampOffsetCalculation_ :boolean

Type:
  • boolean
Source:

firstAudioReferenceStartTime_ :number

Type:
  • number
Source:

firstAudioTimestamp_ :number

Type:
  • number
Source:

firstVideoReferenceStartTime_ :number

Type:
  • number
Source:

firstVideoTimestamp_ :number

Type:
  • number
Source:

ignoreManifestTimestampsInSegmentsMode_ :boolean

Type:
  • boolean
Source:

lastDuration_ :number

Type:
  • number
Source:

manifestType_ :string

Type:
  • string
Source:

mediaSource_ :MediaSource

Type:
  • MediaSource
Source:

needSplitMuxedContent_ :boolean

Type:
  • boolean
Source:

playbackHasBegun_ :boolean

Type:
  • boolean
Source:

queues_ :Object.<string, !Array.<shaka.media.MediaSourceEngine.Operation>>

Type:
Source:

reloadingMediaSource_ :boolean

Type:
  • boolean
Source:

secondarySource_ :HTMLSourceElement

Fallback source element with direct media URI, used for casting purposes.
Type:
  • HTMLSourceElement
Source:

segmentRelativeVttTiming_ :boolean

Type:
  • boolean
Source:

sequenceMode_ :boolean

Type:
  • boolean
Source:

source_ :HTMLSourceElement

Type:
  • HTMLSourceElement
Source:

sourceBuffers_ :Object.<shaka.util.ManifestParserUtils.ContentType, SourceBuffer>

Type:
Source:

streamingAllowed_ :boolean

Type:
  • boolean
Source:

textSequenceModeOffset_ :shaka.util.PublicPromise.<number>

Type:
Source:

transmuxers_ :Object.<string, !shaka.extern.Transmuxer>

Type:
Source:

url_ :string

Type:
  • string
Source:

usingRemotePlayback_ :boolean

Type:
  • boolean
Source:

Methods

isStreamSupported(stream, contentType) → {Promise.<boolean>}

Checks if a certain type is supported.
Parameters:
Name Type Description
stream shaka.extern.Stream
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
Type
Promise.<boolean>

probeSupport() → {Object.<string, boolean>}

Returns a map of MediaSource support for well-known types.
Source:
Returns:
Type
Object.<string, boolean>

abort_(contentType)

Call abort() on the SourceBuffer. This resets MSE's last_decode_timestamp on all track buffers, which should trigger the splicing logic for overlapping segments.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

addExtraFeaturesToMimeType_(mimeType) → {string}

Parameters:
Name Type Description
mimeType string
Source:
Returns:
Type
string

addSecondarySource(uri, mimeType)

Parameters:
Name Type Description
uri string
mimeType string
Source:

append_(contentType, data, timestampOffset)

Append data to the SourceBuffer.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
data BufferSource
timestampOffset number
Source:

appendBuffer(contentType, datanon-null, referencenullable, stream, hasClosedCaptionsnullable, seekedopt, adaptationopt, isChunkedDataopt) → {Promise}

Enqueue an operation to append data to the SourceBuffer. Start and end times are needed for TextEngine, but not for MediaSource. Start and end times may be null for initialization segments; if present they are relative to the presentation timeline.
Parameters:
Name Type Attributes Default Description
contentType shaka.util.ManifestParserUtils.ContentType
data BufferSource
reference shaka.media.SegmentReference <nullable>
The segment reference we are appending, or null for init segments
stream shaka.extern.Stream
hasClosedCaptions boolean <nullable>
True if the buffer contains CEA closed captions
seeked boolean <optional>
false True if we just seeked
adaptation boolean <optional>
false True if we just automatically switched active variant(s).
isChunkedData boolean <optional>
false True if we add to the buffer from the partial read of the segment.
Source:
Returns:
Type
Promise

bufferedAheadOf(contentType, time) → {number}

Computes how far ahead of the given timestamp is buffered for the given content type.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
time number
Source:
Returns:
The amount of time buffered ahead in seconds.
Type
number

bufferEnd(contentType) → {number}

Gets the last timestamp in buffer for the given content type.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
The timestamp in seconds, or null if nothing is buffered.
Type
number

bufferStart(contentType) → {number}

Gets the first timestamp in buffer for the given content type.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
The timestamp in seconds, or null if nothing is buffered.
Type
number

change_(contentType, mimeType, transmuxernullable)

Prepare the SourceBuffer to parse a potentially new type or codec.
Parameters:
Name Type Attributes Description
contentType shaka.util.ManifestParserUtils.ContentType
mimeType string
transmuxer shaka.extern.Transmuxer <nullable>
Source:

changeType(contentType, mimeType, transmuxernullable) → {Promise}

Enqueue an operation to prepare the SourceBuffer to parse a potentially new type or codec.
Parameters:
Name Type Attributes Description
contentType shaka.util.ManifestParserUtils.ContentType
mimeType string
transmuxer shaka.extern.Transmuxer <nullable>
Source:
Returns:
Type
Promise

clear(contentType) → {Promise}

Enqueue an operation to clear the SourceBuffer.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
Type
Promise

clearSelectedClosedCaptionId()

Disable embedded closed captions.
Source:

codecSwitchIfNecessary_(contentType, mimeType, codecs, streamsByTypenon-null) → {Promise.<boolean>}

Codec switch if necessary, this will not resolve until the codec switch is over.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
mimeType string
codecs string
streamsByType Map.<shaka.util.ManifestParserUtils.ContentType, shaka.extern.Stream>
Source:
Returns:
true if there was a codec switch, false otherwise.
Type
Promise.<boolean>

configure(config)

Called by the Player to provide an updated configuration any time it changes. Must be called at least once before init().
Parameters:
Name Type Description
config shaka.extern.MediaSourceConfiguration
Source:

convertNtp_(ntpTimeStamp) → {number}

Convert Ntp ntpTimeStamp to UTC Time
Parameters:
Name Type Description
ntpTimeStamp number
Source:
Returns:
utcTime
Type
number

createMediaSource(pnon-null) → {MediaSource}

Create a MediaSource object, attach it to the video element, and return it. Resolves the given promise when the MediaSource is ready. Replaced by unit tests.
Parameters:
Name Type Description
p shaka.util.PublicPromise
Source:
Returns:
Type
MediaSource

destroy() → {Promise}

Request that this object be destroyed, releasing all resources and shutting down all operations. Returns a Promise which is resolved when destruction is complete. This Promise should never be rejected.
Implements:
Source:
Returns:
Type
Promise

ended() → {boolean}

Source:
Returns:
True if the MediaSource is in an "ended" state, or if the object has been destroyed.
Type
boolean

endOfStream(reasonopt) → {Promise}

Parameters:
Name Type Attributes Description
reason string <optional>
Valid reasons are 'network' and 'decode'.
Source:
See:
Returns:
Type
Promise

enqueueBlockingOperation_(run) → {Promise}

Enqueue an operation which must block all other operations on all SourceBuffers.
Parameters:
Name Type Description
run function(): (Promise|undefined)
Source:
Returns:
Type
Promise

enqueueOperation_(contentType, start, urinullable) → {Promise}

Enqueue an operation and start it if appropriate.
Parameters:
Name Type Attributes Description
contentType shaka.util.ManifestParserUtils.ContentType
start function()
uri string <nullable>
Source:
Returns:
Type
Promise

flush(contentType) → {Promise}

Enqueue an operation to flush the SourceBuffer. This is a workaround for what we believe is a Chromecast bug.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
Type
Promise

flush_(contentType)

Nudge the playhead to force the media pipeline to be flushed. This seems to be necessary on Chromecast to get new content to replace old content.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

getBuffered_(contentType) → {TimeRanges}

Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:
Returns:
The buffered ranges for the given content type, or null if the buffered ranges could not be obtained.
Type
TimeRanges

getBufferedInfo() → {shaka.extern.BufferedInfo}

Returns info about what is currently buffered.
Source:
Returns:
Type
shaka.extern.BufferedInfo

getCaptionParser(mimeType) → {shaka.media.IClosedCaptionParser}

Create a new closed caption parser. This will ONLY be replaced by tests as a way to inject fake closed caption parser instances.
Parameters:
Name Type Description
mimeType string
Source:
Returns:
Type
shaka.media.IClosedCaptionParser

getDuration() → {number}

Get the current MediaSource duration.
Source:
Returns:
Type
number

getTimestampAndDispatchMetadata(contentType, datanon-null, referencenon-null, stream, mimeTypenon-null) → {{timestamp: ?number, metadata: !Array.<shaka.extern.ID3Metadata>}}

This method is only public for testing.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
data BufferSource
reference shaka.media.SegmentReference The segment reference we are appending
stream shaka.extern.Stream
mimeType string
Source:
Returns:
Type
{timestamp: ?number, metadata: !Array.<shaka.extern.ID3Metadata>}

init(streamsByTypenon-null, sequenceModeopt, manifestTypeopt, ignoreManifestTimestampsInSegmentsModeopt) → {Promise}

Initialize MediaSourceEngine. Note that it is not valid to call this multiple times, except to add or reinitialize text streams.
Parameters:
Name Type Attributes Default Description
streamsByType Map.<shaka.util.ManifestParserUtils.ContentType, shaka.extern.Stream> A map of content types to streams. All streams must be supported according to MediaSourceEngine.isStreamSupported.
sequenceMode boolean <optional>
false If true, the media segments are appended to the SourceBuffer in strict sequence.
manifestType string <optional>
Indicates the type of the manifest.
ignoreManifestTimestampsInSegmentsMode boolean <optional>
false If true, don't adjust the timestamp offset to account for manifest segment durations being out of sync with segment durations. In other words, assume that there are no gaps in the segments when appending to the SourceBuffer, even if the manifest and segment times disagree. Indicates if the manifest has text streams.
Source:
Returns:
Type
Promise

initSourceBuffer_(contentType, stream, codecs) → {Promise}

Initialize a specific SourceBuffer.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
stream shaka.extern.Stream
codecs string
Source:
Returns:
Type
Promise

isBuffered(contentType, time) → {boolean}

Determines if the given time is inside the buffered range of the given content type.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
time number Playhead time
Source:
Returns:
Type
boolean

isCodecSwitchNecessary_(contentType, refMimeType, refCodecs) → {boolean}

Returns true if it's necessary codec switch to load the new stream.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
refMimeType string
refCodecs string
Source:
Returns:
Type
boolean

isResetMediaSourceNecessary(contentType, mimeType, codecs) → {boolean}

Returns true if it's necessary reset the media source to load the new stream.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
mimeType string
codecs string
Source:
Returns:
Type
boolean

isStreamingAllowed() → {boolean}

Indicate if the streaming is allowed by MediaSourceEngine. If we using MediaSource we allways returns true.
Source:
Returns:
Type
boolean

onError_(contentType)

Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

onSourceOpen_(p)

Parameters:
Name Type Description
p shaka.util.PublicPromise
Source:

onUpdateEnd_(contentType)

Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

open() → {Promise}

Source:
Returns:
Resolved when MediaSource is open and attached to the media element. This process is actually initiated by the constructor.
Type
Promise

parseEMSG_(referencenon-null, emsgSchemeIdUrisnullable, boxnon-null)

Parse the EMSG box from a MP4 container.
Parameters:
Name Type Attributes Description
reference shaka.media.SegmentReference
emsgSchemeIdUris Array.<string> <nullable>
Array of emsg scheme_id_uri for which emsg boxes should be parsed.
box shaka.extern.ParsedBox
Source:

parsePrft_(timescale, boxnon-null)

Parse PRFT box.
Parameters:
Name Type Description
timescale number
box shaka.extern.ParsedBox
Source:

popFromQueue_(contentType)

Pop from the front of the queue and start a new operation.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

reinitText(mimeType, sequenceMode, external)

Reinitialize the TextEngine for a new text type.
Parameters:
Name Type Description
mimeType string
sequenceMode boolean
external boolean
Source:

remove(contentType, startTime, endTime) → {Promise}

Enqueue an operation to remove data from the SourceBuffer.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
startTime number relative to the start of the presentation
endTime number relative to the start of the presentation
Source:
Returns:
Type
Promise

remove_(contentType, startTime, endTime)

Remove data from the SourceBuffer.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
startTime number relative to the start of the presentation
endTime number relative to the start of the presentation
Source:

reset(streamsByTypenon-null) → {Promise}

Resets the Media Source
Parameters:
Name Type Description
streamsByType Map.<shaka.util.ManifestParserUtils.ContentType, shaka.extern.Stream>
Source:
Returns:
Type
Promise

reset_(streamsByTypenon-null)

Resets the MediaSource and re-adds source buffers due to codec mismatch
Parameters:
Name Type Description
streamsByType Map.<shaka.util.ManifestParserUtils.ContentType, shaka.extern.Stream>
Source:

resetCaptionParser()

Fully reset the state of the caption parser owned by MediaSourceEngine.
Source:

resync(contentType, timestampOffset) → {Promise}

Adjust timestamp offset to maintain AV sync across discontinuities.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
timestampOffset number
Source:
Returns:
Type
Promise

setAppendWindow_(contentType, appendWindowStart, appendWindowEnd)

Set the SourceBuffer's append window end.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
appendWindowStart number
appendWindowEnd number
Source:

setDuration(duration) → {Promise}

Parameters:
Name Type Description
duration number
Source:
Returns:
Type
Promise

setSegmentRelativeVttTiming(segmentRelativeVttTiming)

Parameters:
Name Type Description
segmentRelativeVttTiming boolean
Source:

setSelectedClosedCaptionId(id)

Set the selected closed captions Id and language.
Parameters:
Name Type Description
id string
Source:

setStreamProperties(contentType, timestampOffset, appendWindowStart, appendWindowEnd, ignoreTimestampOffset, mimeType, codecs, streamsByTypenon-null) → {Promise}

Sets the timestamp offset and append window end for the given content type.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
timestampOffset number The timestamp offset. Segments which start at time t will be inserted at time t + timestampOffset instead. This value does not affect segments which have already been inserted.
appendWindowStart number The timestamp to set the append window start to. For future appends, frames/samples with timestamps less than this value will be dropped.
appendWindowEnd number The timestamp to set the append window end to. For future appends, frames/samples with timestamps greater than this value will be dropped.
ignoreTimestampOffset boolean If true, the timestampOffset will not be applied in this step.
mimeType string
codecs string
streamsByType Map.<shaka.util.ManifestParserUtils.ContentType, shaka.extern.Stream> A map of content types to streams. All streams must be supported according to MediaSourceEngine.isStreamSupported.
Source:
Returns:
Type
Promise

setTextDisplayer(textDisplayernon-null)

Parameters:
Name Type Description
textDisplayer shaka.extern.TextDisplayer
Source:

setTimestampOffset_(contentType, timestampOffset)

Set the SourceBuffer's timestamp offset.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
timestampOffset number
Source:

startOperation_(contentType)

Starts the next operation in the queue.
Parameters:
Name Type Description
contentType shaka.util.ManifestParserUtils.ContentType
Source:

updateLcevcDec(lcevcDecnullable)

Update LCEVC Decoder object when ready for LCEVC Decode.
Parameters:
Name Type Attributes Description
lcevcDec shaka.lcevc.Dec <nullable>
Source:

workAroundBrokenPlatforms_(segmentnon-null, startTimenullable, contentType, urinullable) → {BufferSource}

Apply platform-specific transformations to this segment to work around issues in the platform.
Parameters:
Name Type Attributes Description
segment BufferSource
startTime number <nullable>
contentType shaka.util.ManifestParserUtils.ContentType
uri string <nullable>
Source:
Returns:
Type
BufferSource

Type Definitions

Operation

An operation in queue.

Type:
Properties:
Name Type Attributes Description
start function The function which starts the operation.
p shaka.util.PublicPromise The PublicPromise which is associated with this operation.
uri string <nullable>
A segment URI (if any) associated with this operation.
Source:

PlayerInterface

Player interface

Type:
  • {getKeySystem: function(): ?string, onMetadata: function(!Array.<shaka.extern.ID3Metadata>, number, ?number), onEvent: function(!Event), onManifestUpdate: function()}
Properties:
Name Type Description
getKeySystem function Gets currently used key system or null if not used.
onMetadata function Callback to use when metadata arrives.
onEvent function Called when an event occurs that should be sent to the app.
onManifestUpdate function Called when an embedded 'emsg' box should trigger a manifest update.
Source: