-
Notifications
You must be signed in to change notification settings - Fork 156
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(av-cliper): concurrency download for hls-loader #140
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
468f987
feat(av-cliper): concurrency download for hls-loader
pbdm ee7f424
feat(av-cliper): concurrency download for hls-loader
pbdm e790dd2
feat(av-cliper): concurrency download for hls-loader
pbdm a820688
typo
hughfenghen f58342b
clear task queue
hughfenghen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Parser } from 'm3u8-parser'; | ||
import { Log } from '../log'; | ||
|
||
/** | ||
* 创建一个 HLS 资源加载器 | ||
*/ | ||
export async function createHLSLoader(m3u8URL: string) { | ||
export async function createHLSLoader(m3u8URL: string, concurrency = 10) { | ||
const parser = new Parser(); | ||
parser.push(await (await fetch(m3u8URL)).text()); | ||
parser.end(); | ||
|
@@ -17,6 +18,60 @@ export async function createHLSLoader(m3u8URL: string) { | |
); | ||
const base = new URL(m3u8URL, location.href); | ||
|
||
const segmentBufferFetchqueue = {} as Record<string, Promise<ArrayBuffer>>; | ||
|
||
async function downloadSegments( | ||
segments: Parser['manifest']['segments'], | ||
ctrl: ReadableStreamDefaultController<Uint8Array>, | ||
) { | ||
function createTaskQueue(concurrency: number) { | ||
let running = 0; | ||
const queue = [] as Array<() => Promise<ArrayBuffer>>; | ||
|
||
async function runTask(task: () => Promise<ArrayBuffer>) { | ||
queue.push(task); | ||
next(); | ||
} | ||
|
||
async function next() { | ||
if (running < concurrency && queue.length) { | ||
const task = queue.shift(); | ||
running++; | ||
try { | ||
await task?.(); | ||
next(); | ||
} catch (err) { | ||
queue = []; | ||
ctrl.error(err); | ||
Log.error(err); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一个错误不会完全终止后续并发 如果可以的话,补一个单测覆盖错误场景 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个已经有的,next 已经放到 try 里了,我补下单测 |
||
} | ||
running--; | ||
} | ||
} | ||
|
||
return runTask; | ||
} | ||
|
||
async function fetchSegmentBufferPromise(url: string) { | ||
return (await fetch(url)).arrayBuffer(); | ||
} | ||
|
||
const runTask = createTaskQueue(concurrency); | ||
|
||
for (const [, item] of segments.entries()) { | ||
const url = new URL(item.uri, base).href; | ||
runTask( | ||
() => (segmentBufferFetchqueue[url] = fetchSegmentBufferPromise(url)), | ||
); | ||
} | ||
} | ||
|
||
async function getSegmentBuffer(url: string) { | ||
const segmentBuffer = await segmentBufferFetchqueue[url]; | ||
delete segmentBufferFetchqueue[url]; | ||
return segmentBuffer; | ||
} | ||
|
||
return { | ||
/** | ||
* 下载期望时间区间的分配数据,封装成流 | ||
|
@@ -77,6 +132,7 @@ export async function createHLSLoader(m3u8URL: string) { | |
actualEndTime, | ||
stream: new ReadableStream<Uint8Array>({ | ||
start: async (ctrl) => { | ||
downloadSegments(segments, ctrl); | ||
ctrl.enqueue( | ||
new Uint8Array( | ||
await ( | ||
|
@@ -86,13 +142,8 @@ export async function createHLSLoader(m3u8URL: string) { | |
); | ||
}, | ||
pull: async (ctrl) => { | ||
ctrl.enqueue( | ||
new Uint8Array( | ||
await ( | ||
await fetch(new URL(segments[segIdx].uri, base).href) | ||
).arrayBuffer(), | ||
), | ||
); | ||
const url = new URL(segments[segIdx].uri, base).href; | ||
ctrl.enqueue(new Uint8Array(await getSegmentBuffer(url))); | ||
segIdx += 1; | ||
if (segIdx >= segments.length) ctrl.close(); | ||
}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
参数
concurrency = 10
移到内部,默认不对外开放,若有有需求再讨论开放。可以简化 API 、降低破坏性变更几率。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要,不同业务方期望的并行数量应该是不一样的