Skip to content

Commit e513841

Browse files
committed
Add demuxer force close, improve demuxer cleanup on GC
1 parent 2440a48 commit e513841

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ let demuxer = demuxerStream.demuxer({
472472
});
473473
```
474474

475+
To abandon the demuxing process and forcibly close the input file or stream, call the synchronous `forceClose()` method of the demuxer.
476+
475477
### Decoding
476478

477479
Decoding is the process of taking a stream of compressed data in the form of _packets_ and converting it into uncompressed _frames_. In general, to decode an interleaved (multiplexed) media file, you need a decoder for each of the video and the audio streams. For the purpose of keeping the examples simple in this section, only a single stream is decoded. However, it is possible to set up more than one decoder - say for related video and audio streams - and run them asynchronously and in parallel, i.e. to decode the video and audio required to present a frame.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "beamcoder",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"description": "Node.js native bindings to FFmpeg.",
55
"main": "index.js",
66
"types": "index.d.ts",

src/demux.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ void demuxerComplete(napi_env env, napi_status asyncStatus, void* data) {
102102
c->status = napi_set_named_property(env, result, "seek", prop);
103103
REJECT_STATUS;
104104

105+
c->status = napi_create_function(env, "forceClose", NAPI_AUTO_LENGTH, forceCloseInput,
106+
nullptr, &prop);
107+
REJECT_STATUS;
108+
c->status = napi_set_named_property(env, result, "forceClose", prop);
109+
REJECT_STATUS;
110+
105111
napi_status status;
106112
status = napi_resolve_deferred(env, c->_deferred, result);
107113
FLOATING_STATUS;
@@ -520,3 +526,32 @@ napi_value seekFrame(napi_env env, napi_callback_info info) {
520526

521527
return promise;
522528
};
529+
530+
napi_value forceCloseInput(napi_env env, napi_callback_info info) {
531+
napi_status status;
532+
napi_value result, formatJS, formatExt;
533+
AVFormatContext* format;
534+
int ret;
535+
536+
size_t argc = 0;
537+
status = napi_get_cb_info(env, info, &argc, nullptr, &formatJS, nullptr);
538+
CHECK_STATUS;
539+
status = napi_get_named_property(env, formatJS, "_formatContext", &formatExt);
540+
CHECK_STATUS;
541+
status = napi_get_value_external(env, formatExt, (void**) &format);
542+
CHECK_STATUS;
543+
544+
if (format->pb != nullptr) {
545+
ret = avio_closep(&format->pb);
546+
if (ret < 0) {
547+
NAPI_THROW_ERROR(avErrorMsg("Failed to force close demuxer resource: ", ret));
548+
}
549+
} else {
550+
printf("DEBUG: Demuxer IO resource '%s' already closed or not managed by AVIO.\n",
551+
(format->url != nullptr) ? format->url : "unknown");
552+
}
553+
554+
status = napi_get_undefined(env, &result);
555+
CHECK_STATUS;
556+
return result;
557+
}

src/demux.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ napi_value seekFrame(napi_env env, napi_callback_info info);
5252
void demuxerFinalizer(napi_env env, void* data, void* hint);
5353
void readBufferFinalizer(napi_env env, void* data, void* hint);
5454

55+
napi_value forceCloseInput(napi_env env, napi_callback_info info);
56+
5557
struct demuxerCarrier : carrier {
5658
const char* filename = nullptr;
5759
Adaptor *adaptor = nullptr;

src/format.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3850,7 +3850,9 @@ void formatContextFinalizer(napi_env env, void* data, void* hint) {
38503850
if (fc->protocol_blacklist != nullptr) {
38513851
av_freep(fc->protocol_blacklist);
38523852
} */
3853-
if (!adaptor) // crashes otherwise...
3853+
if (fc->iformat != nullptr)
3854+
avformat_close_input(&fc);
3855+
else if (!adaptor) // crashes otherwise...
38543856
avformat_free_context(fc);
38553857
}
38563858

0 commit comments

Comments
 (0)