@@ -3494,7 +3494,9 @@ napi_value formatToJSON(napi_env env, napi_callback_info info) {
34943494napi_status fromAVFormatContext (napi_env env, AVFormatContext* fmtCtx,
34953495 Adaptor *adaptor, napi_value* result) {
34963496 napi_status status;
3497- napi_value jsFmtCtx, extFmtCtx, extAdaptor, truth, undef;
3497+ napi_value jsFmtCtx, extFmtCtxRef, extFmtCtx, extAdaptor, truth, undef;
3498+ fmtCtxRef* fmtRef = new fmtCtxRef;
3499+ fmtRef->fmtCtx = fmtCtx;
34983500
34993501 bool isMuxer = fmtCtx->oformat != nullptr ;
35003502 bool isFormat = !((fmtCtx->oformat == nullptr ) ^ (fmtCtx->iformat == nullptr ));
@@ -3505,7 +3507,9 @@ napi_status fromAVFormatContext(napi_env env, AVFormatContext* fmtCtx,
35053507 PASS_STATUS;
35063508 status = napi_get_undefined (env, &undef);
35073509 PASS_STATUS;
3508- status = napi_create_external (env, fmtCtx, formatContextFinalizer, adaptor, &extFmtCtx);
3510+ status = napi_create_external (env, fmtRef, formatContextFinalizer, adaptor, &extFmtCtxRef);
3511+ PASS_STATUS;
3512+ status = napi_create_external (env, fmtCtx, nullptr , adaptor, &extFmtCtx);
35093513 PASS_STATUS;
35103514 status = napi_create_external (env, adaptor, nullptr , nullptr , &extAdaptor);
35113515 PASS_STATUS;
@@ -3686,11 +3690,12 @@ napi_status fromAVFormatContext(napi_env env, AVFormatContext* fmtCtx,
36863690 { " newStream" , nullptr , newStream, nullptr , nullptr , nullptr ,
36873691 napi_enumerable, fmtCtx },
36883692 { " toJSON" , nullptr , formatToJSON, nullptr , nullptr , nullptr , napi_default, fmtCtx },
3693+ { " _formatContextRef" , nullptr , nullptr , nullptr , nullptr , extFmtCtxRef, napi_default, nullptr },
36893694 { " _formatContext" , nullptr , nullptr , nullptr , nullptr , extFmtCtx, napi_default, nullptr },
36903695 { " _adaptor" , nullptr , nullptr , nullptr , nullptr , extAdaptor, napi_default, nullptr },
36913696 { " __streams" , nullptr , nullptr , nullptr , nullptr , undef, napi_writable, nullptr }
36923697 };
3693- status = napi_define_properties (env, jsFmtCtx, 56 , desc);
3698+ status = napi_define_properties (env, jsFmtCtx, 57 , desc);
36943699 PASS_STATUS;
36953700 } else {
36963701 napi_property_descriptor desc[] = {
@@ -3810,11 +3815,12 @@ napi_status fromAVFormatContext(napi_env env, AVFormatContext* fmtCtx,
38103815 { " newStream" , nullptr , newStream, nullptr , nullptr , nullptr ,
38113816 napi_enumerable, fmtCtx },
38123817 { " toJSON" , nullptr , formatToJSON, nullptr , nullptr , nullptr , napi_default, fmtCtx },
3818+ { " _formatContextRef" , nullptr , nullptr , nullptr , nullptr , extFmtCtxRef, napi_default, nullptr },
38133819 { " _formatContext" , nullptr , nullptr , nullptr , nullptr , extFmtCtx, napi_default, nullptr },
38143820 { " _adaptor" , nullptr , nullptr , nullptr , nullptr , extAdaptor, napi_default, nullptr },
38153821 { " __streams" , nullptr , nullptr , nullptr , nullptr , undef, napi_writable, nullptr }
38163822 };
3817- status = napi_define_properties (env, jsFmtCtx, 57 , desc);
3823+ status = napi_define_properties (env, jsFmtCtx, 58 , desc);
38183824 PASS_STATUS;
38193825 }
38203826
@@ -3823,17 +3829,13 @@ napi_status fromAVFormatContext(napi_env env, AVFormatContext* fmtCtx,
38233829}
38243830
38253831void formatContextFinalizer (napi_env env, void * data, void * hint) {
3826- AVFormatContext* fc = (AVFormatContext*) data;
3832+ fmtCtxRef* fmtRef = (fmtCtxRef*) data;
3833+ AVFormatContext* fc;
38273834 Adaptor *adaptor = (Adaptor *)hint;
38283835 int ret;
38293836
3830- if (fc->iformat != nullptr ) {
3831- // The format context we get here is a copy so the close_input call won't clear the JS format context
3832- // Hence copy the formatContext pointer to null the iformat after close to avoid a double delete
3833- AVFormatContext* closeFc = fc;
3834- avformat_close_input (&closeFc);
3835- fc->iformat = nullptr ;
3836- } else if (fc->oformat != nullptr ) {
3837+ if (fmtRef->fmtCtx != nullptr ) {
3838+ fc = fmtRef->fmtCtx ;
38373839 if (fc->pb != nullptr ) {
38383840 if (adaptor)
38393841 avio_context_free (&fc->pb );
@@ -3845,20 +3847,26 @@ void formatContextFinalizer(napi_env env, void* data, void* hint) {
38453847 }
38463848 }
38473849 }
3848- // FIXME this is segfaulting ... why
3849- /* if (fc->codec_whitelist != nullptr) {
3850- av_freep(fc->codec_whitelist);
3851- }
3852- if (fc->format_whitelist != nullptr) {
3853- av_freep(fc->format_whitelist);
3854- }
3855- if (fc->protocol_whitelist != nullptr) {
3856- av_freep(fc->protocol_whitelist);
3850+
3851+ if (fc->iformat != nullptr ) {
3852+ avformat_close_input (&fc);
3853+ } else {
3854+ // FIXME this is segfaulting ... why
3855+ /* if (fc->codec_whitelist != nullptr) {
3856+ av_freep(fc->codec_whitelist);
3857+ }
3858+ if (fc->format_whitelist != nullptr) {
3859+ av_freep(fc->format_whitelist);
3860+ }
3861+ if (fc->protocol_whitelist != nullptr) {
3862+ av_freep(fc->protocol_whitelist);
3863+ }
3864+ if (fc->protocol_blacklist != nullptr) {
3865+ av_freep(fc->protocol_blacklist);
3866+ } */
38573867 }
3858- if (fc->protocol_blacklist != nullptr) {
3859- av_freep(fc->protocol_blacklist);
3860- } */
3861- if (!adaptor) // crashes otherwise...
3868+
3869+ if (adaptor != nullptr ) // crashes otherwise...
38623870 avformat_free_context (fc);
38633871 }
38643872}
0 commit comments