Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Get thumbnails asynchronously. #37

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -216,16 +216,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
29 changes: 20 additions & 9 deletions sdk/objc/native/src/objc_desktop_capture.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,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_->Invoke<void>(RTC_FROM_HERE, [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_->Invoke<void>(RTC_FROM_HERE, [this] {
capturer_.reset();
});
}

ObjCDesktopCapturer::CaptureState ObjCDesktopCapturer::Start(uint32_t fps) {
Expand Down Expand Up @@ -69,9 +75,14 @@
}
}

capturer_->Start(this);
thread_->Invoke<void>(RTC_FROM_HERE, [this] {
capturer_->Start(this);
});
capture_state_ = CS_RUNNING;
CaptureFrame();
thread_->PostTask(ToQueuedTask(
[this]{
CaptureFrame();
}));
[delegate_ didSourceCaptureStart];
return capture_state_;
}
Expand Down
59 changes: 37 additions & 22 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_->Invoke<void>(RTC_FROM_HERE, [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_->Invoke<void>(RTC_FROM_HERE, [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_->Invoke<void>(RTC_FROM_HERE, [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,23 @@
}

bool ObjCDesktopMediaList::GetThumbnail(MediaSource *source, bool notify) {
callback_->SetCallback([&](webrtc::DesktopCapturer::Result result,

thread_->PostTask(ToQueuedTask(
[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];
}
});
if(capturer_->SelectSource(source->id())){
capturer_->CaptureFrame();
return true;
}
return false;
auto old_thumbnail = source->thumbnail();
source->SaveCaptureResult(result, std::move(frame));
if(old_thumbnail.size() != source->thumbnail().size() && notify) {
[objcMediaList_ mediaSourceThumbnailChanged:source];
}
});
capturer_->CaptureFrame();
}
}));

return true;
}

int ObjCDesktopMediaList::GetSourceCount() const {
Expand Down