blob: c73b3d8c398d6642d082b57839310067cb9944fc [file] [log] [blame]
Brian Silverman14fd0fb2014-01-14 21:42:01 -08001#ifndef AOS_LINUX_CODE_CAMERA_CAMERA_BUFFERS_H_
2#define AOS_LINUX_CODE_CAMERA_CAMERA_BUFFERS_H_
brians343bc112013-02-10 01:53:46 +00003
4#include <sys/socket.h>
5#include <sys/un.h>
6
7#include <string>
8
Brian Silverman14fd0fb2014-01-14 21:42:01 -08009#include "aos/linux_code/ipc_lib/queue.h"
brians343bc112013-02-10 01:53:46 +000010#include "aos/common/type_traits.h"
Brian Silvermane8b29b62014-03-05 17:11:54 -080011#include "aos/linux_code/ipc_lib/unique_message_ptr.h"
brians343bc112013-02-10 01:53:46 +000012
13namespace aos {
14namespace camera {
15
16class Reader;
17class 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 Silverman5f8c4922014-02-11 21:22:38 -080021
brians343bc112013-02-10 01:53:46 +000022 // 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 Silverman5f8c4922014-02-11 21:22:38 -080055
brians343bc112013-02-10 01:53:46 +000056 // NULL for the Reader one.
Brian Silverman5f8c4922014-02-11 21:22:38 -080057 RawQueue *const queue_;
58 // The current one. Sometimes NULL.
59 unique_message_ptr<const Message> message_;
60
61 static const std::string kQueueName;
brians343bc112013-02-10 01:53:46 +000062 // Make the actual mmap calls.
63 // Called by Buffers() automatically.
64 void MMap();
Brian Silverman5f8c4922014-02-11 21:22:38 -080065
brians343bc112013-02-10 01:53:46 +000066 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 Silverman6ae77dd2013-03-29 22:28:08 -070090 static const int32_t kWidth = 320, kHeight = 240;
brians343bc112013-02-10 01:53:46 +000091};
92
93} // namespace camera
94} // namespace aos
95
96#endif