remove some really old camera code
It was never actually used, and it hasn't been built in over a year (and
the dependencies for doing that were a pain when it did get built).
diff --git a/aos/build/aos_all.gyp b/aos/build/aos_all.gyp
index 2584ee2..8623d51 100644
--- a/aos/build/aos_all.gyp
+++ b/aos/build/aos_all.gyp
@@ -10,8 +10,6 @@
'no_rsync': 1,
},
'dependencies': [
- #'../linux_code/camera/camera.gyp:CameraHTTPStreamer',
- #'../linux_code/camera/camera.gyp:CameraReader',
'../linux_code/linux_code.gyp:core',
'../linux_code/logging/logging.gyp:binary_log_writer',
'../linux_code/logging/logging.gyp:log_streamer',
diff --git a/aos/linux_code/camera/Buffers.cpp b/aos/linux_code/camera/Buffers.cpp
deleted file mode 100644
index 19c1d45..0000000
--- a/aos/linux_code/camera/Buffers.cpp
+++ /dev/null
@@ -1,158 +0,0 @@
-#include "aos/linux_code/camera/Buffers.h"
-
-#include <sys/mman.h>
-
-#include "aos/linux_code/camera/V4L2.h"
-#include "aos/common/logging/logging.h"
-
-namespace aos {
-namespace camera {
-
-// Represents an actual v4l2 buffer.
-struct Buffers::Buffer {
- void *start;
- size_t length; // for munmap
-};
-const std::string Buffers::kFDServerName("/tmp/aos_fd_server");
-const std::string Buffers::kQueueName("CameraBufferQueue");
-
-int Buffers::CreateSocket(int (*bind_connect)(int, const sockaddr *, socklen_t)) {
- union af_unix_sockaddr {
- sockaddr_un un;
- sockaddr addr;
- } addr;
- const int r = socket(AF_UNIX, SOCK_STREAM, 0);
- if (r == -1) {
- LOG(FATAL, "socket(AF_UNIX, SOCK_STREAM, 0) failed with %d: %s\n",
- errno, strerror(errno));
- }
- addr.un.sun_family = AF_UNIX;
- memset(addr.un.sun_path, 0, sizeof(addr.un.sun_path));
- strcpy(addr.un.sun_path, kFDServerName.c_str());
- if (bind_connect(r, &addr.addr, sizeof(addr.un)) == -1) {
- LOG(FATAL, "bind_connect(=%p)(%d, %p, %zd) failed with %d: %s\n",
- bind_connect, r, &addr.addr, sizeof(addr.un), errno, strerror(errno));
- }
- return r;
-}
-
-void Buffers::MMap() {
- buffers_ = new Buffer[kNumBuffers];
- v4l2_buffer buf;
- for (unsigned int n = 0; n < kNumBuffers; ++n) {
- memset(&buf, 0, sizeof(buf));
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
- buf.index = n;
- if (xioctl(fd_, VIDIOC_QUERYBUF, &buf) == -1) {
- LOG(FATAL, "ioctl VIDIOC_QUERYBUF(%d, %p) failed with %d: %s\n",
- fd_, &buf, errno, strerror(errno));
- }
- buffers_[n].length = buf.length;
- buffers_[n].start = mmap(NULL, buf.length,
- PROT_READ | PROT_WRITE, MAP_SHARED,
- fd_, buf.m.offset);
- if (buffers_[n].start == MAP_FAILED) {
- LOG(FATAL, "mmap(NULL, %zd, PROT_READ | PROT_WRITE, MAP_SHARED, %d, %jd)"
- " failed with %d: %s\n", buf.length, fd_, static_cast<intmax_t>(buf.m.offset),
- errno, strerror(errno));
- }
- }
-}
-
-void Buffers::Release() {
- message_.reset();
-}
-const void *Buffers::GetNext(bool block, uint32_t *bytesused,
- timeval *timestamp, uint32_t *sequence) {
- Release();
-
- // TODO(brians) make sure the camera reader process hasn't died
- do {
- if (block) {
- message_.reset(static_cast<const Message *>(
- queue_->ReadMessage(RawQueue::kPeek | RawQueue::kBlock)));
- } else {
- static int index = 0;
- message_.reset(static_cast<const Message *>(
- queue_->ReadMessageIndex(RawQueue::kBlock, &index)));
- }
- } while (block && message_ == NULL);
- if (message_ != NULL) {
- if (bytesused != NULL) memcpy(bytesused, &message_->bytesused, sizeof(*bytesused));
- if (timestamp != NULL) memcpy(timestamp, &message_->timestamp, sizeof(*timestamp));
- if (sequence != NULL) memcpy(sequence, &message_->sequence, sizeof(*sequence));
- return buffers_[message_->index].start;
- } else {
- return NULL;
- }
-}
-
-int Buffers::FetchFD() {
- int myfds[Buffers::kNumFDs]; // where to retrieve the fds into
- char buf[CMSG_SPACE(sizeof(myfds))]; // ancillary data buffer
-
- iovec data;
- memset(&data, 0, sizeof(data));
- char dummy;
- data.iov_base = &dummy;
- data.iov_len = sizeof(dummy);
- msghdr msg;
- memset(&msg, 0, sizeof(msg));
- msg.msg_iov = &data;
- msg.msg_iovlen = 1;
- msg.msg_control = buf;
- msg.msg_controllen = sizeof(buf);
-
- switch (recvmsg(server_, &msg, 0)) {
- case 0: // "the peer has performed an orderly shutdown"
- LOG(FATAL, "the fd server shut down (connected on %d)\n", server_);
- case -1:
- LOG(FATAL, "recvmsg(server_(=%d), %p, 0) failed with %d: %s\n",
- server_, &msg, errno, strerror(errno));
- }
- const cmsghdr *const cmsg = CMSG_FIRSTHDR(&msg);
- if (cmsg == NULL) {
- LOG(FATAL, "no headers in message\n");
- }
- if (cmsg->cmsg_len != CMSG_LEN(sizeof(myfds))) {
- LOG(FATAL, "got wrong size. got %d but expected %zd\n",
- cmsg->cmsg_len, CMSG_LEN(sizeof(myfds)));
- }
- if (cmsg->cmsg_level != SOL_SOCKET) {
- LOG(FATAL, "cmsg_level=%d. expected SOL_SOCKET(=%d)\n", cmsg->cmsg_level, SOL_SOCKET);
- }
- if (cmsg->cmsg_type != SCM_RIGHTS) {
- LOG(FATAL, "cmsg_type=%d. expected SCM_RIGHTS(=%d)\n", cmsg->cmsg_type, SCM_RIGHTS);
- }
- memcpy(myfds, CMSG_DATA(cmsg), sizeof(myfds));
-
- return myfds[0];
-}
-Buffers::Buffers()
- : server_(CreateSocket(connect)),
- fd_(FetchFD()),
- queue_(RawQueue::Fetch(kQueueName.c_str(), sizeof(Message), 971, 1)),
- message_(queue_) {
- MMap();
-}
-
-Buffers::~Buffers() {
- Release();
-
- for (unsigned i = 0; i < kNumBuffers; ++i) {
- if (munmap(buffers_[i].start, buffers_[i].length) == -1) {
- LOG(WARNING, "munmap(%p, %zd) for destruction failed with %d: %s\n",
- buffers_[i].start, buffers_[i].length, errno, strerror(errno));
- }
- }
- delete[] buffers_;
-
- if (close(fd_) == -1) {
- LOG(WARNING, "close(%d) for destruction failed with %d: %s\n",
- fd_, errno, strerror(errno));
- }
-}
-
-} // namespace camera
-} // namespace aos
diff --git a/aos/linux_code/camera/Buffers.h b/aos/linux_code/camera/Buffers.h
deleted file mode 100644
index c73b3d8..0000000
--- a/aos/linux_code/camera/Buffers.h
+++ /dev/null
@@ -1,96 +0,0 @@
-#ifndef AOS_LINUX_CODE_CAMERA_CAMERA_BUFFERS_H_
-#define AOS_LINUX_CODE_CAMERA_CAMERA_BUFFERS_H_
-
-#include <sys/socket.h>
-#include <sys/un.h>
-
-#include <string>
-
-#include "aos/linux_code/ipc_lib/queue.h"
-#include "aos/common/type_traits.h"
-#include "aos/linux_code/ipc_lib/unique_message_ptr.h"
-
-namespace aos {
-namespace camera {
-
-class Reader;
-class Buffers {
- // It has to do a lot of the same things as all the other ones, but it gets
- // the information from different places (some of it gets sent out by it).
- friend class Reader;
-
- // Not an abstract name so that an existing one can just be unlinked without
- // disturbing it if necessary (like with shm_link).
- static const std::string kFDServerName;
- // How many v4l2 buffers and other things that depend on that.
- static const unsigned int kNumBuffers = 10;
- // How many fds to transfer from the fd server.
- // Used to make it clear which 1s are 1 and which are this.
- static const size_t kNumFDs = 1;
- // Creates a socket and calls either bind or connect.
- // Returns a bound/connected socket or -1 (the reason for which will already
- // have been logged at ERROR).
- static int CreateSocket(int (*bind_connect)(int, const sockaddr *, socklen_t));
-
- // File descriptor connected to the fd server. Used for detecting if the
- // camera reading process has died.
- // A value of -2 means don't check.
- const int server_;
- // Gets the fd (using server_).
- int FetchFD();
- // File descriptor for the v4l2 device (that's valid in this process of
- // course).
- const int fd_;
-
- struct Buffer;
- // Buffer[kNumBuffers]
- Buffer *buffers_;
- struct Message {
- uint32_t index;
- uint32_t bytesused;
- timeval timestamp;
- uint32_t sequence;
- };
- static_assert(shm_ok<Message>::value, "it's going through queues");
-
- // NULL for the Reader one.
- RawQueue *const queue_;
- // The current one. Sometimes NULL.
- unique_message_ptr<const Message> message_;
-
- static const std::string kQueueName;
- // Make the actual mmap calls.
- // Called by Buffers() automatically.
- void MMap();
-
- public:
- Buffers();
- // Will clean everything up.
- // So that HTTPStreamer can create/destroy one for each client to make it
- // simpler.
- ~Buffers();
-
- // Retrieves the next image. Will return the current one if it hasn't yet.
- // Calls Release() at the beginning.
- // NOTE: this means that the caller can't keep using references to the old
- // return value after calling this function again
- // block is whether to return NULL or wait for a new one
- // the last 3 output parameters will be filled in straight from the
- // v4l2_buffer if they're not NULL
- // (see <http://v4l2spec.bytesex.org/spec/x5953.htm#V4L2-BUFFER> for details)
- // NOTE: guaranteed to return a valid pointer if block is true
- const void *GetNext(bool block,
- uint32_t *bytesused, timeval *timestamp, uint32_t *sequence);
- // Releases the most recent frame buffer. Automatically called by GetNext and
- // the destructor. Safe to call multiple times without getting frames in
- // between.
- void Release();
-
- // How big images are.
- static const int32_t kWidth = 320, kHeight = 240;
-};
-
-} // namespace camera
-} // namespace aos
-
-#endif
diff --git a/aos/linux_code/camera/HTTPStreamer.cpp b/aos/linux_code/camera/HTTPStreamer.cpp
deleted file mode 100644
index 0b0c65f..0000000
--- a/aos/linux_code/camera/HTTPStreamer.cpp
+++ /dev/null
@@ -1,394 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <malloc.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/mman.h>
-#include <netinet/ip.h>
-#include <sys/socket.h>
-#include <inttypes.h>
-
-#include <vector>
-
-#include "aos/common/network_port.h"
-#include "aos/linux_code/init.h"
-#include "aos/linux_code/camera/Buffers.h"
-#include "aos/common/logging/logging.h"
-
-namespace aos {
-namespace camera {
-
-namespace {
-
-// doesn't like being a static class member
-static const unsigned char dht_table[] = {
- 0xff, 0xc4, 0x01, 0xa2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01,
- 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
- 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x01, 0x00, 0x03,
- 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
- 0x0a, 0x0b, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05,
- 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d, 0x01, 0x02, 0x03, 0x00, 0x04,
- 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22,
- 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15,
- 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36,
- 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
- 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66,
- 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
- 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95,
- 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
- 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
- 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5,
- 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
- 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
- 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
- 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
- 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,
- 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33,
- 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25,
- 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36,
- 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
- 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66,
- 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
- 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
- 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
- 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
- 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
- 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
- 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
-};
-
-const char kFirstHeader[] = "HTTP/1.0 200 OK\r\n"
-"Connection: close\r\n"
-"Server: AOS/0.0 Camera\r\n"
-"Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, "
-"post-check=0, max-age=0\r\n" // this and above from mjpg-streamer
-"Pragma: no-cache\r\n"
-"Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n" // also from mjpg-streamer
-"Content-Type: multipart/x-mixed-replace; boundary=boundarydonotcross\r\n";
-
-} // namespace
-
-class HTTPStreamer {
- // Represents a single client. Handles all reading and writing of sockets and
- // queues.
- class Client {
- enum class State {
- kReadHeaders,
- kWriteHeaders,
- kWriteBoundary, // these last 2 loop to each other
- kWriteImage,
- kWriteDHT, // happens in the middle of kWriteImage
- };
- const int sock_;
- State state_;
- inline fd_set *GetFDSetForCurrentState(fd_set *read_fds,
- fd_set *write_fds) {
- if (state_ == State::kReadHeaders) {
- return read_fds;
- } else {
- return write_fds;
- }
- }
- // MUST BE LONG ENOUGH TO HOLD kBoundaryText WITH A BIGGISH # IN IT
- char scratch_[4096];
- int to_write_;
- int zero_reads_;
- static const int kMaxZeroReads = 2000;
- size_t pos_, dht_pos_, dht_start_;
- Buffers buffers_;
- const void *current_;
- uint32_t size_;
-
- Client(const Client &);
- void operator=(const Client &);
-
- public:
- explicit Client(int sock) : sock_(sock), state_(State::kReadHeaders),
- zero_reads_(0), pos_(0) {}
- ~Client() {
- LOG(DEBUG, "closing socket %d\n", sock_);
- if (close(sock_) == -1) {
- LOG(INFO, "closing socket %d for destruction failed with %d: %s\n",
- sock_, errno, strerror(errno));
- }
- }
- // Set any fds necessary into the 2 arguments.
- void FDSet(fd_set *read_fds, fd_set *write_fds) {
- FD_SET(sock_, GetFDSetForCurrentState(read_fds, write_fds));
- }
- // The arguments are the same as the last FDSet call (after a successful
- // select).
- // Return value is whether or not to keep this one around.
- bool Process(fd_set *read_fds, fd_set *write_fds) {
- // if the socket we're waiting on isn't ready
- if (!FD_ISSET(sock_, GetFDSetForCurrentState(read_fds, write_fds))) {
- return true;
- }
-
- ssize_t ret;
- switch (state_) {
- case State::kReadHeaders:
- if (pos_ >= sizeof(scratch_)) {
- LOG(WARNING, "read too many bytes of headers on sock %d."
- " somebody should increase the size of scratch_\n", sock_);
- return false;
- }
- if (zero_reads_ > kMaxZeroReads) {
- LOG(WARNING, "read 0 bytes %d times on sock %d. giving up\n",
- zero_reads_, sock_);
- return false;
- }
- ret = read(sock_, scratch_ + pos_, sizeof(scratch_) - pos_);
- if (ret == -1) {
- LOG(WARNING, "read(%d, %p, %zd) failed with %d: %s\n",
- sock_, scratch_ + pos_, sizeof(scratch_) - pos_,
- errno, strerror(errno));
- return false;
- }
- pos_ += ret;
- // if we just received \r\n\r\n (the end of the headers)
- if (scratch_[pos_ - 4] == '\r' && scratch_[pos_ - 3] == '\n' &&
- scratch_[pos_ - 2] == '\r' && scratch_[pos_ - 1] == '\n') {
- LOG(INFO, "entering state kWriteHeaders"
- " after %zd bytes of headers read\n", pos_ - 1);
- pos_ = 0;
- state_ = State::kWriteHeaders;
- }
- scratch_[pos_] = '\0';
- if (ret == 0) {
- ++zero_reads_;
- } else {
- zero_reads_ = 0;
- LOG(DEBUG, "read %zd bytes of headers scratch_=%s\n",
- ret, scratch_);
- }
- break;
- case State::kWriteHeaders:
- // this intentionally doesn't write the terminating \0 on the string
- ret = write(sock_, kFirstHeader + pos_, sizeof(kFirstHeader) - pos_);
- if (ret == -1) {
- LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n",
- sock_, kFirstHeader + pos_, sizeof(kFirstHeader) - pos_,
- errno, strerror(errno));
- } else {
- pos_ += ret;
- if (pos_ >= sizeof(kFirstHeader)) {
- current_ = NULL;
- state_ = State::kWriteBoundary;
- }
- }
- break;
- case State::kWriteBoundary:
- if (current_ == NULL) {
- timeval timestamp;
- current_ = buffers_.GetNext(false, &size_, ×tamp, NULL);
-
- /*static int skip = 0;
- if (current_ != NULL) skip = (skip + 1) % 30;
- if (!skip) current_ = NULL;
- if (current_ == NULL) break;*/
-
-#if 0
- // set pos_ to where the first header starts
- for (pos_ = 0; static_cast<const uint8_t *>(current_)[pos_] != 0xFF;
- ++pos_);
-#else
- pos_ = 0;
-#endif
-#if 0
- // go through the frame looking for the start of frame marker
- for (dht_start_ = 0;
- static_cast<const uint8_t *>(current_)[dht_start_ + 0] !=
- 0xFF &&
- static_cast<const uint8_t *>(current_)[dht_start_ + 1] !=
- 0xC0 &&
- dht_start_ < size_; ++dht_start_)
- printf("[%zd]=%"PRIx8" ", dht_start_,
- static_cast<const uint8_t *>(current_)[dht_start_]);
- if (dht_start_ >= size_) {
- LOG(WARNING, "couldn't find start of frame marker\n");
- return false;
- }
-#else
- dht_start_ = 0;
-#endif
- dht_pos_ = 0;
-
- // aos.ChannelImageGetter depends on the exact format of this
- to_write_ = snprintf(scratch_, sizeof(scratch_),
- "\r\n--boundarydonotcross\r\n"
- "Content-Type: image/jpeg\r\n"
- "Content-Length: %" PRId32 "\r\n"
- "X-Timestamp: %ld.%06ld\r\n"
- "\r\n",
- size_,
- timestamp.tv_sec, timestamp.tv_usec);
- }
- ret = write(sock_, scratch_ + pos_, to_write_ - pos_);
- if (ret == -1) {
- LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n",
- sock_, scratch_ + pos_, to_write_ - pos_,
- errno, strerror(errno));
- return false;
- } else {
- pos_ += ret;
- if (static_cast<ssize_t>(pos_) >= to_write_) {
- pos_ = 0;
- state_ = State::kWriteImage;
- }
- }
- break;
- case State::kWriteImage:
- ret = write(sock_, static_cast<const char *>(current_) + pos_,
- ((dht_start_ == 0) ? size_ : dht_start_) - pos_);
- if (ret == -1) {
- LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n",
- sock_, static_cast<const char *>(current_) + pos_,
- ((dht_start_ == 0) ? size_ : dht_start_) - pos_,
- errno, strerror(errno));
- return false;
- } else {
- pos_ += ret;
- if (dht_start_ == 0) {
- if (pos_ >= size_) {
- buffers_.Release();
- current_ = NULL;
- state_ = State::kWriteBoundary;
- }
- } else {
- if (pos_ >= dht_start_) {
- dht_start_ = 0;
- state_ = State::kWriteDHT;
- }
- }
- }
- break;
- case State::kWriteDHT:
- ret = write(sock_, dht_table + dht_pos_,
- sizeof(dht_table) - dht_pos_);
- if (ret == -1) {
- LOG(WARNING, "write(%d, %p, %zd) failed with %d: %s\n",
- sock_, dht_table + dht_pos_, sizeof(dht_table) - dht_pos_,
- errno, strerror(errno));
- return false;
- } else {
- dht_pos_ += ret;
- if (dht_pos_ >= sizeof(dht_table)) {
- state_ = State::kWriteImage;
- }
- }
- break;
- default:
- LOG(FATAL, "something weird happened\n");
- }
- return true;
- }
- };
-
- const int bind_socket_;
-
- public:
- HTTPStreamer() : bind_socket_(socket(AF_INET, SOCK_STREAM, 0)) {
- if (bind_socket_ < 0) {
- LOG(FATAL, "socket(AF_INET, SOCK_STREAM, 0) failed with %d: %s\n",
- errno, strerror(errno));
- }
-
- union {
- sockaddr_in in;
- sockaddr addr;
- } bind_sockaddr;
- memset(&bind_sockaddr, 0, sizeof(bind_sockaddr));
- bind_sockaddr.in.sin_family = AF_INET;
- bind_sockaddr.in.sin_port =
- htons(static_cast<uint16_t>(aos::NetworkPort::kCameraStreamer));
- bind_sockaddr.in.sin_addr.s_addr = htonl(INADDR_ANY);
- int optval = 1;
- setsockopt(bind_socket_, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
- if (bind(bind_socket_, &bind_sockaddr.addr,
- sizeof(bind_sockaddr.addr)) == -1) {
- LOG(FATAL, "bind(%d, %p) failed because of %d: %s\n",
- bind_socket_, &bind_sockaddr.addr, errno, strerror(errno));
- }
-
- if (listen(bind_socket_, 10) == -1) {
- LOG(FATAL, "listen(%d, 10) failed because of %d: %s\n", bind_socket_,
- errno, strerror(errno));
- }
- const int flags = fcntl(bind_socket_, F_GETFL, 0);
- if (flags == -1) {
- LOG(FATAL, "fcntl(%d, F_GETFL, 0) failed because of %d: %s\n",
- bind_socket_, errno, strerror(errno));
- }
- if (fcntl(bind_socket_, F_SETFL, flags | O_NONBLOCK) == -1) {
- LOG(FATAL, "fcntl(%d, F_SETFL, %x) failed because of %d: %s\n",
- bind_socket_, flags | O_NONBLOCK, errno, strerror(errno));
- }
- }
- void Run() {
- signal(SIGPIPE, SIG_IGN);
-
- std::vector<Client *> clients;
- fd_set read_fds, write_fds;
- while (true) {
- FD_ZERO(&read_fds);
- FD_ZERO(&write_fds);
- FD_SET(bind_socket_, &read_fds);
- for (auto it = clients.begin(); it != clients.end(); ++it) {
- (*it)->FDSet(&read_fds, &write_fds);
- }
- switch (select(FD_SETSIZE, &read_fds, &write_fds,
- NULL, // err
- NULL)) { // timeout
- case -1:
- LOG(ERROR, "select(FD_SETSIZE(=%d), %p, %p, NULL, NULL) failed"
- " because of %d: %s\n", FD_SETSIZE, &read_fds, &write_fds,
- errno, strerror(errno));
- continue;
- case 0:
- LOG(ERROR, "select with NULL timeout timed out...\n");
- continue;
- }
-
- if (FD_ISSET(bind_socket_, &read_fds)) {
- const int sock = accept4(bind_socket_, NULL, NULL, SOCK_NONBLOCK);
- if (sock == -1) {
- LOG(ERROR, "accept4(%d, NULL, NULL, SOCK_NONBLOCK(=%d) failed"
- " because of %d: %s\n",
- bind_socket_, SOCK_NONBLOCK, errno, strerror(errno));
- } else {
- clients.push_back(new Client(sock));
- }
- }
-
- std::vector<std::vector<Client *>::iterator> to_remove;
- for (auto it = clients.begin(); it != clients.end(); ++it) {
- if (!(*it)->Process(&read_fds, &write_fds)) {
- to_remove.push_back(it);
- delete *it;
- }
- }
- for (auto it = to_remove.rbegin(); it != to_remove.rend(); ++it) {
- LOG(INFO, "removing client\n");
- clients.erase(*it);
- }
- }
- }
-};
-
-} // namespace camera
-} // namespace aos
-
-int main() {
- ::aos::InitNRT();
- ::aos::camera::HTTPStreamer streamer;
- streamer.Run();
- ::aos::Cleanup();
-}
diff --git a/aos/linux_code/camera/Reader.cpp b/aos/linux_code/camera/Reader.cpp
deleted file mode 100644
index cf77548..0000000
--- a/aos/linux_code/camera/Reader.cpp
+++ /dev/null
@@ -1,419 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
-#include <malloc.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/time.h>
-#include <sys/mman.h>
-
-#include <string>
-#include <inttypes.h>
-
-#include "aos/linux_code/init.h"
-#include "aos/linux_code/camera/V4L2.h"
-#include "aos/linux_code/camera/Buffers.h"
-#include "aos/common/logging/logging.h"
-#include "aos/linux_code/ipc_lib/queue.h"
-
-#define CLEAR(x) memset(&(x), 0, sizeof(x))
-
-namespace aos {
-namespace camera {
-
-class Reader {
- static const char *const dev_name;
-
- // of the camera
- int fd_;
- // the bound socket listening for fd requests
- int server_fd_;
-
- RawQueue *queue_, *recycle_queue_;
- // the number of buffers currently queued in v4l2
- uint32_t queued_;
- public:
- Reader() {
- struct stat st;
- if (stat(dev_name, &st) == -1) {
- LOG(FATAL, "Cannot identify '%s' because of %d: %s\n",
- dev_name, errno, strerror(errno));
- }
- if (!S_ISCHR(st.st_mode)) {
- LOG(FATAL, "%s is no device\n", dev_name);
- }
-
- fd_ = open(dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
- if (fd_ == -1) {
- LOG(FATAL, "Cannot open '%s' because of %d: %s\n",
- dev_name, errno, strerror(errno));
- }
-
- queue_ = RawQueue::Fetch(Buffers::kQueueName.c_str(),
- sizeof(Buffers::Message), 971, 1,
- 1, Buffers::kNumBuffers, &recycle_queue_);
- // read off any existing recycled messages
- while (recycle_queue_->ReadMessage(RawQueue::kNonBlock) != NULL);
- queued_ = 0;
-
- InitServer();
- Init();
- }
- private:
- void InitServer() {
- if (unlink(Buffers::kFDServerName.c_str()) == -1 && errno != ENOENT) {
- LOG(WARNING, "unlink(kFDServerName(='%s')) failed with %d: %s\n",
- Buffers::kFDServerName.c_str(), errno, strerror(errno));
- }
- if ((server_fd_ = Buffers::CreateSocket(bind)) == -1) {
- LOG(FATAL, "creating the IPC socket failed\n");
- }
- if (listen(server_fd_, 10) == -1) {
- LOG(FATAL, "listen(%d, 10) failed with %d: %s\n",
- server_fd_, errno, strerror(errno));
- }
- }
- void SendFD(const int sock) {
- int myfds[Buffers::kNumFDs]; /* Contains the file descriptors to pass. */
- myfds[0] = fd_;
- char buf[CMSG_SPACE(sizeof(myfds))]; /* ancillary data buffer */
-
- iovec data;
- memset(&data, 0, sizeof(data));
- char dummy = 'A';
- data.iov_base = &dummy;
- data.iov_len = sizeof(dummy);
- msghdr msg;
- memset(&msg, 0, sizeof(msg));
- msg.msg_iov = &data;
- msg.msg_iovlen = 1;
- msg.msg_control = buf;
- msg.msg_controllen = sizeof(buf);
- cmsghdr *const cmsg = CMSG_FIRSTHDR(&msg);
- cmsg->cmsg_level = SOL_SOCKET;
- cmsg->cmsg_type = SCM_RIGHTS;
- cmsg->cmsg_len = CMSG_LEN(sizeof(myfds));
- /* Initialize the payload: */
- memcpy(CMSG_DATA(cmsg), myfds, sizeof(myfds));
- if (sendmsg(sock, &msg, 0) == -1) {
- LOG(ERROR, "sendmsg(%d, %p, 0) failed with %d: %s\n",
- sock, &msg, errno, strerror(errno));
- }
- // leave it open so that the other end can tell if this process dies
- }
-
-#if 0
- // if we ever do want to do any of these things, this is how
- void Stop() {
- const v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (xioctl(fd_, VIDIOC_STREAMOFF, &type) == -1) {
- errno_exit("VIDIOC_STREAMOFF");
- }
- }
- void Close() {
- if (close(fd_) == -1)
- errno_exit("close");
- fd_ = -1;
- }
-#endif
-
- void QueueBuffer(v4l2_buffer *buf) {
- if (xioctl(fd_, VIDIOC_QBUF, buf) == -1) {
- LOG(WARNING, "ioctl VIDIOC_QBUF(%d, %p) failed with %d: %s."
- " losing buf #%" PRIu32 "\n",
- fd_, &buf, errno, strerror(errno), buf->index);
- } else {
- LOG(DEBUG, "put buf #%" PRIu32 " into driver's queue\n", buf->index);
- ++queued_;
- }
- }
- void ReadFrame() {
- v4l2_buffer buf;
- CLEAR(buf);
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
-
- const Buffers::Message *read;
- do {
- read = static_cast<const Buffers::Message *>(
- // we block waiting for one if we can't dequeue one without leaving
- // the driver <= 2 (to be safe)
- recycle_queue_->ReadMessage((queued_ <= 2) ?
- RawQueue::kBlock : RawQueue::kNonBlock));
- if (read != NULL) {
- buf.index = read->index;
- recycle_queue_->FreeMessage(read);
- QueueBuffer(&buf);
- }
- } while (read != NULL);
-
- if (xioctl(fd_, VIDIOC_DQBUF, &buf) == -1) {
- if (errno != EAGAIN) {
- LOG(ERROR, "ioctl VIDIOC_DQBUF(%d, %p) failed with %d: %s\n",
- fd_, &buf, errno, strerror(errno));
- }
- return;
- }
- --queued_;
- if (buf.index >= Buffers::kNumBuffers) {
- LOG(ERROR, "buf.index (%" PRIu32 ") is >= kNumBuffers (%u)\n",
- buf.index, Buffers::kNumBuffers);
- return;
- }
-
- Buffers::Message *const msg = static_cast<Buffers::Message *>(
- queue_->GetMessage());
- if (msg == NULL) {
- LOG(WARNING,
- "couldn't get a message to send buf #%" PRIu32 " from queue %p."
- " re-queueing now\n", buf.index, queue_);
- QueueBuffer(&buf);
- return;
- }
- msg->index = buf.index;
- msg->bytesused = buf.bytesused;
- memcpy(&msg->timestamp, &buf.timestamp, sizeof(msg->timestamp));
- msg->sequence = buf.sequence;
- if (!queue_->WriteMessage(msg, RawQueue::kOverride)) {
- LOG(WARNING,
- "sending message %p with buf #%" PRIu32 " to queue %p failed."
- " re-queueing now\n", msg, buf.index, queue_);
- QueueBuffer(&buf);
- return;
- } else {
- LOG(DEBUG, "sent message off to queue %p with buffer #%" PRIu32 "\n",
- queue_, buf.index);
- }
- }
-
- void init_mmap() {
- v4l2_requestbuffers req;
- CLEAR(req);
- req.count = Buffers::kNumBuffers;
- req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- req.memory = V4L2_MEMORY_MMAP;
- if (xioctl(fd_, VIDIOC_REQBUFS, &req) == -1) {
- if (EINVAL == errno) {
- LOG(FATAL, "%s does not support memory mapping\n", dev_name);
- } else {
- LOG(FATAL, "ioctl VIDIOC_REQBUFS(%d, %p) failed with %d: %s\n",
- fd_, &req, errno, strerror(errno));
- }
- }
- queued_ = Buffers::kNumBuffers;
- if (req.count < Buffers::kNumBuffers) {
- LOG(FATAL, "Insufficient buffer memory on %s\n", dev_name);
- }
- }
-
- // Sets one of the camera's user-control values.
- // Prints the old and new values.
- // Just prints a message if the camera doesn't support this control or value.
- bool SetCameraControl(uint32_t id, const char *name, int value) {
- struct v4l2_control getArg = {id, 0U};
- int r = xioctl(fd_, VIDIOC_G_CTRL, &getArg);
- if (r == 0) {
- if (getArg.value == value) {
- printf("Camera control %s was already %d\n", name, getArg.value);
- return true;
- }
- } else if (errno == EINVAL) {
- printf("Camera control %s is invalid\n", name);
- errno = 0;
- return false;
- }
-
- struct v4l2_control setArg = {id, value};
- r = xioctl(fd_, VIDIOC_S_CTRL, &setArg);
- if (r == 0) {
- printf("Set camera control %s from %d to %d\n", name, getArg.value, value);
- return true;
- }
-
- printf("Couldn't set camera control %s to %d: %s (errno %d)\n",
- name, value, strerror(errno), errno);
- errno = 0;
- return false;
- }
-
- void Init() {
- v4l2_capability cap;
- if (xioctl(fd_, VIDIOC_QUERYCAP, &cap) == -1) {
- if (EINVAL == errno) {
- LOG(FATAL, "%s is no V4L2 device\n",
- dev_name);
- } else {
- LOG(FATAL, "ioctl VIDIOC_QUERYCAP(%d, %p) failed with %d: %s\n",
- fd_, &cap, errno, strerror(errno));
- }
- }
- if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
- LOG(FATAL, "%s is no video capture device\n",
- dev_name);
- }
- if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
- LOG(FATAL, "%s does not support streaming i/o\n",
- dev_name);
- }
-
- /* Select video input, video standard and tune here. */
-
- v4l2_cropcap cropcap;
- CLEAR(cropcap);
- cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (xioctl(fd_, VIDIOC_CROPCAP, &cropcap) == 0) {
- v4l2_crop crop;
- crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- crop.c = cropcap.defrect; /* reset to default */
-
- if (xioctl(fd_, VIDIOC_S_CROP, &crop) == -1) {
- switch (errno) {
- case EINVAL:
- /* Cropping not supported. */
- break;
- default:
- /* Errors ignored. */
- break;
- }
- }
- } else {
- /* Errors ignored. */
- }
-
- v4l2_format fmt;
- CLEAR(fmt);
- fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- fmt.fmt.pix.width = Buffers::kWidth;
- fmt.fmt.pix.height = Buffers::kHeight;
- fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
- fmt.fmt.pix.field = V4L2_FIELD_ANY;
- //fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
- //fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
- if (xioctl(fd_, VIDIOC_S_FMT, &fmt) == -1) {
- LOG(FATAL, "ioctl VIDIC_S_FMT(%d, %p) failed with %d: %s\n",
- fd_, &fmt, errno, strerror(errno));
- }
- /* Note VIDIOC_S_FMT may change width and height. */
-
- /* Buggy driver paranoia. */
- unsigned int min = fmt.fmt.pix.width * 2;
- if (fmt.fmt.pix.bytesperline < min)
- fmt.fmt.pix.bytesperline = min;
- min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
- if (fmt.fmt.pix.sizeimage < min)
- fmt.fmt.pix.sizeimage = min;
-
- if (!SetCameraControl(V4L2_CID_EXPOSURE_AUTO,
- "V4L2_CID_EXPOSURE_AUTO", V4L2_EXPOSURE_MANUAL)) {
- LOG(FATAL, "Failed to set exposure\n");
- }
-
- if (!SetCameraControl(V4L2_CID_EXPOSURE_ABSOLUTE,
- "V4L2_CID_EXPOSURE_ABSOLUTE", 65)) {
- LOG(FATAL, "Failed to set exposure\n");
- }
-
- if (!SetCameraControl(V4L2_CID_BRIGHTNESS, "V4L2_CID_BRIGHTNESS", 128)) {
- LOG(FATAL, "Failed to set up camera\n");
- }
-
- if (!SetCameraControl(V4L2_CID_GAIN, "V4L2_CID_GAIN", 0)) {
- LOG(FATAL, "Failed to set up camera\n");
- }
-
-#if 0
- // set framerate
- struct v4l2_streamparm *setfps;
- setfps = (struct v4l2_streamparm *) calloc(1, sizeof(struct v4l2_streamparm));
- memset(setfps, 0, sizeof(struct v4l2_streamparm));
- setfps->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- setfps->parm.capture.timeperframe.numerator = 1;
- setfps->parm.capture.timeperframe.denominator = 20;
- if (xioctl(fd_, VIDIOC_S_PARM, setfps) == -1) {
- LOG(ERROR, "ioctl VIDIOC_S_PARM(%d, %p) failed with %d: %s\n",
- fd_, setfps, errno, strerror(errno));
- exit(EXIT_FAILURE);
- }
- LOG(INFO, "framerate ended up at %d/%d\n",
- setfps->parm.capture.timeperframe.numerator,
- setfps->parm.capture.timeperframe.denominator);
-#endif
-
- init_mmap();
- }
-
- void Start() {
- LOG(DEBUG, "queueing buffers for the first time\n");
- v4l2_buffer buf;
- for (unsigned int i = 0; i < Buffers::kNumBuffers; ++i) {
- CLEAR(buf);
- buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- buf.memory = V4L2_MEMORY_MMAP;
- buf.index = i;
- QueueBuffer(&buf);
- }
- LOG(DEBUG, "done with first queue\n");
-
- v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
- if (xioctl(fd_, VIDIOC_STREAMON, &type) == -1) {
- LOG(FATAL, "ioctl VIDIOC_STREAMON(%d, %p) failed with %d: %s\n",
- fd_, &type, errno, strerror(errno));
- }
- }
-
- public:
- void Run() {
- Start();
-
- fd_set fds;
- timeval tv;
- while (true) {
- // HAVE TO DO THIS EACH TIME THROUGH THE LOOP
- tv.tv_sec = 2;
- tv.tv_usec = 0;
-
- FD_ZERO(&fds);
- FD_SET(fd_, &fds);
- FD_SET(server_fd_, &fds);
- switch (select(std::max(fd_, server_fd_) + 1, &fds, NULL, NULL, &tv)) {
- case -1:
- if (errno != EINTR) {
- LOG(ERROR, "select(%d, %p, NULL, NULL, %p) failed with %d: %s\n",
- std::max(fd_, server_fd_) + 1, &fds, &tv, errno, strerror(errno));
- }
- continue;
- case 0:
- LOG(WARNING, "select timed out\n");
- continue;
- }
-
- if (FD_ISSET(fd_, &fds)) {
- LOG(DEBUG, "Got a frame\n");
- ReadFrame();
- }
- if (FD_ISSET(server_fd_, &fds)) {
- const int sock = accept4(server_fd_, NULL, NULL, SOCK_NONBLOCK);
- if (sock == -1) {
- LOG(ERROR, "accept4(%d, NULL, NULL, SOCK_NONBLOCK(=%d) failed with %d: %s\n",
- server_fd_, SOCK_NONBLOCK, errno, strerror(errno));
- } else {
- SendFD(sock);
- }
- }
- }
- }
-};
-const char *const Reader::dev_name = "/dev/video0";
-
-} // namespace camera
-} // namespace aos
-
-int main() {
- ::aos::InitNRT();
- ::aos::camera::Reader reader;
- reader.Run();
- ::aos::Cleanup();
-}
diff --git a/aos/linux_code/camera/V4L2.h b/aos/linux_code/camera/V4L2.h
deleted file mode 100644
index e018bea..0000000
--- a/aos/linux_code/camera/V4L2.h
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef AOS_LINUX_CODE_CAMREA_V4L2_H_
-#define AOS_LINUX_CODE_CAMREA_V4L2_H_
-
-// This file handles including everything needed to use V4L2 and has some
-// utility functions.
-
-#include <sys/ioctl.h>
-
-#include <asm/types.h> /* for videodev2.h */
-#include <linux/videodev2.h>
-
-namespace aos {
-namespace camera {
-
-static inline int xioctl(int fd, int request, void *arg) {
- int r;
- do {
- r = ioctl(fd, request, reinterpret_cast<uintptr_t>(arg));
- } while (r == -1 && errno == EINTR);
- return r;
-}
-
-} // namespace camera
-} // namespace aos
-
-#endif
-
diff --git a/aos/linux_code/camera/aos.jar_manifest b/aos/linux_code/camera/aos.jar_manifest
deleted file mode 100644
index 8b13789..0000000
--- a/aos/linux_code/camera/aos.jar_manifest
+++ /dev/null
@@ -1 +0,0 @@
-
diff --git a/aos/linux_code/camera/camera.gyp b/aos/linux_code/camera/camera.gyp
deleted file mode 100644
index 119d105..0000000
--- a/aos/linux_code/camera/camera.gyp
+++ /dev/null
@@ -1,45 +0,0 @@
-{
- 'targets': [
- {
- 'target_name': 'buffers',
- 'type': 'static_library',
- 'sources': [
- 'Buffers.cpp',
- ],
- 'dependencies': [
- '<(AOS)/linux_code/ipc_lib/ipc_lib.gyp:queue',
- '<(AOS)/build/aos.gyp:logging',
- '<(AOS)/atom_code/ipc_lib/ipc_lib.gyp:scoped_message_ptr',
- ],
- 'export_dependent_settings': [
- '<(AOS)/linux_code/ipc_lib/ipc_lib.gyp:queue',
- '<(AOS)/linux_code/ipc_lib/ipc_lib.gyp:scoped_message_ptr',
- ],
- },
- {
- 'target_name': 'CameraHTTPStreamer',
- 'type': 'executable',
- 'sources': [
- 'HTTPStreamer.cpp',
- ],
- 'dependencies': [
- 'buffers',
- '<(AOS)/linux_code/linux_code.gyp:init',
- '<(AOS)/build/aos.gyp:logging',
- ],
- },
- {
- 'target_name': 'CameraReader',
- 'type': 'executable',
- 'sources': [
- 'Reader.cpp',
- ],
- 'dependencies': [
- 'buffers',
- '<(AOS)/linux_code/linux_code.gyp:init',
- '<(AOS)/build/aos.gyp:logging',
- '<(AOS)/linux_code/ipc_lib/ipc_lib.gyp:queue',
- ],
- },
- ],
-}