-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpd.h
87 lines (69 loc) · 2.29 KB
/
httpd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef _HTTPD_H
#define _HTTPD_H
// A class dealing with stream output to HTTP.
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#include <atomic>
#include <condition_variable>
#include <deque>
#include <mutex>
#include <set>
#include <string>
struct MHD_Connection;
struct MHD_Daemon;
class HTTPD {
public:
HTTPD();
~HTTPD();
// Should be called before start().
void set_header(const std::string &data) {
header = data;
}
void start(int port);
void add_data(const char *buf, size_t size, bool keyframe);
private:
static int answer_to_connection_thunk(void *cls, MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls);
int answer_to_connection(MHD_Connection *connection,
const char *url, const char *method,
const char *version, const char *upload_data,
size_t *upload_data_size, void **con_cls);
static void free_stream(void *cls);
class Stream {
public:
enum Framing {
FRAMING_RAW,
FRAMING_METACUBE
};
Stream(HTTPD *parent, Framing framing) : parent(parent), framing(framing) {}
static ssize_t reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max);
ssize_t reader_callback(uint64_t pos, char *buf, size_t max);
enum DataType {
DATA_TYPE_HEADER,
DATA_TYPE_KEYFRAME,
DATA_TYPE_OTHER
};
void add_data(const char *buf, size_t size, DataType data_type);
void stop();
HTTPD *get_parent() const { return parent; }
private:
HTTPD *parent;
Framing framing;
std::mutex buffer_mutex;
bool should_quit = false; // Under <buffer_mutex>.
std::condition_variable has_buffered_data;
std::deque<std::string> buffered_data; // Protected by <buffer_mutex>.
size_t used_of_buffered_data = 0; // How many bytes of the first element of <buffered_data> that is already used. Protected by <mutex>.
size_t seen_keyframe = false;
};
MHD_Daemon *mhd = nullptr;
std::mutex streams_mutex;
std::set<Stream *> streams; // Not owned.
std::string header;
// Metrics.
std::atomic<int64_t> metric_num_connected_clients{0};
};
#endif // !defined(_HTTPD_H)