Skip to content

Commit 7481842

Browse files
authored
fix: Fix memory leak on macOS/iOS when streaming high resolution video (#872)
* add limit of buffer count for texture pool * fix MetalTexture2D destructor * fix changelog * [skip ci] Update plugins
1 parent c32afa5 commit 7481842

File tree

12 files changed

+17
-10
lines changed

12 files changed

+17
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ All notable changes to the webrtc package will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7-
## [3.0.0-pre.4] - 2023-01-06
7+
## [3.0.0-pre.4] - 2023-01-13
88

99
### Fixed
1010

1111
- Fix KeyNotFoundException in `RTCPeerConnection` when firing the callback after disposing the its instance.
1212
- Fix crash when streaming high resolution video on Android device.
13+
- Fix memory leak on macOS/iOS when streaming high resolution video.
1314

1415
## [3.0.0-pre.3] - 2022-12-16
1516

Plugin~/WebRTCPlugin/GpuMemoryBufferPool.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ namespace webrtc
7272
(*result)->MarkUnused(clock_->CurrentTime());
7373
}
7474

75-
void GpuMemoryBufferPool::ReleaseStaleBuffers(Timestamp now)
75+
void GpuMemoryBufferPool::ReleaseStaleBuffers(Timestamp now, TimeDelta timeLimit)
7676
{
7777
auto it = resourcesPool_.begin();
7878
while (it != resourcesPool_.end())
7979
{
8080
FrameResources* resources = (*it).get();
8181

82-
constexpr TimeDelta kStaleFrameLimit = TimeDelta::Seconds(10);
83-
if (!resources->IsUsed() && now - resources->lastUseTime() > kStaleFrameLimit)
82+
if (!resources->IsUsed() && now - resources->lastUseTime() > timeLimit)
8483
{
8584
resourcesPool_.erase(it++);
8685
}

Plugin~/WebRTCPlugin/GpuMemoryBufferPool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace webrtc
2222

2323
rtc::scoped_refptr<VideoFrame>
2424
CreateFrame(NativeTexPtr ptr, const Size& size, UnityRenderingExtTextureFormat format, Timestamp timestamp);
25-
void ReleaseStaleBuffers(Timestamp timestamp);
25+
void ReleaseStaleBuffers(Timestamp timestamp, TimeDelta timeLimit);
2626

2727
size_t bufferCount() { return resourcesPool_.size(); }
2828

Plugin~/WebRTCPlugin/GraphicsDevice/Metal/MetalTexture2D.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace webrtc
1212
{
1313
public:
1414
MetalTexture2D(uint32_t w, uint32_t h, id<MTLTexture> tex);
15-
virtual ~MetalTexture2D();
15+
virtual ~MetalTexture2D() override;
1616

1717
inline void* GetNativeTexturePtrV() override;
1818
inline const void* GetNativeTexturePtrV() const override;

Plugin~/WebRTCPlugin/GraphicsDevice/Metal/MetalTexture2D.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
MetalTexture2D::~MetalTexture2D()
1717
{
18-
m_texture = nullptr;
18+
[m_texture release];
1919
}
2020

2121
} // end namespace webrtc

Plugin~/WebRTCPlugin/UnityRenderEvent.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace webrtc
3535
static std::map<const uint32_t, std::shared_ptr<UnityVideoRenderer>> s_mapVideoRenderer;
3636
static std::unique_ptr<Clock> s_clock;
3737

38+
static const size_t kLimitBufferCount = 10;
39+
static constexpr TimeDelta kStaleFrameLimit = TimeDelta::Seconds(10);
3840
static const UnityProfilerMarkerDesc* s_MarkerEncode = nullptr;
3941
static const UnityProfilerMarkerDesc* s_MarkerDecode = nullptr;
4042
static std::unique_ptr<IGraphicsDevice> s_gfxDevice;
@@ -250,6 +252,8 @@ static void UNITY_INTERFACE_API OnRenderEvent(int eventID, void* data)
250252
UnityGfxRenderer gfxRenderer = device->GetGfxRenderer();
251253
void* ptr = GraphicsUtility::TextureHandleToNativeGraphicsPtr(encodeData->texture, device, gfxRenderer);
252254
unity::webrtc::Size size(encodeData->width, encodeData->height);
255+
256+
if (s_bufferPool->bufferCount() < kLimitBufferCount)
253257
{
254258
std::unique_ptr<const ScopedProfiler> profiler;
255259
if (s_ProfilerMarkerFactory)
@@ -258,14 +262,14 @@ static void UNITY_INTERFACE_API OnRenderEvent(int eventID, void* data)
258262
auto frame = s_bufferPool->CreateFrame(ptr, size, encodeData->format, timestamp);
259263
source->OnFrameCaptured(std::move(frame));
260264
}
261-
s_bufferPool->ReleaseStaleBuffers(timestamp);
265+
s_bufferPool->ReleaseStaleBuffers(timestamp, kStaleFrameLimit);
262266
}
263267

264268
static void UNITY_INTERFACE_API OnReleaseBuffers(int eventID, void* data)
265269
{
266270
// Release all buffers.
267271
if (s_bufferPool)
268-
s_bufferPool->ReleaseStaleBuffers(Timestamp::PlusInfinity());
272+
s_bufferPool->ReleaseStaleBuffers(Timestamp::PlusInfinity(), kStaleFrameLimit);
269273
}
270274

271275
extern "C" UnityRenderingEventAndData UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc(Context* context)

Plugin~/WebRTCPluginTest/GpuMemoryBufferPoolTest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ namespace webrtc
3838
return std::unique_ptr<ITexture2D>(tex);
3939
}
4040

41-
void ReleaseStaleBuffers(Timestamp timestamp) { bufferPool_->ReleaseStaleBuffers(timestamp); }
41+
void ReleaseStaleBuffers(Timestamp timestamp)
42+
{
43+
bufferPool_->ReleaseStaleBuffers(timestamp, TimeDelta::Seconds(10));
44+
}
4245

4346
std::unique_ptr<GraphicsDeviceContainer> container_;
4447
IGraphicsDevice* device_;

Runtime/Plugins/Android/libwebrtc.aar

-669 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Runtime/Plugins/macOS/libwebrtc.dylib

48 Bytes
Binary file not shown.

Runtime/Plugins/x86_64/libwebrtc.so

64 Bytes
Binary file not shown.

Runtime/Plugins/x86_64/webrtc.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)