Implement DesktopCapturerWrapper and CaptureResultDesktopCapturerWrapper

Wrapper pattern is widely used in DesktopCapturer implementations. So this
change adds DesktopCapturerWrapper and CaptureResultDesktopCapturerWrapper as
the base classes of other wrappers. Implementing a new wrapper should become
easy, the implementation does not need to care about the uninteresting
overrides.

Bug: chromium:764258
Change-Id: If91c1b5e778805906f7f77854ea5600aa61bf64a
Reviewed-on: https://webrtc-review.googlesource.com/1420
Commit-Queue: Zijie He <zijiehe@google.com>
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Cr-Commit-Position: refs/heads/master@{#19868}
diff --git a/modules/desktop_capture/BUILD.gn b/modules/desktop_capture/BUILD.gn
index 75311bd..d08e69d 100644
--- a/modules/desktop_capture/BUILD.gn
+++ b/modules/desktop_capture/BUILD.gn
@@ -207,6 +207,8 @@
   sources = [
     "blank_detector_desktop_capturer_wrapper.cc",
     "blank_detector_desktop_capturer_wrapper.h",
+    "capture_result_desktop_capturer_wrapper.cc",
+    "capture_result_desktop_capturer_wrapper.h",
     "cropped_desktop_frame.cc",
     "cropped_desktop_frame.h",
     "cropping_window_capturer.cc",
@@ -220,6 +222,8 @@
     "desktop_capturer.h",
     "desktop_capturer_differ_wrapper.cc",
     "desktop_capturer_differ_wrapper.h",
+    "desktop_capturer_wrapper.cc",
+    "desktop_capturer_wrapper.h",
     "desktop_frame_rotation.cc",
     "desktop_frame_rotation.h",
     "desktop_frame_win.cc",
diff --git a/modules/desktop_capture/capture_result_desktop_capturer_wrapper.cc b/modules/desktop_capture/capture_result_desktop_capturer_wrapper.cc
new file mode 100644
index 0000000..b15ce2b
--- /dev/null
+++ b/modules/desktop_capture/capture_result_desktop_capturer_wrapper.cc
@@ -0,0 +1,49 @@
+/*
+ *  Copyright (c) 2017 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/capture_result_desktop_capturer_wrapper.h"
+
+#include <memory>
+#include <utility>
+
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+CaptureResultDesktopCapturerWrapper::CaptureResultDesktopCapturerWrapper(
+    std::unique_ptr<DesktopCapturer> base_capturer,
+    ResultObserver* observer)
+    : DesktopCapturerWrapper(std::move(base_capturer)),
+      observer_(observer) {
+  RTC_DCHECK(observer_);
+}
+
+CaptureResultDesktopCapturerWrapper::
+~CaptureResultDesktopCapturerWrapper() = default;
+
+void CaptureResultDesktopCapturerWrapper::Start(Callback* callback) {
+  if ((callback_ == nullptr) != (callback == nullptr)) {
+    if (callback) {
+      base_capturer_->Start(this);
+    } else {
+      base_capturer_->Start(nullptr);
+    }
+  }
+  callback_ = callback;
+}
+
+void CaptureResultDesktopCapturerWrapper::OnCaptureResult(
+    Result result,
+    std::unique_ptr<DesktopFrame> frame) {
+  observer_->Observe(&result, &frame);
+  callback_->OnCaptureResult(result, std::move(frame));
+}
+
+}  // namespace webrtc
diff --git a/modules/desktop_capture/capture_result_desktop_capturer_wrapper.h b/modules/desktop_capture/capture_result_desktop_capturer_wrapper.h
new file mode 100644
index 0000000..3f97bc5
--- /dev/null
+++ b/modules/desktop_capture/capture_result_desktop_capturer_wrapper.h
@@ -0,0 +1,64 @@
+/*
+ *  Copyright (c) 2017 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.
+ */
+
+#ifndef MODULES_DESKTOP_CAPTURE_CAPTURE_RESULT_DESKTOP_CAPTURER_WRAPPER_H_
+#define MODULES_DESKTOP_CAPTURE_CAPTURE_RESULT_DESKTOP_CAPTURER_WRAPPER_H_
+
+#include <memory>
+
+#include "modules/desktop_capture/desktop_capturer.h"
+#include "modules/desktop_capture/desktop_capturer_wrapper.h"
+
+namespace webrtc {
+
+// A DesktopCapturerWrapper implementation to capture the result of
+// |base_capturer|. Derived classes are expected to provide a ResultObserver
+// implementation to observe the DesktopFrame returned by |base_capturer_|.
+class CaptureResultDesktopCapturerWrapper
+    : public DesktopCapturerWrapper,
+      public DesktopCapturer::Callback {
+ public:
+  using Callback = DesktopCapturer::Callback;
+
+  // Provides a way to let derived classes or clients to modify the result
+  // returned by |base_capturer_|.
+  class ResultObserver {
+   public:
+    ResultObserver();
+    virtual ~ResultObserver();
+
+    virtual void Observe(Result* result,
+                         std::unique_ptr<DesktopFrame>* frame) = 0;
+  };
+
+  // |observer| must outlive this instance and can be |this|. |observer| is
+  // guaranteed to be executed only after the constructor and before the
+  // destructor.
+  CaptureResultDesktopCapturerWrapper(
+      std::unique_ptr<DesktopCapturer> base_capturer,
+      ResultObserver* observer);
+
+  ~CaptureResultDesktopCapturerWrapper() override;
+
+  // DesktopCapturer implementations.
+  void Start(Callback* callback) final;
+
+ private:
+  // DesktopCapturer::Callback implementation.
+  void OnCaptureResult(Result result,
+                       std::unique_ptr<DesktopFrame> frame) final;
+
+  ResultObserver* const observer_;
+  Callback* callback_ = nullptr;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_DESKTOP_CAPTURE_CAPTURE_RESULT_DESKTOP_CAPTURER_WRAPPER_H_
diff --git a/modules/desktop_capture/desktop_capturer_wrapper.cc b/modules/desktop_capture/desktop_capturer_wrapper.cc
new file mode 100644
index 0000000..4bbdd6c
--- /dev/null
+++ b/modules/desktop_capture/desktop_capturer_wrapper.cc
@@ -0,0 +1,60 @@
+/*
+ *  Copyright (c) 2017 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/desktop_capturer_wrapper.h"
+
+#include <utility>
+
+#include "rtc_base/checks.h"
+
+namespace webrtc {
+
+DesktopCapturerWrapper::DesktopCapturerWrapper(
+    std::unique_ptr<DesktopCapturer> base_capturer)
+    : base_capturer_(std::move(base_capturer)) {
+  RTC_DCHECK(base_capturer_);
+}
+
+DesktopCapturerWrapper::~DesktopCapturerWrapper() = default;
+
+void DesktopCapturerWrapper::Start(Callback* callback) {
+  base_capturer_->Start(callback);
+}
+
+void DesktopCapturerWrapper::SetSharedMemoryFactory(
+    std::unique_ptr<SharedMemoryFactory> shared_memory_factory) {
+  base_capturer_->SetSharedMemoryFactory(std::move(shared_memory_factory));
+}
+
+void DesktopCapturerWrapper::CaptureFrame() {
+  base_capturer_->CaptureFrame();
+}
+
+void DesktopCapturerWrapper::SetExcludedWindow(WindowId window) {
+  base_capturer_->SetExcludedWindow(window);
+}
+
+bool DesktopCapturerWrapper::GetSourceList(SourceList* sources) {
+  return base_capturer_->GetSourceList(sources);
+}
+
+bool DesktopCapturerWrapper::SelectSource(SourceId id) {
+  return base_capturer_->SelectSource(id);
+}
+
+bool DesktopCapturerWrapper::FocusOnSelectedSource() {
+  return base_capturer_->FocusOnSelectedSource();
+}
+
+bool DesktopCapturerWrapper::IsOccluded(const DesktopVector& pos) {
+  return base_capturer_->IsOccluded(pos);
+}
+
+}  // namespace webrtc
diff --git a/modules/desktop_capture/desktop_capturer_wrapper.h b/modules/desktop_capture/desktop_capturer_wrapper.h
new file mode 100644
index 0000000..a4b7b86
--- /dev/null
+++ b/modules/desktop_capture/desktop_capturer_wrapper.h
@@ -0,0 +1,45 @@
+/*
+ *  Copyright (c) 2017 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.
+ */
+
+#ifndef MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_WRAPPER_H_
+#define MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_WRAPPER_H_
+
+#include <memory>
+
+#include "modules/desktop_capture/desktop_capturer.h"
+
+namespace webrtc {
+
+// Wraps a DesktopCapturer and forwards all the function calls to it.
+class DesktopCapturerWrapper : public DesktopCapturer {
+ public:
+  explicit DesktopCapturerWrapper(
+      std::unique_ptr<DesktopCapturer> base_capturer);
+  ~DesktopCapturerWrapper() override;
+
+  // DesktopCapturer implementations.
+  void Start(Callback* callback) override;
+  void SetSharedMemoryFactory(
+      std::unique_ptr<SharedMemoryFactory> shared_memory_factory) override;
+  void CaptureFrame() override;
+  void SetExcludedWindow(WindowId window) override;
+  bool GetSourceList(SourceList* sources) override;
+  bool SelectSource(SourceId id) override;
+  bool FocusOnSelectedSource() override;
+  bool IsOccluded(const DesktopVector& pos) override;
+
+ protected:
+  // Guaranteed to be valid.
+  const std::unique_ptr<DesktopCapturer> base_capturer_;
+};
+
+}  // namespace webrtc
+
+#endif  // MODULES_DESKTOP_CAPTURE_DESKTOP_CAPTURER_WRAPPER_H_