@@ -273,7 +273,8 @@ DesktopCaptureImpl::DesktopCaptureImpl(const int32_t aId, const char* aUniqueId,
273273 mDeviceType(aType),
274274 mControlThread(mozilla::GetCurrentSerialEventTarget()),
275275 mNextFrameMinimumTime(Timestamp::Zero()),
276- mCallbacks(" DesktopCaptureImpl::mCallbacks" ) {}
276+ mCallbacks(" DesktopCaptureImpl::mCallbacks" ),
277+ mBufferPool(false , 2 ) {}
277278
278279DesktopCaptureImpl::~DesktopCaptureImpl () {
279280 MOZ_ASSERT (!mCaptureThread );
@@ -399,6 +400,8 @@ int32_t DesktopCaptureImpl::StopCapture() {
399400 mRequestedCapability = mozilla::Nothing ();
400401 }
401402
403+ mBufferPool .Release ();
404+
402405 if (mCaptureThread ) {
403406 // CaptureThread shutdown.
404407 mCaptureThread ->AsyncShutdown ();
@@ -472,18 +475,24 @@ void DesktopCaptureImpl::OnCaptureResult(DesktopCapturer::Result aResult,
472475 return ;
473476 }
474477
475- int stride_y = width;
476- int stride_uv = (width + 1 ) / 2 ;
477-
478478 // Setting absolute height (in case it was negative).
479479 // In Windows, the image starts bottom left, instead of top left.
480480 // Setting a negative source height, inverts the image (within LibYuv).
481481
482482 mozilla::PerformanceRecorder<mozilla::CopyVideoStage> rec (
483483 " DesktopCaptureImpl::ConvertToI420" _ns, mTrackingId , width, abs (height));
484- // TODO(nisse): Use a pool?
485- webrtc::scoped_refptr<I420Buffer> buffer =
486- I420Buffer::Create (width, abs (height), stride_y, stride_uv, stride_uv);
484+ // The more basic I420Buffer constructor defaults stride_y to width;
485+ // and stride_u and stride_v as (width + 1) / 2, so we can use this
486+ // call to CreateI420Buffer that takes only width and height.
487+ webrtc::scoped_refptr<webrtc::I420Buffer> buffer =
488+ mBufferPool .CreateI420Buffer (width, abs (height));
489+ if (!buffer) {
490+ RTC_LOG (LS_ERROR) << " Failed to allocate I420Buffer from pool." ;
491+ MOZ_ASSERT_UNREACHABLE (
492+ " We might fail to allocate a buffer, but with this "
493+ " being a recycling pool that shouldn't happen" );
494+ return ;
495+ }
487496
488497 const int conversionResult = libyuv::ConvertToI420 (
489498 videoFrame, videoFrameLength, buffer->MutableDataY (), buffer->StrideY (),
0 commit comments