Skip to content

Add demuxer.interrupt() #56

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions src/demux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ void demuxerExecute(napi_env env, void* data) {
return;
}

// Use the user-defined opaque field as a boolean flag to signal if blocking I/O should
// be stopped. ffmpeg will periodically poll this value through the interrupt_callback.
c->format->opaque = 0;
c->format->interrupt_callback.callback = interrupt_callback;
c->format->interrupt_callback.opaque = &c->format->opaque;

if (c->adaptor) {
AVIOContext* avio_ctx = avio_alloc_context(nullptr, 0, 0, c->adaptor, &read_packet, nullptr, nullptr);
if (!avio_ctx) {
Expand Down Expand Up @@ -108,6 +114,12 @@ void demuxerComplete(napi_env env, napi_status asyncStatus, void* data) {
c->status = napi_set_named_property(env, result, "forceClose", prop);
REJECT_STATUS;

c->status = napi_create_function(env, "interrupt", NAPI_AUTO_LENGTH, interrupt,
c, &prop);
REJECT_STATUS;
c->status = napi_set_named_property(env, result, "interrupt", prop);
REJECT_STATUS;

napi_status status;
status = napi_resolve_deferred(env, c->_deferred, result);
FLOATING_STATUS;
Expand Down Expand Up @@ -582,3 +594,29 @@ napi_value forceCloseInput(napi_env env, napi_callback_info info) {
CHECK_STATUS;
return result;
}

napi_value interrupt(napi_env env, napi_callback_info info) {
napi_status status;
napi_value result, formatJS, formatRefExt;
fmtCtxRef* fmtRef;
size_t argc = 0;

status = napi_get_cb_info(env, info, &argc, nullptr, &formatJS, nullptr);
CHECK_STATUS;
status = napi_get_named_property(env, formatJS, "_formatContextRef", &formatRefExt);
CHECK_STATUS;
status = napi_get_value_external(env, formatRefExt, (void**) &fmtRef);
CHECK_STATUS;

fmtRef->fmtCtx->opaque = (void *)1;

status = napi_get_undefined(env, &result);
CHECK_STATUS;

return result;
}

int interrupt_callback(void *opaque) {
int* i = (int*) opaque;
return *i;
}
2 changes: 2 additions & 0 deletions src/demux.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void demuxerFinalizer(napi_env env, void* data, void* hint);
void readBufferFinalizer(napi_env env, void* data, void* hint);

napi_value forceCloseInput(napi_env env, napi_callback_info info);
napi_value interrupt(napi_env env, napi_callback_info info);
int interrupt_callback(void *opaque);

struct demuxerCarrier : carrier {
const char* filename = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions types/Demuxer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ export interface Demuxer extends Omit<FormatContext,
* Abandon the demuxing process and forcibly close the file or stream without waiting for it to finish
*/
forceClose(): undefined

/**
* This causes any currently blocking IO to be interrupted and raise an "Immediate exit requested" error.
*/
interrupt(): undefined;
}

/**
Expand Down