brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 1 | #ifndef AOS_ATOM_CODE_CAMERA_CAMERA_BUFFERS_H_ |
| 2 | #define AOS_ATOM_CODE_CAMERA_CAMERA_BUFFERS_H_ |
| 3 | |
| 4 | #include <sys/socket.h> |
| 5 | #include <sys/un.h> |
| 6 | |
| 7 | #include <string> |
| 8 | |
Brian Silverman | f665d69 | 2013-02-17 22:11:39 -0800 | [diff] [blame] | 9 | #include "aos/atom_code/ipc_lib/queue.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 10 | #include "aos/common/type_traits.h" |
| 11 | |
| 12 | namespace aos { |
| 13 | namespace camera { |
| 14 | |
| 15 | class Reader; |
| 16 | class Buffers { |
| 17 | // It has to do a lot of the same things as all the other ones, but it gets |
| 18 | // the information from different places (some of it gets sent out by it). |
| 19 | friend class Reader; |
| 20 | // Not an abstract name so that an existing one can just be unlinked without |
| 21 | // disturbing it if necessary (like with shm_link). |
| 22 | static const std::string kFDServerName; |
| 23 | // How many v4l2 buffers and other things that depend on that. |
| 24 | static const unsigned int kNumBuffers = 10; |
| 25 | // How many fds to transfer from the fd server. |
| 26 | // Used to make it clear which 1s are 1 and which are this. |
| 27 | static const size_t kNumFDs = 1; |
| 28 | // Creates a socket and calls either bind or connect. |
| 29 | // Returns a bound/connected socket or -1 (the reason for which will already |
| 30 | // have been logged at ERROR). |
| 31 | static int CreateSocket(int (*bind_connect)(int, const sockaddr *, socklen_t)); |
| 32 | |
| 33 | // File descriptor connected to the fd server. Used for detecting if the |
| 34 | // camera reading process has died. |
| 35 | // A value of -2 means don't check. |
| 36 | const int server_; |
| 37 | // Gets the fd (using server_). |
| 38 | int FetchFD(); |
| 39 | // File descriptor for the v4l2 device (that's valid in this process of |
| 40 | // course). |
| 41 | const int fd_; |
| 42 | |
| 43 | struct Buffer; |
| 44 | // Buffer[kNumBuffers] |
| 45 | Buffer *buffers_; |
| 46 | struct Message { |
| 47 | uint32_t index; |
| 48 | uint32_t bytesused; |
| 49 | timeval timestamp; |
| 50 | uint32_t sequence; |
| 51 | }; |
| 52 | static_assert(shm_ok<Message>::value, "it's going through queues"); |
| 53 | // The current one. Sometimes NULL. |
| 54 | const Message *message_; |
| 55 | static const std::string kQueueName; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 56 | // NULL for the Reader one. |
Brian Silverman | 08661c7 | 2013-09-01 17:24:38 -0700 | [diff] [blame] | 57 | RawQueue *queue_; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 58 | // Make the actual mmap calls. |
| 59 | // Called by Buffers() automatically. |
| 60 | void MMap(); |
| 61 | public: |
| 62 | Buffers(); |
| 63 | // Will clean everything up. |
| 64 | // So that HTTPStreamer can create/destroy one for each client to make it |
| 65 | // simpler. |
| 66 | ~Buffers(); |
| 67 | |
| 68 | // Retrieves the next image. Will return the current one if it hasn't yet. |
| 69 | // Calls Release() at the beginning. |
| 70 | // NOTE: this means that the caller can't keep using references to the old |
| 71 | // return value after calling this function again |
| 72 | // block is whether to return NULL or wait for a new one |
| 73 | // the last 3 output parameters will be filled in straight from the |
| 74 | // v4l2_buffer if they're not NULL |
| 75 | // (see <http://v4l2spec.bytesex.org/spec/x5953.htm#V4L2-BUFFER> for details) |
| 76 | // NOTE: guaranteed to return a valid pointer if block is true |
| 77 | const void *GetNext(bool block, |
| 78 | uint32_t *bytesused, timeval *timestamp, uint32_t *sequence); |
| 79 | // Releases the most recent frame buffer. Automatically called by GetNext and |
| 80 | // the destructor. Safe to call multiple times without getting frames in |
| 81 | // between. |
| 82 | void Release(); |
| 83 | |
| 84 | // How big images are. |
Brian Silverman | 6ae77dd | 2013-03-29 22:28:08 -0700 | [diff] [blame] | 85 | static const int32_t kWidth = 320, kHeight = 240; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace camera |
| 89 | } // namespace aos |
| 90 | |
| 91 | #endif |
| 92 | |