Skip to content

Commit 2c1eab9

Browse files
committed
fix bug
1 parent a2351f3 commit 2c1eab9

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

webrtc-sys/src/mpp/h264_encoder_impl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,18 +429,25 @@ int32_t MppH264EncoderImpl::Encode(
429429
if (ret != MPP_OK) {
430430
RTC_LOG(LS_ERROR) << "encode_get_packet failed: " << ret;
431431
mpp_frame_deinit(&frame);
432-
mpp_packet_deinit(&packet);
432+
// After encode_put_frame succeeded, MPP owns the packet via metadata;
433+
// do not deinit packet here to avoid double-free.
433434
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
434435
}
435436

436437
int32_t result = WEBRTC_VIDEO_CODEC_OK;
437438
if (out_packet) {
438439
result = ProcessEncodedPacket(out_packet, input_frame);
440+
// out_packet is the same object as packet (MPP fills and returns the
441+
// pre-allocated packet we attached via KEY_OUTPUT_PACKET metadata).
442+
// Only deinit once to avoid double-free / negative ref-count errors.
439443
mpp_packet_deinit(&out_packet);
444+
packet = nullptr; // prevent double deinit below
440445
}
441446

442447
mpp_frame_deinit(&frame);
443-
mpp_packet_deinit(&packet);
448+
if (packet) {
449+
mpp_packet_deinit(&packet);
450+
}
444451

445452
return result;
446453
}

webrtc-sys/src/mpp/h265_encoder_impl.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,26 @@ int32_t MppH265EncoderImpl::Encode(
382382
if (ret != MPP_OK) {
383383
RTC_LOG(LS_ERROR) << "encode_get_packet failed: " << ret;
384384
mpp_frame_deinit(&frame);
385-
mpp_packet_deinit(&packet);
385+
// After encode_put_frame succeeded, MPP owns the packet via metadata;
386+
// do not deinit packet here to avoid double-free.
386387
return WEBRTC_VIDEO_CODEC_ENCODER_FAILURE;
387388
}
388389

389390
int32_t result = WEBRTC_VIDEO_CODEC_OK;
390391
if (out_packet) {
391392
result = ProcessEncodedPacket(out_packet, input_frame);
393+
// out_packet is the same object as packet (MPP fills and returns the
394+
// pre-allocated packet we attached via KEY_OUTPUT_PACKET metadata).
395+
// Only deinit once to avoid double-free / negative ref-count errors.
392396
mpp_packet_deinit(&out_packet);
397+
packet = nullptr; // prevent double deinit below
393398
}
394399

395400
current_encoding_is_keyframe_ = false;
396401
mpp_frame_deinit(&frame);
397-
mpp_packet_deinit(&packet);
402+
if (packet) {
403+
mpp_packet_deinit(&packet);
404+
}
398405

399406
return result;
400407
}

0 commit comments

Comments
 (0)