Skip to content

Commit

Permalink
allow for HLS playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
adrums86 committed Dec 11, 2023
1 parent 1a85807 commit 2904088
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
40 changes: 13 additions & 27 deletions src/dash-playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import containerRequest from './util/container-request.js';
import {toUint8} from '@videojs/vhs-utils/es/byte-helpers';
import logger from './util/logger';
import {merge} from './util/vjs-compat';
import { arrayBufferToHexString } from './util/string.js';

const { EventTarget } = videojs;

Expand Down Expand Up @@ -314,7 +313,6 @@ export default class DashPlaylistLoader extends EventTarget {
this.vhs_ = vhs;
this.withCredentials = withCredentials;
this.addMetadataToTextTrack = options.addMetadataToTextTrack;
this.keyStatusMap_ = new Map();

if (!srcUrlOrPlaylist) {
throw new Error('A non-empty playlist URL or object is required');
Expand Down Expand Up @@ -474,7 +472,6 @@ export default class DashPlaylistLoader extends EventTarget {
}

this.off();
this.keyStatusMap_.clear();
}

hasPendingRequest() {
Expand Down Expand Up @@ -928,29 +925,18 @@ export default class DashPlaylistLoader extends EventTarget {
}
}

excludeNonUsablePlaylists() {
this.mainPlaylistLoader_.main.playlists.forEach((playlist) => {
if (!playlist.contentProtection.mp4protection ||
!playlist.contentProtection.mp4protection.attributes['cenc:default_KID']) {
return;
}
const playlistKID = playlist.contentProtection.mp4protection.attributes['cenc:default_KID'].replace(/-/g, '');
const hasUsableKeystatus = this.keyStatusMap_.has(playlistKID) && this.keyStatusMap_.get(playlistKID) === 'usable';

if (!hasUsableKeystatus) {
playlist.excludeUntil = Infinity;
playlist.lastExcludeReason_ = 'non-usable';
} else {
delete playlist.excludeUntil;
delete playlist.lastExcludeReason_;
}
});
}

addKeyStatus(keyId, status) {
// 32 digit keyId hex string.
const keyIdHexString = arrayBufferToHexString(keyId).slice(0, 32);

this.keyStatusMap_.set(keyIdHexString, status);
/**
* Returns the default_KID from a DASH playlist.
*
* @param {playlist} playlist to fetch the default_KID from.
* @return a 32 digit hex string that represents the keyId of the encrypted playlist.
*/
getKID(playlist) {
if (playlist.contentProtection &&
playlist.contentProtection.mp4protection &&
playlist.contentProtection.mp4protection.attributes['cenc:default_KID']) {

Check warning on line 937 in src/dash-playlist-loader.js

View check run for this annotation

Codecov / codecov/patch

src/dash-playlist-loader.js#L934-L937

Added lines #L934 - L937 were not covered by tests
// default KID is a 32 digit hext string separated by '-'.
return playlist.contentProtection.mp4protection.attributes['cenc:default_KID'].replace(/-/g, '');

Check warning on line 939 in src/dash-playlist-loader.js

View check run for this annotation

Codecov / codecov/patch

src/dash-playlist-loader.js#L939

Added line #L939 was not covered by tests
}
}
}
30 changes: 30 additions & 0 deletions src/playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import logger from './util/logger';
import {merge, createTimeRanges} from './util/vjs-compat';
import { addMetadata, createMetadataTrackIfNotExists, addDateRangeMetadata } from './util/text-tracks';
import ContentSteeringController from './content-steering-controller';
import { arrayBufferToHexString } from './util/string.js';

const ABORT_EARLY_EXCLUSION_SECONDS = 10;

Expand Down Expand Up @@ -235,6 +236,7 @@ export class PlaylistController extends videojs.EventTarget {
this.sourceUpdater_ = new SourceUpdater(this.mediaSource);
this.inbandTextTracks_ = {};
this.timelineChangeController_ = new TimelineChangeController();
this.keyStatusMap_ = new Map();

const segmentLoaderSettings = {
vhs: this.vhs_,
Expand Down Expand Up @@ -1708,6 +1710,7 @@ export class PlaylistController extends videojs.EventTarget {
this.mainPlaylistLoader_.dispose();
this.mainSegmentLoader_.dispose();
this.contentSteeringController_.dispose();
this.keyStatusMap_.clear();

if (this.loadOnPlay_) {
this.tech_.off('play', this.loadOnPlay_);
Expand Down Expand Up @@ -2380,4 +2383,31 @@ export class PlaylistController extends videojs.EventTarget {

this.switchMedia_(nextPlaylist, 'content-steering');
}

excludeNonUsablePlaylistsByKID() {
this.mainPlaylistLoader_.main.playlists.forEach((playlist) => {
const keyId = this.mainPlaylistLoader_.getKID(playlist);

Check warning on line 2389 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2387-L2389

Added lines #L2387 - L2389 were not covered by tests
// If the playlist doesn't have a keyID lets not exclude it.

if (!keyId) {
return;

Check warning on line 2393 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2392-L2393

Added lines #L2392 - L2393 were not covered by tests
}
const hasUsableKeystatus = this.keyStatusMap_.has(keyId) && this.keyStatusMap_.get(keyId) === 'usable';

Check warning on line 2395 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2395

Added line #L2395 was not covered by tests

if (!hasUsableKeystatus) {
playlist.excludeUntil = Infinity;
playlist.lastExcludeReason_ = 'non-usable';

Check warning on line 2399 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2397-L2399

Added lines #L2397 - L2399 were not covered by tests
} else {
delete playlist.excludeUntil;
delete playlist.lastExcludeReason_;

Check warning on line 2402 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2401-L2402

Added lines #L2401 - L2402 were not covered by tests
}
});
}

addKeyStatus(keyId, status) {

Check warning on line 2407 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2407

Added line #L2407 was not covered by tests
// 32 digit keyId hex string.
const keyIdHexString = arrayBufferToHexString(keyId).slice(0, 32);

Check warning on line 2409 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2409

Added line #L2409 was not covered by tests

this.keyStatusMap_.set(keyIdHexString, status);

Check warning on line 2411 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L2411

Added line #L2411 was not covered by tests
}
}
16 changes: 16 additions & 0 deletions src/playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1194,4 +1194,20 @@ export default class PlaylistLoader extends EventTarget {

return attributes;
}

/**
* Returns the key ID from a playlist.
*
* @param {playlist} playlist to fetch the key ID from.
* @return a 32 digit hex string that represents the keyId of the encrypted playlist.
*/
getKID(playlist) {

Check warning on line 1204 in src/playlist-loader.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-loader.js#L1204

Added line #L1204 was not covered by tests
// Currently we only parse the keyId for widevine in HLS so only look there.
if (playlist.contentProtection &&
playlist.contentProtection['com.widevine.alpha'] &&
playlist.contentProtection['com.widevine.alpha'].attributes.keyId) {

Check warning on line 1208 in src/playlist-loader.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-loader.js#L1206-L1208

Added lines #L1206 - L1208 were not covered by tests
// default KID is a 32 digit hext string often separated by '-'.
return playlist.contentProtection['com.widevine.alpha'].attributes.keyId.replace(/-/g, '');

Check warning on line 1210 in src/playlist-loader.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-loader.js#L1210

Added line #L1210 was not covered by tests
}
}
}

0 comments on commit 2904088

Please sign in to comment.