Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2004-2007 Constantin Kaplinsky. All Rights Reserved. |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 2 | * |
| 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 Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 22 | #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 | |
| 33 | static LogWriter vlog("PollingMgr"); |
| 34 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 35 | const 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 Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 42 | // FIXME: Check that the parameter's value is in the allowed range. |
| 43 | // This applies to all other parameters as well. |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 44 | IntParameter PollingManager::m_videoPriority("VideoPriority", |
| 45 | "Priority of sending updates for video area (0..8)", 2); |
| 46 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 47 | // |
| 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 Kaplinsky | 936c369 | 2007-12-27 08:42:53 +0000 | [diff] [blame] | 53 | // FIXME: Pass XPixelBuffer* instead of Image*. |
| 54 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 55 | |
| 56 | PollingManager::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 Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 61 | m_numVideoPasses(0), |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 62 | m_pollingStep(0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 63 | { |
| 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 Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 75 | // Create additional images used in polling algorithm, warn if |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 76 | // 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 Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 79 | 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 Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 89 | int numTiles = m_widthTiles * m_heightTiles; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 90 | m_rateMatrix = new char[numTiles]; |
| 91 | m_videoFlags = new char[numTiles]; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 92 | memset(m_rateMatrix, 0, numTiles); |
| 93 | memset(m_videoFlags, 0, numTiles); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | PollingManager::~PollingManager() |
| 97 | { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 98 | delete[] m_videoFlags; |
| 99 | delete[] m_rateMatrix; |
| 100 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 101 | delete m_rowImage; |
Constantin Kaplinsky | ed3cf5d | 2007-12-28 17:59:10 +0000 | [diff] [blame] | 102 | delete m_columnImage; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // |
| 106 | // Register VNCServer object. |
| 107 | // |
| 108 | |
| 109 | void PollingManager::setVNCServer(VNCServer *s) |
| 110 | { |
| 111 | m_server = s; |
| 112 | } |
| 113 | |
| 114 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 115 | // DEBUG: Measuring time spent in the poll() function, |
| 116 | // as well as time intervals between poll() calls. |
| 117 | // |
| 118 | |
| 119 | #ifdef DEBUG |
| 120 | void 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 | |
| 128 | void 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 | |
| 142 | void PollingManager::poll() |
| 143 | { |
| 144 | #ifdef DEBUG |
| 145 | debugBeforePoll(); |
| 146 | #endif |
| 147 | |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 148 | // Perform polling and try update clients if changes were detected. |
| 149 | if (pollScreen()) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 150 | m_server->tryUpdate(); |
| 151 | |
| 152 | #ifdef DEBUG |
| 153 | debugAfterPoll(); |
| 154 | #endif |
| 155 | } |
| 156 | |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 157 | bool PollingManager::pollScreen() |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 158 | { |
| 159 | if (!m_server) |
| 160 | return false; |
| 161 | |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 162 | // 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 Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 167 | // |
| 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 Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 171 | 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 Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 182 | // 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 Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame] | 185 | // |
| 186 | // FIXME: Should we use a member variable in place of changeFlags? |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 187 | bool *changeFlags = new bool[m_widthTiles * m_heightTiles]; |
| 188 | memset(changeFlags, 0, m_widthTiles * m_heightTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 189 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 190 | // 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 Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 193 | // contents and raise corresponding member values of changeFlags[]. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 194 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 195 | bool *pChangeFlags = changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 196 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 197 | for (int y = scanOffset; y < m_height; y += 32) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 198 | nTilesChanged += checkRow(0, y, m_width, pChangeFlags); |
| 199 | pChangeFlags += m_widthTiles; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 202 | // Do the work related to video area detection, if enabled. |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 203 | bool haveVideoRect = false; |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 204 | if ((int)m_videoPriority != 0) { |
| 205 | handleVideo(changeFlags); |
| 206 | if (!m_videoRect.is_empty()) { |
| 207 | getScreenRect(m_videoRect); |
| 208 | haveVideoRect = true; |
| 209 | } |
| 210 | } |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 211 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 212 | // If some changes have been detected: |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 213 | if (nTilesChanged) { |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 214 | // Try to find more changes around. Before doing that, mark the |
| 215 | // video area as changed, to skip comparisons of its pixels. |
| 216 | flagVideoArea(changeFlags, true); |
| 217 | checkNeighbors(changeFlags); |
| 218 | |
| 219 | // Inform the server about the changes. This time, we mark the |
| 220 | // video area as NOT changed, to prevent reading its pixels again. |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 221 | flagVideoArea(changeFlags, false); |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 222 | sendChanges(changeFlags); |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 223 | } |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 224 | |
| 225 | // Cleanup. |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 226 | delete[] changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 227 | |
Constantin Kaplinsky | 52f29d3 | 2007-12-28 18:30:54 +0000 | [diff] [blame] | 228 | #ifdef DEBUG_PRINT_NUM_CHANGED_TILES |
| 229 | printf("%3d ", nTilesChanged); |
| 230 | if (m_pollingStep % 32 == 0) { |
| 231 | printf("\n"); |
| 232 | } |
| 233 | #endif |
| 234 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 235 | #ifdef DEBUG |
Constantin Kaplinsky | 52f29d3 | 2007-12-28 18:30:54 +0000 | [diff] [blame] | 236 | // FIXME: Move this to sendChanges(); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 237 | if (nTilesChanged != 0) { |
| 238 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 239 | } |
| 240 | #endif |
| 241 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 242 | return (nTilesChanged != 0 || haveVideoRect); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 245 | int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags) |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 246 | { |
| 247 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 248 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 249 | |
Constantin Kaplinsky | 29d3205 | 2007-10-08 14:16:02 +0000 | [diff] [blame] | 250 | if (x == 0 && w == m_width) { |
| 251 | getFullRow(y); // use more efficient method if possible |
| 252 | } else { |
| 253 | getRow(x, y, w); |
| 254 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 255 | |
| 256 | char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel; |
| 257 | char *ptr_new = m_rowImage->xim->data; |
| 258 | |
| 259 | int nTilesChanged = 0; |
| 260 | for (int i = 0; i < (w + 31) / 32; i++) { |
| 261 | int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32; |
| 262 | int nBytes = tile_w * bytesPerPixel; |
| 263 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 264 | *pChangeFlags = true; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 265 | nTilesChanged++; |
| 266 | } |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 267 | pChangeFlags++; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 268 | ptr_old += nBytes; |
| 269 | ptr_new += nBytes; |
| 270 | } |
| 271 | |
| 272 | return nTilesChanged; |
| 273 | } |
| 274 | |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 275 | int PollingManager::checkColumn(int x, int y, int h, bool *pChangeFlags) |
| 276 | { |
| 277 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 278 | |
| 279 | getColumn(x, y, h); |
| 280 | |
| 281 | int nTilesChanged = 0; |
| 282 | for (int nTile = 0; nTile < (h + 31) / 32; nTile++) { |
| 283 | if (!*pChangeFlags) { |
| 284 | int tile_h = (h - nTile * 32 >= 32) ? 32 : h - nTile * 32; |
| 285 | for (int i = 0; i < tile_h; i++) { |
| 286 | // FIXME: Provide an inline function Image::locatePixel(x, y). |
| 287 | // FIXME: Do not compute these pointers in the inner cycle. |
| 288 | char *ptr_old = (m_image->xim->data + |
| 289 | (y + nTile * 32 + i) * m_image->xim->bytes_per_line + |
| 290 | x * bytesPerPixel); |
| 291 | char *ptr_new = (m_columnImage->xim->data + |
| 292 | (nTile * 32 + i) * m_columnImage->xim->bytes_per_line); |
| 293 | if (memcmp(ptr_old, ptr_new, bytesPerPixel)) { |
| 294 | *pChangeFlags = true; |
| 295 | nTilesChanged++; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | pChangeFlags += m_widthTiles; |
| 301 | } |
| 302 | |
| 303 | return nTilesChanged; |
| 304 | } |
| 305 | |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 306 | void PollingManager::sendChanges(const bool *pChangeFlags) |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 307 | { |
| 308 | Rect rect; |
| 309 | for (int y = 0; y < m_heightTiles; y++) { |
| 310 | for (int x = 0; x < m_widthTiles; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 311 | if (*pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 312 | // Count successive tiles marked as changed. |
| 313 | int count = 1; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 314 | while (x + count < m_widthTiles && *pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 315 | count++; |
| 316 | } |
| 317 | // Compute the coordinates and the size of this band. |
| 318 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 319 | if (rect.br.x > m_width) |
| 320 | rect.br.x = m_width; |
| 321 | if (rect.br.y > m_height) |
| 322 | rect.br.y = m_height; |
| 323 | // Add to the changed region maintained by the server. |
| 324 | getScreenRect(rect); |
| 325 | m_server->add_changed(rect); |
| 326 | // Skip processed tiles. |
| 327 | x += count; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 333 | void PollingManager::handleVideo(const bool *pChangeFlags) |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 334 | { |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 335 | // Update counters in m_rateMatrix. |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 336 | int numTiles = m_heightTiles * m_widthTiles; |
| 337 | for (int i = 0; i < numTiles; i++) |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 338 | m_rateMatrix[i] += (pChangeFlags[i] != false); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 339 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 340 | // Once per eight calls: detect video rectangle by examining |
| 341 | // m_rateMatrix[], then reset counters in m_rateMatrix[]. |
| 342 | if (m_pollingStep % 8 == 0) { |
| 343 | detectVideo(); |
| 344 | memset(m_rateMatrix, 0, numTiles); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 345 | } |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Constantin Kaplinsky | bd39035 | 2007-12-28 08:44:59 +0000 | [diff] [blame] | 348 | void PollingManager::flagVideoArea(bool *pChangeFlags, bool value) |
| 349 | { |
Constantin Kaplinsky | 4879263 | 2007-12-28 18:21:42 +0000 | [diff] [blame] | 350 | if (m_videoRect.is_empty()) |
| 351 | return; |
| 352 | |
Constantin Kaplinsky | bd39035 | 2007-12-28 08:44:59 +0000 | [diff] [blame] | 353 | Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32, |
| 354 | m_videoRect.br.x / 32, m_videoRect.br.y / 32); |
| 355 | |
| 356 | for (int y = r.tl.y; y < r.br.y; y++) |
| 357 | for (int x = r.tl.x; x < r.br.x; x++) |
| 358 | pChangeFlags[y * m_widthTiles + x] = value; |
| 359 | } |
| 360 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 361 | void |
Constantin Kaplinsky | 553340c | 2007-12-28 18:37:04 +0000 | [diff] [blame] | 362 | PollingManager::checkNeighbors(bool *pChangeFlags) |
| 363 | { |
| 364 | // Check neighboring pixels at the right of changed tiles. |
| 365 | for (int x = 0; x < m_widthTiles - 1; x++) { |
| 366 | for (int y = 0; y < m_heightTiles; y++) { |
| 367 | if (pChangeFlags[y * m_widthTiles + x] && |
| 368 | !pChangeFlags[y * m_widthTiles + x + 1]) { |
| 369 | // FIXME: Check pChangeFlags[] to decrease height of the column. |
| 370 | // FIXME: Check _only_ the tiles neighboring to changed tiles? |
| 371 | checkColumn((x + 1) * 32, y * 32, m_height - y * 32, |
| 372 | &pChangeFlags[y * m_widthTiles + x + 1]); |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 380 | PollingManager::detectVideo() |
| 381 | { |
| 382 | // Configurable parameters. |
| 383 | const int VIDEO_THRESHOLD_0 = 3; |
| 384 | const int VIDEO_THRESHOLD_1 = 5; |
| 385 | |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 386 | // In m_rateMatrix, clear counters corresponding to non-32x32 tiles. |
| 387 | // This will guarantee that the size of the video area is always a |
| 388 | // multiple of 32 pixels. This is important for hardware JPEG encoders. |
| 389 | int numTiles = m_heightTiles * m_widthTiles; |
| 390 | if (m_width % 32 != 0) { |
| 391 | for (int n = m_widthTiles - 1; n < numTiles; n += m_widthTiles) |
| 392 | m_rateMatrix[n] = 0; |
| 393 | } |
| 394 | if (m_height % 32 != 0) { |
| 395 | for (int n = numTiles - m_widthTiles; n < numTiles; n++) |
| 396 | m_rateMatrix[n] = 0; |
| 397 | } |
| 398 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 399 | // First, detect candidate region that looks like video. In other |
| 400 | // words, find a region that consists of continuously changing |
| 401 | // pixels. Save the result in m_videoFlags[]. |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 402 | for (int i = 0; i < numTiles; i++) { |
| 403 | if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) { |
| 404 | m_videoFlags[i] = 0; |
| 405 | } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) { |
| 406 | m_videoFlags[i] = 1; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // Now, choose the biggest rectangle from that candidate region. |
| 411 | Rect newRect; |
| 412 | getVideoAreaRect(&newRect); |
| 413 | |
| 414 | // Does new rectangle differ from the previously detected one? |
| 415 | // If it does, save new rectangle and inform the server. |
| 416 | if (!newRect.equals(m_videoRect)) { |
| 417 | if (newRect.is_empty()) { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 418 | vlog.debug("No video detected"); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 419 | } else { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 420 | vlog.debug("Detected video %dx%d at (%d,%d)", |
| 421 | newRect.width(), newRect.height(), |
| 422 | newRect.tl.x, newRect.tl.y); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 423 | } |
| 424 | m_videoRect = newRect; |
| 425 | m_server->set_video_area(newRect); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 430 | PollingManager::getVideoAreaRect(Rect *result) |
| 431 | { |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 432 | int *mx_hlen, *mx_vlen; |
| 433 | constructLengthMatrices(&mx_hlen, &mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 434 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 435 | int full_h = m_heightTiles; |
| 436 | int full_w = m_widthTiles; |
| 437 | int x, y; |
| 438 | Rect max_rect(0, 0, 0, 0); |
| 439 | Rect local_rect; |
| 440 | |
| 441 | for (y = 0; y < full_h; y++) { |
| 442 | for (x = 0; x < full_w; x++) { |
| 443 | int max_w = mx_hlen[y * full_w + x]; |
| 444 | int max_h = mx_vlen[y * full_w + x]; |
| 445 | if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) { |
| 446 | local_rect.tl.x = x; |
| 447 | local_rect.tl.y = y; |
| 448 | findMaxLocalRect(&local_rect, mx_hlen, mx_vlen); |
| 449 | if (local_rect.area() > max_rect.area()) { |
| 450 | max_rect = local_rect; |
| 451 | } |
| 452 | } |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 456 | destroyLengthMatrices(mx_hlen, mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 457 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 458 | max_rect.tl.x *= 32; |
| 459 | max_rect.tl.y *= 32; |
| 460 | max_rect.br.x *= 32; |
| 461 | max_rect.br.y *= 32; |
| 462 | if (max_rect.br.x > m_width) |
| 463 | max_rect.br.x = m_width; |
| 464 | if (max_rect.br.y > m_height) |
| 465 | max_rect.br.y = m_height; |
| 466 | *result = max_rect; |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 467 | } |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 468 | |
| 469 | void |
| 470 | PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v) |
| 471 | { |
| 472 | // Handy shortcuts. |
| 473 | int h = m_heightTiles; |
| 474 | int w = m_widthTiles; |
| 475 | |
| 476 | // Allocate memory. |
| 477 | int *mx_h = new int[h * w]; |
| 478 | memset(mx_h, 0, h * w * sizeof(int)); |
| 479 | int *mx_v = new int[h * w]; |
| 480 | memset(mx_v, 0, h * w * sizeof(int)); |
| 481 | |
| 482 | int x, y, len, i; |
| 483 | |
| 484 | // Fill in horizontal length matrix. |
| 485 | for (y = 0; y < h; y++) { |
| 486 | for (x = 0; x < w; x++) { |
| 487 | len = 0; |
| 488 | while (x + len < w && m_videoFlags[y * w + x + len]) { |
| 489 | len++; |
| 490 | } |
| 491 | for (i = 0; i < len; i++) { |
| 492 | mx_h[y * w + x + i] = len - i; |
| 493 | } |
| 494 | x += len; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | // Fill in vertical length matrix. |
| 499 | for (x = 0; x < w; x++) { |
| 500 | for (y = 0; y < h; y++) { |
| 501 | len = 0; |
| 502 | while (y + len < h && m_videoFlags[(y + len) * w + x]) { |
| 503 | len++; |
| 504 | } |
| 505 | for (i = 0; i < len; i++) { |
| 506 | mx_v[(y + i) * w + x] = len - i; |
| 507 | } |
| 508 | y += len; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | *pmx_h = mx_h; |
| 513 | *pmx_v = mx_v; |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v) |
| 518 | { |
| 519 | delete[] mx_h; |
| 520 | delete[] mx_v; |
| 521 | } |
| 522 | |
| 523 | // NOTE: This function assumes that current tile has non-zero in mx_h[], |
| 524 | // otherwise we get division by zero. |
| 525 | void |
| 526 | PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[]) |
| 527 | { |
| 528 | int idx = r->tl.y * m_widthTiles + r->tl.x; |
| 529 | |
| 530 | // NOTE: Rectangle's maximum width and height are 25 and 18 |
| 531 | // (in tiles, where each tile is usually 32x32 pixels). |
| 532 | int max_w = mx_h[idx]; |
| 533 | if (max_w > 25) |
| 534 | max_w = 25; |
| 535 | int cur_h = 18; |
| 536 | |
| 537 | int best_w = max_w; |
| 538 | int best_area = 1 * best_w; |
| 539 | |
| 540 | for (int i = 0; i < max_w; i++) { |
| 541 | int h = mx_v[idx + i]; |
| 542 | if (h < cur_h) { |
| 543 | cur_h = h; |
| 544 | if (cur_h * max_w <= best_area) |
| 545 | break; |
| 546 | } |
| 547 | if (cur_h * (i + 1) > best_area) { |
| 548 | best_w = i + 1; |
| 549 | best_area = cur_h * best_w; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | r->br.x = r->tl.x + best_w; |
| 554 | r->br.y = r->tl.y + best_area / best_w; |
| 555 | } |
| 556 | |