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