summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/desktop_capture/mac/desktop_frame_provider.mm
blob: 009504a22bd8e0a633808c8f97b771a8fb6f5c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 *  Copyright (c) 2018 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_frame_provider.h"

#include <utility>

#include "modules/desktop_capture/mac/desktop_frame_cgimage.h"
#include "modules/desktop_capture/mac/desktop_frame_iosurface.h"

namespace webrtc {

DesktopFrameProvider::DesktopFrameProvider(bool allow_iosurface)
    : allow_iosurface_(allow_iosurface) {
  thread_checker_.Detach();
}

DesktopFrameProvider::~DesktopFrameProvider() {
  RTC_DCHECK(thread_checker_.IsCurrent());

  Release();
}

std::unique_ptr<DesktopFrame> DesktopFrameProvider::TakeLatestFrameForDisplay(
    CGDirectDisplayID display_id) {
  RTC_DCHECK(thread_checker_.IsCurrent());

  if (!allow_iosurface_ || !io_surfaces_[display_id]) {
    // Regenerate a snapshot. If iosurface is on it will be empty until the
    // stream handler is called.
    return DesktopFrameCGImage::CreateForDisplay(display_id);
  }

  return io_surfaces_[display_id]->Share();
}

void DesktopFrameProvider::InvalidateIOSurface(CGDirectDisplayID display_id,
                                               rtc::ScopedCFTypeRef<IOSurfaceRef> io_surface) {
  RTC_DCHECK(thread_checker_.IsCurrent());

  if (!allow_iosurface_) {
    return;
  }

  std::unique_ptr<DesktopFrameIOSurface> desktop_frame_iosurface =
      DesktopFrameIOSurface::Wrap(io_surface);

  io_surfaces_[display_id] = desktop_frame_iosurface ?
      SharedDesktopFrame::Wrap(std::move(desktop_frame_iosurface)) :
      nullptr;
}

void DesktopFrameProvider::Release() {
  RTC_DCHECK(thread_checker_.IsCurrent());

  if (!allow_iosurface_) {
    return;
  }

  io_surfaces_.clear();
}

}  // namespace webrtc