Skip to content

Commit

Permalink
refactor: ♻️cache seek fetch request
Browse files Browse the repository at this point in the history
  • Loading branch information
Banou26 committed Jan 28, 2024
1 parent 8758ebf commit 146428e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ extern "C" {
vector<std::string> init_buffers;
int init_buffer_count = 0;

bool first_seek = true;
bool seeking = false;

vector<std::string> seek_buffers;
int seek_buffer_count = 0;

val promise = val::undefined();
// val promise = val::global("Promise")["resolve"]().as<val>();
// val promise = val::global("Promise").get("resolve").as<val>();
Expand Down Expand Up @@ -230,6 +236,7 @@ extern "C" {
void init () {
initializing = true;
init_buffer_count = 0;
seek_buffer_count = 0;
int res;
is_header = true;
duration = 0;
Expand Down Expand Up @@ -614,6 +621,7 @@ extern "C" {
}

int _seek(int timestamp) {
seeking = true;
destroy();
init();

Expand All @@ -629,6 +637,8 @@ extern "C" {
if ((res = av_seek_frame(input_format_context, video_stream_index, timestamp, AVSEEK_FLAG_BACKWARD)) < 0) {
printf("ERROR: could not seek frame | %s \n", av_err2str(res));
}
seeking = false;
first_seek = false;
return 0;
}

Expand Down Expand Up @@ -697,6 +707,22 @@ extern "C" {
buffer = remuxObject.init_buffers[remuxObject.init_buffer_count];
remuxObject.init_buffer_count++;
}
} else if(remuxObject.seeking) {
emscripten::val &randomRead = remuxObject.randomRead;
if (remuxObject.first_seek) {
buffer =
randomRead(
to_string(remuxObject.input_format_context->pb->pos),
buf_size
)
.await()
.as<std::string>();
remuxObject.seek_buffers.push_back(buffer);
} else {
remuxObject.promise.await();
buffer = remuxObject.seek_buffers[remuxObject.seek_buffer_count];
remuxObject.seek_buffer_count++;
}
} else {
emscripten::val result =
remuxObject
Expand Down
5 changes: 2 additions & 3 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Chunk = {
}

const BUFFER_SIZE = 2_500_000
// const VIDEO_URL = '../video5.mkv'
const VIDEO_URL = '../spidey.mkv'
const VIDEO_URL = '../video5.mkv'
// const VIDEO_URL = '../spidey.mkv'

export default async function saveFile(plaintext: ArrayBuffer, fileName: string, fileType: string) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -314,7 +314,6 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
})

const seek = queuedDebounceWithLastCall(500, async (seekTime: number) => {
const p = performance.now()
seeking = true
await appendBuffer(headerChunk.buffer)
chunks = []
Expand Down

0 comments on commit 146428e

Please sign in to comment.