Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
61 changes: 48 additions & 13 deletions src/playback/processing/streamProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,29 @@ const _extractSeekProxy = (
: undefined
}

const _extractSeekUserAgent = (streamInfo: StreamInfo): string | undefined => {
const additionalData = streamInfo?.additionalData as
| Record<string, unknown>
| undefined

return typeof additionalData?.userAgent === 'string'
? additionalData.userAgent
: undefined
}

async function _fetchRange(
url: string,
start: number,
endInclusive: number,
proxy?: HttpProxyConfig
proxy?: HttpProxyConfig,
userAgent?: string
): Promise<Buffer> {
if (proxy) {
const response = await http1makeRequest(url, {
method: 'GET',
headers: {
Range: `bytes=${start}-${endInclusive}`
Range: `bytes=${start}-${endInclusive}`,
...(userAgent ? { 'User-Agent': userAgent } : {})
} as HttpRequestHeaders,
responseType: 'buffer',
proxy
Expand All @@ -377,7 +389,10 @@ async function _fetchRange(
}

const res = await fetch(url, {
headers: { Range: `bytes=${start}-${endInclusive}` }
headers: {
Range: `bytes=${start}-${endInclusive}`,
...(userAgent ? { 'User-Agent': userAgent } : {})
}
})
if (!res.ok) {
throw new Error(`HTTP ${res.status} while fetching range`)
Expand All @@ -389,13 +404,15 @@ async function _fetchRange(
async function _openRangeStream(
url: string,
start: number,
proxy?: HttpProxyConfig
proxy?: HttpProxyConfig,
userAgent?: string
): Promise<Readable> {
if (proxy) {
const response = await http1makeRequest(url, {
method: 'GET',
headers: {
Range: `bytes=${start}-`
Range: `bytes=${start}-`,
...(userAgent ? { 'User-Agent': userAgent } : {})
} as HttpRequestHeaders,
streamOnly: true,
proxy
Expand All @@ -414,7 +431,10 @@ async function _openRangeStream(
}

const res = await fetch(url, {
headers: { Range: `bytes=${start}-` }
headers: {
Range: `bytes=${start}-`,
...(userAgent ? { 'User-Agent': userAgent } : {})
}
})
if (!res.ok) {
throw new Error(`HTTP ${res.status} while opening range stream`)
Expand All @@ -434,7 +454,8 @@ const _seekOffset = (res: MP4BoxSeekResult): number => {
async function _buildMp4SeekOptions(
url: string,
seekTimeMs: number,
proxy?: HttpProxyConfig
proxy?: HttpProxyConfig,
userAgent?: string
): Promise<MP4ToAACStreamOptions> {
const mp4Box = await getMP4Box()
const mp4 = mp4Box.createFile() as unknown as MP4BoxFile
Expand All @@ -459,7 +480,8 @@ async function _buildMp4SeekOptions(
url,
nextStart,
nextStart + CHUNK - 1,
proxy
proxy,
userAgent
)
const ab = _toArrayBufferWithFileStart(buf, nextStart)

Expand Down Expand Up @@ -524,7 +546,8 @@ type SeekableResponseLike = Readable & {
}

const _createSeekableProxyRequest = (
proxy?: HttpProxyConfig
proxy?: HttpProxyConfig,
userAgent?: string
):
| ((
requestUrl: string | URL,
Expand All @@ -543,11 +566,16 @@ const _createSeekableProxyRequest = (
headers?: Record<string, string>
}
): Promise<SeekableResponseLike> => {
const headers = {
...(userAgent ? { 'User-Agent': userAgent } : {}),
...(options?.headers ?? {})
} as HttpRequestHeaders

const response = await http1makeRequest(
typeof requestUrl === 'string' ? requestUrl : requestUrl.toString(),
{
method: options?.method ?? 'GET',
headers: (options?.headers ?? {}) as HttpRequestHeaders,
headers,
streamOnly: true,
proxy
}
Expand Down Expand Up @@ -3103,6 +3131,7 @@ export const createSeekeableAudioResource = async (
const ext = _extFromUrl(url)
const containerGuess = hinted || ext
const seekProxy = _extractSeekProxy(player.streamInfo)
const seekUserAgent = _extractSeekUserAgent(player.streamInfo)

logger(
'debug',
Expand All @@ -3111,12 +3140,18 @@ export const createSeekeableAudioResource = async (
)

if (_isMp4Format(containerGuess)) {
const mp4Seek = await _buildMp4SeekOptions(url, seekTime, seekProxy)
const mp4Seek = await _buildMp4SeekOptions(
url,
seekTime,
seekProxy,
seekUserAgent
)

const ranged = await _openRangeStream(
url,
mp4Seek.baseFileStart ?? 0,
seekProxy
seekProxy,
seekUserAgent
)

const passthroughStream = new PassThrough({
Expand Down Expand Up @@ -3161,7 +3196,7 @@ export const createSeekeableAudioResource = async (
seekTime,
endTime,
{},
_createSeekableProxyRequest(seekProxy)
_createSeekableProxyRequest(seekProxy, seekUserAgent)
)) as { stream: Readable; meta: SeekableStreamMeta }

const passthroughStream = new PassThrough({
Expand Down
Loading
Loading