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