Chromium Code Reviews| Index: webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
| diff --git a/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d89086ff3dcb440f8a4de96416bf483ae4071cc2 |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/win/screen_capturer_win_directx.cc |
| @@ -0,0 +1,435 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/desktop_capture/win/screen_capturer_win_directx.h" |
| + |
| +#include <string.h> |
| + |
| +#include <comdef.h> |
| +#include <wincodec.h> |
| +#include <DXGI.h> |
| + |
| +#include "webrtc/base/checks.h" |
| +#include "webrtc/base/criticalsection.h" |
| +#include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
| +#include "webrtc/system_wrappers/include/logging.h" |
| +#include "webrtc/system_wrappers/include/tick_util.h" |
| + |
| +namespace webrtc { |
| + |
| +using Microsoft::WRL::ComPtr; |
| +using rtc::CriticalSection; |
| +using rtc::CritScope; |
| + |
| +namespace { |
| + |
| +rtc::GlobalLock initialize_lock; |
|
Sergey Ulanov
2016/04/08 21:22:26
non-POD is not allowed as global.
What you can do
Hzj_jie
2016/04/11 22:19:16
Changed into rtc::GlobalLockPod, which is a POD sp
|
| +CriticalSection* duplication_lock; |
| +CriticalSection* acquire_lock; |
| + |
| +bool initialized GUARDED_BY(initialize_lock) = false; |
| +bool initialize_result GUARDED_BY(initialize_lock) = false; |
| +ID3D11Device* d3d11_device GUARDED_BY(initialize_lock) = nullptr; |
| +ID3D11DeviceContext* d3d11_context GUARDED_BY(initialize_lock) = nullptr; |
| +IDXGIOutput1* dxgi_output1 GUARDED_BY(initialize_lock) = nullptr; |
| +ComPtr<IDXGIOutputDuplication> dxgi_output_duplication |
| + GUARDED_BY(duplication_lock); |
| +DesktopSize desktop_size GUARDED_BY(acquire_lock); |
| +std::vector<BYTE> metadata GUARDED_BY(acquire_lock); |
| + |
| +} // namespace |
| + |
| +bool ScreenCapturerWinDirectX::Initialize() { |
| + if (!initialized) { |
| + rtc::GlobalLockScope lock(&initialize_lock); |
| + if (!initialized) { |
| + initialize_result = DoInitialize(); |
| + initialized = true; |
| + if (initialize_result) { |
| + return true; |
| + } |
| + |
| + // Clean up if DirectX cannot work on the system. |
| + if (dxgi_output_duplication) { |
| + dxgi_output_duplication.Reset(); |
| + } |
| + |
| + if (dxgi_output1 != nullptr) { |
| + dxgi_output1->Release(); |
| + dxgi_output1 = nullptr; |
| + } |
| + |
| + if (d3d11_context != nullptr) { |
| + d3d11_context->Release(); |
| + d3d11_context = nullptr; |
| + } |
| + |
| + if (d3d11_device != nullptr) { |
| + d3d11_device->Release(); |
| + d3d11_device = nullptr; |
| + } |
| + |
| + return false; |
| + } |
| + } |
| + |
| + return initialize_result; |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::DoInitialize() { |
| + D3D_FEATURE_LEVEL feature_level; |
| + _com_error err(D3D11CreateDevice( |
| + nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, |
| + D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED, |
| + nullptr, 0, D3D11_SDK_VERSION, &d3d11_device, &feature_level, |
| + &d3d11_context)); |
| + if (err.Error() != S_OK || d3d11_device == nullptr || |
| + d3d11_context == nullptr) { |
| + LOG(LS_WARNING) << "D3D11CreateDeivce returns error " << err.ErrorMessage() |
| + << " with code " << err.Error(); |
| + return false; |
| + } |
| + |
| + if (feature_level < D3D_FEATURE_LEVEL_11_0) { |
| + LOG(LS_WARNING) << "D3D11CreateDevice returns an instance without DirectX " |
| + "11 support, level " |
| + << feature_level; |
| + return false; |
| + } |
| + |
| + ComPtr<IDXGIDevice> device; |
| + err = _com_error(d3d11_device->QueryInterface( |
| + __uuidof(IDXGIDevice), reinterpret_cast<void**>(device.GetAddressOf()))); |
| + if (err.Error() != S_OK || !device) { |
| + LOG(LS_WARNING) << "ID3D11Device is not an implementation of IDXGIDevice, " |
| + "this usually means the system does not support DirectX " |
| + "11"; |
| + return false; |
| + } |
| + |
| + ComPtr<IDXGIAdapter> adapter; |
| + err = _com_error(device->GetAdapter(adapter.GetAddressOf())); |
| + if (err.Error() != S_OK || !adapter) { |
| + LOG(LS_WARNING) << "Failed to get an IDXGIAdapter implementation from " |
| + "IDXGIDevice."; |
| + return false; |
| + } |
| + |
| + ComPtr<IDXGIOutput> output; |
| + for (int i = 0;; i++) { |
| + err = _com_error(adapter->EnumOutputs(i, output.GetAddressOf())); |
| + if (err.Error() == DXGI_ERROR_NOT_FOUND) { |
| + LOG(LS_WARNING) << "No output detected."; |
| + return false; |
| + } |
| + if (err.Error() == S_OK && output) { |
| + DXGI_OUTPUT_DESC desc; |
| + err = _com_error(output->GetDesc(&desc)); |
| + if (err.Error() == S_OK) { |
| + if (desc.AttachedToDesktop) { |
| + // Current output instance is the device attached to desktop. |
| + break; |
| + } |
| + } else { |
| + LOG(LS_WARNING) << "Failed to get output description of device " << i |
| + << ", ignore."; |
| + } |
| + } |
| + } |
| + |
| + RTC_DCHECK(output); |
| + err = _com_error(output.CopyTo(__uuidof(IDXGIOutput1), |
| + reinterpret_cast<void**>(&dxgi_output1))); |
| + if (err.Error() != S_OK || dxgi_output1 == nullptr) { |
| + LOG(LS_WARNING) << "Failed to convert IDXGIOutput to IDXGIOutput1, this " |
| + "usually means the system does not support DirectX 11"; |
| + return false; |
| + } |
| + |
| + duplication_lock = new CriticalSection(); |
| + acquire_lock = new CriticalSection(); |
| + if (DuplicateOutput()) { |
| + DesktopFrameWinDxgi::Initialize(d3d11_device, d3d11_context); |
| + return true; |
| + } |
| + |
| + return false; |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::DuplicateOutput() { |
| + RTC_DCHECK(dxgi_output1 != nullptr); |
| + // We are updating the instance. |
| + CritScope lock(duplication_lock); |
| + // Make sure nobody is using current instance. |
| + CritScope lock2(acquire_lock); |
| + if (dxgi_output_duplication) { |
| + dxgi_output_duplication->ReleaseFrame(); |
| + dxgi_output_duplication.Reset(); |
| + } |
| + |
| + for (int i = 0; i < kDuplicateOutputAttempts; i++) { |
| + _com_error err( |
| + dxgi_output1->DuplicateOutput(static_cast<IUnknown*>(d3d11_device), |
| + dxgi_output_duplication.GetAddressOf())); |
| + if (err.Error() == S_OK && dxgi_output_duplication) { |
| + DXGI_OUTDUPL_DESC desc; |
| + dxgi_output_duplication->GetDesc(&desc); |
| + if (desc.ModeDesc.Format != DXGI_FORMAT_B8G8R8A8_UNORM) { |
| + LOG(LS_ERROR) << "IDXGIDuplicateOutput does not use RGBA (8 bit) " |
| + "format, which is required by downstream components, " |
| + "format is " |
| + << desc.ModeDesc.Format; |
| + return false; |
| + } |
| + |
| + desktop_size.set(desc.ModeDesc.Width, desc.ModeDesc.Height); |
| + return true; |
| + } else { |
| + // Make sure we have correct signal and duplicate the output next time. |
| + dxgi_output_duplication.Reset(); |
| + LOG(LS_WARNING) << "Failed to duplicate output from IDXGIOutput1, error " |
| + << err.ErrorMessage() << ", with code " << err.Error(); |
| + Sleep(kDuplicateOutputWait); |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| +ScreenCapturerWinDirectX::ScreenCapturerWinDirectX( |
| + const DesktopCaptureOptions& options) |
| + : callback_(nullptr), set_thread_execution_state_failed_(false) { |
| + RTC_DCHECK(initialized && initialize_result); |
| +} |
| + |
| +ScreenCapturerWinDirectX::~ScreenCapturerWinDirectX() {} |
| + |
| +void ScreenCapturerWinDirectX::Start(Callback* callback) { |
| + RTC_DCHECK(callback_ == nullptr); |
| + RTC_DCHECK(callback != nullptr); |
| + |
| + callback_ = callback; |
| +} |
| + |
| +void ScreenCapturerWinDirectX::SetSharedMemoryFactory( |
| + rtc::scoped_ptr<SharedMemoryFactory> shared_memory_factory) { |
| + shared_memory_factory_ = |
| + rtc::ScopedToUnique(std::move(shared_memory_factory)); |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::DetectUpdatedRegion( |
| + const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| + DesktopFrame* frame) { |
| + RTC_DCHECK(dxgi_output_duplication); |
| + RTC_DCHECK(frame != nullptr); |
| + DesktopRegion& updated_region = *frame->mutable_updated_region(); |
| + updated_region.Clear(); |
| + if (frame_info.TotalMetadataBufferSize == 0) { |
| + // This should not happen, since frame_info.AccumulatedFrames > 0. |
| + LOG(LS_ERROR) << "frame_info.AccumulatedFrames > 0, " |
| + "but TotalMetadataBufferSize == 0"; |
| + return false; |
| + } |
| + |
| + if (metadata.size() < frame_info.TotalMetadataBufferSize) { |
| + metadata.clear(); // Avoid data copy |
| + metadata.reserve(frame_info.TotalMetadataBufferSize); |
| + } |
| + |
| + UINT buff_size = 0; |
| + DXGI_OUTDUPL_MOVE_RECT* move_rects = nullptr; |
| + size_t move_rects_count = 0; |
| + RECT* dirty_rects = nullptr; |
| + size_t dirty_rects_count = 0; |
| + for (int i = 0; i < 2; i++) { |
| + _com_error err(S_OK); |
| + if (i == 0) { |
| + move_rects = reinterpret_cast<DXGI_OUTDUPL_MOVE_RECT*>(metadata.data()); |
| + err = _com_error(dxgi_output_duplication->GetFrameMoveRects( |
| + static_cast<UINT>(metadata.capacity()), move_rects, &buff_size)); |
| + } else { |
| + dirty_rects = reinterpret_cast<RECT*>(metadata.data() + buff_size); |
| + err = _com_error(dxgi_output_duplication->GetFrameDirtyRects( |
| + static_cast<UINT>(metadata.capacity()) - buff_size, dirty_rects, |
| + &buff_size)); |
| + } |
| + if (err.Error() != S_OK) { |
| + if (err.Error() == DXGI_ERROR_ACCESS_LOST) { |
| + DuplicateOutput(); |
| + } else { |
| + LOG(LS_ERROR) << "Failed to get " << (i == 0 ? "move" : "dirty") |
| + << " rectangles, error " << err.ErrorMessage() |
| + << ", code " << err.Error(); |
| + } |
| + // Send entire desktop as we cannot get dirty or move rectangles. |
| + return false; |
| + } |
| + if (i == 0) { |
| + move_rects_count = buff_size / sizeof(DXGI_OUTDUPL_MOVE_RECT); |
| + } else { |
| + dirty_rects_count = buff_size / sizeof(RECT); |
| + } |
| + } |
| + |
| + LOG(LS_INFO) << "Found " << move_rects_count << " moved rectangles and " |
| + << dirty_rects_count << " dirty rectangles"; |
| + while (move_rects_count > 0) { |
| + updated_region.AddRect(DesktopRect::MakeXYWH( |
| + move_rects->SourcePoint.x, move_rects->SourcePoint.y, |
| + move_rects->DestinationRect.right - move_rects->DestinationRect.left, |
| + move_rects->DestinationRect.bottom - move_rects->DestinationRect.top)); |
| + updated_region.AddRect(DesktopRect::MakeLTRB( |
| + move_rects->DestinationRect.left, move_rects->DestinationRect.top, |
| + move_rects->DestinationRect.right, move_rects->DestinationRect.bottom)); |
| + move_rects++; |
| + move_rects_count--; |
| + } |
| + |
| + while (dirty_rects_count > 0) { |
| + updated_region.AddRect( |
| + DesktopRect::MakeLTRB(dirty_rects->left, dirty_rects->top, |
| + dirty_rects->right, dirty_rects->bottom)); |
| + dirty_rects++; |
| + dirty_rects_count--; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::ProcessFrame( |
| + const DXGI_OUTDUPL_FRAME_INFO& frame_info, |
| + IDXGIResource* resource) { |
| + RTC_DCHECK(resource != nullptr); |
| + RTC_DCHECK(frame_info.AccumulatedFrames > 0); |
| + // We have something to update, so move to next frame. |
| + frames_.MoveToNextFrame(); |
| + DesktopFrameWinDxgi* frame = ReplaceCurrentFrameIfEmpty(); |
| + |
| + if (!frame->Capture(frame_info, resource)) { |
| + return false; |
| + } |
| + |
| + if (!DetectUpdatedRegion(frame_info, frame)) { |
| + frame->mutable_updated_region()->Clear(); |
| + frame->mutable_updated_region()->AddRect( |
| + DesktopRect::MakeSize(desktop_size)); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void ScreenCapturerWinDirectX::Capture(const DesktopRegion& region) { |
| + RTC_DCHECK(callback_ != nullptr); |
| + |
| + if (!dxgi_output_duplication && !DuplicateOutput()) { |
| + // Receive a capture request when application is shutting down, or between |
| + // mode change. |
| + callback_->OnCaptureCompleted(nullptr); |
| + return; |
| + } |
| + |
| + TickTime capture_start_time = TickTime::Now(); |
| + |
| + // Make sure even nothing captured, we still have a valid frame to emit. |
| + ReplaceCurrentFrameIfEmpty(); |
| + // Before we move to next frame, current frame cannot be changed, it may be |
| + // being used by encoder. |
| + // TODO(zijiehe): Use differ to detect changed region. |
| + |
| + if (!SetThreadExecutionState(ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED)) { |
| + if (!set_thread_execution_state_failed_) { |
| + set_thread_execution_state_failed_ = true; |
| + LOG(LS_WARNING) << "Failed to make system & display power assertion: " |
| + << GetLastError(); |
| + } |
| + } |
| + |
| + DXGI_OUTDUPL_FRAME_INFO frame_info{}; |
| + ComPtr<IDXGIResource> resource; |
| + CritScope lock(acquire_lock); |
| + _com_error err(dxgi_output_duplication->AcquireNextFrame( |
| + kAcquireTimeout, &frame_info, resource.GetAddressOf())); |
| + if (err.Error() == DXGI_ERROR_ACCESS_LOST) { |
| + LOG(LS_ERROR) << "Access lost " << err.ErrorMessage(); |
| + if (DuplicateOutput()) { |
| + EmitCurrentFrame(); |
| + } else { |
| + callback_->OnCaptureCompleted(nullptr); |
| + } |
| + return; |
| + } |
| + |
| + if (err.Error() == DXGI_ERROR_WAIT_TIMEOUT) { |
| + LOG(LS_INFO) << "Acquire timeout " << err.ErrorMessage(); |
| + // Nothing changed. |
| + EmitCurrentFrame(); |
| + return; |
| + } |
| + |
| + if (err.Error() != S_OK) { |
| + LOG(LS_ERROR) << "Failed to capture frame, error " << err.ErrorMessage() |
| + << ", code " << err.Error(); |
| + callback_->OnCaptureCompleted(nullptr); |
| + return; |
| + } |
| + |
| + if (frame_info.AccumulatedFrames == 0) { |
| + LOG(LS_INFO) << "Nothing captured"; |
| + EmitCurrentFrame(); |
| + dxgi_output_duplication->ReleaseFrame(); |
| + return; |
| + } |
| + |
| + LOG(LS_INFO) << "Captured " << frame_info.AccumulatedFrames << " frames"; |
| + // Everything looks good so far, build next frame. |
| + bool result = ProcessFrame(frame_info, resource.Get()); |
| + // DetectUpdatedRegion may release last dxgi_output_duplication. But |
| + // DuplicateOutput function will always release last frame, so there is no |
| + // potential leak. |
| + if (dxgi_output_duplication) { |
| + dxgi_output_duplication->ReleaseFrame(); |
| + } |
| + if (result) { |
| + frames_.current_frame()->set_capture_time_ms( |
| + (TickTime::Now() - capture_start_time).Milliseconds()); |
| + EmitCurrentFrame(); |
| + } else { |
| + callback_->OnCaptureCompleted(nullptr); |
| + } |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::GetScreenList(ScreenList* screens) { |
| + RTC_DCHECK(screens != nullptr); |
| + RTC_DCHECK(screens->size() == 0); |
| + screens->push_back(Screen{}); |
| + return true; |
| +} |
| + |
| +bool ScreenCapturerWinDirectX::SelectScreen(ScreenId id) { |
| + return id == 0 || id == kFullDesktopScreenId; |
| +} |
| + |
| +DesktopFrameWinDxgi* ScreenCapturerWinDirectX::ReplaceCurrentFrameIfEmpty() { |
| + if (frames_.current_frame() == nullptr) { |
| + std::unique_ptr<DesktopFrameWinDxgi> frame(DesktopFrameWinDxgi::Create( |
| + desktop_size, shared_memory_factory_.get())); |
| + frames_.ReplaceCurrentFrame(frame.get()); |
| + return frame.release(); |
| + } |
| + |
| + return reinterpret_cast<DesktopFrameWinDxgi*>( |
| + frames_.current_frame()->GetUnderlyingFrame()); |
| +} |
| + |
| +void ScreenCapturerWinDirectX::EmitCurrentFrame() { |
| + callback_->OnCaptureCompleted(frames_.current_frame()->Share()); |
| +} |
| + |
| +} // namespace webrtc |