Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create PlaylistLoader classes that share more code and do less #1208

Open
wants to merge 31 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5064aab
feat: first draft for hls and dash
brandonocasey Aug 12, 2021
0b99422
Merge branch 'main' into feat/refactor-playlist
brandonocasey Sep 24, 2021
5ee1bda
more work and some tests
brandonocasey Sep 24, 2021
381b73d
fully tested the base class
brandonocasey Sep 29, 2021
6ea4dfe
add tests for HlsMainPlaylistLoader
brandonocasey Sep 29, 2021
66b7288
remove manifestString function
brandonocasey Sep 29, 2021
349ae74
test some functions that i missed
brandonocasey Sep 29, 2021
1103217
dash media playlist loader tests
brandonocasey Sep 29, 2021
d89e9bd
parse media in main
brandonocasey Sep 30, 2021
66f66ca
tests
brandonocasey Oct 13, 2021
ac0280e
Merge branch 'main' into feat/refactor-playlist
brandonocasey Oct 15, 2021
5edcabf
Merge branch 'main' into feat/refactor-playlist
brandonocasey Oct 26, 2021
50e7422
change to just deepEqual
brandonocasey Oct 27, 2021
dc5a707
finish hls media playlist loader
brandonocasey Oct 27, 2021
ec33235
finish utils tests
brandonocasey Oct 28, 2021
c40596f
Merge branch 'main' into feat/refactor-playlist
brandonocasey Oct 29, 2021
dac2593
increase code coverage
brandonocasey Oct 29, 2021
59d2360
move to helper
brandonocasey Oct 29, 2021
86c785b
refactor for sidx and merge logic
brandonocasey Oct 29, 2021
297510d
refactor for sidx and merge logic
brandonocasey Oct 29, 2021
e1d58f7
fix tests for from refactor
brandonocasey Nov 1, 2021
3050154
finish sidx tests
brandonocasey Nov 1, 2021
3933f1a
finish testing
brandonocasey Nov 2, 2021
1506eb1
fix test errors caused by error refactor
brandonocasey Nov 2, 2021
6ee8886
another test fix
brandonocasey Nov 2, 2021
3b9de24
jsdocs
brandonocasey Nov 2, 2021
4369374
coverage updates
brandonocasey Nov 2, 2021
c53d464
ie 11 fixes
brandonocasey Nov 2, 2021
c1449d2
remove sidxMapping
brandonocasey Nov 3, 2021
c8cb617
do not merge mediaGroups either
brandonocasey Nov 3, 2021
c258254
add jsdocs for getMediaAccessor
brandonocasey Nov 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 179 additions & 0 deletions src/playlist-loader/dash-main-playlist-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import PlaylistLoader from './playlist-loader.js';
import {resolveUrl} from '../resolve-url';
import {parse as parseMpd, parseUTCTiming} from 'mpd-parser';
import {mergeManifest, forEachPlaylist} from './utils.js';

