blob: 7f1206d0d56a61534277ca3a215beb0e1f18022a [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#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
9#include "aos/aos_core.h"
10#include "aos/common/type_traits.h"
11
12namespace aos {
13namespace camera {
14
15class Reader;
16class 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;
56 static const aos_type_sig kSignature;
57 // NULL for the Reader one.
58 aos_queue *queue_;
59 // Make the actual mmap calls.
60 // Called by Buffers() automatically.
61 void MMap();
62 public:
63 Buffers();
64 // Will clean everything up.
65 // So that HTTPStreamer can create/destroy one for each client to make it
66 // simpler.
67 ~Buffers();
68
69 // Retrieves the next image. Will return the current one if it hasn't yet.
70 // Calls Release() at the beginning.
71 // NOTE: this means that the caller can't keep using references to the old
72 // return value after calling this function again
73 // block is whether to return NULL or wait for a new one
74 // the last 3 output parameters will be filled in straight from the
75 // v4l2_buffer if they're not NULL
76 // (see <http://v4l2spec.bytesex.org/spec/x5953.htm#V4L2-BUFFER> for details)
77 // NOTE: guaranteed to return a valid pointer if block is true
78 const void *GetNext(bool block,
79 uint32_t *bytesused, timeval *timestamp, uint32_t *sequence);
80 // Releases the most recent frame buffer. Automatically called by GetNext and
81 // the destructor. Safe to call multiple times without getting frames in
82 // between.
83 void Release();
84
85 // How big images are.
86 static const int32_t kWidth = 640, kHeight = 480;
87};
88
89} // namespace camera
90} // namespace aos
91
92#endif
93