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 | |
| 13 | #include <string> |
| 14 | #include <inttypes.h> |
| 15 | |
| 16 | #include "aos/aos_core.h" |
| 17 | #include "aos/atom_code/camera/V4L2.h" |
| 18 | #include "aos/atom_code/camera/Buffers.h" |
| 19 | |
| 20 | #define CLEAR(x) memset(&(x), 0, sizeof(x)) |
| 21 | |
| 22 | namespace aos { |
| 23 | namespace camera { |
| 24 | |
| 25 | class Reader { |
| 26 | static const char *const dev_name; |
| 27 | |
| 28 | // of the camera |
| 29 | int fd_; |
| 30 | // the bound socket listening for fd requests |
| 31 | int server_fd_; |
| 32 | |
| 33 | static const aos_type_sig kRecycleSignature; |
| 34 | aos_queue *queue_, *recycle_queue_; |
| 35 | // the number of buffers currently queued in v4l2 |
| 36 | uint32_t queued_; |
| 37 | public: |
| 38 | Reader() { |
| 39 | struct stat st; |
| 40 | if (stat(dev_name, &st) == -1) { |
| 41 | LOG(FATAL, "Cannot identify '%s' because of %d: %s\n", |
| 42 | dev_name, errno, strerror(errno)); |
| 43 | } |
| 44 | if (!S_ISCHR(st.st_mode)) { |
| 45 | LOG(FATAL, "%s is no device\n", dev_name); |
| 46 | } |
| 47 | |
| 48 | fd_ = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0); |
| 49 | if (fd_ == -1) { |
| 50 | LOG(FATAL, "Cannot open '%s' because of %d: %s\n", |
| 51 | dev_name, errno, strerror(errno)); |
| 52 | } |
| 53 | |
| 54 | queue_ = aos_fetch_queue_recycle(Buffers::kQueueName.c_str(), &Buffers::kSignature, |
| 55 | &kRecycleSignature, &recycle_queue_); |
| 56 | // read off any existing recycled messages |
| 57 | while (aos_queue_read_msg(recycle_queue_, NON_BLOCK) != NULL); |
| 58 | queued_ = 0; |
| 59 | |
| 60 | InitServer(); |
| 61 | Init(); |
| 62 | } |
| 63 | private: |
| 64 | void InitServer() { |
| 65 | if (unlink(Buffers::kFDServerName.c_str()) == -1 && errno != ENOENT) { |
| 66 | LOG(WARNING, "unlink(kFDServerName(='%s')) failed with %d: %s\n", |
| 67 | Buffers::kFDServerName.c_str(), errno, strerror(errno)); |
| 68 | } |
| 69 | if ((server_fd_ = Buffers::CreateSocket(bind)) == -1) { |
| 70 | LOG(FATAL, "creating the IPC socket failed\n"); |
| 71 | } |
| 72 | if (listen(server_fd_, 10) == -1) { |
| 73 | LOG(FATAL, "listen(%d, 10) failed with %d: %s\n", |
| 74 | server_fd_, errno, strerror(errno)); |
| 75 | } |
| 76 | } |
| 77 | void SendFD(const int sock) { |
| 78 | int myfds[Buffers::kNumFDs]; /* Contains the file descriptors to pass. */ |
| 79 | myfds[0] = fd_; |
| 80 | char buf[CMSG_SPACE(sizeof(myfds))]; /* ancillary data buffer */ |
| 81 | |
| 82 | iovec data; |
| 83 | memset(&data, 0, sizeof(data)); |
| 84 | char dummy = 'A'; |
| 85 | data.iov_base = &dummy; |
| 86 | data.iov_len = sizeof(dummy); |
| 87 | msghdr msg; |
| 88 | memset(&msg, 0, sizeof(msg)); |
| 89 | msg.msg_iov = &data; |
| 90 | msg.msg_iovlen = 1; |
| 91 | msg.msg_control = buf; |
| 92 | msg.msg_controllen = sizeof(buf); |
| 93 | cmsghdr *const cmsg = CMSG_FIRSTHDR(&msg); |
| 94 | cmsg->cmsg_level = SOL_SOCKET; |
| 95 | cmsg->cmsg_type = SCM_RIGHTS; |
| 96 | cmsg->cmsg_len = CMSG_LEN(sizeof(myfds)); |
| 97 | /* Initialize the payload: */ |
| 98 | memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds)); |
| 99 | if (sendmsg(sock, &msg, 0) == -1) { |
| 100 | LOG(ERROR, "sendmsg(%d, %p, 0) failed with %d: %s\n", |
| 101 | sock, &msg, errno, strerror(errno)); |
| 102 | } |
| 103 | // leave it open so that the other end can tell if this process dies |
| 104 | } |
| 105 | |
| 106 | #if 0 |
| 107 | // if we ever do want to do any of these things, this is how |
| 108 | void Stop() { |
| 109 | const v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 110 | if (xioctl(fd_, VIDIOC_STREAMOFF, &type) == -1) { |
| 111 | errno_exit("VIDIOC_STREAMOFF"); |
| 112 | } |
| 113 | } |
| 114 | void Close() { |
| 115 | if (close(fd_) == -1) |
| 116 | errno_exit("close"); |
| 117 | fd_ = -1; |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | void QueueBuffer(v4l2_buffer *buf) { |
| 122 | if (xioctl(fd_, VIDIOC_QBUF, buf) == -1) { |
| 123 | LOG(WARNING, "ioctl VIDIOC_QBUF(%d, %p) failed with %d: %s. losing buf #%"PRIu32"\n", |
| 124 | fd_, &buf, errno, strerror(errno), buf->index); |
| 125 | } else { |
| 126 | LOG(DEBUG, "put buf #%"PRIu32" into driver's queue\n", buf->index); |
| 127 | ++queued_; |
| 128 | } |
| 129 | } |
| 130 | void ReadFrame() { |
| 131 | v4l2_buffer buf; |
| 132 | CLEAR(buf); |
| 133 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 134 | buf.memory = V4L2_MEMORY_MMAP; |
| 135 | |
| 136 | const Buffers::Message *read; |
| 137 | do { |
| 138 | read = static_cast<const Buffers::Message *>( |
| 139 | // we block waiting for one if we can't dequeue one without leaving |
| 140 | // the driver <= 2 (to be safe) |
| 141 | aos_queue_read_msg(recycle_queue_, (queued_ <= 2) ? BLOCK : NON_BLOCK)); |
| 142 | if (read != NULL) { |
| 143 | buf.index = read->index; |
| 144 | aos_queue_free_msg(recycle_queue_, read); |
| 145 | QueueBuffer(&buf); |
| 146 | } |
| 147 | } while (read != NULL); |
| 148 | |
| 149 | if (xioctl(fd_, VIDIOC_DQBUF, &buf) == -1) { |
| 150 | if (errno != EAGAIN) { |
| 151 | LOG(ERROR, "ioctl VIDIOC_DQBUF(%d, %p) failed with %d: %s\n", |
| 152 | fd_, &buf, errno, strerror(errno)); |
| 153 | } |
| 154 | return; |
| 155 | } |
| 156 | --queued_; |
| 157 | if (buf.index >= Buffers::kNumBuffers) { |
| 158 | LOG(ERROR, "buf.index (%"PRIu32") is >= kNumBuffers (%u)\n", |
| 159 | buf.index, Buffers::kNumBuffers); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | Buffers::Message *const msg = static_cast<Buffers::Message *>( |
| 164 | aos_queue_get_msg(queue_)); |
| 165 | if (msg == NULL) { |
| 166 | LOG(WARNING, "couldn't get a message to send buf #%"PRIu32" from queue %p." |
| 167 | " re-queueing now\n", buf.index, queue_); |
| 168 | QueueBuffer(&buf); |
| 169 | return; |
| 170 | } |
| 171 | msg->index = buf.index; |
| 172 | msg->bytesused = buf.bytesused; |
| 173 | memcpy(&msg->timestamp, &buf.timestamp, sizeof(msg->timestamp)); |
| 174 | msg->sequence = buf.sequence; |
| 175 | if (aos_queue_write_msg_free(queue_, msg, OVERRIDE) == -1) { |
| 176 | LOG(WARNING, "sending message %p with buf #%"PRIu32" to queue %p failed." |
| 177 | " re-queueing now\n", msg, buf.index, queue_); |
| 178 | QueueBuffer(&buf); |
| 179 | return; |
| 180 | } else { |
| 181 | LOG(DEBUG, "sent message off to queue %p with buffer #%"PRIu32"\n", |
| 182 | queue_, buf.index); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void init_mmap() { |
| 187 | v4l2_requestbuffers req; |
| 188 | CLEAR(req); |
| 189 | req.count = Buffers::kNumBuffers; |
| 190 | req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 191 | req.memory = V4L2_MEMORY_MMAP; |
| 192 | if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) { |
| 193 | if (EINVAL == errno) { |
| 194 | LOG(FATAL, "%s does not support memory mapping\n", dev_name); |
| 195 | } else { |
| 196 | LOG(FATAL, "ioctl VIDIOC_REQBUFS(%d, %p) failed with %d: %s\n", |
| 197 | fd_, &req, errno, strerror(errno)); |
| 198 | } |
| 199 | } |
| 200 | queued_ = Buffers::kNumBuffers; |
| 201 | if (req.count < Buffers::kNumBuffers) { |
| 202 | LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void Init() { |
| 207 | v4l2_capability cap; |
| 208 | if (xioctl(fd_, VIDIOC_QUERYCAP, &cap) == -1) { |
| 209 | if (EINVAL == errno) { |
| 210 | LOG(FATAL, "%s is no V4L2 device\n", |
| 211 | dev_name); |
| 212 | } else { |
| 213 | LOG(FATAL, "ioctl VIDIOC_QUERYCAP(%d, %p) failed with %d: %s\n", |
| 214 | fd_, &cap, errno, strerror(errno)); |
| 215 | } |
| 216 | } |
| 217 | if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { |
| 218 | LOG(FATAL, "%s is no video capture device\n", |
| 219 | dev_name); |
| 220 | } |
| 221 | if (!(cap.capabilities & V4L2_CAP_STREAMING)) { |
| 222 | LOG(FATAL, "%s does not support streaming i/o\n", |
| 223 | dev_name); |
| 224 | } |
| 225 | |
| 226 | /* Select video input, video standard and tune here. */ |
| 227 | |
| 228 | v4l2_cropcap cropcap; |
| 229 | CLEAR(cropcap); |
| 230 | cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 231 | if (xioctl(fd_, VIDIOC_CROPCAP, &cropcap) == 0) { |
| 232 | v4l2_crop crop; |
| 233 | crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 234 | crop.c = cropcap.defrect; /* reset to default */ |
| 235 | |
| 236 | if (xioctl(fd_, VIDIOC_S_CROP, &crop) == -1) { |
| 237 | switch (errno) { |
| 238 | case EINVAL: |
| 239 | /* Cropping not supported. */ |
| 240 | break; |
| 241 | default: |
| 242 | /* Errors ignored. */ |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | } else { |
| 247 | /* Errors ignored. */ |
| 248 | } |
| 249 | |
| 250 | v4l2_format fmt; |
| 251 | CLEAR(fmt); |
| 252 | fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 253 | fmt.fmt.pix.width = Buffers::kWidth; |
| 254 | fmt.fmt.pix.height = Buffers::kHeight; |
| 255 | fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG; |
| 256 | fmt.fmt.pix.field = V4L2_FIELD_ANY; |
| 257 | //fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV; |
| 258 | //fmt.fmt.pix.field = V4L2_FIELD_INTERLACED; |
| 259 | if (xioctl(fd_, VIDIOC_S_FMT, &fmt) == -1) { |
| 260 | LOG(FATAL, "ioctl VIDIC_S_FMT(%d, %p) failed with %d: %s\n", |
| 261 | fd_, &fmt, errno, strerror(errno)); |
| 262 | } |
| 263 | /* Note VIDIOC_S_FMT may change width and height. */ |
| 264 | |
| 265 | /* Buggy driver paranoia. */ |
| 266 | unsigned int min = fmt.fmt.pix.width * 2; |
| 267 | if (fmt.fmt.pix.bytesperline < min) |
| 268 | fmt.fmt.pix.bytesperline = min; |
| 269 | min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height; |
| 270 | if (fmt.fmt.pix.sizeimage < min) |
| 271 | fmt.fmt.pix.sizeimage = min; |
| 272 | |
| 273 | #if 0 |
| 274 | // set framerate |
| 275 | struct v4l2_streamparm *setfps; |
| 276 | setfps = (struct v4l2_streamparm *) calloc(1, sizeof(struct v4l2_streamparm)); |
| 277 | memset(setfps, 0, sizeof(struct v4l2_streamparm)); |
| 278 | setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 279 | setfps->parm.capture.timeperframe.numerator = 1; |
| 280 | setfps->parm.capture.timeperframe.denominator = 20; |
| 281 | if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) { |
| 282 | LOG(ERROR, "ioctl VIDIOC_S_PARM(%d, %p) failed with %d: %s\n", |
| 283 | fd_, setfps, errno, strerror(errno)); |
| 284 | exit(EXIT_FAILURE); |
| 285 | } |
| 286 | LOG(INFO, "framerate ended up at %d/%d\n", |
| 287 | setfps->parm.capture.timeperframe.numerator, |
| 288 | setfps->parm.capture.timeperframe.denominator); |
| 289 | #endif |
| 290 | |
| 291 | init_mmap(); |
| 292 | } |
| 293 | |
| 294 | void Start() { |
| 295 | LOG(DEBUG, "queueing buffers for the first time\n"); |
| 296 | v4l2_buffer buf; |
| 297 | for (unsigned int i = 0; i < Buffers::kNumBuffers; ++i) { |
| 298 | CLEAR(buf); |
| 299 | buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 300 | buf.memory = V4L2_MEMORY_MMAP; |
| 301 | buf.index = i; |
| 302 | QueueBuffer(&buf); |
| 303 | } |
| 304 | LOG(DEBUG, "done with first queue\n"); |
| 305 | |
| 306 | v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 307 | if (xioctl(fd_, VIDIOC_STREAMON, &type) == -1) { |
| 308 | LOG(FATAL, "ioctl VIDIOC_STREAMON(%d, %p) failed with %d: %s\n", |
| 309 | fd_, &type, errno, strerror(errno)); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | public: |
| 314 | void Run() { |
| 315 | Start(); |
| 316 | |
| 317 | fd_set fds; |
| 318 | timeval tv; |
| 319 | while (true) { |
| 320 | // HAVE TO DO THIS EACH TIME THROUGH THE LOOP |
| 321 | tv.tv_sec = 2; |
| 322 | tv.tv_usec = 0; |
| 323 | |
| 324 | FD_ZERO(&fds); |
| 325 | FD_SET(fd_, &fds); |
| 326 | FD_SET(server_fd_, &fds); |
| 327 | switch (select(std::max(fd_, server_fd_) + 1, &fds, NULL, NULL, &tv)) { |
| 328 | case -1: |
| 329 | if (errno != EINTR) { |
| 330 | LOG(ERROR, "select(%d, %p, NULL, NULL, %p) failed with %d: %s\n", |
| 331 | std::max(fd_, server_fd_) + 1, &fds, &tv, errno, strerror(errno)); |
| 332 | } |
| 333 | continue; |
| 334 | case 0: |
| 335 | LOG(WARNING, "select timed out\n"); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | if (FD_ISSET(fd_, &fds)) { |
| 340 | ReadFrame(); |
| 341 | } |
| 342 | if (FD_ISSET(server_fd_, &fds)) { |
| 343 | const int sock = accept4(server_fd_, NULL, NULL, SOCK_NONBLOCK); |
| 344 | if (sock == -1) { |
| 345 | LOG(ERROR, "accept4(%d, NULL, NULL, SOCK_NONBLOCK(=%d) failed with %d: %s\n", |
| 346 | server_fd_, SOCK_NONBLOCK, errno, strerror(errno)); |
| 347 | } else { |
| 348 | SendFD(sock); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | }; |
| 354 | const char *const Reader::dev_name = "/dev/video0"; |
| 355 | const aos_type_sig Reader::kRecycleSignature{ |
| 356 | sizeof(Buffers::Message), 1, Buffers::kNumBuffers}; |
| 357 | |
| 358 | } // namespace camera |
| 359 | } // namespace aos |
| 360 | |
| 361 | AOS_RUN_NRT(aos::camera::Reader) |
| 362 | |