Skip to content
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 5 commits into from
Jun 20, 2024

Conversation

pbdm
Copy link
Contributor

@pbdm pbdm commented Jun 19, 2024

No description provided.

}

async function getSegmentBuffer(url: string) {
return segmentBufferFetchqueue[url].then((value) => value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

替换为 return await segmentBufferFetchqueue[url]


const runTask = createTaskQueue(concurrency);

for (const [, gData] of Object.entries(segGroup)) {
Copy link
Member

@hughfenghen hughfenghen Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

复用 load 中转换的数据 filterSegGroup,有些片段是无需下载的。

@@ -3,7 +3,7 @@ import { Parser } from 'm3u8-parser';
/**
* 创建一个 HLS 资源加载器
*/
export async function createHLSLoader(m3u8URL: string) {
export async function createHLSLoader(m3u8URL: string, concurrency = 10) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

参数 concurrency = 10 移到内部,默认不对外开放,若有有需求再讨论开放。

可以简化 API 、降低破坏性变更几率。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要,不同业务方期望的并行数量应该是不一样的

const task = queue.shift();
running++;
try {
if (task) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

替换为 await task?.()

await task();
}
} catch (err) {
console.error(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

替换为 Log.error

async function downloadSegGroup() {
function createTaskQueue(concurrency: number) {
let running = 0;
const queue = [] as Function[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

替换为 as Array<() => Promise<void>>

await task();
}
} catch (err) {
console.error(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

异常时需要中断流,否则流永远不会结束,且使用方无法感知到错误。

} catch (err) {
console.error(err);
// 异常时中断流
ctrl.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接 close 不会抛出错误,应使用 ctrl.error(err)

@@ -65,7 +76,7 @@ export async function createHLSLoader(m3u8URL: string, concurrency = 10) {
}

async function getSegmentBuffer(url: string) {
return segmentBufferFetchqueue[url].then((value) => value);
return segmentBufferFetchqueue[url];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

移除 async 或 添加 await, await segmentBufferFetchqueue[url]
或移除该函数,因为只有一个行代码

console.error(err);
// 异常时中断流
ctrl.close();
Log.error(err);
Copy link
Member

@hughfenghen hughfenghen Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一个错误不会完全终止后续并发
抛出错误后,应该加一个状态位,阻止后续的并发请求,或者说阻止后续 next 的执行

如果可以的话,补一个单测覆盖错误场景

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个已经有的,next 已经放到 try 里了,我补下单测

@@ -77,6 +139,7 @@ export async function createHLSLoader(m3u8URL: string) {
actualEndTime,
stream: new ReadableStream<Uint8Array>({
start: async (ctrl) => {
downloadSegGroup(filterSegGroup, ctrl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该 download segments,而不是 filterSegGroup。

}

async function getSegmentBuffer(url: string) {
return segmentBufferFetchqueue[url];
Copy link
Member

@hughfenghen hughfenghen Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用完的数据需要删除引用,否则不会被回收,增加内存占用。
delete segmentBufferFetchqueue[url]

@hughfenghen hughfenghen merged commit 8f6ec55 into bilibili:main Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants