diff --git a/sdk/objc/components/video_codec/RTCVideoEncoderH264.mm b/sdk/objc/components/video_codec/RTCVideoEncoderH264.mm index 6584c88c61..5b90922fca 100644 --- a/sdk/objc/components/video_codec/RTCVideoEncoderH264.mm +++ b/sdk/objc/components/video_codec/RTCVideoEncoderH264.mm @@ -782,9 +782,7 @@ NSUInteger GetMaxSampleRate(const webrtc::H264::ProfileLevelId &profile_level_id RTC_LOG(LS_INFO) << "Generated keyframe"; } - // Convert the sample buffer into a buffer suitable for RTP packetization. - // TODO(tkchin): Allocate buffers through a pool. - std::unique_ptr buffer(new rtc::Buffer()); + __block std::unique_ptr buffer = std::make_unique(); RTCRtpFragmentationHeader *header; { std::unique_ptr header_cpp; @@ -797,7 +795,12 @@ NSUInteger GetMaxSampleRate(const webrtc::H264::ProfileLevelId &profile_level_id } RTCEncodedImage *frame = [[RTCEncodedImage alloc] init]; - frame.buffer = [NSData dataWithBytesNoCopy:buffer->data() length:buffer->size() freeWhenDone:NO]; + // This assumes ownership of `buffer` and is responsible for freeing it when done. + frame.buffer = [[NSData alloc] initWithBytesNoCopy:buffer->data() + length:buffer->size() + deallocator:^(void *bytes, NSUInteger size) { + buffer.reset(); + }]; frame.encodedWidth = width; frame.encodedHeight = height; frame.completeFrame = YES;