|
| 1 | +// Copyright (c) 2012 Intel Corp |
| 2 | +// Copyright (c) 2012 The Chromium Authors |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co |
| 8 | +// pies of the Software, and to permit persons to whom the Software is furnished |
| 9 | +// to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in al |
| 12 | +// l copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM |
| 15 | +// PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES |
| 16 | +// S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 17 | +// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH |
| 18 | +// ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + |
| 21 | +#include "content/nw/src/api/screen/desktop_capture_monitor.h" |
| 22 | + |
| 23 | +#include "base/values.h" |
| 24 | +#include "base/strings/utf_string_conversions.h" |
| 25 | +#include "base/strings/string16.h" |
| 26 | +#include "content/nw/src/api/dispatcher_host.h" |
| 27 | +#include "chrome/browser/media/desktop_streams_registry.h" |
| 28 | +#include "chrome/browser/media/media_capture_devices_dispatcher.h" |
| 29 | + |
| 30 | +#include "third_party/webrtc/modules/desktop_capture/desktop_capture_options.h" |
| 31 | +#include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
| 32 | +#include "third_party/webrtc/modules/desktop_capture/window_capturer.h" |
| 33 | + |
| 34 | +#include "base/base64.h" |
| 35 | +#include "ui/gfx/codec/png_codec.h" |
| 36 | +#include "ui/gfx/image/image.h" |
| 37 | +#include "ui/gfx/image/image_skia.h" |
| 38 | + |
| 39 | +#ifdef _WIN32 |
| 40 | +#include <windows.h> |
| 41 | +#endif |
| 42 | + |
| 43 | +namespace nwapi { |
| 44 | + |
| 45 | +DesktopCaptureMonitor::DesktopCaptureMonitor(int id, |
| 46 | + const base::WeakPtr<DispatcherHost>& dispatcher_host, |
| 47 | + const base::DictionaryValue& option) |
| 48 | + : Base(id, dispatcher_host, option) { |
| 49 | +} |
| 50 | + |
| 51 | +DesktopCaptureMonitor::~DesktopCaptureMonitor() {} |
| 52 | + |
| 53 | +int DesktopCaptureMonitor::GetPrimaryMonitorIndex(){ |
| 54 | +#ifdef _WIN32 |
| 55 | + int count=0; |
| 56 | + for (int i = 0;; ++i) { |
| 57 | + DISPLAY_DEVICE device; |
| 58 | + device.cb = sizeof(device); |
| 59 | + BOOL ret = EnumDisplayDevices(NULL, i, &device, 0); |
| 60 | + if(!ret) |
| 61 | + break; |
| 62 | + if (device.StateFlags & DISPLAY_DEVICE_ACTIVE){ |
| 63 | + if (device.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE){ |
| 64 | + return count; |
| 65 | + } |
| 66 | + count++; |
| 67 | + } |
| 68 | + } |
| 69 | +#endif |
| 70 | + return -1; |
| 71 | +} |
| 72 | + |
| 73 | +void DesktopCaptureMonitor::CallSync(const std::string& method, const base::ListValue& arguments, base::ListValue* result){ |
| 74 | + if (method == "start") { |
| 75 | + bool screens, windows; |
| 76 | + if (arguments.GetBoolean(0, &screens) && arguments.GetBoolean(1, &windows)) |
| 77 | + Start(screens, windows); |
| 78 | + }else if (method == "stop") { |
| 79 | + Stop(); |
| 80 | + } |
| 81 | + else { |
| 82 | + NOTREACHED() << "Invalid call to DesktopCapture method:" << method |
| 83 | + << " arguments:" << arguments; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void DesktopCaptureMonitor::Start(bool screens, bool windows) { |
| 88 | + webrtc::DesktopCaptureOptions options = webrtc::DesktopCaptureOptions::CreateDefault(); |
| 89 | + options.set_disable_effects(false); |
| 90 | + scoped_ptr<webrtc::ScreenCapturer> screen_capturer(screens ? webrtc::ScreenCapturer::Create(options) : NULL); |
| 91 | + scoped_ptr<webrtc::WindowCapturer> window_capturer(windows ? webrtc::WindowCapturer::Create(options) : NULL); |
| 92 | + |
| 93 | + media_list_.reset(new NativeDesktopMediaList(screen_capturer.Pass(), window_capturer.Pass())); |
| 94 | + |
| 95 | + media_list_->StartUpdating(this); |
| 96 | +} |
| 97 | + |
| 98 | +void DesktopCaptureMonitor::Stop() { |
| 99 | + media_list_.reset(); |
| 100 | +} |
| 101 | + |
| 102 | +void DesktopCaptureMonitor::OnSourceAdded(int index){ |
| 103 | + DesktopMediaList::Source src = media_list_->GetSource(index); |
| 104 | + |
| 105 | + std::string type; |
| 106 | + if (src.id.type == content::DesktopMediaID::TYPE_AURA_WINDOW || src.id.type == content::DesktopMediaID::TYPE_WINDOW){ |
| 107 | + type = "window"; |
| 108 | + } |
| 109 | + else if (src.id.type == content::DesktopMediaID::TYPE_SCREEN){ |
| 110 | + type = "screen"; |
| 111 | + } |
| 112 | + else if (src.id.type == content::DesktopMediaID::TYPE_NONE){ |
| 113 | + type = "none"; |
| 114 | + } |
| 115 | + else{ |
| 116 | + type = "unknown"; |
| 117 | + } |
| 118 | + |
| 119 | + base::ListValue param; |
| 120 | + param.AppendString(src.id.ToString()); |
| 121 | + param.AppendString(src.name); |
| 122 | + param.AppendInteger(index); |
| 123 | + param.AppendString(type); |
| 124 | + if(src.id.type == content::DesktopMediaID::TYPE_SCREEN){ |
| 125 | + param.AppendBoolean(GetPrimaryMonitorIndex()==index); |
| 126 | + } |
| 127 | + this->dispatcher_host()->SendEvent(this, "__nw_desktop_capture_monitor_listner_added", param); |
| 128 | +} |
| 129 | +void DesktopCaptureMonitor::OnSourceRemoved(int index){ |
| 130 | + base::ListValue param; |
| 131 | + param.AppendInteger(index); //pass by index here, because the information about which ID was at that index is lost before the removed callback is called. Its saved in the javascript though, so we can look it up there |
| 132 | + this->dispatcher_host()->SendEvent(this, "__nw_desktop_capture_monitor_listner_removed", param); |
| 133 | +} |
| 134 | +void DesktopCaptureMonitor::OnSourceMoved(int old_index, int new_index){ |
| 135 | + DesktopMediaList::Source src = media_list_->GetSource(new_index); |
| 136 | + base::ListValue param; |
| 137 | + param.AppendString(src.id.ToString()); |
| 138 | + param.AppendInteger(new_index); |
| 139 | + param.AppendInteger(old_index); |
| 140 | + this->dispatcher_host()->SendEvent(this, "__nw_desktop_capture_monitor_listner_moved", param); |
| 141 | +} |
| 142 | +void DesktopCaptureMonitor::OnSourceNameChanged(int index){ |
| 143 | + DesktopMediaList::Source src = media_list_->GetSource(index); |
| 144 | + |
| 145 | + base::ListValue param; |
| 146 | + param.AppendString(src.id.ToString()); |
| 147 | + param.AppendString(src.name); |
| 148 | + this->dispatcher_host()->SendEvent(this, "__nw_desktop_capture_monitor_listner_namechanged", param); |
| 149 | +} |
| 150 | + |
| 151 | +void DesktopCaptureMonitor::OnSourceThumbnailChanged(int index){ |
| 152 | + std::string base64; |
| 153 | + |
| 154 | + DesktopMediaList::Source src = media_list_->GetSource(index); |
| 155 | + SkBitmap bitmap = src.thumbnail.GetRepresentation(1).sk_bitmap(); |
| 156 | + SkAutoLockPixels lock_image(bitmap); |
| 157 | + std::vector<unsigned char> data; |
| 158 | + bool success = gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &data); |
| 159 | + if (success){ |
| 160 | + base::StringPiece raw_str(reinterpret_cast<const char*>(&data[0]), data.size()); |
| 161 | + base::Base64Encode(raw_str, &base64); |
| 162 | + } |
| 163 | + base::ListValue param; |
| 164 | + param.AppendString(src.id.ToString()); |
| 165 | + param.AppendString(base64); |
| 166 | + this->dispatcher_host()->SendEvent(this, "__nw_desktop_capture_monitor_listner_thumbnailchanged", param); |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace nwapi |
0 commit comments