Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "webrtc/modules/desktop_capture/desktop_device_info.h"
6 :
7 : #include <cstddef>
8 : #include <cstdlib>
9 : #include <cstdio>
10 : #include <cstring>
11 :
12 : namespace webrtc {
13 :
14 0 : static inline void SetStringMember(char **member, const char *value) {
15 0 : if (!value) {
16 0 : return;
17 : }
18 :
19 0 : if (*member) {
20 0 : delete [] *member;
21 0 : *member = NULL;
22 : }
23 :
24 0 : size_t nBufLen = strlen(value) + 1;
25 0 : char *buffer = new char[nBufLen];
26 0 : memcpy(buffer, value, nBufLen - 1);
27 0 : buffer[nBufLen - 1] = '\0';
28 0 : *member = buffer;
29 : }
30 :
31 0 : DesktopDisplayDevice::DesktopDisplayDevice() {
32 0 : screenId_ = kInvalidScreenId;
33 0 : deviceUniqueIdUTF8_ = NULL;
34 0 : deviceNameUTF8_ = NULL;
35 0 : pid_ = 0;
36 0 : }
37 :
38 0 : DesktopDisplayDevice::~DesktopDisplayDevice() {
39 0 : screenId_ = kInvalidScreenId;
40 :
41 0 : if (deviceUniqueIdUTF8_){
42 0 : delete [] deviceUniqueIdUTF8_;
43 : }
44 :
45 0 : if (deviceNameUTF8_){
46 0 : delete [] deviceNameUTF8_;
47 : }
48 :
49 0 : deviceUniqueIdUTF8_ = NULL;
50 0 : deviceNameUTF8_ = NULL;
51 0 : }
52 :
53 0 : void DesktopDisplayDevice::setScreenId(const ScreenId screenId) {
54 0 : screenId_ = screenId;
55 0 : }
56 :
57 0 : void DesktopDisplayDevice::setDeviceName(const char *deviceNameUTF8) {
58 0 : SetStringMember(&deviceNameUTF8_, deviceNameUTF8);
59 0 : }
60 :
61 0 : void DesktopDisplayDevice::setUniqueIdName(const char *deviceUniqueIdUTF8) {
62 0 : SetStringMember(&deviceUniqueIdUTF8_, deviceUniqueIdUTF8);
63 0 : }
64 :
65 0 : void DesktopDisplayDevice::setPid(const int pid) {
66 0 : pid_ = pid;
67 0 : }
68 :
69 0 : ScreenId DesktopDisplayDevice::getScreenId() {
70 0 : return screenId_;
71 : }
72 :
73 0 : const char *DesktopDisplayDevice::getDeviceName() {
74 0 : return deviceNameUTF8_;
75 : }
76 :
77 0 : const char *DesktopDisplayDevice::getUniqueIdName() {
78 0 : return deviceUniqueIdUTF8_;
79 : }
80 :
81 0 : pid_t DesktopDisplayDevice::getPid() {
82 0 : return pid_;
83 : }
84 :
85 0 : DesktopDisplayDevice& DesktopDisplayDevice::operator= (DesktopDisplayDevice& other) {
86 0 : if (&other == this) {
87 0 : return *this;
88 : }
89 0 : screenId_ = other.getScreenId();
90 0 : setUniqueIdName(other.getUniqueIdName());
91 0 : setDeviceName(other.getDeviceName());
92 0 : pid_ = other.getPid();
93 :
94 0 : return *this;
95 : }
96 :
97 :
98 0 : DesktopApplication::DesktopApplication() {
99 0 : processId_ = 0;
100 0 : processPathNameUTF8_= NULL;
101 0 : applicationNameUTF8_= NULL;
102 0 : processUniqueIdUTF8_= NULL;
103 0 : windowCount_ = 0;
104 0 : }
105 :
106 0 : DesktopApplication::~DesktopApplication() {
107 0 : if (processPathNameUTF8_) {
108 0 : delete [] processPathNameUTF8_;
109 : }
110 :
111 0 : if (applicationNameUTF8_) {
112 0 : delete [] applicationNameUTF8_;
113 : }
114 :
115 0 : if (processUniqueIdUTF8_) {
116 0 : delete [] processUniqueIdUTF8_;
117 : }
118 :
119 0 : processPathNameUTF8_= NULL;
120 0 : applicationNameUTF8_= NULL;
121 0 : processUniqueIdUTF8_= NULL;
122 0 : }
123 :
124 0 : void DesktopApplication::setProcessId(const ProcessId processId) {
125 0 : processId_ = processId;
126 0 : }
127 :
128 0 : void DesktopApplication::setProcessPathName(const char *appPathNameUTF8) {
129 0 : SetStringMember(&processPathNameUTF8_, appPathNameUTF8);
130 0 : }
131 :
132 0 : void DesktopApplication::setUniqueIdName(const char *appUniqueIdUTF8) {
133 0 : SetStringMember(&processUniqueIdUTF8_, appUniqueIdUTF8);
134 0 : }
135 :
136 0 : void DesktopApplication::setProcessAppName(const char *appNameUTF8) {
137 0 : SetStringMember(&applicationNameUTF8_, appNameUTF8);
138 0 : }
139 :
140 0 : void DesktopApplication::setWindowCount(const uint32_t count) {
141 0 : windowCount_ = count;
142 0 : }
143 :
144 0 : ProcessId DesktopApplication::getProcessId() {
145 0 : return processId_;
146 : }
147 :
148 0 : const char *DesktopApplication::getProcessPathName() {
149 0 : return processPathNameUTF8_;
150 : }
151 :
152 0 : const char *DesktopApplication::getUniqueIdName() {
153 0 : return processUniqueIdUTF8_;
154 : }
155 :
156 0 : const char *DesktopApplication::getProcessAppName() {
157 0 : return applicationNameUTF8_;
158 : }
159 :
160 0 : uint32_t DesktopApplication::getWindowCount() {
161 0 : return windowCount_;
162 : }
163 :
164 0 : DesktopApplication& DesktopApplication::operator= (DesktopApplication& other) {
165 0 : processId_ = other.getProcessId();
166 0 : setProcessPathName(other.getProcessPathName());
167 0 : setUniqueIdName(other.getUniqueIdName());
168 0 : setProcessAppName(other.getProcessAppName());
169 :
170 0 : return *this;
171 : }
172 :
173 0 : DesktopDeviceInfoImpl::DesktopDeviceInfoImpl() {
174 0 : }
175 :
176 0 : DesktopDeviceInfoImpl::~DesktopDeviceInfoImpl() {
177 0 : CleanUp();
178 0 : }
179 :
180 0 : int32_t DesktopDeviceInfoImpl::getDisplayDeviceCount() {
181 0 : return desktop_display_list_.size();
182 : }
183 :
184 0 : int32_t DesktopDeviceInfoImpl::getDesktopDisplayDeviceInfo(int32_t nIndex,
185 : DesktopDisplayDevice & desktopDisplayDevice) {
186 0 : if(nIndex < 0 || nIndex >= desktop_display_list_.size()) {
187 0 : return -1;
188 : }
189 :
190 0 : std::map<intptr_t,DesktopDisplayDevice*>::iterator iter = desktop_display_list_.begin();
191 0 : std::advance (iter, nIndex);
192 0 : DesktopDisplayDevice * pDesktopDisplayDevice = iter->second;
193 0 : if(pDesktopDisplayDevice) {
194 0 : desktopDisplayDevice = (*pDesktopDisplayDevice);
195 : }
196 :
197 0 : return 0;
198 : }
199 :
200 0 : int32_t DesktopDeviceInfoImpl::getWindowCount() {
201 0 : return desktop_window_list_.size();
202 : }
203 0 : int32_t DesktopDeviceInfoImpl::getWindowInfo(int32_t nIndex,
204 : DesktopDisplayDevice &windowDevice) {
205 0 : if (nIndex < 0 || nIndex >= desktop_window_list_.size()) {
206 0 : return -1;
207 : }
208 :
209 0 : std::map<intptr_t, DesktopDisplayDevice *>::iterator itr = desktop_window_list_.begin();
210 0 : std::advance(itr, nIndex);
211 0 : DesktopDisplayDevice * pWindow = itr->second;
212 0 : if (!pWindow) {
213 0 : return -1;
214 : }
215 :
216 0 : windowDevice = (*pWindow);
217 0 : return 0;
218 : }
219 :
220 0 : int32_t DesktopDeviceInfoImpl::getApplicationCount() {
221 0 : return desktop_application_list_.size();
222 : }
223 :
224 0 : int32_t DesktopDeviceInfoImpl::getApplicationInfo(int32_t nIndex,
225 : DesktopApplication & desktopApplication) {
226 0 : if(nIndex < 0 || nIndex >= desktop_application_list_.size()) {
227 0 : return -1;
228 : }
229 :
230 0 : std::map<intptr_t,DesktopApplication*>::iterator iter = desktop_application_list_.begin();
231 0 : std::advance (iter, nIndex);
232 0 : DesktopApplication * pDesktopApplication = iter->second;
233 0 : if (pDesktopApplication) {
234 0 : desktopApplication = (*pDesktopApplication);
235 : }
236 :
237 0 : return 0;
238 : }
239 :
240 0 : void DesktopDeviceInfoImpl::CleanUp() {
241 0 : CleanUpScreenList();
242 0 : CleanUpWindowList();
243 0 : CleanUpApplicationList();
244 0 : }
245 0 : int32_t DesktopDeviceInfoImpl::Init() {
246 0 : InitializeScreenList();
247 0 : InitializeWindowList();
248 0 : InitializeApplicationList();
249 :
250 0 : return 0;
251 : }
252 0 : int32_t DesktopDeviceInfoImpl::Refresh() {
253 0 : RefreshScreenList();
254 0 : RefreshWindowList();
255 0 : RefreshApplicationList();
256 :
257 0 : return 0;
258 : }
259 :
260 0 : void DesktopDeviceInfoImpl::CleanUpWindowList() {
261 0 : std::map<intptr_t, DesktopDisplayDevice *>::iterator iterWindow;
262 0 : for (iterWindow = desktop_window_list_.begin(); iterWindow != desktop_window_list_.end(); iterWindow++) {
263 0 : DesktopDisplayDevice *pWindow = iterWindow->second;
264 0 : delete pWindow;
265 0 : iterWindow->second = NULL;
266 : }
267 0 : desktop_window_list_.clear();
268 0 : }
269 0 : void DesktopDeviceInfoImpl::InitializeWindowList() {
270 0 : std::unique_ptr<DesktopCapturer> pWinCap = DesktopCapturer::CreateWindowCapturer(DesktopCaptureOptions::CreateDefault());
271 0 : DesktopCapturer::SourceList list;
272 0 : if (pWinCap && pWinCap->GetSourceList(&list)) {
273 0 : DesktopCapturer::SourceList::iterator itr;
274 0 : for (itr = list.begin(); itr != list.end(); itr++) {
275 0 : DesktopDisplayDevice *pWinDevice = new DesktopDisplayDevice;
276 0 : if (!pWinDevice) {
277 0 : continue;
278 : }
279 :
280 0 : pWinDevice->setScreenId(itr->id);
281 0 : pWinDevice->setDeviceName(itr->title.c_str());
282 0 : pWinDevice->setPid(itr->pid);
283 :
284 : char idStr[BUFSIZ];
285 : #if WEBRTC_WIN
286 : _snprintf_s(idStr, sizeof(idStr), sizeof(idStr) - 1, "%ld", pWinDevice->getScreenId());
287 : #else
288 0 : snprintf(idStr, sizeof(idStr), "%ld", pWinDevice->getScreenId());
289 : #endif
290 0 : pWinDevice->setUniqueIdName(idStr);
291 0 : desktop_window_list_[pWinDevice->getScreenId()] = pWinDevice;
292 : }
293 : }
294 0 : }
295 0 : void DesktopDeviceInfoImpl::RefreshWindowList() {
296 0 : CleanUpWindowList();
297 0 : InitializeWindowList();
298 0 : }
299 :
300 0 : void DesktopDeviceInfoImpl::CleanUpApplicationList() {
301 0 : std::map<intptr_t,DesktopApplication*>::iterator iterApp;
302 0 : for (iterApp = desktop_application_list_.begin(); iterApp != desktop_application_list_.end(); iterApp++){
303 0 : DesktopApplication *pDesktopApplication = iterApp->second;
304 0 : delete pDesktopApplication;
305 0 : iterApp->second = NULL;
306 : }
307 0 : desktop_application_list_.clear();
308 0 : }
309 0 : void DesktopDeviceInfoImpl::RefreshApplicationList() {
310 0 : CleanUpApplicationList();
311 0 : InitializeApplicationList();
312 0 : }
313 :
314 0 : void DesktopDeviceInfoImpl::CleanUpScreenList() {
315 0 : std::map<intptr_t,DesktopDisplayDevice*>::iterator iterDevice;
316 0 : for (iterDevice=desktop_display_list_.begin(); iterDevice != desktop_display_list_.end(); iterDevice++){
317 0 : DesktopDisplayDevice *pDesktopDisplayDevice = iterDevice->second;
318 0 : delete pDesktopDisplayDevice;
319 0 : iterDevice->second = NULL;
320 : }
321 0 : desktop_display_list_.clear();
322 0 : }
323 0 : void DesktopDeviceInfoImpl::RefreshScreenList() {
324 0 : CleanUpScreenList();
325 0 : InitializeScreenList();
326 0 : }
327 : }
|