OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/native_desktop_media_list.h" | 5 #include "chrome/browser/media/native_desktop_media_list.h" |
6 | 6 |
7 #include "base/hash.h" | 7 #include "base/hash.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
10 #include "chrome/browser/media/desktop_media_list_observer.h" | 10 #include "chrome/browser/media/desktop_media_list_observer.h" |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_list.h" | |
13 #include "chrome/browser/ui/browser_window.h" | |
11 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
12 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
13 #include "media/base/video_util.h" | 16 #include "media/base/video_util.h" |
14 #include "third_party/libyuv/include/libyuv/scale_argb.h" | 17 #include "third_party/libyuv/include/libyuv/scale_argb.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 19 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
17 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" | 20 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
18 #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" | 21 #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" |
22 #include "ui/aura/window.h" | |
23 #include "ui/aura/window_tree_host.h" | |
19 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
25 #include "ui/gfx/native_widget_types.h" | |
26 #include "ui/snapshot/snapshot.h" | |
20 | 27 |
21 using content::BrowserThread; | 28 using content::BrowserThread; |
22 using content::DesktopMediaID; | |
23 | 29 |
24 namespace { | 30 namespace { |
25 | 31 |
26 // Update the list every second. | 32 // Update the list every second. |
27 const int kDefaultUpdatePeriod = 1000; | 33 const int kDefaultUpdatePeriod = 1000; |
28 | 34 |
29 // Returns a hash of a DesktopFrame content to detect when image for a desktop | 35 // Returns a hash of a DesktopFrame content to detect when image for a desktop |
30 // media source has changed. | 36 // media source has changed. |
31 uint32_t GetFrameHash(webrtc::DesktopFrame* frame) { | 37 uint32_t GetFrameHash(webrtc::DesktopFrame* frame) { |
32 int data_size = frame->stride() * frame->size().height(); | 38 int data_size = frame->stride() * frame->size().height(); |
33 return base::SuperFastHash(reinterpret_cast<char*>(frame->data()), data_size); | 39 return base::Hash(reinterpret_cast<char*>(frame->data()), data_size); |
34 } | 40 } |
35 | 41 |
36 gfx::ImageSkia ScaleDesktopFrame(scoped_ptr<webrtc::DesktopFrame> frame, | 42 gfx::ImageSkia ScaleDesktopFrame(scoped_ptr<webrtc::DesktopFrame> frame, |
37 gfx::Size size) { | 43 gfx::Size size) { |
38 gfx::Rect scaled_rect = media::ComputeLetterboxRegion( | 44 gfx::Rect scaled_rect = media::ComputeLetterboxRegion( |
39 gfx::Rect(0, 0, size.width(), size.height()), | 45 gfx::Rect(0, 0, size.width(), size.height()), |
40 gfx::Size(frame->size().width(), frame->size().height())); | 46 gfx::Size(frame->size().width(), frame->size().height())); |
41 | 47 |
42 SkBitmap result; | 48 SkBitmap result; |
43 result.allocN32Pixels(scaled_rect.width(), scaled_rect.height(), true); | 49 result.allocN32Pixels(scaled_rect.width(), scaled_rect.height(), true); |
(...skipping 27 matching lines...) Expand all Loading... | |
71 | 77 |
72 class NativeDesktopMediaList::Worker | 78 class NativeDesktopMediaList::Worker |
73 : public webrtc::DesktopCapturer::Callback { | 79 : public webrtc::DesktopCapturer::Callback { |
74 public: | 80 public: |
75 Worker(base::WeakPtr<NativeDesktopMediaList> media_list, | 81 Worker(base::WeakPtr<NativeDesktopMediaList> media_list, |
76 scoped_ptr<webrtc::ScreenCapturer> screen_capturer, | 82 scoped_ptr<webrtc::ScreenCapturer> screen_capturer, |
77 scoped_ptr<webrtc::WindowCapturer> window_capturer); | 83 scoped_ptr<webrtc::WindowCapturer> window_capturer); |
78 ~Worker() override; | 84 ~Worker() override; |
79 | 85 |
80 void Refresh(const gfx::Size& thumbnail_size, | 86 void Refresh(const gfx::Size& thumbnail_size, |
81 content::DesktopMediaID::Id view_dialog_id); | 87 DesktopMediaID::Id view_dialog_id, |
88 NativeAuraIdMap native_aura_id_map); | |
82 | 89 |
83 private: | 90 private: |
84 typedef std::map<DesktopMediaID, uint32_t> ImageHashesMap; | 91 typedef std::map<DesktopMediaID, uint32_t> ImageHashesMap; |
85 | 92 |
86 // webrtc::DesktopCapturer::Callback interface. | 93 // webrtc::DesktopCapturer::Callback interface. |
87 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; | 94 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; |
88 | 95 |
89 base::WeakPtr<NativeDesktopMediaList> media_list_; | 96 base::WeakPtr<NativeDesktopMediaList> media_list_; |
90 | 97 |
91 scoped_ptr<webrtc::ScreenCapturer> screen_capturer_; | 98 scoped_ptr<webrtc::ScreenCapturer> screen_capturer_; |
(...skipping 16 matching lines...) Expand all Loading... | |
108 if (screen_capturer_) | 115 if (screen_capturer_) |
109 screen_capturer_->Start(this); | 116 screen_capturer_->Start(this); |
110 if (window_capturer_) | 117 if (window_capturer_) |
111 window_capturer_->Start(this); | 118 window_capturer_->Start(this); |
112 } | 119 } |
113 | 120 |
114 NativeDesktopMediaList::Worker::~Worker() {} | 121 NativeDesktopMediaList::Worker::~Worker() {} |
115 | 122 |
116 void NativeDesktopMediaList::Worker::Refresh( | 123 void NativeDesktopMediaList::Worker::Refresh( |
117 const gfx::Size& thumbnail_size, | 124 const gfx::Size& thumbnail_size, |
118 content::DesktopMediaID::Id view_dialog_id) { | 125 DesktopMediaID::Id view_dialog_id, |
126 NativeAuraIdMap native_aura_id_map) { | |
119 std::vector<SourceDescription> sources; | 127 std::vector<SourceDescription> sources; |
128 std::vector<DesktopMediaID> aura_media_ids; | |
120 | 129 |
121 if (screen_capturer_) { | 130 if (screen_capturer_) { |
122 webrtc::ScreenCapturer::ScreenList screens; | 131 webrtc::ScreenCapturer::ScreenList screens; |
123 if (screen_capturer_->GetScreenList(&screens)) { | 132 if (screen_capturer_->GetScreenList(&screens)) { |
124 bool mutiple_screens = screens.size() > 1; | 133 bool mutiple_screens = screens.size() > 1; |
125 base::string16 title; | 134 base::string16 title; |
126 for (size_t i = 0; i < screens.size(); ++i) { | 135 for (size_t i = 0; i < screens.size(); ++i) { |
127 if (mutiple_screens) { | 136 if (mutiple_screens) { |
128 title = l10n_util::GetStringFUTF16Int( | 137 title = l10n_util::GetStringFUTF16Int( |
129 IDS_DESKTOP_MEDIA_PICKER_MULTIPLE_SCREEN_NAME, | 138 IDS_DESKTOP_MEDIA_PICKER_MULTIPLE_SCREEN_NAME, |
130 static_cast<int>(i + 1)); | 139 static_cast<int>(i + 1)); |
131 } else { | 140 } else { |
132 title = l10n_util::GetStringUTF16( | 141 title = l10n_util::GetStringUTF16( |
133 IDS_DESKTOP_MEDIA_PICKER_SINGLE_SCREEN_NAME); | 142 IDS_DESKTOP_MEDIA_PICKER_SINGLE_SCREEN_NAME); |
134 } | 143 } |
135 sources.push_back(SourceDescription(DesktopMediaID( | 144 sources.push_back(SourceDescription(DesktopMediaID( |
136 DesktopMediaID::TYPE_SCREEN, screens[i].id), title)); | 145 DesktopMediaID::TYPE_SCREEN, screens[i].id), title)); |
137 } | 146 } |
138 } | 147 } |
139 } | 148 } |
140 | 149 |
141 if (window_capturer_) { | 150 if (window_capturer_) { |
142 webrtc::WindowCapturer::WindowList windows; | 151 webrtc::WindowCapturer::WindowList windows; |
143 if (window_capturer_->GetWindowList(&windows)) { | 152 if (window_capturer_->GetWindowList(&windows)) { |
144 for (webrtc::WindowCapturer::WindowList::iterator it = windows.begin(); | 153 for (webrtc::WindowCapturer::WindowList::iterator it = windows.begin(); |
145 it != windows.end(); ++it) { | 154 it != windows.end(); ++it) { |
146 // Skip the picker dialog window. | 155 // Skip the picker dialog window. |
147 if (it->id != view_dialog_id) { | 156 if (it->id != view_dialog_id) { |
148 sources.push_back(SourceDescription( | 157 DesktopMediaID media_id(DesktopMediaID::TYPE_WINDOW, it->id); |
149 DesktopMediaID(DesktopMediaID::TYPE_WINDOW, it->id), | 158 #if defined(USE_AURA) |
150 base::UTF8ToUTF16(it->title))); | 159 // Associate aura id with native id. |
160 if (native_aura_id_map.count(media_id.id)) { | |
161 media_id.aura_id = native_aura_id_map[media_id.id]; | |
162 aura_media_ids.push_back(media_id); | |
163 } | |
164 #endif | |
165 sources.push_back( | |
166 SourceDescription(media_id, base::UTF8ToUTF16(it->title))); | |
151 } | 167 } |
152 } | 168 } |
153 } | 169 } |
154 } | 170 } |
171 | |
155 // Update list of windows before updating thumbnails. | 172 // Update list of windows before updating thumbnails. |
156 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 173 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
157 base::Bind(&NativeDesktopMediaList::UpdateSourcesList, | 174 base::Bind(&NativeDesktopMediaList::UpdateSourcesList, |
158 media_list_, sources)); | 175 media_list_, sources)); |
159 | 176 |
160 ImageHashesMap new_image_hashes; | 177 ImageHashesMap new_image_hashes; |
161 | 178 |
162 // Get a thumbnail for each source. | 179 // Get a thumbnail for each source. |
163 for (size_t i = 0; i < sources.size(); ++i) { | 180 for (size_t i = 0; i < sources.size(); ++i) { |
164 SourceDescription& source = sources[i]; | 181 SourceDescription& source = sources[i]; |
182 | |
165 switch (source.id.type) { | 183 switch (source.id.type) { |
166 case DesktopMediaID::TYPE_SCREEN: | 184 case DesktopMediaID::TYPE_SCREEN: |
167 if (!screen_capturer_->SelectScreen(source.id.id)) | 185 if (!screen_capturer_->SelectScreen(source.id.id)) |
168 continue; | 186 continue; |
169 screen_capturer_->Capture(webrtc::DesktopRegion()); | 187 screen_capturer_->Capture(webrtc::DesktopRegion()); |
170 break; | 188 break; |
171 | 189 |
172 case DesktopMediaID::TYPE_WINDOW: | 190 case DesktopMediaID::TYPE_WINDOW: |
191 #if defined(USE_AURA) | |
192 if (source.id.aura_id > 0) | |
Sergey Ulanov
2016/03/07 21:01:50
Add a comment to explain what happens here.
Sergey Ulanov
2016/03/07 21:01:50
aura_id != DesktopMediaID::kInvalidId.
GeorgeZ
2016/03/07 23:29:20
Done.
GeorgeZ
2016/03/07 23:29:20
Done.
| |
193 continue; | |
194 #endif | |
173 if (!window_capturer_->SelectWindow(source.id.id)) | 195 if (!window_capturer_->SelectWindow(source.id.id)) |
174 continue; | 196 continue; |
175 window_capturer_->Capture(webrtc::DesktopRegion()); | 197 window_capturer_->Capture(webrtc::DesktopRegion()); |
176 break; | 198 break; |
177 | 199 |
178 default: | 200 default: |
179 NOTREACHED(); | 201 NOTREACHED(); |
180 } | 202 } |
181 | 203 |
182 // Expect that DesktopCapturer to always captures frames synchronously. | 204 // Expect that DesktopCapturer to always captures frames synchronously. |
183 // |current_frame_| may be NULL if capture failed (e.g. because window has | 205 // |current_frame_| may be NULL if capture failed (e.g. because window has |
184 // been closed). | 206 // been closed). |
185 if (current_frame_) { | 207 if (current_frame_) { |
186 uint32_t frame_hash = GetFrameHash(current_frame_.get()); | 208 uint32_t frame_hash = GetFrameHash(current_frame_.get()); |
187 new_image_hashes[source.id] = frame_hash; | 209 new_image_hashes[source.id] = frame_hash; |
188 | 210 |
189 // Scale the image only if it has changed. | 211 // Scale the image only if it has changed. |
190 ImageHashesMap::iterator it = image_hashes_.find(source.id); | 212 ImageHashesMap::iterator it = image_hashes_.find(source.id); |
191 if (it == image_hashes_.end() || it->second != frame_hash) { | 213 if (it == image_hashes_.end() || it->second != frame_hash) { |
192 gfx::ImageSkia thumbnail = | 214 gfx::ImageSkia thumbnail = |
193 ScaleDesktopFrame(std::move(current_frame_), thumbnail_size); | 215 ScaleDesktopFrame(std::move(current_frame_), thumbnail_size); |
194 BrowserThread::PostTask( | 216 BrowserThread::PostTask( |
195 BrowserThread::UI, FROM_HERE, | 217 BrowserThread::UI, FROM_HERE, |
196 base::Bind(&NativeDesktopMediaList::OnSourceThumbnail, media_list_, | 218 base::Bind(&NativeDesktopMediaList::OnSourceThumbnailCaptured, |
197 i, thumbnail)); | 219 media_list_, i, thumbnail)); |
198 } | 220 } |
199 } | 221 } |
200 } | 222 } |
201 | 223 |
202 image_hashes_.swap(new_image_hashes); | 224 image_hashes_.swap(new_image_hashes); |
203 | 225 |
204 BrowserThread::PostTask( | 226 #if defined(USE_AURA) |
205 BrowserThread::UI, FROM_HERE, | 227 // Aura thumbnail captures are at the last. When they are done, thumbnail |
206 base::Bind(&NativeDesktopMediaList::ScheduleNextRefresh, media_list_)); | 228 // captures for all sources are done. |
229 for (const auto& media_id : aura_media_ids) { | |
230 BrowserThread::PostTask( | |
231 BrowserThread::UI, FROM_HERE, | |
232 base::Bind(&NativeDesktopMediaList::CaptureAuraWindowThumbnail, | |
233 media_list_, media_id, aura_media_ids.size())); | |
234 } | |
235 #endif | |
236 | |
237 // If no aura window need capture, we can schedule next refresh. | |
238 if (aura_media_ids.size() == 0) { | |
Sergey Ulanov
2016/03/07 21:01:50
The logic here is hard to follow. I suggest you po
GeorgeZ
2016/03/07 23:29:20
Good idea.
| |
239 BrowserThread::PostTask( | |
240 BrowserThread::UI, FROM_HERE, | |
241 base::Bind(&NativeDesktopMediaList::ScheduleNextRefresh, media_list_)); | |
242 } | |
207 } | 243 } |
208 | 244 |
209 void NativeDesktopMediaList::Worker::OnCaptureCompleted( | 245 void NativeDesktopMediaList::Worker::OnCaptureCompleted( |
210 webrtc::DesktopFrame* frame) { | 246 webrtc::DesktopFrame* frame) { |
211 current_frame_.reset(frame); | 247 current_frame_.reset(frame); |
212 } | 248 } |
213 | 249 |
214 NativeDesktopMediaList::NativeDesktopMediaList( | 250 NativeDesktopMediaList::NativeDesktopMediaList( |
215 scoped_ptr<webrtc::ScreenCapturer> screen_capturer, | 251 scoped_ptr<webrtc::ScreenCapturer> screen_capturer, |
216 scoped_ptr<webrtc::WindowCapturer> window_capturer) | 252 scoped_ptr<webrtc::WindowCapturer> window_capturer) |
217 : DesktopMediaListBase( | 253 : DesktopMediaListBase( |
218 base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)), | 254 base::TimeDelta::FromMilliseconds(kDefaultUpdatePeriod)), |
219 weak_factory_(this) { | 255 weak_factory_(this) { |
220 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); | 256 base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); |
221 capture_task_runner_ = worker_pool->GetSequencedTaskRunner( | 257 capture_task_runner_ = worker_pool->GetSequencedTaskRunner( |
222 worker_pool->GetSequenceToken()); | 258 worker_pool->GetSequenceToken()); |
223 | 259 |
224 worker_.reset(new Worker(weak_factory_.GetWeakPtr(), | 260 worker_.reset(new Worker(weak_factory_.GetWeakPtr(), |
225 std::move(screen_capturer), | 261 std::move(screen_capturer), |
226 std::move(window_capturer))); | 262 std::move(window_capturer))); |
227 } | 263 } |
228 | 264 |
229 NativeDesktopMediaList::~NativeDesktopMediaList() { | 265 NativeDesktopMediaList::~NativeDesktopMediaList() { |
230 capture_task_runner_->DeleteSoon(FROM_HERE, worker_.release()); | 266 capture_task_runner_->DeleteSoon(FROM_HERE, worker_.release()); |
231 } | 267 } |
232 | 268 |
233 void NativeDesktopMediaList::Refresh() { | 269 void NativeDesktopMediaList::Refresh() { |
270 NativeAuraIdMap native_aura_id_map; | |
271 #if defined(USE_AURA) | |
272 native_aura_id_map = GetBrowserNativeAuraIdMap(); | |
273 #endif | |
274 processed_aura_window_count_ = 0; | |
275 cur_aura_thumbnail_hashes_.clear(); | |
276 | |
234 capture_task_runner_->PostTask( | 277 capture_task_runner_->PostTask( |
235 FROM_HERE, base::Bind(&Worker::Refresh, base::Unretained(worker_.get()), | 278 FROM_HERE, |
236 thumbnail_size_, view_dialog_id_.id)); | 279 base::Bind(&Worker::Refresh, base::Unretained(worker_.get()), |
280 thumbnail_size_, view_dialog_id_.id, native_aura_id_map)); | |
237 } | 281 } |
238 | 282 |
239 void NativeDesktopMediaList::OnSourceThumbnail(int index, | 283 void NativeDesktopMediaList::OnSourceThumbnailCaptured( |
240 const gfx::ImageSkia& image) { | 284 int index, |
285 const gfx::ImageSkia& image) { | |
241 UpdateSourceThumbnail(GetSource(index).id, image); | 286 UpdateSourceThumbnail(GetSource(index).id, image); |
242 } | 287 } |
288 | |
289 void NativeDesktopMediaList::OnAuraThumbnailCaptured(DesktopMediaID id, | |
290 int total_aura_windows, | |
291 const gfx::Image& image) { | |
292 if (!image.IsEmpty()) { | |
293 // Only new or changed thumbnail need update. | |
294 cur_aura_thumbnail_hashes_[id] = DesktopMediaListBase::GetImageHash(image); | |
295 if (!aura_thumbnail_hashes_.count(id) || | |
296 aura_thumbnail_hashes_[id] != cur_aura_thumbnail_hashes_[id]) { | |
297 UpdateSourceThumbnail(id, image.AsImageSkia()); | |
298 } | |
299 } | |
300 | |
301 // After all aura windows are processed, schedule next refresh; | |
302 processed_aura_window_count_++; | |
303 if (processed_aura_window_count_ == total_aura_windows) { | |
304 swap(aura_thumbnail_hashes_, cur_aura_thumbnail_hashes_); | |
305 ScheduleNextRefresh(); | |
306 } | |
307 } | |
308 | |
309 #if defined(USE_AURA) | |
310 | |
311 NativeDesktopMediaList::NativeAuraIdMap | |
312 NativeDesktopMediaList::GetBrowserNativeAuraIdMap() { | |
313 NativeAuraIdMap id_map; | |
314 for (auto* browser : *BrowserList::GetInstance()) { | |
315 const BrowserWindow* browser_window = browser->window(); | |
316 if (!browser_window) | |
317 continue; | |
318 const gfx::NativeWindow native_window = browser_window->GetNativeWindow(); | |
319 if (!native_window) | |
320 continue; | |
321 aura::WindowTreeHost* host = native_window->GetHost(); | |
322 if (!host) | |
323 continue; | |
324 gfx::AcceleratedWidget widget = host->GetAcceleratedWidget(); | |
325 #if defined(OS_WIN) | |
326 DesktopMediaID::Id native_id = reinterpret_cast<DesktopMediaID::Id>(widget); | |
327 #else | |
328 DesktopMediaID::Id native_id = widget; | |
329 #endif | |
330 DesktopMediaID media_id = DesktopMediaID::RegisterAuraWindow( | |
331 DesktopMediaID::TYPE_WINDOW, native_window); | |
332 id_map.insert(std::make_pair(native_id, media_id.aura_id)); | |
333 } | |
334 | |
335 return id_map; | |
336 } | |
337 | |
338 void NativeDesktopMediaList::CaptureAuraWindowThumbnail( | |
339 DesktopMediaID id, | |
340 int total_aura_windows) { | |
341 gfx::NativeWindow window = DesktopMediaID::GetAuraWindowById(id); | |
342 if (window == NULL) { | |
Sergey Ulanov
2016/03/07 21:01:50
if (!window) for consistency with the code above
GeorgeZ
2016/03/07 23:29:20
Done.
| |
343 OnAuraThumbnailCaptured(id, total_aura_windows, gfx::Image()); | |
344 return; | |
345 } | |
346 | |
347 gfx::Rect window_rect(window->bounds().width(), window->bounds().height()); | |
348 gfx::Rect scaled_rect = media::ComputeLetterboxRegion( | |
349 gfx::Rect(thumbnail_size_), window_rect.size()); | |
350 | |
351 ui::GrabWindowSnapshotAndScaleAsync( | |
352 window, window_rect, scaled_rect.size(), BrowserThread::GetBlockingPool(), | |
353 base::Bind(&NativeDesktopMediaList::OnAuraThumbnailCaptured, | |
354 weak_factory_.GetWeakPtr(), id, total_aura_windows)); | |
355 } | |
356 | |
357 #endif | |
Sergey Ulanov
2016/03/07 21:01:50
// defined(USE_AURA)
GeorgeZ
2016/03/07 23:29:20
Done.
| |
OLD | NEW |