brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | #include <errno.h> |
| 7 | #include <malloc.h> |
| 8 | #include <sys/stat.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <netinet/ip.h> |
| 13 | #include <sys/socket.h> |
| 14 | #include <inttypes.h> |
| 15 | |
| 16 | #include <vector> |
| 17 | |
| 18 | #include "aos/common/Configuration.h" |
| 19 | #include "aos/aos_core.h" |
| 20 | #include "aos/atom_code/camera/Buffers.h" |
| 21 | |
| 22 | namespace aos { |
| 23 | namespace camera { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // doesn't like being a static class member |
| 28 | static const unsigned char dht_table[] = { |
| 29 | 0xff, 0xc4, 0x01, 0xa2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, |
| 30 | 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, |
| 31 | 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, 0x00, 0x03, |
| 32 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, |
| 33 | 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, |
| 34 | 0x0a, 0x0b, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, |
| 35 | 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04, |
| 36 | 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, |
| 37 | 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, |
| 38 | 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, |
| 39 | 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, |
| 40 | 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, |
| 41 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, |
| 42 | 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, |
| 43 | 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, |
| 44 | 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, |
| 45 | 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, |
| 46 | 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, |
| 47 | 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 48 | 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, |
| 49 | 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, |
| 50 | 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, |
| 51 | 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, |
| 52 | 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, |
| 53 | 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, |
| 54 | 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, |
| 55 | 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, |
| 56 | 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, |
| 57 | 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, |
| 58 | 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, |
| 59 | 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, |
| 60 | 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, |
| 61 | 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, |
| 62 | 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, |
| 63 | 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa |
| 64 | }; |
| 65 | |
| 66 | const char kFirstHeader[] = "HTTP/1.0 200 OK\r\n" |
| 67 | "Connection: close\r\n" |
| 68 | "Server: AOS/0.0 Camera\r\n" |
| 69 | "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, " |
| 70 | "post-check=0, max-age=0\r\n" // this and above from mjpg-streamer |
| 71 | "Pragma: no-cache\r\n" |
| 72 | "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n" // also from mjpg-streamer |
| 73 | "Content-Type: multipart/x-mixed-replace; boundary=boundarydonotcross\r\n"; |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | class HTTPStreamer { |
| 78 | // Represents a single client. Handles all reading and writing of sockets and |
| 79 | // queues. |
| 80 | class Client { |
| 81 | enum class State { |
| 82 | kReadHeaders, |
| 83 | kWriteHeaders, |
| 84 | kWriteBoundary, // these last 2 loop to each other |
| 85 | kWriteImage, |
| 86 | kWriteDHT, // happens in the middle of kWriteImage |
| 87 | }; |
| 88 | const int sock_; |
| 89 | State state_; |
| 90 | inline fd_set *GetFDSetForCurrentState(fd_set *read_fds, |
| 91 | fd_set *write_fds) { |
| 92 | if (state_ == State::kReadHeaders) { |
| 93 | return read_fds; |
| 94 | } else { |
| 95 | return write_fds; |
| 96 | } |
| 97 | } |
| 98 | // MUST BE LONG ENOUGH TO HOLD kBoundaryText WITH A BIGGISH # IN IT |
| 99 | char scratch_[4096]; |
| 100 | int to_write_; |
| 101 | int zero_reads_; |
| 102 | static const int kMaxZeroReads = 2000; |
| 103 | size_t pos_, dht_pos_, dht_start_; |
| 104 | Buffers buffers_; |
| 105 | const void *current_; |
| 106 | uint32_t size_; |
| 107 | |
| 108 | Client(const Client &); |
| 109 | void operator=(const Client &); |
| 110 | |
| 111 | public: |
| 112 | explicit Client(int sock) : sock_(sock), state_(State::kReadHeaders), |
| 113 | zero_reads_(0), pos_(0) {} |
| 114 | ~Client() { |
| 115 | LOG(DEBUG, "closing socket %d\n", sock_); |
| 116 | if (close(sock_) == -1) { |
| 117 | LOG(INFO, "closing socket %d for destruction failed with %d: %s\n", |
| 118 | sock_, errno, strerror(errno)); |
| 119 | } |
| 120 | } |
| 121 | // Set any fds necessary into the 2 arguments. |
| 122 | void FDSet(fd_set *read_fds, fd_set *write_fds) { |
| 123 | FD_SET(sock_, GetFDSetForCurrentState(read_fds, write_fds)); |
| 124 | } |
| 125 | // The arguments are the same as the last FDSet call (after a successful |
| 126 | // select). |
| 127 | // Return value is whether or not to keep this one around. |
| 128 | bool Process(fd_set *read_fds, fd_set *write_fds) { |
| 129 | // if the socket we're waiting on isn't ready |
| 130 | if (!FD_ISSET(sock_, GetFDSetForCurrentState(read_fds, write_fds))) { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | ssize_t ret; |
| 135 | switch (state_) { |
| 136 | case State::kReadHeaders: |
| 137 | if (pos_ >= sizeof(scratch_)) { |
| 138 | LOG(WARNING, "read too many bytes of headers on sock %d." |
| 139 | " somebody should increase the size of scratch_\n", sock_); |
| 140 | return false; |
| 141 | } |
| 142 | if (zero_reads_ > kMaxZeroReads) { |
| 143 | LOG(WARNING, "read 0 bytes %d times on sock %d. giving up\n", |
| 144 | zero_reads_, sock_); |
| 145 | return false; |
| 146 | } |
| 147 | ret = read(sock_, scratch_ + pos_, sizeof(scratch_) - pos_); |
| 148 | if (ret == -1) { |
| 149 | LOG(WARNING, "read(%d, %p, %zd) failed with %d: %s\n", |
| 150 | sock_, scratch_ + pos_, sizeof(scratch_) - pos_, |
| 151 | errno, strerror(errno)); |
| 152 | return false; |
| 153 | } |
| 154 | pos_ += ret; |
| 155 | // if we just received \r\n\r\n (the end of the headers) |
| 156 | if (scratch_[pos_ - 4] == '\r' && scratch_[pos_ - 3] == '\n' && |
| 157 | scratch_[pos_ - 2] == '\r' && scratch_[pos_ - 1] == '\n') { |
| 158 | LOG(INFO, "entering state kWriteHeaders" |
| 159 | " after %zd bytes of headers read\n", pos_ - 1); |
| 160 | pos_ = 0; |
| 161 | state_ = State::kWriteHeaders; |
| 162 | } |
| 163 | scratch_[pos_] = '\0'; |
| 164 | if (ret == 0) { |
| 165 | ++zero_reads_; |
| 166 | } else { |
| 167 | zero_reads_ = 0; |
| 168 | LOG(DEBUG, "read %zd bytes of headers scratch_=%s\n", |
| 169 | ret, scratch_); |
| 170 | } |
| 171 | break; |
| 172 | case State::kWriteHeaders: |
| 173 | // this intentionally doesn't write the terminating \0 on the string |
| 174 | ret = write(sock_, kFirstHeader + pos_, sizeof(kFirstHeader) - pos_); |
| 175 | if (ret == -1) { |
| 176 | LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n", |
| 177 | sock_, kFirstHeader + pos_, sizeof(kFirstHeader) - pos_, |
| 178 | errno, strerror(errno)); |
| 179 | } else { |
| 180 | pos_ += ret; |
| 181 | if (pos_ >= sizeof(kFirstHeader)) { |
| 182 | current_ = NULL; |
| 183 | state_ = State::kWriteBoundary; |
| 184 | } |
| 185 | } |
| 186 | break; |
| 187 | case State::kWriteBoundary: |
| 188 | if (current_ == NULL) { |
| 189 | timeval timestamp; |
| 190 | current_ = buffers_.GetNext(false, &size_, ×tamp, NULL); |
| 191 | |
| 192 | /*static int skip = 0; |
| 193 | if (current_ != NULL) skip = (skip + 1) % 30; |
| 194 | if (!skip) current_ = NULL; |
| 195 | if (current_ == NULL) break;*/ |
| 196 | |
| 197 | #if 0 |
| 198 | // set pos_ to where the first header starts |
| 199 | for (pos_ = 0; static_cast<const uint8_t *>(current_)[pos_] != 0xFF; |
| 200 | ++pos_); |
| 201 | #else |
| 202 | pos_ = 0; |
| 203 | #endif |
| 204 | #if 0 |
| 205 | // go through the frame looking for the start of frame marker |
| 206 | for (dht_start_ = 0; |
| 207 | static_cast<const uint8_t *>(current_)[dht_start_ + 0] != |
| 208 | 0xFF && |
| 209 | static_cast<const uint8_t *>(current_)[dht_start_ + 1] != |
| 210 | 0xC0 && |
| 211 | dht_start_ < size_; ++dht_start_) |
| 212 | printf("[%zd]=%"PRIx8" ", dht_start_, |
| 213 | static_cast<const uint8_t *>(current_)[dht_start_]); |
| 214 | if (dht_start_ >= size_) { |
| 215 | LOG(WARNING, "couldn't find start of frame marker\n"); |
| 216 | return false; |
| 217 | } |
| 218 | #else |
| 219 | dht_start_ = 0; |
| 220 | #endif |
| 221 | dht_pos_ = 0; |
| 222 | |
| 223 | // aos.ChannelImageGetter depends on the exact format of this |
| 224 | to_write_ = snprintf(scratch_, sizeof(scratch_), |
| 225 | "\r\n--boundarydonotcross\r\n" |
| 226 | "Content-Type: image/jpeg\r\n" |
| 227 | "Content-Length: %"PRId32"\r\n" |
| 228 | "X-Timestamp: %ld.%06ld\r\n" |
| 229 | "\r\n", |
| 230 | size_, |
| 231 | timestamp.tv_sec, timestamp.tv_usec); |
| 232 | } |
| 233 | ret = write(sock_, scratch_ + pos_, to_write_ - pos_); |
| 234 | if (ret == -1) { |
| 235 | LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n", |
| 236 | sock_, scratch_ + pos_, to_write_ - pos_, |
| 237 | errno, strerror(errno)); |
| 238 | return false; |
| 239 | } else { |
| 240 | pos_ += ret; |
| 241 | if (static_cast<ssize_t>(pos_) >= to_write_) { |
| 242 | pos_ = 0; |
| 243 | state_ = State::kWriteImage; |
| 244 | } |
| 245 | } |
| 246 | break; |
| 247 | case State::kWriteImage: |
| 248 | ret = write(sock_, static_cast<const char *>(current_) + pos_, |
| 249 | ((dht_start_ == 0) ? size_ : dht_start_) - pos_); |
| 250 | if (ret == -1) { |
| 251 | LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n", |
| 252 | sock_, static_cast<const char *>(current_) + pos_, |
| 253 | ((dht_start_ == 0) ? size_ : dht_start_) - pos_, |
| 254 | errno, strerror(errno)); |
| 255 | return false; |
| 256 | } else { |
| 257 | pos_ += ret; |
| 258 | if (dht_start_ == 0) { |
| 259 | if (pos_ >= size_) { |
| 260 | buffers_.Release(); |
| 261 | current_ = NULL; |
| 262 | state_ = State::kWriteBoundary; |
| 263 | } |
| 264 | } else { |
| 265 | if (pos_ >= dht_start_) { |
| 266 | dht_start_ = 0; |
| 267 | state_ = State::kWriteDHT; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | break; |
| 272 | case State::kWriteDHT: |
| 273 | ret = write(sock_, dht_table + dht_pos_, |
| 274 | sizeof(dht_table) - dht_pos_); |
| 275 | if (ret == -1) { |
| 276 | LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n", |
| 277 | sock_, dht_table + dht_pos_, sizeof(dht_table) - dht_pos_, |
| 278 | errno, strerror(errno)); |
| 279 | return false; |
| 280 | } else { |
| 281 | dht_pos_ += ret; |
| 282 | if (dht_pos_ >= sizeof(dht_table)) { |
| 283 | state_ = State::kWriteImage; |
| 284 | } |
| 285 | } |
| 286 | break; |
| 287 | default: |
| 288 | LOG(FATAL, "something weird happened\n"); |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | const int bind_socket_; |
| 295 | |
| 296 | public: |
| 297 | HTTPStreamer() : bind_socket_(socket(AF_INET, SOCK_STREAM, 0)) { |
| 298 | if (bind_socket_ < 0) { |
| 299 | LOG(FATAL, "socket(AF_INET, SOCK_STREAM, 0) failed with %d: %s\n", |
| 300 | errno, strerror(errno)); |
| 301 | } |
| 302 | |
| 303 | union { |
| 304 | sockaddr_in in; |
| 305 | sockaddr addr; |
| 306 | } bind_sockaddr; |
| 307 | memset(&bind_sockaddr, 0, sizeof(bind_sockaddr)); |
| 308 | bind_sockaddr.in.sin_family = AF_INET; |
| 309 | bind_sockaddr.in.sin_port = |
| 310 | htons(static_cast<uint16_t>(aos::NetworkPort::kCameraStreamer)); |
| 311 | bind_sockaddr.in.sin_addr.s_addr = htonl(INADDR_ANY); |
| 312 | int optval = 1; |
| 313 | setsockopt(bind_socket_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); |
| 314 | if (bind(bind_socket_, &bind_sockaddr.addr, |
| 315 | sizeof(bind_sockaddr.addr)) == -1) { |
| 316 | LOG(FATAL, "bind(%d, %p) failed because of %d: %s\n", |
| 317 | bind_socket_, &bind_sockaddr.addr, errno, strerror(errno)); |
| 318 | } |
| 319 | |
| 320 | if (listen(bind_socket_, 10) == -1) { |
| 321 | LOG(FATAL, "listen(%d, 10) failed because of %d: %s\n", bind_socket_, |
| 322 | errno, strerror(errno)); |
| 323 | } |
| 324 | const int flags = fcntl(bind_socket_, F_GETFL, 0); |
| 325 | if (flags == -1) { |
| 326 | LOG(FATAL, "fcntl(%d, F_GETFL, 0) failed because of %d: %s\n", |
| 327 | bind_socket_, errno, strerror(errno)); |
| 328 | } |
| 329 | if (fcntl(bind_socket_, F_SETFL, flags | O_NONBLOCK) == -1) { |
| 330 | LOG(FATAL, "fcntl(%d, F_SETFL, %x) failed because of %d: %s\n", |
| 331 | bind_socket_, flags | O_NONBLOCK, errno, strerror(errno)); |
| 332 | } |
| 333 | } |
| 334 | void Run() { |
| 335 | signal(SIGPIPE, SIG_IGN); |
| 336 | |
| 337 | std::vector<Client *> clients; |
| 338 | fd_set read_fds, write_fds; |
| 339 | while (true) { |
| 340 | FD_ZERO(&read_fds); |
| 341 | FD_ZERO(&write_fds); |
| 342 | FD_SET(bind_socket_, &read_fds); |
| 343 | for (auto it = clients.begin(); it != clients.end(); ++it) { |
| 344 | (*it)->FDSet(&read_fds, &write_fds); |
| 345 | } |
| 346 | switch (select(FD_SETSIZE, &read_fds, &write_fds, |
| 347 | NULL, // err |
| 348 | NULL)) { // timeout |
| 349 | case -1: |
| 350 | LOG(ERROR, "select(FD_SETSIZE(=%d), %p, %p, NULL, NULL) failed" |
| 351 | " because of %d: %s\n", FD_SETSIZE, &read_fds, &write_fds, |
| 352 | errno, strerror(errno)); |
| 353 | continue; |
| 354 | case 0: |
| 355 | LOG(ERROR, "select with NULL timeout timed out...\n"); |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | if (FD_ISSET(bind_socket_, &read_fds)) { |
| 360 | const int sock = accept4(bind_socket_, NULL, NULL, SOCK_NONBLOCK); |
| 361 | if (sock == -1) { |
| 362 | LOG(ERROR, "accept4(%d, NULL, NULL, SOCK_NONBLOCK(=%d) failed" |
| 363 | " because of %d: %s\n", |
| 364 | bind_socket_, SOCK_NONBLOCK, errno, strerror(errno)); |
| 365 | } else { |
| 366 | clients.push_back(new Client(sock)); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | std::vector<std::vector<Client *>::iterator> to_remove; |
| 371 | for (auto it = clients.begin(); it != clients.end(); ++it) { |
| 372 | if (!(*it)->Process(&read_fds, &write_fds)) { |
| 373 | to_remove.push_back(it); |
| 374 | delete *it; |
| 375 | } |
| 376 | } |
| 377 | for (auto it = to_remove.rbegin(); it != to_remove.rend(); ++it) { |
| 378 | LOG(INFO, "removing client\n"); |
| 379 | clients.erase(*it); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | }; |
| 384 | |
| 385 | } // namespace camera |
| 386 | } // namespace aos |
| 387 | |
| 388 | AOS_RUN_NRT(aos::camera::HTTPStreamer) |