blob: 747817ad8cb8167a4649a8b6cf021ea84bd18292 [file] [log] [blame]
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +00001/* Copyright (C) 2004-2007 Constantin Kaplinsky. All Rights Reserved.
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +00002 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// PollingManager.cxx
20//
21
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000022#include <stdio.h>
23#include <string.h>
24#include <time.h>
25#include <X11/Xlib.h>
26#include <rfb/LogWriter.h>
27#include <rfb/VNCServer.h>
28#include <rfb/Configuration.h>
29#include <rfb/ServerCore.h>
30
31#include <x0vncserver/PollingManager.h>
32
33static LogWriter vlog("PollingMgr");
34
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000035const int PollingManager::m_pollingOrder[32] = {
36 0, 16, 8, 24, 4, 20, 12, 28,
37 10, 26, 18, 2, 22, 6, 30, 14,
38 1, 17, 9, 25, 7, 23, 15, 31,
39 19, 3, 27, 11, 29, 13, 5, 21
40};
41
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +000042// FIXME: Check that the parameter's value is in the allowed range.
43// This applies to all other parameters as well.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000044IntParameter PollingManager::m_videoPriority("VideoPriority",
45 "Priority of sending updates for video area (0..8)", 2);
46
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000047//
48// Constructor.
49//
50// Note that dpy and image should remain valid during the object
51// lifetime, while factory is used only in the constructor itself.
52//
Constantin Kaplinsky936c3692007-12-27 08:42:53 +000053// FIXME: Pass XPixelBuffer* instead of Image*.
54//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000055
56PollingManager::PollingManager(Display *dpy, Image *image,
57 ImageFactory *factory,
58 int offsetLeft, int offsetTop)
59 : m_dpy(dpy), m_server(0), m_image(image),
60 m_offsetLeft(offsetLeft), m_offsetTop(offsetTop),
Constantin Kaplinsky1d378022007-12-25 17:43:09 +000061 m_numVideoPasses(0),
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +000062 m_pollingStep(0)
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000063{
64 // Save width and height of the screen (and the image).
65 m_width = m_image->xim->width;
66 m_height = m_image->xim->height;
67
68 // Compute width and height in 32x32 tiles.
69 m_widthTiles = (m_width + 31) / 32;
70 m_heightTiles = (m_height + 31) / 32;
71
72 // Get initial screen image.
73 m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop);
74
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +000075 // Create additional images used in polling algorithm, warn if
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000076 // underlying class names are different from the class name of the
77 // primary image.
78 m_rowImage = factory->newImage(m_dpy, m_width, 1);
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +000079 m_columnImage = factory->newImage(m_dpy, 1, m_height);
80 const char *primaryImgClass = m_image->className();
81 const char *rowImgClass = m_rowImage->className();
82 const char *columnImgClass = m_columnImage->className();
83 if (strcmp(rowImgClass, primaryImgClass) != 0 ||
84 strcmp(columnImgClass, primaryImgClass) != 0) {
85 vlog.error("Image types do not match (%s, %s, %s)",
86 primaryImgClass, rowImgClass, columnImgClass);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000087 }
88
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000089 int numTiles = m_widthTiles * m_heightTiles;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000090 m_rateMatrix = new char[numTiles];
91 m_videoFlags = new char[numTiles];
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000092 memset(m_rateMatrix, 0, numTiles);
93 memset(m_videoFlags, 0, numTiles);
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000094}
95
96PollingManager::~PollingManager()
97{
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +000098 delete[] m_videoFlags;
99 delete[] m_rateMatrix;
100
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000101 delete m_rowImage;
Constantin Kaplinskyed3cf5d2007-12-28 17:59:10 +0000102 delete m_columnImage;
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000103}
104
105//
106// Register VNCServer object.
107//
108
109void PollingManager::setVNCServer(VNCServer *s)
110{
111 m_server = s;
112}
113
114//
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000115// DEBUG: Measuring time spent in the poll() function,
116// as well as time intervals between poll() calls.
117//
118
119#ifdef DEBUG
120void PollingManager::debugBeforePoll()
121{
122 TimeMillis timeNow;
123 int diff = timeNow.diffFrom(m_timeSaved);
124 fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32);
125 m_timeSaved = timeNow;
126}
127
128void PollingManager::debugAfterPoll()
129{
130 TimeMillis timeNow;
131 int diff = timeNow.diffFrom(m_timeSaved);
132 fprintf(stderr, "[poll%4dms]\n", diff);
133 m_timeSaved = timeNow;
134}
135
136#endif
137
138//
139// Search for changed rectangles on the screen.
140//
141
142void PollingManager::poll()
143{
144#ifdef DEBUG
145 debugBeforePoll();
146#endif
147
Constantin Kaplinskyd0b15c62007-10-09 08:15:25 +0000148 // Perform polling and try update clients if changes were detected.
149 if (pollScreen())
Constantin Kaplinskyb30ae7f2006-05-25 05:04:46 +0000150 m_server->tryUpdate();
151
152#ifdef DEBUG
153 debugAfterPoll();
154#endif
155}
156
Constantin Kaplinsky9ee8dc62007-10-09 07:46:32 +0000157bool PollingManager::pollScreen()
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000158{
159 if (!m_server)
160 return false;
161
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000162 // If video data should have higher priority, and video area was
163 // detected, perform special passes to send video data only. Such
164 // "video passes" will be performed between normal polling passes.
165 // No actual polling is performed in a video pass since we know that
166 // video is changing continuously.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000167 //
168 // FIXME: Should we move this block into a separate function?
169 // FIXME: Giving higher priority to video area lengthens video
170 // detection cycles. Should we do something with that?
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000171 if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) {
172 if (m_numVideoPasses > 0) {
173 m_numVideoPasses--;
174 getScreenRect(m_videoRect);
175 return true; // we've got changes
176 } else {
177 // Normal pass now, but schedule video passes for next calls.
178 m_numVideoPasses = (int)m_videoPriority - 1;
179 }
180 }
181
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000182 // changeFlags[] array will hold boolean values corresponding to
183 // each 32x32 tile. If a value is true, then we've detected a change
184 // in that tile. Initially, we fill in the array with zero values.
Constantin Kaplinsky04e910b2007-12-25 18:10:35 +0000185 //
186 // FIXME: Should we use a member variable in place of changeFlags?
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000187 bool *changeFlags = new bool[m_widthTiles * m_heightTiles];
188 memset(changeFlags, 0, m_widthTiles * m_heightTiles * sizeof(bool));
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000189
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000190 // First pass over the framebuffer. Here we scan 1/32 part of the
191 // framebuffer -- that is, one line in each (32 * m_width) stripe.
192 // We compare the pixels of that line with previous framebuffer
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000193 // contents and raise corresponding member values of changeFlags[].
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000194 int scanOffset = m_pollingOrder[m_pollingStep++ % 32];
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000195 bool *pChangeFlags = changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000196 int nTilesChanged = 0;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000197 for (int y = scanOffset; y < m_height; y += 32) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000198 nTilesChanged += checkRow(0, y, m_width, pChangeFlags);
199 pChangeFlags += m_widthTiles;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000200 }
201
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000202#ifdef DEBUG_REPORT_CHANGED_TILES
203 printChanges("After 1st pass", changeFlags);
204#endif
205
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000206 // Do the work related to video area detection, if enabled.
Constantin Kaplinsky1d378022007-12-25 17:43:09 +0000207 bool haveVideoRect = false;
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000208 if ((int)m_videoPriority != 0) {
209 handleVideo(changeFlags);
210 if (!m_videoRect.is_empty()) {
211 getScreenRect(m_videoRect);
212 haveVideoRect = true;
213 }
214 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000215
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000216 // If some changes have been detected:
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000217 if (nTilesChanged) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000218 // Try to find more changes around. Before doing that, mark the
219 // video area as changed, to skip comparisons of its pixels.
220 flagVideoArea(changeFlags, true);
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000221#ifdef DEBUG_REPORT_CHANGED_TILES
222 printChanges("Before checking neighbors", changeFlags);
223#endif
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000224 checkNeighbors(changeFlags);
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000225#ifdef DEBUG_REPORT_CHANGED_TILES
226 printChanges("After checking neighbors", changeFlags);
227#endif
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000228
229 // Inform the server about the changes. This time, we mark the
230 // video area as NOT changed, to prevent reading its pixels again.
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000231 flagVideoArea(changeFlags, false);
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000232#ifdef DEBUG_REPORT_CHANGED_TILES
233 printChanges("Before sending", changeFlags);
234#endif
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000235 nTilesChanged = sendChanges(changeFlags);
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000236 }
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000237
238 // Cleanup.
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000239 delete[] changeFlags;
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000240
Constantin Kaplinsky52f29d32007-12-28 18:30:54 +0000241#ifdef DEBUG_PRINT_NUM_CHANGED_TILES
242 printf("%3d ", nTilesChanged);
243 if (m_pollingStep % 32 == 0) {
244 printf("\n");
245 }
246#endif
247
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000248#ifdef DEBUG
249 if (nTilesChanged != 0) {
250 fprintf(stderr, "#%d# ", nTilesChanged);
251 }
252#endif
253
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000254 return (nTilesChanged != 0 || haveVideoRect);
Constantin Kaplinskya119b482007-10-04 06:02:02 +0000255}
256
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000257int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags)
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000258{
259 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
260 int bytesPerLine = m_image->xim->bytes_per_line;
261
Constantin Kaplinsky29d32052007-10-08 14:16:02 +0000262 if (x == 0 && w == m_width) {
263 getFullRow(y); // use more efficient method if possible
264 } else {
265 getRow(x, y, w);
266 }
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000267
268 char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel;
269 char *ptr_new = m_rowImage->xim->data;
270
271 int nTilesChanged = 0;
272 for (int i = 0; i < (w + 31) / 32; i++) {
273 int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32;
274 int nBytes = tile_w * bytesPerPixel;
275 if (memcmp(ptr_old, ptr_new, nBytes)) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000276 *pChangeFlags = true;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000277 nTilesChanged++;
278 }
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000279 pChangeFlags++;
Constantin Kaplinskybc6b9e22007-10-04 11:43:41 +0000280 ptr_old += nBytes;
281 ptr_new += nBytes;
282 }
283
284 return nTilesChanged;
285}
286
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000287int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags)
288{
289 int bytesPerPixel = m_image->xim->bits_per_pixel / 8;
290
291 getColumn(x, y, h);
292
293 int nTilesChanged = 0;
294 for (int nTile = 0; nTile < (h + 31) / 32; nTile++) {
295 if (!*pChangeFlags) {
296 int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32;
297 for (int i = 0; i < tile_h; i++) {
298 // FIXME: Provide an inline function Image::locatePixel(x, y).
299 // FIXME: Do not compute these pointers in the inner cycle.
300 char *ptr_old = (m_image->xim->data +
301 (y + nTile * 32 + i) * m_image->xim->bytes_per_line +
302 x * bytesPerPixel);
303 char *ptr_new = (m_columnImage->xim->data +
304 (nTile * 32 + i) * m_columnImage->xim->bytes_per_line);
305 if (memcmp(ptr_old, ptr_new, bytesPerPixel)) {
306 *pChangeFlags = true;
307 nTilesChanged++;
308 break;
309 }
310 }
311 }
312 pChangeFlags += m_widthTiles;
313 }
314
315 return nTilesChanged;
316}
317
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000318int PollingManager::sendChanges(const bool *pChangeFlags)
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000319{
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000320 int nTilesChanged = 0;
321
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000322 Rect rect;
323 for (int y = 0; y < m_heightTiles; y++) {
324 for (int x = 0; x < m_widthTiles; x++) {
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000325 if (*pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000326 // Count successive tiles marked as changed.
327 int count = 1;
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000328 while (x + count < m_widthTiles && *pChangeFlags++) {
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000329 count++;
330 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000331 nTilesChanged += count;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000332 // Compute the coordinates and the size of this band.
333 rect.setXYWH(x * 32, y * 32, count * 32, 32);
334 if (rect.br.x > m_width)
335 rect.br.x = m_width;
336 if (rect.br.y > m_height)
337 rect.br.y = m_height;
338 // Add to the changed region maintained by the server.
339 getScreenRect(rect);
340 m_server->add_changed(rect);
341 // Skip processed tiles.
342 x += count;
343 }
344 }
345 }
Constantin Kaplinsky1a032112008-01-09 10:22:42 +0000346 return nTilesChanged;
Constantin Kaplinskya79255b2007-10-07 13:03:55 +0000347}
348
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000349void PollingManager::handleVideo(const bool *pChangeFlags)
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000350{
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000351 // Update counters in m_rateMatrix.
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000352 int numTiles = m_heightTiles * m_widthTiles;
353 for (int i = 0; i < numTiles; i++)
Constantin Kaplinsky808db552007-10-09 12:48:26 +0000354 m_rateMatrix[i] += (pChangeFlags[i] != false);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000355
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000356 // Once per eight calls: detect video rectangle by examining
357 // m_rateMatrix[], then reset counters in m_rateMatrix[].
358 if (m_pollingStep % 8 == 0) {
359 detectVideo();
360 memset(m_rateMatrix, 0, numTiles);
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000361 }
Constantin Kaplinskyc1984e02007-10-08 14:54:18 +0000362}
363
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000364void PollingManager::flagVideoArea(bool *pChangeFlags, bool value)
365{
Constantin Kaplinsky48792632007-12-28 18:21:42 +0000366 if (m_videoRect.is_empty())
367 return;
368
Constantin Kaplinskybd390352007-12-28 08:44:59 +0000369 Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32,
370 m_videoRect.br.x / 32, m_videoRect.br.y / 32);
371
372 for (int y = r.tl.y; y < r.br.y; y++)
373 for (int x = r.tl.x; x < r.br.x; x++)
374 pChangeFlags[y * m_widthTiles + x] = value;
375}
376
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000377void
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000378PollingManager::checkNeighbors(bool *pChangeFlags)
379{
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000380 int x, y;
381
382 // Check neighboring pixels above and below changed tiles.
383 // FIXME: Fast skip to the first changed tile (and to the last, too).
384 // FIXME: Check the full-width line above the first changed tile?
385 for (y = 0; y < m_heightTiles; y++) {
386 bool doneAbove = false;
387 bool doneBelow = false;
388 for (x = 0; x < m_widthTiles; x++) {
389 if (!doneAbove && y > 0 &&
390 pChangeFlags[y * m_widthTiles + x] &&
391 !pChangeFlags[(y - 1) * m_widthTiles + x]) {
392 // FIXME: Check pChangeFlags[] to decrease height of the row.
393 checkRow(x * 32, y * 32 - 1, m_width - x * 32,
394 &pChangeFlags[(y - 1) * m_widthTiles + x]);
395 doneAbove = true;
396 }
397 if (!doneBelow && y < m_heightTiles - 1 &&
398 pChangeFlags[y * m_widthTiles + x] &&
399 !pChangeFlags[(y + 1) * m_widthTiles + x]) {
400 // FIXME: Check pChangeFlags[] to decrease height of the row.
401 checkRow(x * 32, (y + 1) * 32, m_width - x * 32,
402 &pChangeFlags[(y + 1) * m_widthTiles + x]);
403 doneBelow = true;
404 }
405 if (doneBelow && doneAbove)
406 break;
407 }
408 }
409
410 // Check neighboring pixels at the right side of changed tiles.
411 for (x = 0; x < m_widthTiles - 1; x++) {
412 for (y = 0; y < m_heightTiles; y++) {
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000413 if (pChangeFlags[y * m_widthTiles + x] &&
414 !pChangeFlags[y * m_widthTiles + x + 1]) {
415 // FIXME: Check pChangeFlags[] to decrease height of the column.
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000416 checkColumn((x + 1) * 32, y * 32, m_height - y * 32,
417 &pChangeFlags[y * m_widthTiles + x + 1]);
418 break;
419 }
420 }
421 }
Constantin Kaplinsky474b12f2008-01-09 10:25:34 +0000422
423 // Check neighboring pixels at the left side of changed tiles.
424 for (x = m_widthTiles - 1; x > 0; x--) {
425 for (y = 0; y < m_heightTiles; y++) {
426 if (pChangeFlags[y * m_widthTiles + x] &&
427 !pChangeFlags[y * m_widthTiles + x - 1]) {
428 // FIXME: Check pChangeFlags[] to decrease height of the column.
429 checkColumn(x * 32 - 1, y * 32, m_height - y * 32,
430 &pChangeFlags[y * m_widthTiles + x - 1]);
431 break;
432 }
433 }
434 }
Constantin Kaplinsky553340c2007-12-28 18:37:04 +0000435}
436
437void
Constantin Kaplinskyf50bd7f2008-01-10 15:27:42 +0000438PollingManager::printChanges(const char *header, const bool *pChangeFlags)
439{
440 fprintf(stderr, "%s:", header);
441
442 for (int y = 0; y < m_heightTiles; y++) {
443 for (int x = 0; x < m_widthTiles; x++) {
444 if (*pChangeFlags++) {
445 // Count successive tiles marked as changed.
446 int count = 1;
447 while (x + count < m_widthTiles && *pChangeFlags++) {
448 count++;
449 }
450 // Print.
451 fprintf(stderr, " (%d,%d)*%d", x, y, count);
452 // Skip processed tiles.
453 x += count;
454 }
455 }
456 }
457
458 fprintf(stderr, "\n");
459}
460
461void
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000462PollingManager::detectVideo()
463{
464 // Configurable parameters.
465 const int VIDEO_THRESHOLD_0 = 3;
466 const int VIDEO_THRESHOLD_1 = 5;
467
Constantin Kaplinskybb563772007-12-25 11:25:07 +0000468 // In m_rateMatrix, clear counters corresponding to non-32x32 tiles.
469 // This will guarantee that the size of the video area is always a
470 // multiple of 32 pixels. This is important for hardware JPEG encoders.
471 int numTiles = m_heightTiles * m_widthTiles;
472 if (m_width % 32 != 0) {
473 for (int n = m_widthTiles - 1; n < numTiles; n += m_widthTiles)
474 m_rateMatrix[n] = 0;
475 }
476 if (m_height % 32 != 0) {
477 for (int n = numTiles - m_widthTiles; n < numTiles; n++)
478 m_rateMatrix[n] = 0;
479 }
480
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000481 // First, detect candidate region that looks like video. In other
482 // words, find a region that consists of continuously changing
483 // pixels. Save the result in m_videoFlags[].
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000484 for (int i = 0; i < numTiles; i++) {
485 if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) {
486 m_videoFlags[i] = 0;
487 } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) {
488 m_videoFlags[i] = 1;
489 }
490 }
491
492 // Now, choose the biggest rectangle from that candidate region.
493 Rect newRect;
494 getVideoAreaRect(&newRect);
495
496 // Does new rectangle differ from the previously detected one?
497 // If it does, save new rectangle and inform the server.
498 if (!newRect.equals(m_videoRect)) {
499 if (newRect.is_empty()) {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000500 vlog.debug("No video detected");
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000501 } else {
Constantin Kaplinskydab9a562007-10-10 01:33:39 +0000502 vlog.debug("Detected video %dx%d at (%d,%d)",
503 newRect.width(), newRect.height(),
504 newRect.tl.x, newRect.tl.y);
Constantin Kaplinsky6bb4bf12007-10-09 12:03:15 +0000505 }
506 m_videoRect = newRect;
507 m_server->set_video_area(newRect);
508 }
509}
510
511void
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000512PollingManager::getVideoAreaRect(Rect *result)
513{
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000514 int *mx_hlen, *mx_vlen;
515 constructLengthMatrices(&mx_hlen, &mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000516
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000517 int full_h = m_heightTiles;
518 int full_w = m_widthTiles;
519 int x, y;
520 Rect max_rect(0, 0, 0, 0);
521 Rect local_rect;
522
523 for (y = 0; y < full_h; y++) {
524 for (x = 0; x < full_w; x++) {
525 int max_w = mx_hlen[y * full_w + x];
526 int max_h = mx_vlen[y * full_w + x];
527 if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) {
528 local_rect.tl.x = x;
529 local_rect.tl.y = y;
530 findMaxLocalRect(&local_rect, mx_hlen, mx_vlen);
531 if (local_rect.area() > max_rect.area()) {
532 max_rect = local_rect;
533 }
534 }
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000535 }
536 }
537
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000538 destroyLengthMatrices(mx_hlen, mx_vlen);
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000539
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000540 max_rect.tl.x *= 32;
541 max_rect.tl.y *= 32;
542 max_rect.br.x *= 32;
543 max_rect.br.y *= 32;
544 if (max_rect.br.x > m_width)
545 max_rect.br.x = m_width;
546 if (max_rect.br.y > m_height)
547 max_rect.br.y = m_height;
548 *result = max_rect;
Constantin Kaplinsky56649982007-09-04 09:15:35 +0000549}
Constantin Kaplinsky0fc9f172007-09-29 04:00:02 +0000550
551void
552PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v)
553{
554 // Handy shortcuts.
555 int h = m_heightTiles;
556 int w = m_widthTiles;
557
558 // Allocate memory.
559 int *mx_h = new int[h * w];
560 memset(mx_h, 0, h * w * sizeof(int));
561 int *mx_v = new int[h * w];
562 memset(mx_v, 0, h * w * sizeof(int));
563
564 int x, y, len, i;
565
566 // Fill in horizontal length matrix.
567 for (y = 0; y < h; y++) {
568 for (x = 0; x < w; x++) {
569 len = 0;
570 while (x + len < w && m_videoFlags[y * w + x + len]) {
571 len++;
572 }
573 for (i = 0; i < len; i++) {
574 mx_h[y * w + x + i] = len - i;
575 }
576 x += len;
577 }
578 }
579
580 // Fill in vertical length matrix.
581 for (x = 0; x < w; x++) {
582 for (y = 0; y < h; y++) {
583 len = 0;
584 while (y + len < h && m_videoFlags[(y + len) * w + x]) {
585 len++;
586 }
587 for (i = 0; i < len; i++) {
588 mx_v[(y + i) * w + x] = len - i;
589 }
590 y += len;
591 }
592 }
593
594 *pmx_h = mx_h;
595 *pmx_v = mx_v;
596}
597
598void
599PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v)
600{
601 delete[] mx_h;
602 delete[] mx_v;
603}
604
605// NOTE: This function assumes that current tile has non-zero in mx_h[],
606// otherwise we get division by zero.
607void
608PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[])
609{
610 int idx = r->tl.y * m_widthTiles + r->tl.x;
611
612 // NOTE: Rectangle's maximum width and height are 25 and 18
613 // (in tiles, where each tile is usually 32x32 pixels).
614 int max_w = mx_h[idx];
615 if (max_w > 25)
616 max_w = 25;
617 int cur_h = 18;
618
619 int best_w = max_w;
620 int best_area = 1 * best_w;
621
622 for (int i = 0; i < max_w; i++) {
623 int h = mx_v[idx + i];
624 if (h < cur_h) {
625 cur_h = h;
626 if (cur_h * max_w <= best_area)
627 break;
628 }
629 if (cur_h * (i + 1) > best_area) {
630 best_w = i + 1;
631 best_area = cur_h * best_w;
632 }
633 }
634
635 r->br.x = r->tl.x + best_w;
636 r->br.y = r->tl.y + best_area / best_w;
637}
638