webrtc_m130/modules/desktop_capture/mac/desktop_configuration_monitor.cc
Emircan Uysaler eaf337a141 Remove event wait logic from DesktopConfigurationMonitor
This class exposes Wait()-Set() logic to synchronize events.
- There is a bug in checking EventWrapper::Wait() as it returns [1,2]. Negating
these values cause us to always pass timeout checks.
- There is a general problem in this class with waiter. There are 2 scenarios:
1) Lock()-Unlock()-DisplaysReconfigured()
In this scenario, Wait() in DisplaysReconfigured() immediately passes as event
is already signaled. Next Lock() call won't continue until Set() is called in
DisplaysReconfigured(). This blocks capture thread from accessing display until
reconfiguration completes.
2) Lock()-DisplaysReconfigured()-Unlock()
In this scenario, Wait() in DisplaysReconfigured() passes when Unlock() called.
Capture thread accesses display while reconfiguration happens. Note that we are
only delaying the OS delegate thread here. As an experiment, adding Sleep() in
DisplaysReconfigured() results in no change, because it looks like OS uses this
thread for only delegates but not for the actual display switch.

Overall, (1) doesnt seem necessary as (2) already accesses display while
reconfiguration happens. (2) doesn't seem necessary as blocking system delegate
thread doesn't help. Therefore, I changed the class to only protect from race
condition on |desktop_configuration_|.

Bug: chromium:796889
Change-Id: I37263305e5ac629e21ff9e977952cf4a21bae19f
Reviewed-on: https://webrtc-review.googlesource.com/c/108560
Commit-Queue: Emircan Uysaler <emircan@webrtc.org>
Reviewed-by: Tommi <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25437}
2018-10-30 16:05:21 +00:00

75 lines
2.7 KiB
C++

/*
* Copyright (c) 2014 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 "modules/desktop_capture/mac/desktop_configuration_monitor.h"
#include "modules/desktop_capture/mac/desktop_configuration.h"
#include "rtc_base/logging.h"
#include "rtc_base/trace_event.h"
#include "system_wrappers/include/event_wrapper.h"
namespace webrtc {
DesktopConfigurationMonitor::DesktopConfigurationMonitor() {
CGError err = CGDisplayRegisterReconfigurationCallback(
DesktopConfigurationMonitor::DisplaysReconfiguredCallback, this);
if (err != kCGErrorSuccess)
RTC_LOG(LS_ERROR) << "CGDisplayRegisterReconfigurationCallback " << err;
rtc::CritScope cs(&desktop_configuration_lock_);
desktop_configuration_ = MacDesktopConfiguration::GetCurrent(
MacDesktopConfiguration::TopLeftOrigin);
}
DesktopConfigurationMonitor::~DesktopConfigurationMonitor() {
CGError err = CGDisplayRemoveReconfigurationCallback(
DesktopConfigurationMonitor::DisplaysReconfiguredCallback, this);
if (err != kCGErrorSuccess)
RTC_LOG(LS_ERROR) << "CGDisplayRemoveReconfigurationCallback " << err;
}
MacDesktopConfiguration DesktopConfigurationMonitor::desktop_configuration() {
rtc::CritScope crit(&desktop_configuration_lock_);
return desktop_configuration_;
}
// static
// This method may be called on any system thread.
void DesktopConfigurationMonitor::DisplaysReconfiguredCallback(
CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags,
void* user_parameter) {
DesktopConfigurationMonitor* monitor =
reinterpret_cast<DesktopConfigurationMonitor*>(user_parameter);
monitor->DisplaysReconfigured(display, flags);
}
void DesktopConfigurationMonitor::DisplaysReconfigured(
CGDirectDisplayID display,
CGDisplayChangeSummaryFlags flags) {
TRACE_EVENT0("webrtc", "DesktopConfigurationMonitor::DisplaysReconfigured");
RTC_LOG(LS_INFO) << "DisplaysReconfigured: "
<< "DisplayID " << display << "; ChangeSummaryFlags "
<< flags;
if (flags & kCGDisplayBeginConfigurationFlag) {
reconfiguring_displays_.insert(display);
return;
}
reconfiguring_displays_.erase(display);
if (reconfiguring_displays_.empty()) {
rtc::CritScope cs(&desktop_configuration_lock_);
desktop_configuration_ = MacDesktopConfiguration::GetCurrent(
MacDesktopConfiguration::TopLeftOrigin);
}
}
} // namespace webrtc