]> git.sesse.net Git - nageru/blob - httpd.cpp
Pull the file muxing out of the HTTPD. (It was pretty ugly all along.)
[nageru] / httpd.cpp
1 #include <assert.h>
2 #include <microhttpd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 extern "C" {
8 #include <libavcodec/avcodec.h>
9 #include <libavutil/channel_layout.h>
10 #include <libavutil/mathematics.h>
11 #include <libavutil/mem.h>
12 #include <libavutil/pixfmt.h>
13 #include <libavutil/rational.h>
14 #include <libavutil/samplefmt.h>
15 }
16
17 #include <vector>
18
19 #include "httpd.h"
20
21 #include "defs.h"
22 #include "flags.h"
23 #include "timebase.h"
24
25 struct MHD_Connection;
26 struct MHD_Response;
27
28 using namespace std;
29
30 HTTPD::HTTPD(int width, int height)
31         : width(width), height(height)
32 {
33 }
34
35 void HTTPD::start(int port)
36 {
37         MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
38                          port,
39                          nullptr, nullptr,
40                          &answer_to_connection_thunk, this,
41                          MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this, 
42                          MHD_OPTION_END);
43 }
44
45 void HTTPD::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
46 {
47         unique_lock<mutex> lock(streams_mutex);
48         for (Stream *stream : streams) {
49                 stream->add_packet(pkt, pts, dts);
50         }
51 }
52
53 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
54                                       const char *url, const char *method,
55                                       const char *version, const char *upload_data,
56                                       size_t *upload_data_size, void **con_cls)
57 {
58         HTTPD *httpd = (HTTPD *)cls;
59         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
60 }
61
62 int HTTPD::answer_to_connection(MHD_Connection *connection,
63                                 const char *url, const char *method,
64                                 const char *version, const char *upload_data,
65                                 size_t *upload_data_size, void **con_cls)
66 {
67         AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
68         assert(oformat != nullptr);
69
70         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
71         HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height, time_base);
72         {
73                 unique_lock<mutex> lock(streams_mutex);
74                 streams.insert(stream);
75         }
76         *con_cls = stream;
77
78         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
79         MHD_Response *response = MHD_create_response_from_callback(
80                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
81         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
82         //MHD_destroy_response(response);
83
84         return ret;
85 }
86
87 void HTTPD::free_stream(void *cls)
88 {
89         // FIXME: When is this actually called, if ever?
90         // Also, shouldn't we remove it from streams?
91         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
92         delete stream;
93 }
94
95 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
96 {
97         HTTPD *httpd = (HTTPD *)cls;
98         return httpd->request_completed(connection, con_cls, toe);
99 }
100
101 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
102 {
103         if (con_cls == nullptr) {
104                 // Request was never set up.
105                 return;
106         }
107         HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
108         {
109                 unique_lock<mutex> lock(streams_mutex);
110                 delete stream;
111                 streams.erase(stream);
112         }
113 }
114
115 HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_base)
116 {
117         AVFormatContext *avctx = avformat_alloc_context();
118         avctx->oformat = oformat;
119         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
120         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &HTTPD::Stream::write_packet_thunk, nullptr);
121
122         Mux::Codec video_codec;
123         if (global_flags.uncompressed_video_to_http) {
124                 video_codec = Mux::CODEC_NV12;
125         } else {
126                 video_codec = Mux::CODEC_H264;
127         }
128
129         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
130
131         mux.reset(new Mux(avctx, width, height, video_codec, time_base));
132 }
133
134 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
135 {
136         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
137         return stream->reader_callback(pos, buf, max);
138 }
139
140 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
141 {
142         unique_lock<mutex> lock(buffer_mutex);
143         has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
144
145         ssize_t ret = 0;
146         while (max > 0 && !buffered_data.empty()) {
147                 const string &s = buffered_data.front();
148                 assert(s.size() > used_of_buffered_data);
149                 size_t len = s.size() - used_of_buffered_data;
150                 if (max >= len) {
151                         // Consume the entire (rest of the) string.
152                         memcpy(buf, s.data() + used_of_buffered_data, len);
153                         buf += len;
154                         ret += len;
155                         max -= len;
156                         buffered_data.pop_front();
157                         used_of_buffered_data = 0;
158                 } else {
159                         // We don't need the entire string; just use the first part of it.
160                         memcpy(buf, s.data() + used_of_buffered_data, max);
161                         buf += max;
162                         used_of_buffered_data += max;
163                         ret += max;
164                         max = 0;
165                 }
166         }
167
168         return ret;
169 }
170
171 void HTTPD::Stream::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
172 {
173         mux->add_packet(pkt, pts, dts);
174 }
175
176 int HTTPD::Stream::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
177 {
178         HTTPD::Stream *stream = (HTTPD::Stream *)opaque;
179         return stream->write_packet(buf, buf_size);
180 }
181
182 int HTTPD::Stream::write_packet(uint8_t *buf, int buf_size)
183 {
184         unique_lock<mutex> lock(buffer_mutex);
185         buffered_data.emplace_back((char *)buf, buf_size);
186         has_buffered_data.notify_all(); 
187         return buf_size;
188 }
189