Skip to content

Commit

Permalink
feat: 🎸 (xgplayer-hls) 加密hls支持更多的Key System
Browse files Browse the repository at this point in the history
  • Loading branch information
gemxx committed Mar 6, 2024
1 parent 8b2409c commit e2ae13f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ export function parseMediaPlaylist (lines, parentUrl, useLowLatency) {
curKey = null
break
}
if (attr.METHOD !== 'AES-128') throw new Error(`encrypt ${attr.METHOD}/${attr.KEYFORMAT} is not supported`)
curKey = new MediaSegmentKey()
curKey.method = attr.METHOD
curKey.url = /^blob:/.test(attr.URI) ? attr.URI : getAbsoluteUrl(attr.URI, parentUrl)
curKey.keyFormat = attr.KEYFORMAT || 'identity'
curKey.keyFormatVersions = attr.KEYFORMATVERSIONS
if (!curKey.isSupported()) {
throw new Error(`encrypt ${attr.METHOD}/${attr.KEYFORMAT} is not supported`)
}
if (attr.IV) {
let str = attr.IV.slice(2)
str = (str.length & 1 ? '0' : '') + str
Expand Down
46 changes: 45 additions & 1 deletion packages/xgplayer-hls/src/hls/manifest-loader/parser/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ export class MasterPlaylist {
isMaster = true
}


const MediaType = {
Audio: 'AUDIO',
Video: 'VIDEO',
SubTitle: 'SUBTITLE',
ClosedCaptions: 'CLOSED-CAPTIONS'
}

// #EXT-X-KEY KEYFORMAT values
const KeySystems = {
CLEAR_KEY: 'org.w3.clearkey',
FAIRPLAY: 'com.apple.streamingkeydelivery',
WIDEVINE: 'urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed',
PLAYREADY: 'com.microsoft.playready'
}

export class MediaStream {
id = 0
url = ''
Expand Down Expand Up @@ -165,4 +172,41 @@ export class MediaSegmentKey {
}
}
}

isSegmentEncrypted () {
const { method } = this.method
return method === 'AES-128' // || method === 'AES-256' || method === 'AES-256-CTR'
}

isValidKeySystem () {
const isKeyFormatValid =
[
KeySystems.CLEAR_KEY,
KeySystems.FAIRPLAY,
KeySystems.WIDEVINE,
KeySystems.PLAYREADY
].indexOf(this.keyFormat) > -1
if (!isKeyFormatValid) {
return false
}

const isMethodValid =
['SAMPLE-AES', 'SAMPLE-AES-CENC', 'SAMPLE-AES-CTR'].indexOf(this.method) > -1
if (!isMethodValid) {
return false
}
return true
}

isSupported () {
if (!this.method) {
return false
}
if (this.isSegmentEncrypted()) {
return true
} else if (this.isValidKeySystem()) {
return true
}
return false
}
}
2 changes: 1 addition & 1 deletion packages/xgplayer-hls/src/hls/segment-loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class SegmentLoader {
}

const keyUrl = seg.key?.url
if (keyUrl) {
if (keyUrl && seg.key.isSegmentEncrypted()) {
keyIv = seg.key.iv
key = this._keyCache[keyUrl]
if (!key) {
Expand Down
1 change: 1 addition & 0 deletions packages/xgplayer-hls/src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class HlsPlugin extends BasePlugin {

if (this.hls) this.hls.destroy()
this.player.switchURL = this._onSwitchURL
this.player.handleSource = false // disable player source handle

const hlsOpts = config.hls || {}
hlsOpts.innerDegrade = hlsOpts.innerDegrade || config.innerDegrade
Expand Down

0 comments on commit e2ae13f

Please sign in to comment.