OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... | |
25 #include "webrtc/modules/desktop_capture/mac/window_list_utils.h" | 25 #include "webrtc/modules/desktop_capture/mac/window_list_utils.h" |
26 #include "webrtc/system_wrappers/include/logging.h" | 26 #include "webrtc/system_wrappers/include/logging.h" |
27 | 27 |
28 namespace webrtc { | 28 namespace webrtc { |
29 | 29 |
30 namespace { | 30 namespace { |
31 | 31 |
32 // Returns true if the window exists. | 32 // Returns true if the window exists. |
33 bool IsWindowValid(CGWindowID id) { | 33 bool IsWindowValid(CGWindowID id) { |
34 CFArrayRef window_id_array = | 34 CFArrayRef window_id_array = |
35 CFArrayCreate(NULL, reinterpret_cast<const void **>(&id), 1, NULL); | 35 CFArrayCreate(nullptr, reinterpret_cast<const void **>(&id), 1, nullptr); |
36 CFArrayRef window_array = | 36 CFArrayRef window_array = |
37 CGWindowListCreateDescriptionFromArray(window_id_array); | 37 CGWindowListCreateDescriptionFromArray(window_id_array); |
38 bool valid = window_array && CFArrayGetCount(window_array); | 38 bool valid = window_array && CFArrayGetCount(window_array); |
39 CFRelease(window_id_array); | 39 CFRelease(window_id_array); |
40 CFRelease(window_array); | 40 CFRelease(window_array); |
41 | 41 |
42 return valid; | 42 return valid; |
43 } | 43 } |
44 | 44 |
45 class WindowCapturerMac : public WindowCapturer { | 45 class WindowCapturerMac : public WindowCapturer { |
46 public: | 46 public: |
47 explicit WindowCapturerMac(rtc::scoped_refptr<FullScreenChromeWindowDetector> | 47 explicit WindowCapturerMac(rtc::scoped_refptr<FullScreenChromeWindowDetector> |
48 full_screen_chrome_window_detector); | 48 full_screen_chrome_window_detector); |
49 virtual ~WindowCapturerMac(); | 49 virtual ~WindowCapturerMac(); |
50 | 50 |
51 // WindowCapturer interface. | 51 // WindowCapturer interface. |
52 bool GetWindowList(WindowList* windows) override; | 52 bool GetWindowList(WindowList* windows) override; |
53 bool SelectWindow(WindowId id) override; | 53 bool SelectWindow(WindowId id) override; |
54 bool BringSelectedWindowToFront() override; | 54 bool BringSelectedWindowToFront() override; |
55 | 55 |
56 // DesktopCapturer interface. | 56 // DesktopCapturer interface. |
57 void Start(Callback* callback) override; | 57 void Start(Callback* callback) override; |
58 void Capture(const DesktopRegion& region) override; | 58 void Capture(const DesktopRegion& region) override; |
59 | 59 |
60 private: | 60 private: |
61 Callback* callback_; | 61 Callback* callback_ = nullptr; |
62 | 62 |
63 // The window being captured. | 63 // The window being captured. |
64 CGWindowID window_id_; | 64 CGWindowID window_id_ = 0; |
65 | 65 |
66 rtc::scoped_refptr<FullScreenChromeWindowDetector> | 66 rtc::scoped_refptr<FullScreenChromeWindowDetector> |
67 full_screen_chrome_window_detector_; | 67 full_screen_chrome_window_detector_; |
68 | 68 |
69 RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerMac); | 69 RTC_DISALLOW_COPY_AND_ASSIGN(WindowCapturerMac); |
70 }; | 70 }; |
71 | 71 |
72 WindowCapturerMac::WindowCapturerMac(rtc::scoped_refptr< | 72 WindowCapturerMac::WindowCapturerMac( |
73 FullScreenChromeWindowDetector> full_screen_chrome_window_detector) | 73 rtc::scoped_refptr<FullScreenChromeWindowDetector> |
74 : callback_(NULL), | 74 full_screen_chrome_window_detector) |
75 window_id_(0), | 75 : full_screen_chrome_window_detector_(full_screen_chrome_window_detector) {} |
76 full_screen_chrome_window_detector_(full_screen_chrome_window_detector) { | |
77 } | |
78 | 76 |
79 WindowCapturerMac::~WindowCapturerMac() { | 77 WindowCapturerMac::~WindowCapturerMac() {} |
80 } | |
81 | 78 |
82 bool WindowCapturerMac::GetWindowList(WindowList* windows) { | 79 bool WindowCapturerMac::GetWindowList(WindowList* windows) { |
83 // Only get on screen, non-desktop windows. | 80 // Only get on screen, non-desktop windows. |
84 CFArrayRef window_array = CGWindowListCopyWindowInfo( | 81 CFArrayRef window_array = CGWindowListCopyWindowInfo( |
85 kCGWindowListExcludeDesktopElements, | 82 kCGWindowListExcludeDesktopElements, |
86 kCGNullWindowID); | 83 kCGNullWindowID); |
87 if (!window_array) | 84 if (!window_array) |
88 return false; | 85 return false; |
89 MacDesktopConfiguration desktop_config = MacDesktopConfiguration::GetCurrent( | 86 MacDesktopConfiguration desktop_config = MacDesktopConfiguration::GetCurrent( |
90 MacDesktopConfiguration::TopLeftOrigin); | 87 MacDesktopConfiguration::TopLeftOrigin); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 return true; | 132 return true; |
136 } | 133 } |
137 | 134 |
138 bool WindowCapturerMac::BringSelectedWindowToFront() { | 135 bool WindowCapturerMac::BringSelectedWindowToFront() { |
139 if (!window_id_) | 136 if (!window_id_) |
140 return false; | 137 return false; |
141 | 138 |
142 CGWindowID ids[1]; | 139 CGWindowID ids[1]; |
143 ids[0] = window_id_; | 140 ids[0] = window_id_; |
144 CFArrayRef window_id_array = | 141 CFArrayRef window_id_array = |
145 CFArrayCreate(NULL, reinterpret_cast<const void **>(&ids), 1, NULL); | 142 CFArrayCreate(nullptr, reinterpret_cast<const void **>(&ids), 1, nullptr); |
146 | 143 |
147 CFArrayRef window_array = | 144 CFArrayRef window_array = |
148 CGWindowListCreateDescriptionFromArray(window_id_array); | 145 CGWindowListCreateDescriptionFromArray(window_id_array); |
149 if (window_array == NULL || 0 == CFArrayGetCount(window_array)) { | 146 if (!window_array || 0 == CFArrayGetCount(window_array)) { |
150 // Could not find the window. It might have been closed. | 147 // Could not find the window. It might have been closed. |
151 LOG(LS_INFO) << "Window not found"; | 148 LOG(LS_INFO) << "Window not found"; |
152 CFRelease(window_id_array); | 149 CFRelease(window_id_array); |
153 return false; | 150 return false; |
154 } | 151 } |
155 | 152 |
156 CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>( | 153 CFDictionaryRef window = reinterpret_cast<CFDictionaryRef>( |
157 CFArrayGetValueAtIndex(window_array, 0)); | 154 CFArrayGetValueAtIndex(window_array, 0)); |
158 CFNumberRef pid_ref = reinterpret_cast<CFNumberRef>( | 155 CFNumberRef pid_ref = reinterpret_cast<CFNumberRef>( |
159 CFDictionaryGetValue(window, kCGWindowOwnerPID)); | 156 CFDictionaryGetValue(window, kCGWindowOwnerPID)); |
(...skipping 14 matching lines...) Expand all Loading... | |
174 | 171 |
175 void WindowCapturerMac::Start(Callback* callback) { | 172 void WindowCapturerMac::Start(Callback* callback) { |
176 assert(!callback_); | 173 assert(!callback_); |
177 assert(callback); | 174 assert(callback); |
178 | 175 |
179 callback_ = callback; | 176 callback_ = callback; |
180 } | 177 } |
181 | 178 |
182 void WindowCapturerMac::Capture(const DesktopRegion& region) { | 179 void WindowCapturerMac::Capture(const DesktopRegion& region) { |
183 if (!IsWindowValid(window_id_)) { | 180 if (!IsWindowValid(window_id_)) { |
184 callback_->OnCaptureCompleted(NULL); | 181 callback_->OnCaptureCompleted(std::unique_ptr<DesktopFrame>()); |
Wez
2016/05/18 01:29:51
Why not using nullptr here?
Sergey Ulanov
2016/05/31 12:02:49
because OnCaptureCompleted(nullptr) would call wro
| |
185 return; | 182 return; |
186 } | 183 } |
187 | 184 |
188 CGWindowID on_screen_window = window_id_; | 185 CGWindowID on_screen_window = window_id_; |
189 if (full_screen_chrome_window_detector_) { | 186 if (full_screen_chrome_window_detector_) { |
190 CGWindowID full_screen_window = | 187 CGWindowID full_screen_window = |
191 full_screen_chrome_window_detector_->FindFullScreenWindow(window_id_); | 188 full_screen_chrome_window_detector_->FindFullScreenWindow(window_id_); |
192 | 189 |
193 if (full_screen_window != kCGNullWindowID) | 190 if (full_screen_window != kCGNullWindowID) |
194 on_screen_window = full_screen_window; | 191 on_screen_window = full_screen_window; |
195 } | 192 } |
196 | 193 |
197 CGImageRef window_image = CGWindowListCreateImage( | 194 CGImageRef window_image = CGWindowListCreateImage( |
198 CGRectNull, kCGWindowListOptionIncludingWindow, | 195 CGRectNull, kCGWindowListOptionIncludingWindow, |
199 on_screen_window, kCGWindowImageBoundsIgnoreFraming); | 196 on_screen_window, kCGWindowImageBoundsIgnoreFraming); |
200 | 197 |
201 if (!window_image) { | 198 if (!window_image) { |
202 callback_->OnCaptureCompleted(NULL); | 199 callback_->OnCaptureCompleted(std::unique_ptr<DesktopFrame>()); |
203 return; | 200 return; |
204 } | 201 } |
205 | 202 |
206 int bits_per_pixel = CGImageGetBitsPerPixel(window_image); | 203 int bits_per_pixel = CGImageGetBitsPerPixel(window_image); |
207 if (bits_per_pixel != 32) { | 204 if (bits_per_pixel != 32) { |
208 LOG(LS_ERROR) << "Unsupported window image depth: " << bits_per_pixel; | 205 LOG(LS_ERROR) << "Unsupported window image depth: " << bits_per_pixel; |
209 CFRelease(window_image); | 206 CFRelease(window_image); |
210 callback_->OnCaptureCompleted(NULL); | 207 callback_->OnCaptureCompleted(std::unique_ptr<DesktopFrame>()); |
211 return; | 208 return; |
212 } | 209 } |
213 | 210 |
214 int width = CGImageGetWidth(window_image); | 211 int width = CGImageGetWidth(window_image); |
215 int height = CGImageGetHeight(window_image); | 212 int height = CGImageGetHeight(window_image); |
216 CGDataProviderRef provider = CGImageGetDataProvider(window_image); | 213 CGDataProviderRef provider = CGImageGetDataProvider(window_image); |
217 CFDataRef cf_data = CGDataProviderCopyData(provider); | 214 CFDataRef cf_data = CGDataProviderCopyData(provider); |
218 DesktopFrame* frame = new BasicDesktopFrame( | 215 std::unique_ptr<DesktopFrame> frame( |
219 DesktopSize(width, height)); | 216 new BasicDesktopFrame(DesktopSize(width, height))); |
220 | 217 |
221 int src_stride = CGImageGetBytesPerRow(window_image); | 218 int src_stride = CGImageGetBytesPerRow(window_image); |
222 const uint8_t* src_data = CFDataGetBytePtr(cf_data); | 219 const uint8_t* src_data = CFDataGetBytePtr(cf_data); |
223 for (int y = 0; y < height; ++y) { | 220 for (int y = 0; y < height; ++y) { |
224 memcpy(frame->data() + frame->stride() * y, src_data + src_stride * y, | 221 memcpy(frame->data() + frame->stride() * y, src_data + src_stride * y, |
225 DesktopFrame::kBytesPerPixel * width); | 222 DesktopFrame::kBytesPerPixel * width); |
226 } | 223 } |
227 | 224 |
228 CFRelease(cf_data); | 225 CFRelease(cf_data); |
229 CFRelease(window_image); | 226 CFRelease(window_image); |
230 | 227 |
231 frame->mutable_updated_region()->SetRect( | 228 frame->mutable_updated_region()->SetRect( |
232 DesktopRect::MakeSize(frame->size())); | 229 DesktopRect::MakeSize(frame->size())); |
233 | 230 |
234 callback_->OnCaptureCompleted(frame); | 231 callback_->OnCaptureCompleted(std::move(frame)); |
235 | 232 |
236 if (full_screen_chrome_window_detector_) | 233 if (full_screen_chrome_window_detector_) |
237 full_screen_chrome_window_detector_->UpdateWindowListIfNeeded(window_id_); | 234 full_screen_chrome_window_detector_->UpdateWindowListIfNeeded(window_id_); |
238 } | 235 } |
239 | 236 |
240 } // namespace | 237 } // namespace |
241 | 238 |
242 // static | 239 // static |
243 WindowCapturer* WindowCapturer::Create(const DesktopCaptureOptions& options) { | 240 WindowCapturer* WindowCapturer::Create(const DesktopCaptureOptions& options) { |
244 return new WindowCapturerMac(options.full_screen_chrome_window_detector()); | 241 return new WindowCapturerMac(options.full_screen_chrome_window_detector()); |
245 } | 242 } |
246 | 243 |
247 } // namespace webrtc | 244 } // namespace webrtc |
OLD | NEW |