Skip to content

Commit 6c39ebc

Browse files
update: add seek support for sabr & format with biome
We never formatted this with biome before, so the changes are big :p
1 parent d03f692 commit 6c39ebc

5 files changed

Lines changed: 2594 additions & 1957 deletions

File tree

src/playback/player.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -994,16 +994,47 @@ export class Player {
994994

995995
this.position = position
996996
this.track.endTime = endTime
997+
let previousSession = null
998+
let reuseUrlData = null
999+
1000+
if (this.streamInfo?.protocol === 'sabr' && this.connection?.audioStream) {
1001+
const inputStream = this.connection.audioStream.pipes?.[0]
1002+
if (inputStream && typeof inputStream.getSessionState === 'function') {
1003+
previousSession = inputStream.getSessionState()
1004+
if (previousSession) {
1005+
logger(
1006+
'debug',
1007+
'Player',
1008+
`Extracted SABR session state: rn=${previousSession.requestNumber}, hasCookie=${!!previousSession.nextRequestPolicy?.playbackCookie}`
1009+
)
1010+
1011+
reuseUrlData = {
1012+
protocol: this.streamInfo.protocol,
1013+
url: this.streamInfo.url,
1014+
additionalData: {
1015+
...this.streamInfo.additionalData,
1016+
previousSession,
1017+
startTime: position
1018+
}
1019+
}
1020+
1021+
logger(
1022+
'debug',
1023+
'Player',
1024+
`Reusing existing SABR streaming URL for seek to maintain session`
1025+
)
1026+
}
1027+
}
1028+
}
9971029

9981030
const trackInfo = {
9991031
...this.track.info,
10001032
audioTrackId: this.track.audioTrackId
10011033
}
10021034

1003-
const urlData = await this.nodelink.sources.getTrackUrl(trackInfo)
1035+
const urlData = reuseUrlData || await this.nodelink.sources.getTrackUrl(trackInfo)
10041036
this.streamInfo = { ...urlData, trackInfo: this.track.info }
10051037

1006-
10071038
if (urlData.exception) {
10081039
const err = new Error(urlData.exception.message)
10091040
this._onError(err)

src/sources/youtube/YouTube.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ export default class YouTubeSource {
950950
clientInfo: additionalData.clientInfo,
951951
formats: additionalData.formats,
952952
startTime: additionalData.startTime || 0,
953-
positionCallback: additionalData.positionCallback
953+
positionCallback: additionalData.positionCallback,
954+
previousSession: additionalData.previousSession
954955
})
955956

956957
const stream = new PassThrough()
@@ -1064,6 +1065,12 @@ export default class YouTubeSource {
10641065
this.activeStreams.delete(streamKey)
10651066
})
10661067

1068+
stream._sabrStream = sabr
1069+
stream.getSessionState = () => {
1070+
if (isDestroying || stream.destroyed) return null
1071+
return sabr.getSessionState()
1072+
}
1073+
10671074
const bestAudio = additionalData.formats
10681075
.filter((f) => f.mimeType?.includes('audio'))
10691076
.sort((a, b) => (b.bitrate || 0) - (a.bitrate || 0))[0]

0 commit comments

Comments
 (0)