Index: webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc |
diff --git a/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc b/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc |
index c17bd1481c7fffa9e09939e3fbcb75f70deaee87..a0bc95fff10e766cffa0174e75a550a2a0d02660 100644 |
--- a/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc |
+++ b/webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc |
@@ -16,6 +16,7 @@ |
#include <string> |
#include "webrtc/base/checks.h" |
+#include "webrtc/base/logging.h" |
#include "webrtc/base/timeutils.h" |
#include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
#include "webrtc/modules/desktop_capture/win/dxgi_frame.h" |
@@ -25,18 +26,30 @@ |
namespace webrtc { |
// static |
-DxgiDuplicatorController* DxgiDuplicatorController::Instance() { |
+rtc::scoped_refptr<DxgiDuplicatorController> |
+DxgiDuplicatorController::Instance() { |
// The static instance won't be deleted to ensure it can be used by other |
// threads even during program exiting. |
static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); |
- return instance; |
+ return rtc::scoped_refptr<DxgiDuplicatorController>(instance); |
} |
-DxgiDuplicatorController::DxgiDuplicatorController() = default; |
+DxgiDuplicatorController::DxgiDuplicatorController() |
+ : refcount_(0) {} |
-DxgiDuplicatorController::~DxgiDuplicatorController() { |
- rtc::CritScope lock(&lock_); |
- Deinitialize(); |
+void DxgiDuplicatorController::AddRef() { |
+ int refcount = (++refcount_); |
+ RTC_DCHECK(refcount > 0); |
+} |
+ |
+void DxgiDuplicatorController::Release() { |
+ int refcount = (--refcount_); |
+ RTC_DCHECK(refcount >= 0); |
+ if (refcount == 0) { |
+ LOG(LS_WARNING) << "Count of references reaches zero, " |
+ "DxgiDuplicatorController will be unloaded."; |
+ Unload(); |
+ } |
} |
bool DxgiDuplicatorController::IsSupported() { |
@@ -129,6 +142,11 @@ DxgiDuplicatorController::DoDuplicate(DxgiFrame* frame, int monitor_id) { |
return Result::DUPLICATION_FAILED; |
} |
+void DxgiDuplicatorController::Unload() { |
+ rtc::CritScope lock(&lock_); |
+ Deinitialize(); |
+} |
+ |
void DxgiDuplicatorController::Unregister(const Context* const context) { |
rtc::CritScope lock(&lock_); |
if (ContextExpired(context)) { |