Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 5240ef1

Browse files
committed
Remove some redundant code.
1 parent cb71e52 commit 5240ef1

File tree

2 files changed

+58
-46
lines changed

2 files changed

+58
-46
lines changed

talk/owt/sdk/base/win/videorendererd3d11.cc

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ WebrtcVideoRendererD3D11Impl::WebrtcVideoRendererD3D11Impl(HWND wnd)
2424
CreateDXGIFactory(__uuidof(IDXGIFactory2), (void**)(&dxgi_factory_));
2525
}
2626

27+
// The swapchain needs to use window height/width of even number.
28+
bool WebrtcVideoRendererD3D11Impl::GetWindowSizeForSwapChain(int& width, int& height) {
29+
if (!wnd_ || !IsWindow(wnd_))
30+
return false;
31+
32+
RECT rect;
33+
GetClientRect(wnd_, &rect);
34+
width = rect.right - rect.left;
35+
height = rect.bottom - rect.top;
36+
37+
if (width % 2) {
38+
window_width += 1;
39+
}
40+
if (height % 2) {
41+
height += 1;
42+
}
43+
44+
return true;
45+
}
46+
2747
void WebrtcVideoRendererD3D11Impl::OnFrame(
2848
const webrtc::VideoFrame& video_frame) {
2949
uint16_t width = video_frame.video_frame_buffer()->width();
@@ -36,6 +56,8 @@ void WebrtcVideoRendererD3D11Impl::OnFrame(
3656
if (!wnd_ || !IsWindow(wnd_) || !IsWindowVisible(wnd_))
3757
return;
3858

59+
// Window width here is used to scale down the I420 frame,
60+
// so we're not rounding it up to even number.
3961
RECT rect;
4062
GetClientRect(wnd_, &rect);
4163
int window_width_ = rect.right - rect.left;
@@ -132,17 +154,12 @@ bool WebrtcVideoRendererD3D11Impl::InitSwapChain(int width,
132154
}
133155

134156
HRESULT hr = S_OK;
135-
RECT rect;
136-
GetClientRect(wnd_, &rect);
137-
window_width = rect.right - rect.left;
138-
window_height = rect.bottom - rect.top;
139157

140-
if (window_width % 2) {
141-
window_width += 1;
142-
}
143-
if (window_height % 2) {
144-
window_height += 1;
158+
if (!GetWindowSizeForSwapChain(window_width, window_height)) {
159+
RTC_LOG(LS_ERROR) << "Failed to get window size for swapchian.";
160+
return false;
145161
}
162+
146163
rtc::CritScope lock(&d3d11_texture_lock_);
147164
if (swap_chain_for_hwnd_) {
148165
DXGI_SWAP_CHAIN_DESC desc;
@@ -161,7 +178,7 @@ bool WebrtcVideoRendererD3D11Impl::InitSwapChain(int width,
161178
hr = swap_chain_for_hwnd_->ResizeBuffers(0, window_width, window_height,
162179
DXGI_FORMAT_UNKNOWN, desc.Flags);
163180
if (FAILED(hr)) {
164-
RTC_LOG(LS_ERROR) << "failed to resize buffer for swapchain.";
181+
RTC_LOG(LS_ERROR) << "Failed to resize buffer for swapchain.";
165182
return false;
166183
}
167184
} else {
@@ -234,8 +251,10 @@ void WebrtcVideoRendererD3D11Impl::RenderNativeHandleFrame(
234251

235252
ID3D11Device* render_device = native_handle->d3d11_device;
236253

237-
if (!render_device)
254+
if (!render_device) {
255+
RTC_LOG(LS_ERROR) << "Decoder passed an invalid d3d11 device.";
238256
return;
257+
}
239258

240259
d3d11_device_ = render_device;
241260
d3d11_texture_ = native_handle->texture;
@@ -258,29 +277,28 @@ void WebrtcVideoRendererD3D11Impl::RenderNV12DXGIMPO(int width, int height) {
258277
return;
259278
}
260279

261-
RECT rect;
262-
GetClientRect(wnd_, &rect);
263-
window_width = rect.right - rect.left;
264-
window_height = rect.bottom - rect.top;
265-
266-
if (window_width % 2) {
267-
window_width += 1;
268-
}
269-
if (window_height % 2) {
270-
window_height += 1;
280+
if (!GetWindowSizeForSwapChain(window_width, window_height)) {
281+
RTC_LOG(LS_ERROR) << "Failed to get window size for swapchain.";
282+
return;
271283
}
284+
272285
if (!d3d11_video_device_) {
273286
hr = d3d11_device_->QueryInterface(__uuidof(ID3D11VideoDevice),
274287
(void**)&d3d11_video_device_);
275-
if (FAILED(hr))
288+
if (FAILED(hr)) {
289+
RTC_LOG(LS_ERROR)
290+
<< "Failed to get d3d11 video device from d3d11 device.";
276291
return;
292+
}
277293
}
278294

279295
if (swap_chain_for_hwnd_) {
280296
DXGI_SWAP_CHAIN_DESC desc;
281297
hr = swap_chain_for_hwnd_->GetDesc(&desc);
282-
if (FAILED(hr))
298+
if (FAILED(hr)) {
299+
RTC_LOG(LS_ERROR) << "Failed to get the swapchain descriptor.";
283300
return;
301+
}
284302

285303
if (desc.BufferDesc.Width != (unsigned int)window_width ||
286304
desc.BufferDesc.Height != (unsigned int)window_height) {
@@ -290,18 +308,23 @@ void WebrtcVideoRendererD3D11Impl::RenderNV12DXGIMPO(int width, int height) {
290308

291309
hr = swap_chain_for_hwnd_->ResizeBuffers(0, window_width, window_height,
292310
DXGI_FORMAT_UNKNOWN, desc.Flags);
293-
if (FAILED(hr))
311+
if (FAILED(hr)) {
312+
RTC_LOG(LS_ERROR) << "Resizing compositor swapchain failed.";
294313
return;
314+
}
295315
}
296316
}
297317

318+
// We are actually not resetting video processor when no input/output size change.
298319
bool reset = false;
299320

300321
if (!d3d11_video_context_) {
301322
hr = d3d11_device_context_->QueryInterface(__uuidof(ID3D11VideoContext),
302323
(void**)&d3d11_video_context_);
303-
if (FAILED(hr))
324+
if (FAILED(hr)) {
325+
RTC_LOG(LS_ERROR) << "Querying d3d11 video context failed.";
304326
return;
327+
}
305328
}
306329

307330
if (!CreateVideoProcessor(width, height, reset))
@@ -369,21 +392,12 @@ bool WebrtcVideoRendererD3D11Impl::InitMPO(int width, int height) {
369392
RECT rect;
370393
GetClientRect(wnd_, &rect);
371394

372-
// width and height has to be even number
373-
int rect_width = rect.right - rect.left;
374-
int rect_height = rect.bottom - rect.top;
375-
376-
window_width = rect_width;
377-
window_height = rect_height;
378-
379-
if (window_width % 2) {
380-
window_width += 1;
381-
}
382-
if (window_height % 2) {
383-
window_height += 1;
395+
if (!GetWindowSizeForSwapChain(window_width, window_height)) {
396+
RTC_LOG(LS_ERROR) << "Failed to get window size for creating swapchain.";
397+
return false;
384398
}
385-
swapChainDesc.Width = (rect_width % 2 != 0) ? (rect_width + 1) : rect_width;
386-
swapChainDesc.Height = (rect_height % 2 != 0) ? (rect_height + 1) : rect_height;
399+
swapChainDesc.Width = window_width;
400+
swapChainDesc.Height = window_height;
387401
swapChainDesc.Format = DXGI_FORMAT_NV12;
388402
swapChainDesc.Stereo = false;
389403
swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling.
@@ -427,10 +441,9 @@ bool WebrtcVideoRendererD3D11Impl::CreateVideoProcessor(int width,
427441
if (width < 0 || height < 0)
428442
return false;
429443

430-
RECT rect;
431-
GetClientRect(wnd_, &rect);
432-
int window_width = rect.right - rect.left;
433-
int window_height = rect.bottom - rect.top;
444+
if (!GetWindowSizeForSwapChain(window_width, window_height))
445+
return false;
446+
434447
D3D11_VIDEO_PROCESSOR_CONTENT_DESC content_desc;
435448
ZeroMemory(&content_desc, sizeof(content_desc));
436449

@@ -480,13 +493,11 @@ void WebrtcVideoRendererD3D11Impl::RenderD3D11Texture(int width, int height) {
480493
HRESULT hr = S_OK;
481494

482495
if (swap_chain_for_hwnd_ == nullptr) {
483-
RTC_LOG(LS_ERROR) << "Invalid swap chain.";
496+
RTC_LOG(LS_ERROR) << "Invalid swapchain.";
484497
return;
485498
}
486499

487500
Microsoft::WRL::ComPtr<ID3D11Texture2D> dxgi_back_buffer;
488-
//hr = swap_chain_for_hwnd_->GetBuffer(0, __uuidof(ID3D11Texture2D),
489-
// (void**)&dxgi_back_buffer);
490501
hr = swap_chain_for_hwnd_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer));
491502
if (FAILED(hr)) {
492503
std::string message = std::system_category().message(hr);

talk/owt/sdk/base/win/videorendererd3d11.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class WebrtcVideoRendererD3D11Impl
5151
bool InitD3D11(int width, int height);
5252
bool InitSwapChain(int widht, int height, bool reset);
5353
bool CreateStagingTexture(int width, int height);
54+
bool GetWindowSizeForSwapChain(int& width, int& height);
5455

5556
// Render window objects
5657
HWND wnd_ = nullptr;

0 commit comments

Comments
 (0)