/**
* An instance of the `DashMainPlaylistLoader` class is created when VHS is passed a DASH
* manifest. For dash main playlists are the only thing that needs to be refreshed. This
* is important to note as a lot of the `DashMediaPlaylistLoader` logic looks to
* `DashMainPlaylistLoader` for guidance.
*
* @extends PlaylistLoader
*/
class DashMainPlaylistLoader extends PlaylistLoader {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class deals with getting the sever clock offset and merging the old main manifest into the new one.


/**
* Create an instance of this class.
*
* @param {Element} uri
* The uri of the manifest.
*
* @param {Object} options
* Options that can be used, see the base class for more information.
*/
constructor(uri, options) {
super(uri, options);
this.clientOffset_ = null;
this.clientClockOffset_ = null;
this.setMediaRefreshTimeout_ = this.setMediaRefreshTimeout_.bind(this);
}

/**
* Get an array of all playlists in this manifest, including media group
* playlists.
*
* @return {Object[]}
* An array of playlists.
*/
playlists() {
const playlists = [];

forEachPlaylist(this.manifest_, (media) => {
playlists.push(media);
});

return playlists;
}

/**
* Parse a new manifest and merge it with an old one. Calls back
* with the merged manifest and weather or not it was updated.
*
* @param {string} manifestString
* A manifest string directly from the request response.
*
* @param {Function} callback
* A callback that takes the manifest and updated
*
* @private
*/
parseManifest_(manifestString, callback) {
this.syncClientServerClock_(manifestString, (clientOffset) => {
const parsedManifest = parseMpd(manifestString, {
manifestUri: this.uri_,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that sidxMapping is not done her at all. Instead each individual DashMediaPlaylistLoader handles all of that logic on it's own.

clientOffset
});

// merge everything except for playlists, they will merge themselves
const mergeResult = mergeManifest(this.manifest_, parsedManifest, ['playlists', 'mediaGroups']);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not also that we do not merge the playlists or mediaGroups here, which means only the new unmerged playlists will exist after this point. We do this because each individual DashMediaPlaylistLoader merges it's own playlist (if the playlist loader is started).


// always trigger updated, as playlists will have to update themselves
callback(mergeResult.manifest, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always report updated here as we want DashMediaPlaylistLoaders to be the ones that check for stale playlist updates. The DashMainPlaylistLoader will hardly ever change substantially.

});
}

/**
* Used by parsedManifest to get the client server sync offest.
*
* @param {string} manifestString
* A manifest string directly from the request response.
*
* @param {Function} callback
* A callback that takes the client offset
*
* @private
*/
syncClientServerClock_(manifestString, callback) {
let utcTiming;

try {
utcTiming = parseUTCTiming(manifestString);
} catch (e) {
utcTiming = null;
}

// No UTCTiming element found in the mpd. Use Date header from mpd request as the
// server clock
if (utcTiming === null) {
return callback(this.lastRequestTime() - Date.now());
}

if (utcTiming.method === 'DIRECT') {
return callback(utcTiming.value - Date.now());
}

this.makeRequest_({
uri: resolveUrl(this.uri(), utcTiming.value),
method: utcTiming.method,
handleErrors: false
}, (request, wasRedirected, error) => {
let serverTime = this.lastRequestTime();

if (!error && utcTiming.method === 'HEAD' && request.responseHeaders && request.responseHeaders.date) {
serverTime = Date.parse(request.responseHeaders.date);
}

if (!error && request.responseText) {
serverTime = Date.parse(request.responseText);
}

callback(serverTime - Date.now());
});
}

/**
* Used by DashMediaPlaylistLoader in cases where
* minimumUpdatePeriod is zero. This allows the currently active
* playlist to set the mediaRefreshTime_ time to it's targetDuration.
*
* @param {number} time
* Set the mediaRefreshTime
*
* @private
*/
setMediaRefreshTime_(time) {
this.mediaRefreshTime_ = time;
this.setMediaRefreshTimeout_();
}

/**
* Get the amount of time that should elapse before the media is
* re-requested. Returns null if it shouldn't be re-requested. For
* Dash we look at minimumUpdatePeriod (from the manifest) or the
* targetDuration of the currently selected media
* (from a DashMediaPlaylistLoader).
*
* @return {number}
* Returns the media refresh time
*
* @private
*/
getMediaRefreshTime_() {
const minimumUpdatePeriod = this.manifest_.minimumUpdatePeriod;

// if minimumUpdatePeriod is invalid or <= zero, which
// can happen when a live video becomes VOD. We do not have
// a media refresh time.
if (typeof minimumUpdatePeriod !== 'number' || minimumUpdatePeriod < 0) {
return null;
}

// If the minimumUpdatePeriod has a value of 0, that indicates that the current
// MPD has no future validity, so a new one will need to be acquired when new
// media segments are to be made available. Thus, we use the target duration
// in this case
// TODO: can we do this in a better way? It would be much better
Copy link
Contributor Author

@brandonocasey brandonocasey Nov 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone has a better idea for this it would make the separation between DashMediaPlaylistLoader and DashMainPlaylistLoader much more complete. It would also prevent the scenario that we currently have where an audio playlist target duration could take precedence over a video playlist.

// if DashMainPlaylistLoader didn't care about media playlist loaders at all.
// Right now DashMainPlaylistLoader's call `setMediaRefreshTime_` to set
// the medias target duration.
if (minimumUpdatePeriod === 0) {
return this.mediaRefreshTime_;
}

return minimumUpdatePeriod;
}

}

export default DashMainPlaylistLoader;
Loading