Skip to content

Commit be5a771

Browse files
committed
fix: Get thumbnails asynchronously. (#37)
* fix: Get thumbnails asynchronously. * Fixed the problem that only the mouse movement was captured and the background was not updated when capturing full screen on mac.
1 parent f447f68 commit be5a771

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

modules/desktop_capture/mac/screen_capturer_mac.mm

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,7 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, float dip_to_pixel_s
297297
ScreenConfigurationChanged();
298298
}
299299

300-
// When screen is zoomed in/out, OSX only updates the part of Rects currently
301-
// displayed on screen, with relative location to current top-left on screen.
302-
// This will cause problems when we copy the dirty regions to the captured
303-
// image. So we invalidate the whole screen to copy all the screen contents.
304-
// With CGI method, the zooming will be ignored and the whole screen contents
305-
// will be captured as before.
306-
// With IOSurface method, the zoomed screen contents will be captured.
307-
if (UAZoomEnabled()) {
308-
helper_.InvalidateScreen(screen_pixel_bounds_.size());
309-
}
300+
helper_.InvalidateScreen(screen_pixel_bounds_.size());
310301

311302
DesktopRegion region;
312303
helper_.TakeInvalidRegion(&region);

sdk/objc/native/src/objc_desktop_capture.mm

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@
3030
webrtc::DesktopCapturer::SourceId source_id,
3131
id<RTC_OBJC_TYPE(DesktopCapturerDelegate)> delegate)
3232
: thread_(rtc::Thread::Create()), source_id_(source_id), delegate_(delegate) {
33+
RTC_DCHECK(thread_);
34+
type_ = type;
35+
thread_->Start();
3336
options_ = webrtc::DesktopCaptureOptions::CreateDefault();
3437
options_.set_detect_updated_region(true);
3538
options_.set_allow_iosurface(true);
36-
if (type == kScreen) {
37-
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateScreenCapturer(options_), options_);
38-
}
39-
else { capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateWindowCapturer(options_), options_); }
40-
type_ = type;
41-
thread_->Start();
39+
thread_->BlockingCall([this, type] {
40+
if (type == kScreen) {
41+
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateScreenCapturer(options_), options_);
42+
} else {
43+
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateWindowCapturer(options_), options_);
44+
}
45+
});
4246
}
4347

4448
ObjCDesktopCapturer::~ObjCDesktopCapturer() {
45-
thread_->Stop();
49+
thread_->BlockingCall([this] {
50+
capturer_.reset();
51+
});
4652
}
4753

4854
ObjCDesktopCapturer::CaptureState ObjCDesktopCapturer::Start(uint32_t fps) {
@@ -74,7 +80,9 @@
7480
}
7581
}
7682

77-
capturer_->Start(this);
83+
thread_->BlockingCall([this] {
84+
capturer_->Start(this);
85+
});
7886
capture_state_ = CS_RUNNING;
7987

8088
thread_->PostTask([this] {

sdk/objc/native/src/objc_desktop_media_list.mm

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,29 @@
3636
ObjCDesktopMediaList::ObjCDesktopMediaList(DesktopType type,
3737
RTC_OBJC_TYPE(RTCDesktopMediaList)* objcMediaList)
3838
:thread_(rtc::Thread::Create()),objcMediaList_(objcMediaList),type_(type) {
39+
RTC_DCHECK(thread_);
40+
thread_->Start();
3941
options_ = webrtc::DesktopCaptureOptions::CreateDefault();
4042
options_.set_detect_updated_region(true);
4143
options_.set_allow_iosurface(true);
42-
if (type == kScreen) {
43-
capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options_);
44-
} else {
45-
capturer_ = webrtc::DesktopCapturer::CreateWindowCapturer(options_);
46-
}
44+
4745
callback_ = std::make_unique<CallbackProxy>();
48-
thread_->Start();
49-
capturer_->Start(callback_.get());
46+
47+
thread_->BlockingCall([this, type] {
48+
if (type == kScreen) {
49+
capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options_);
50+
} else {
51+
capturer_ = webrtc::DesktopCapturer::CreateWindowCapturer(options_);
52+
}
53+
capturer_->Start(callback_.get());
54+
});
55+
5056
}
5157

5258
ObjCDesktopMediaList::~ObjCDesktopMediaList() {
53-
thread_->Stop();
59+
thread_->BlockingCall([this] {
60+
capturer_.reset();
61+
});
5462
}
5563

5664
int32_t ObjCDesktopMediaList::UpdateSourceList(bool force_reload, bool get_thumbnail) {
@@ -63,7 +71,10 @@
6371
}
6472

6573
webrtc::DesktopCapturer::SourceList new_sources;
66-
capturer_->GetSourceList(&new_sources);
74+
75+
thread_->BlockingCall([this,&new_sources] {
76+
capturer_->GetSourceList(&new_sources);
77+
});
6778

6879
typedef std::set<DesktopCapturer::SourceId> SourceSet;
6980
SourceSet new_source_set;
@@ -91,8 +102,8 @@
91102
if (old_source_set.find(new_sources[i].id) == old_source_set.end()) {
92103
MediaSource* source = new MediaSource(this, new_sources[i],type_);
93104
sources_.insert(sources_.begin() + i, std::shared_ptr<MediaSource>(source));
94-
GetThumbnail(source, false);
95105
[objcMediaList_ mediaSourceAdded:source];
106+
GetThumbnail(source, true);
96107
}
97108
}
98109
}
@@ -135,19 +146,21 @@
135146
}
136147

137148
bool ObjCDesktopMediaList::GetThumbnail(MediaSource *source, bool notify) {
138-
callback_->SetCallback([&](webrtc::DesktopCapturer::Result result,
149+
thread_->PostTask([this, source, notify] {
150+
if(capturer_->SelectSource(source->id())){
151+
callback_->SetCallback([&](webrtc::DesktopCapturer::Result result,
139152
std::unique_ptr<webrtc::DesktopFrame> frame) {
140-
auto old_thumbnail = source->thumbnail();
141-
source->SaveCaptureResult(result, std::move(frame));
142-
if(old_thumbnail.size() != source->thumbnail().size() && notify) {
143-
[objcMediaList_ mediaSourceThumbnailChanged:source];
144-
}
153+
auto old_thumbnail = source->thumbnail();
154+
source->SaveCaptureResult(result, std::move(frame));
155+
if(old_thumbnail.size() != source->thumbnail().size() && notify) {
156+
[objcMediaList_ mediaSourceThumbnailChanged:source];
157+
}
158+
});
159+
capturer_->CaptureFrame();
160+
}
145161
});
146-
if(capturer_->SelectSource(source->id())){
147-
capturer_->CaptureFrame();
148-
return true;
149-
}
150-
return false;
162+
163+
return true;
151164
}
152165

153166
int ObjCDesktopMediaList::GetSourceCount() const {

0 commit comments

Comments
 (0)