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 | // |
| 53 | |
| 54 | PollingManager::PollingManager(Display *dpy, Image *image, |
| 55 | ImageFactory *factory, |
| 56 | int offsetLeft, int offsetTop) |
| 57 | : m_dpy(dpy), m_server(0), m_image(image), |
| 58 | m_offsetLeft(offsetLeft), m_offsetTop(offsetTop), |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 59 | m_numVideoPasses(0), |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 60 | m_pollingStep(0) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 61 | { |
| 62 | // Save width and height of the screen (and the image). |
| 63 | m_width = m_image->xim->width; |
| 64 | m_height = m_image->xim->height; |
| 65 | |
| 66 | // Compute width and height in 32x32 tiles. |
| 67 | m_widthTiles = (m_width + 31) / 32; |
| 68 | m_heightTiles = (m_height + 31) / 32; |
| 69 | |
| 70 | // Get initial screen image. |
| 71 | m_image->get(DefaultRootWindow(m_dpy), m_offsetLeft, m_offsetTop); |
| 72 | |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 73 | // Create additional images used in polling algorithm, warn if |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 74 | // underlying class names are different from the class name of the |
| 75 | // primary image. |
| 76 | m_rowImage = factory->newImage(m_dpy, m_width, 1); |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 77 | if (strcmp(m_image->className(), m_rowImage->className()) != 0) { |
| 78 | vlog.error("Image types do not match (%s)", |
| 79 | m_rowImage->className()); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 82 | int numTiles = m_widthTiles * m_heightTiles; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 83 | m_rateMatrix = new char[numTiles]; |
| 84 | m_videoFlags = new char[numTiles]; |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 85 | memset(m_rateMatrix, 0, numTiles); |
| 86 | memset(m_videoFlags, 0, numTiles); |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | PollingManager::~PollingManager() |
| 90 | { |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 91 | delete[] m_videoFlags; |
| 92 | delete[] m_rateMatrix; |
| 93 | |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 94 | delete m_rowImage; |
| 95 | } |
| 96 | |
| 97 | // |
| 98 | // Register VNCServer object. |
| 99 | // |
| 100 | |
| 101 | void PollingManager::setVNCServer(VNCServer *s) |
| 102 | { |
| 103 | m_server = s; |
| 104 | } |
| 105 | |
| 106 | // |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 107 | // DEBUG: Measuring time spent in the poll() function, |
| 108 | // as well as time intervals between poll() calls. |
| 109 | // |
| 110 | |
| 111 | #ifdef DEBUG |
| 112 | void PollingManager::debugBeforePoll() |
| 113 | { |
| 114 | TimeMillis timeNow; |
| 115 | int diff = timeNow.diffFrom(m_timeSaved); |
| 116 | fprintf(stderr, "[wait%4dms]\t[step %2d]\t", diff, m_pollingStep % 32); |
| 117 | m_timeSaved = timeNow; |
| 118 | } |
| 119 | |
| 120 | void PollingManager::debugAfterPoll() |
| 121 | { |
| 122 | TimeMillis timeNow; |
| 123 | int diff = timeNow.diffFrom(m_timeSaved); |
| 124 | fprintf(stderr, "[poll%4dms]\n", diff); |
| 125 | m_timeSaved = timeNow; |
| 126 | } |
| 127 | |
| 128 | #endif |
| 129 | |
| 130 | // |
| 131 | // Search for changed rectangles on the screen. |
| 132 | // |
| 133 | |
| 134 | void PollingManager::poll() |
| 135 | { |
| 136 | #ifdef DEBUG |
| 137 | debugBeforePoll(); |
| 138 | #endif |
| 139 | |
Constantin Kaplinsky | d0b15c6 | 2007-10-09 08:15:25 +0000 | [diff] [blame] | 140 | // Perform polling and try update clients if changes were detected. |
| 141 | if (pollScreen()) |
Constantin Kaplinsky | b30ae7f | 2006-05-25 05:04:46 +0000 | [diff] [blame] | 142 | m_server->tryUpdate(); |
| 143 | |
| 144 | #ifdef DEBUG |
| 145 | debugAfterPoll(); |
| 146 | #endif |
| 147 | } |
| 148 | |
Constantin Kaplinsky | 9ee8dc6 | 2007-10-09 07:46:32 +0000 | [diff] [blame] | 149 | bool PollingManager::pollScreen() |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 150 | { |
| 151 | if (!m_server) |
| 152 | return false; |
| 153 | |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 154 | // If video data should have higher priority, and video area was |
| 155 | // detected, perform special passes to send video data only. Such |
| 156 | // "video passes" will be performed between normal polling passes. |
| 157 | // No actual polling is performed in a video pass since we know that |
| 158 | // video is changing continuously. |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame^] | 159 | // |
| 160 | // FIXME: Should we move this block into a separate function? |
| 161 | // FIXME: Giving higher priority to video area lengthens video |
| 162 | // detection cycles. Should we do something with that? |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 163 | if ((int)m_videoPriority > 1 && !m_videoRect.is_empty()) { |
| 164 | if (m_numVideoPasses > 0) { |
| 165 | m_numVideoPasses--; |
| 166 | getScreenRect(m_videoRect); |
| 167 | return true; // we've got changes |
| 168 | } else { |
| 169 | // Normal pass now, but schedule video passes for next calls. |
| 170 | m_numVideoPasses = (int)m_videoPriority - 1; |
| 171 | } |
| 172 | } |
| 173 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 174 | // changeFlags[] array will hold boolean values corresponding to |
| 175 | // each 32x32 tile. If a value is true, then we've detected a change |
| 176 | // in that tile. Initially, we fill in the array with zero values. |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame^] | 177 | // |
| 178 | // FIXME: Should we use a member variable in place of changeFlags? |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 179 | bool *changeFlags = new bool[m_widthTiles * m_heightTiles]; |
| 180 | memset(changeFlags, 0, m_widthTiles * m_heightTiles * sizeof(bool)); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 181 | |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 182 | // First pass over the framebuffer. Here we scan 1/32 part of the |
| 183 | // framebuffer -- that is, one line in each (32 * m_width) stripe. |
| 184 | // We compare the pixels of that line with previous framebuffer |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 185 | // contents and raise corresponding member values of changeFlags[]. |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 186 | int scanOffset = m_pollingOrder[m_pollingStep++ % 32]; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 187 | bool *pChangeFlags = changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 188 | int nTilesChanged = 0; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 189 | for (int y = scanOffset; y < m_height; y += 32) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 190 | nTilesChanged += checkRow(0, y, m_width, pChangeFlags); |
| 191 | pChangeFlags += m_widthTiles; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 194 | // Do the work related to video area detection. |
Constantin Kaplinsky | 1d37802 | 2007-12-25 17:43:09 +0000 | [diff] [blame] | 195 | bool haveVideoRect = false; |
| 196 | if ((int)m_videoPriority != 0) |
| 197 | haveVideoRect = handleVideo(changeFlags); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 198 | |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 199 | // Inform the server about the changes. |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame^] | 200 | // |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 201 | // FIXME: It's possible that (nTilesChanged != 0) but changeFlags[] |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 202 | // array is empty. That's possible because handleVideo() |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 203 | // modifies changeFlags[]. |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 204 | if (nTilesChanged) |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 205 | sendChanges(changeFlags); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 206 | |
| 207 | // Cleanup. |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 208 | delete[] changeFlags; |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 209 | |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 210 | #ifdef DEBUG |
| 211 | if (nTilesChanged != 0) { |
| 212 | fprintf(stderr, "#%d# ", nTilesChanged); |
| 213 | } |
| 214 | #endif |
| 215 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 216 | return (nTilesChanged != 0 || haveVideoRect); |
Constantin Kaplinsky | a119b48 | 2007-10-04 06:02:02 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 219 | int PollingManager::checkRow(int x, int y, int w, bool *pChangeFlags) |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 220 | { |
| 221 | int bytesPerPixel = m_image->xim->bits_per_pixel / 8; |
| 222 | int bytesPerLine = m_image->xim->bytes_per_line; |
| 223 | |
Constantin Kaplinsky | 29d3205 | 2007-10-08 14:16:02 +0000 | [diff] [blame] | 224 | if (x == 0 && w == m_width) { |
| 225 | getFullRow(y); // use more efficient method if possible |
| 226 | } else { |
| 227 | getRow(x, y, w); |
| 228 | } |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 229 | |
| 230 | char *ptr_old = m_image->xim->data + y * bytesPerLine + x * bytesPerPixel; |
| 231 | char *ptr_new = m_rowImage->xim->data; |
| 232 | |
| 233 | int nTilesChanged = 0; |
| 234 | for (int i = 0; i < (w + 31) / 32; i++) { |
| 235 | int tile_w = (w - i * 32 >= 32) ? 32 : w - i * 32; |
| 236 | int nBytes = tile_w * bytesPerPixel; |
| 237 | if (memcmp(ptr_old, ptr_new, nBytes)) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 238 | *pChangeFlags = true; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 239 | nTilesChanged++; |
| 240 | } |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 241 | pChangeFlags++; |
Constantin Kaplinsky | bc6b9e2 | 2007-10-04 11:43:41 +0000 | [diff] [blame] | 242 | ptr_old += nBytes; |
| 243 | ptr_new += nBytes; |
| 244 | } |
| 245 | |
| 246 | return nTilesChanged; |
| 247 | } |
| 248 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 249 | void PollingManager::sendChanges(bool *pChangeFlags) |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 250 | { |
| 251 | Rect rect; |
| 252 | for (int y = 0; y < m_heightTiles; y++) { |
| 253 | for (int x = 0; x < m_widthTiles; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 254 | if (*pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 255 | // Count successive tiles marked as changed. |
| 256 | int count = 1; |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 257 | while (x + count < m_widthTiles && *pChangeFlags++) { |
Constantin Kaplinsky | a79255b | 2007-10-07 13:03:55 +0000 | [diff] [blame] | 258 | count++; |
| 259 | } |
| 260 | // Compute the coordinates and the size of this band. |
| 261 | rect.setXYWH(x * 32, y * 32, count * 32, 32); |
| 262 | if (rect.br.x > m_width) |
| 263 | rect.br.x = m_width; |
| 264 | if (rect.br.y > m_height) |
| 265 | rect.br.y = m_height; |
| 266 | // Add to the changed region maintained by the server. |
| 267 | getScreenRect(rect); |
| 268 | m_server->add_changed(rect); |
| 269 | // Skip processed tiles. |
| 270 | x += count; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 276 | bool PollingManager::handleVideo(bool *pChangeFlags) |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 277 | { |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 278 | // Update counters in m_rateMatrix. |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 279 | int numTiles = m_heightTiles * m_widthTiles; |
| 280 | for (int i = 0; i < numTiles; i++) |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 281 | m_rateMatrix[i] += (pChangeFlags[i] != false); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 282 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 283 | // Once per eight calls: detect video rectangle by examining |
| 284 | // m_rateMatrix[], then reset counters in m_rateMatrix[]. |
| 285 | if (m_pollingStep % 8 == 0) { |
| 286 | detectVideo(); |
| 287 | memset(m_rateMatrix, 0, numTiles); |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Constantin Kaplinsky | 04e910b | 2007-12-25 18:10:35 +0000 | [diff] [blame^] | 290 | // FIXME: It looks like the code below rather belongs to |
| 291 | // pollScreen(). Perhaps handleVideo() should be merged back |
| 292 | // to pollScreen(), and then pollScreen() should be split in |
| 293 | // some better way, if needed at all. |
| 294 | |
Constantin Kaplinsky | 646998a | 2007-10-09 09:31:41 +0000 | [diff] [blame] | 295 | // Grab the pixels of video area. Also, exclude video rectangle from |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 296 | // pChangeFlags[], to prevent grabbing the same pixels twice. |
Constantin Kaplinsky | 646998a | 2007-10-09 09:31:41 +0000 | [diff] [blame] | 297 | if (!m_videoRect.is_empty()) { |
| 298 | Rect r(m_videoRect.tl.x / 32, m_videoRect.tl.y / 32, |
| 299 | m_videoRect.br.x / 32, m_videoRect.br.y / 32); |
| 300 | for (int y = r.tl.y; y < r.br.y; y++) { |
| 301 | for (int x = r.tl.x; x < r.br.x; x++) { |
Constantin Kaplinsky | 808db55 | 2007-10-09 12:48:26 +0000 | [diff] [blame] | 302 | pChangeFlags[y * m_widthTiles + x] = false; |
Constantin Kaplinsky | 646998a | 2007-10-09 09:31:41 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | getScreenRect(m_videoRect); |
| 306 | return true; // we've got a video rectangle |
| 307 | } |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 308 | |
Constantin Kaplinsky | 646998a | 2007-10-09 09:31:41 +0000 | [diff] [blame] | 309 | return false; // video rectangle is empty |
Constantin Kaplinsky | c1984e0 | 2007-10-08 14:54:18 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 312 | void |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 313 | PollingManager::detectVideo() |
| 314 | { |
| 315 | // Configurable parameters. |
| 316 | const int VIDEO_THRESHOLD_0 = 3; |
| 317 | const int VIDEO_THRESHOLD_1 = 5; |
| 318 | |
Constantin Kaplinsky | bb56377 | 2007-12-25 11:25:07 +0000 | [diff] [blame] | 319 | // In m_rateMatrix, clear counters corresponding to non-32x32 tiles. |
| 320 | // This will guarantee that the size of the video area is always a |
| 321 | // multiple of 32 pixels. This is important for hardware JPEG encoders. |
| 322 | int numTiles = m_heightTiles * m_widthTiles; |
| 323 | if (m_width % 32 != 0) { |
| 324 | for (int n = m_widthTiles - 1; n < numTiles; n += m_widthTiles) |
| 325 | m_rateMatrix[n] = 0; |
| 326 | } |
| 327 | if (m_height % 32 != 0) { |
| 328 | for (int n = numTiles - m_widthTiles; n < numTiles; n++) |
| 329 | m_rateMatrix[n] = 0; |
| 330 | } |
| 331 | |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 332 | // First, detect candidate region that looks like video. In other |
| 333 | // words, find a region that consists of continuously changing |
| 334 | // pixels. Save the result in m_videoFlags[]. |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 335 | for (int i = 0; i < numTiles; i++) { |
| 336 | if (m_rateMatrix[i] <= VIDEO_THRESHOLD_0) { |
| 337 | m_videoFlags[i] = 0; |
| 338 | } else if (m_rateMatrix[i] >= VIDEO_THRESHOLD_1) { |
| 339 | m_videoFlags[i] = 1; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Now, choose the biggest rectangle from that candidate region. |
| 344 | Rect newRect; |
| 345 | getVideoAreaRect(&newRect); |
| 346 | |
| 347 | // Does new rectangle differ from the previously detected one? |
| 348 | // If it does, save new rectangle and inform the server. |
| 349 | if (!newRect.equals(m_videoRect)) { |
| 350 | if (newRect.is_empty()) { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 351 | vlog.debug("No video detected"); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 352 | } else { |
Constantin Kaplinsky | dab9a56 | 2007-10-10 01:33:39 +0000 | [diff] [blame] | 353 | vlog.debug("Detected video %dx%d at (%d,%d)", |
| 354 | newRect.width(), newRect.height(), |
| 355 | newRect.tl.x, newRect.tl.y); |
Constantin Kaplinsky | 6bb4bf1 | 2007-10-09 12:03:15 +0000 | [diff] [blame] | 356 | } |
| 357 | m_videoRect = newRect; |
| 358 | m_server->set_video_area(newRect); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 363 | PollingManager::getVideoAreaRect(Rect *result) |
| 364 | { |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 365 | int *mx_hlen, *mx_vlen; |
| 366 | constructLengthMatrices(&mx_hlen, &mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 367 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 368 | int full_h = m_heightTiles; |
| 369 | int full_w = m_widthTiles; |
| 370 | int x, y; |
| 371 | Rect max_rect(0, 0, 0, 0); |
| 372 | Rect local_rect; |
| 373 | |
| 374 | for (y = 0; y < full_h; y++) { |
| 375 | for (x = 0; x < full_w; x++) { |
| 376 | int max_w = mx_hlen[y * full_w + x]; |
| 377 | int max_h = mx_vlen[y * full_w + x]; |
| 378 | if (max_w > 2 && max_h > 1 && max_h * max_w > (int)max_rect.area()) { |
| 379 | local_rect.tl.x = x; |
| 380 | local_rect.tl.y = y; |
| 381 | findMaxLocalRect(&local_rect, mx_hlen, mx_vlen); |
| 382 | if (local_rect.area() > max_rect.area()) { |
| 383 | max_rect = local_rect; |
| 384 | } |
| 385 | } |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 389 | destroyLengthMatrices(mx_hlen, mx_vlen); |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 390 | |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 391 | max_rect.tl.x *= 32; |
| 392 | max_rect.tl.y *= 32; |
| 393 | max_rect.br.x *= 32; |
| 394 | max_rect.br.y *= 32; |
| 395 | if (max_rect.br.x > m_width) |
| 396 | max_rect.br.x = m_width; |
| 397 | if (max_rect.br.y > m_height) |
| 398 | max_rect.br.y = m_height; |
| 399 | *result = max_rect; |
Constantin Kaplinsky | 5664998 | 2007-09-04 09:15:35 +0000 | [diff] [blame] | 400 | } |
Constantin Kaplinsky | 0fc9f17 | 2007-09-29 04:00:02 +0000 | [diff] [blame] | 401 | |
| 402 | void |
| 403 | PollingManager::constructLengthMatrices(int **pmx_h, int **pmx_v) |
| 404 | { |
| 405 | // Handy shortcuts. |
| 406 | int h = m_heightTiles; |
| 407 | int w = m_widthTiles; |
| 408 | |
| 409 | // Allocate memory. |
| 410 | int *mx_h = new int[h * w]; |
| 411 | memset(mx_h, 0, h * w * sizeof(int)); |
| 412 | int *mx_v = new int[h * w]; |
| 413 | memset(mx_v, 0, h * w * sizeof(int)); |
| 414 | |
| 415 | int x, y, len, i; |
| 416 | |
| 417 | // Fill in horizontal length matrix. |
| 418 | for (y = 0; y < h; y++) { |
| 419 | for (x = 0; x < w; x++) { |
| 420 | len = 0; |
| 421 | while (x + len < w && m_videoFlags[y * w + x + len]) { |
| 422 | len++; |
| 423 | } |
| 424 | for (i = 0; i < len; i++) { |
| 425 | mx_h[y * w + x + i] = len - i; |
| 426 | } |
| 427 | x += len; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Fill in vertical length matrix. |
| 432 | for (x = 0; x < w; x++) { |
| 433 | for (y = 0; y < h; y++) { |
| 434 | len = 0; |
| 435 | while (y + len < h && m_videoFlags[(y + len) * w + x]) { |
| 436 | len++; |
| 437 | } |
| 438 | for (i = 0; i < len; i++) { |
| 439 | mx_v[(y + i) * w + x] = len - i; |
| 440 | } |
| 441 | y += len; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | *pmx_h = mx_h; |
| 446 | *pmx_v = mx_v; |
| 447 | } |
| 448 | |
| 449 | void |
| 450 | PollingManager::destroyLengthMatrices(int *mx_h, int *mx_v) |
| 451 | { |
| 452 | delete[] mx_h; |
| 453 | delete[] mx_v; |
| 454 | } |
| 455 | |
| 456 | // NOTE: This function assumes that current tile has non-zero in mx_h[], |
| 457 | // otherwise we get division by zero. |
| 458 | void |
| 459 | PollingManager::findMaxLocalRect(Rect *r, int mx_h[], int mx_v[]) |
| 460 | { |
| 461 | int idx = r->tl.y * m_widthTiles + r->tl.x; |
| 462 | |
| 463 | // NOTE: Rectangle's maximum width and height are 25 and 18 |
| 464 | // (in tiles, where each tile is usually 32x32 pixels). |
| 465 | int max_w = mx_h[idx]; |
| 466 | if (max_w > 25) |
| 467 | max_w = 25; |
| 468 | int cur_h = 18; |
| 469 | |
| 470 | int best_w = max_w; |
| 471 | int best_area = 1 * best_w; |
| 472 | |
| 473 | for (int i = 0; i < max_w; i++) { |
| 474 | int h = mx_v[idx + i]; |
| 475 | if (h < cur_h) { |
| 476 | cur_h = h; |
| 477 | if (cur_h * max_w <= best_area) |
| 478 | break; |
| 479 | } |
| 480 | if (cur_h * (i + 1) > best_area) { |
| 481 | best_w = i + 1; |
| 482 | best_area = cur_h * best_w; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | r->br.x = r->tl.x + best_w; |
| 487 | r->br.y = r->tl.y + best_area / best_w; |
| 488 | } |
| 489 | |