Skip to content

Commit

Permalink
fix: Get thumbnails asynchronously. (#37)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
cloudwebrtc committed Jun 6, 2023
1 parent f447f68 commit be5a771
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 39 deletions.
11 changes: 1 addition & 10 deletions modules/desktop_capture/mac/screen_capturer_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -297,16 +297,7 @@ DesktopRect GetExcludedWindowPixelBounds(CGWindowID window, float dip_to_pixel_s
ScreenConfigurationChanged();
}

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

DesktopRegion region;
helper_.TakeInvalidRegion(&region);
Expand Down
24 changes: 16 additions & 8 deletions sdk/objc/native/src/objc_desktop_capture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,25 @@
webrtc::DesktopCapturer::SourceId source_id,
id<RTC_OBJC_TYPE(DesktopCapturerDelegate)> delegate)
: thread_(rtc::Thread::Create()), source_id_(source_id), delegate_(delegate) {
RTC_DCHECK(thread_);
type_ = type;
thread_->Start();
options_ = webrtc::DesktopCaptureOptions::CreateDefault();
options_.set_detect_updated_region(true);
options_.set_allow_iosurface(true);
if (type == kScreen) {
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateScreenCapturer(options_), options_);
}
else { capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateWindowCapturer(options_), options_); }
type_ = type;
thread_->Start();
thread_->BlockingCall([this, type] {
if (type == kScreen) {
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateScreenCapturer(options_), options_);
} else {
capturer_ = std::make_unique<DesktopAndCursorComposer>(webrtc::DesktopCapturer::CreateWindowCapturer(options_), options_);
}
});
}

ObjCDesktopCapturer::~ObjCDesktopCapturer() {
thread_->Stop();
thread_->BlockingCall([this] {
capturer_.reset();
});
}

ObjCDesktopCapturer::CaptureState ObjCDesktopCapturer::Start(uint32_t fps) {
Expand Down Expand Up @@ -74,7 +80,9 @@
}
}

capturer_->Start(this);
thread_->BlockingCall([this] {
capturer_->Start(this);
});
capture_state_ = CS_RUNNING;

thread_->PostTask([this] {
Expand Down
55 changes: 34 additions & 21 deletions sdk/objc/native/src/objc_desktop_media_list.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,29 @@
ObjCDesktopMediaList::ObjCDesktopMediaList(DesktopType type,
RTC_OBJC_TYPE(RTCDesktopMediaList)* objcMediaList)
:thread_(rtc::Thread::Create()),objcMediaList_(objcMediaList),type_(type) {
RTC_DCHECK(thread_);
thread_->Start();
options_ = webrtc::DesktopCaptureOptions::CreateDefault();
options_.set_detect_updated_region(true);
options_.set_allow_iosurface(true);
if (type == kScreen) {
capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options_);
} else {
capturer_ = webrtc::DesktopCapturer::CreateWindowCapturer(options_);
}

callback_ = std::make_unique<CallbackProxy>();
thread_->Start();
capturer_->Start(callback_.get());

thread_->BlockingCall([this, type] {
if (type == kScreen) {
capturer_ = webrtc::DesktopCapturer::CreateScreenCapturer(options_);
} else {
capturer_ = webrtc::DesktopCapturer::CreateWindowCapturer(options_);
}
capturer_->Start(callback_.get());
});

}

ObjCDesktopMediaList::~ObjCDesktopMediaList() {
thread_->Stop();
thread_->BlockingCall([this] {
capturer_.reset();
});
}

int32_t ObjCDesktopMediaList::UpdateSourceList(bool force_reload, bool get_thumbnail) {
Expand All @@ -63,7 +71,10 @@
}

webrtc::DesktopCapturer::SourceList new_sources;
capturer_->GetSourceList(&new_sources);

thread_->BlockingCall([this,&new_sources] {
capturer_->GetSourceList(&new_sources);
});

typedef std::set<DesktopCapturer::SourceId> SourceSet;
SourceSet new_source_set;
Expand Down Expand Up @@ -91,8 +102,8 @@
if (old_source_set.find(new_sources[i].id) == old_source_set.end()) {
MediaSource* source = new MediaSource(this, new_sources[i],type_);
sources_.insert(sources_.begin() + i, std::shared_ptr<MediaSource>(source));
GetThumbnail(source, false);
[objcMediaList_ mediaSourceAdded:source];
GetThumbnail(source, true);
}
}
}
Expand Down Expand Up @@ -135,19 +146,21 @@
}

bool ObjCDesktopMediaList::GetThumbnail(MediaSource *source, bool notify) {
callback_->SetCallback([&](webrtc::DesktopCapturer::Result result,
thread_->PostTask([this, source, notify] {
if(capturer_->SelectSource(source->id())){
callback_->SetCallback([&](webrtc::DesktopCapturer::Result result,
std::unique_ptr<webrtc::DesktopFrame> frame) {
auto old_thumbnail = source->thumbnail();
source->SaveCaptureResult(result, std::move(frame));
if(old_thumbnail.size() != source->thumbnail().size() && notify) {
[objcMediaList_ mediaSourceThumbnailChanged:source];
}
auto old_thumbnail = source->thumbnail();
source->SaveCaptureResult(result, std::move(frame));
if(old_thumbnail.size() != source->thumbnail().size() && notify) {
[objcMediaList_ mediaSourceThumbnailChanged:source];
}
});
capturer_->CaptureFrame();
}
});
if(capturer_->SelectSource(source->id())){
capturer_->CaptureFrame();
return true;
}
return false;

return true;
}

int ObjCDesktopMediaList::GetSourceCount() const {
Expand Down

0 comments on commit be5a771

Please sign in to comment.