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