Skip to content

Commit fea1e54

Browse files
jakeloorogerwang
authored andcommitted
DesktopCaptureMonitor: Allow screens and windows both being monitor at
the same time
1 parent 8393b51 commit fea1e54

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

src/api/nw_screen_api.cc

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace extensions {
4949
void OnSourceThumbnailChanged(DesktopMediaList* list, int index) override;
5050

5151
bool started_;
52-
std::unique_ptr<DesktopMediaList> media_list_;
52+
std::vector<std::unique_ptr<DesktopMediaList>> media_list_;
5353

5454
DISALLOW_COPY_AND_ASSIGN(NwDesktopCaptureMonitor);
5555
};
@@ -191,8 +191,7 @@ namespace extensions {
191191
}
192192

193193
NwDesktopCaptureMonitor::NwDesktopCaptureMonitor()
194-
: started_(false)
195-
, media_list_(nullptr) {
194+
: started_(false) {
196195
}
197196

198197
void NwDesktopCaptureMonitor::Start(bool screens, bool windows) {
@@ -205,17 +204,30 @@ namespace extensions {
205204
webrtc::DesktopCaptureOptions options = webrtc::DesktopCaptureOptions::CreateDefault();
206205
options.set_disable_effects(false);
207206

208-
if (screens)
209-
media_list_.reset(new NativeDesktopMediaList(content::DesktopMediaID::TYPE_SCREEN, webrtc::DesktopCapturer::CreateScreenCapturer(options)));
210-
else if (windows)
211-
media_list_.reset(new NativeDesktopMediaList(content::DesktopMediaID::TYPE_WINDOW, webrtc::DesktopCapturer::CreateWindowCapturer(options)));
207+
if (screens) {
208+
std::unique_ptr<DesktopMediaList> screen_media_list =
209+
base::MakeUnique<NativeDesktopMediaList>(
210+
content::DesktopMediaID::TYPE_SCREEN,
211+
webrtc::DesktopCapturer::CreateScreenCapturer(options));
212+
media_list_.push_back(std::move(screen_media_list));
213+
}
214+
215+
if (windows) {
216+
std::unique_ptr<DesktopMediaList> window_media_list =
217+
base::MakeUnique<NativeDesktopMediaList>(
218+
content::DesktopMediaID::TYPE_WINDOW,
219+
webrtc::DesktopCapturer::CreateWindowCapturer(options));
220+
media_list_.push_back(std::move(window_media_list));
221+
}
212222

213-
media_list_->StartUpdating(this);
223+
for (auto& media_list : media_list_) {
224+
media_list->StartUpdating(this);
225+
}
214226
}
215227

216228
void NwDesktopCaptureMonitor::Stop() {
217229
started_ = false;
218-
media_list_.reset();
230+
media_list_.clear();
219231
}
220232

221233
bool NwDesktopCaptureMonitor::IsStarted() {
@@ -243,8 +255,8 @@ namespace extensions {
243255
}
244256

245257
void NwDesktopCaptureMonitor::OnSourceAdded(DesktopMediaList* list, int index) {
246-
DesktopMediaList::Source src = media_list_->GetSource(index);
247-
258+
DesktopMediaList::Source src = list->GetSource(index);
259+
248260
std::string type;
249261
switch(src.id.type) {
250262
case content::DesktopMediaID::TYPE_WINDOW:
@@ -283,19 +295,19 @@ void NwDesktopCaptureMonitor::OnSourceRemoved(DesktopMediaList* list, int index)
283295
}
284296

285297
void NwDesktopCaptureMonitor::OnSourceMoved(DesktopMediaList* list, int old_index, int new_index) {
286-
DesktopMediaList::Source src = media_list_->GetSource(new_index);
298+
DesktopMediaList::Source src = list->GetSource(new_index);
287299
std::unique_ptr<base::ListValue> args = nwapi::nw__screen::OnSourceOrderChanged::Create(
288300
src.id.ToString(),
289301
new_index,
290302
old_index);
291303
DispatchEvent(
292304
events::HistogramValue::UNKNOWN,
293305
nwapi::nw__screen::OnSourceOrderChanged::kEventName,
294-
std::move(args));
306+
std::move(args));
295307
}
296308

297309
void NwDesktopCaptureMonitor::OnSourceNameChanged(DesktopMediaList* list, int index) {
298-
DesktopMediaList::Source src = media_list_->GetSource(index);
310+
DesktopMediaList::Source src = list->GetSource(index);
299311
std::unique_ptr<base::ListValue> args = nwapi::nw__screen::OnSourceNameChanged::Create(
300312
src.id.ToString(),
301313
base::UTF16ToUTF8(src.name));
@@ -308,7 +320,7 @@ void NwDesktopCaptureMonitor::OnSourceNameChanged(DesktopMediaList* list, int in
308320
void NwDesktopCaptureMonitor::OnSourceThumbnailChanged(DesktopMediaList* list, int index) {
309321
std::string base64;
310322

311-
DesktopMediaList::Source src = media_list_->GetSource(index);
323+
DesktopMediaList::Source src = list->GetSource(index);
312324
SkBitmap bitmap = src.thumbnail.GetRepresentation(1).sk_bitmap();
313325
std::vector<unsigned char> data;
314326
bool success = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data);

0 commit comments

Comments
 (0)