Skip to content

Commit 3cb5911

Browse files
committed
Update demuxer force close
1 parent e513841 commit 3cb5911

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/demux.cc

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ napi_value forceCloseInput(napi_env env, napi_callback_info info) {
531531
napi_status status;
532532
napi_value result, formatJS, formatExt;
533533
AVFormatContext* format;
534-
int ret;
535534

536535
size_t argc = 0;
537536
status = napi_get_cb_info(env, info, &argc, nullptr, &formatJS, nullptr);
@@ -541,15 +540,7 @@ napi_value forceCloseInput(napi_env env, napi_callback_info info) {
541540
status = napi_get_value_external(env, formatExt, (void**) &format);
542541
CHECK_STATUS;
543542

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-
}
543+
formatContextFinalizer(env, format, format->pb);
553544

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

src/format.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3850,9 +3850,13 @@ void formatContextFinalizer(napi_env env, void* data, void* hint) {
38503850
if (fc->protocol_blacklist != nullptr) {
38513851
av_freep(fc->protocol_blacklist);
38523852
} */
3853-
if (fc->iformat != nullptr)
3854-
avformat_close_input(&fc);
3855-
else if (!adaptor) // crashes otherwise...
3853+
if (fc->iformat != nullptr) {
3854+
// The format context we get here is copy so the close_input call won't clear the JS format context
3855+
// Hence copy the formatContext pointer to null the iformat after close to avoid a double delete
3856+
AVFormatContext* closeFc = fc;
3857+
avformat_close_input(&closeFc);
3858+
fc->iformat = nullptr;
3859+
} else if (fc->oformat != nullptr && !adaptor) // crashes otherwise...
38563860
avformat_free_context(fc);
38573861
}
38583862

0 commit comments

Comments
 (0)