]> git.sesse.net Git - nageru/blob - httpd.cpp
Fix video in saved files.
[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, PacketDestination destination)
46 {
47         unique_lock<mutex> lock(streams_mutex);
48         if (destination != DESTINATION_FILE_ONLY) {
49                 for (Stream *stream : streams) {
50                         stream->add_packet(pkt, pts, dts);
51                 }
52         }
53         if (file_mux && destination != DESTINATION_HTTP_ONLY) {
54                 file_mux->add_packet(pkt, pts, dts);
55         }
56 }
57
58 void HTTPD::open_output_file(const string &filename)
59 {
60         AVFormatContext *avctx = avformat_alloc_context();
61         avctx->oformat = av_guess_format(NULL, filename.c_str(), NULL);
62         assert(filename.size() < sizeof(avctx->filename) - 1);
63         strcpy(avctx->filename, filename.c_str());
64
65         string url = "file:" + filename;
66         int ret = avio_open2(&avctx->pb, url.c_str(), AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL);
67         if (ret < 0) {
68                 char tmp[AV_ERROR_MAX_STRING_SIZE];
69                 fprintf(stderr, "%s: avio_open2() failed: %s\n", filename.c_str(), av_make_error_string(tmp, sizeof(tmp), ret));
70                 exit(1);
71         }
72
73         file_mux.reset(new Mux(avctx, width, height, Mux::CODEC_H264));
74 }
75
76 void HTTPD::close_output_file()
77 {
78         file_mux.reset();
79 }
80
81 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
82                                       const char *url, const char *method,
83                                       const char *version, const char *upload_data,
84                                       size_t *upload_data_size, void **con_cls)
85 {
86         HTTPD *httpd = (HTTPD *)cls;
87         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
88 }
89
90 int HTTPD::answer_to_connection(MHD_Connection *connection,
91                                 const char *url, const char *method,
92                                 const char *version, const char *upload_data,
93                                 size_t *upload_data_size, void **con_cls)
94 {
95         AVOutputFormat *oformat = av_guess_format(STREAM_MUX_NAME, nullptr, nullptr);
96         assert(oformat != nullptr);
97         HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height);
98         {
99                 unique_lock<mutex> lock(streams_mutex);
100                 streams.insert(stream);
101         }
102         *con_cls = stream;
103
104         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
105         MHD_Response *response = MHD_create_response_from_callback(
106                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
107         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
108         //MHD_destroy_response(response);
109
110         return ret;
111 }
112
113 void HTTPD::free_stream(void *cls)
114 {
115         // FIXME: When is this actually called, if ever?
116         // Also, shouldn't we remove it from streams?
117         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
118         delete stream;
119 }
120
121 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
122 {
123         HTTPD *httpd = (HTTPD *)cls;
124         return httpd->request_completed(connection, con_cls, toe);
125 }
126
127 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
128 {
129         if (con_cls == nullptr) {
130                 // Request was never set up.
131                 return;
132         }
133         HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
134         {
135                 unique_lock<mutex> lock(streams_mutex);
136                 delete stream;
137                 streams.erase(stream);
138         }
139 }
140
141 HTTPD::Mux::Mux(AVFormatContext *avctx, int width, int height, Codec codec)
142         : avctx(avctx)
143 {
144         AVCodec *codec_video = avcodec_find_encoder((codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
145         avstream_video = avformat_new_stream(avctx, codec_video);
146         if (avstream_video == nullptr) {
147                 fprintf(stderr, "avformat_new_stream() failed\n");
148                 exit(1);
149         }
150         avstream_video->time_base = AVRational{1, TIMEBASE};
151         avstream_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
152         if (codec == CODEC_H264) {
153                 avstream_video->codec->codec_id = AV_CODEC_ID_H264;
154         } else {
155                 assert(codec == CODEC_NV12);
156                 avstream_video->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
157                 avstream_video->codec->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
158         }
159         avstream_video->codec->width = width;
160         avstream_video->codec->height = height;
161         avstream_video->codec->time_base = AVRational{1, TIMEBASE};
162         avstream_video->codec->ticks_per_frame = 1;  // or 2?
163
164         // Colorspace details. Closely correspond to settings in EffectChain_finalize,
165         // as noted in each comment.
166         // Note that the H.264 stream also contains this information and depending on the
167         // mux, this might simply get ignored. See sps_rbsp().
168         avstream_video->codec->color_primaries = AVCOL_PRI_BT709;  // RGB colorspace (inout_format.color_space).
169         avstream_video->codec->color_trc = AVCOL_TRC_UNSPECIFIED;  // Gamma curve (inout_format.gamma_curve).
170         avstream_video->codec->colorspace = AVCOL_SPC_SMPTE170M;  // YUV colorspace (output_ycbcr_format.luma_coefficients).
171         avstream_video->codec->color_range = AVCOL_RANGE_MPEG;  // Full vs. limited range (output_ycbcr_format.full_range).
172         avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT;  // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
173         avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE;
174         if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
175                 avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
176         }
177
178         AVCodec *codec_audio = avcodec_find_encoder(AUDIO_OUTPUT_CODEC);
179         avstream_audio = avformat_new_stream(avctx, codec_audio);
180         if (avstream_audio == nullptr) {
181                 fprintf(stderr, "avformat_new_stream() failed\n");
182                 exit(1);
183         }
184         avstream_audio->time_base = AVRational{1, TIMEBASE};
185         avstream_audio->codec->bit_rate = AUDIO_OUTPUT_BIT_RATE;
186         avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY;
187         avstream_audio->codec->sample_fmt = AUDIO_OUTPUT_SAMPLE_FMT;
188         avstream_audio->codec->channels = 2;
189         avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
190         avstream_audio->codec->time_base = AVRational{1, TIMEBASE};
191         if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
192                 avstream_audio->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
193         }
194
195         AVDictionary *options = NULL;
196         vector<pair<string, string>> opts = MUX_OPTS;
197         for (pair<string, string> opt : opts) {
198                 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
199         }
200         if (avformat_write_header(avctx, &options) < 0) {
201                 fprintf(stderr, "avformat_write_header() failed\n");
202                 exit(1);
203         }
204 }
205
206 HTTPD::Mux::~Mux()
207 {
208         av_write_trailer(avctx);
209         av_free(avctx->pb->buffer);
210         av_free(avctx->pb);
211         avformat_free_context(avctx);
212 }
213
214 void HTTPD::Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
215 {
216         if (!seen_keyframe && !(pkt.stream_index == 0 && (pkt.flags & AV_PKT_FLAG_KEY))) {
217                 // Wait until we see the first (video) key frame.
218                 return;
219         }
220         seen_keyframe = true;
221
222         AVPacket pkt_copy;
223         av_copy_packet(&pkt_copy, &pkt);
224         if (pkt.stream_index == 0) {
225                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
226                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
227         } else if (pkt.stream_index == 1) {
228                 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
229                 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
230         } else {
231                 assert(false);
232         }
233
234         if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
235                 fprintf(stderr, "av_interleaved_write_frame() failed\n");
236                 exit(1);
237         }
238
239         av_packet_unref(&pkt_copy);
240 }
241
242 HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height)
243 {
244         AVFormatContext *avctx = avformat_alloc_context();
245         avctx->oformat = oformat;
246         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
247         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &HTTPD::Stream::write_packet_thunk, nullptr);
248
249         Mux::Codec codec;
250         if (global_flags.uncompressed_video_to_http) {
251                 codec = Mux::CODEC_NV12;
252         } else {
253                 codec = Mux::CODEC_H264;
254         }
255
256         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
257
258         mux.reset(new Mux(avctx, width, height, codec));
259 }
260
261 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
262 {
263         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
264         return stream->reader_callback(pos, buf, max);
265 }
266
267 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
268 {
269         unique_lock<mutex> lock(buffer_mutex);
270         has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
271
272         ssize_t ret = 0;
273         while (max > 0 && !buffered_data.empty()) {
274                 const string &s = buffered_data.front();
275                 assert(s.size() > used_of_buffered_data);
276                 size_t len = s.size() - used_of_buffered_data;
277                 if (max >= len) {
278                         // Consume the entire (rest of the) string.
279                         memcpy(buf, s.data() + used_of_buffered_data, len);
280                         ret += len;
281                         max -= len;
282                         buffered_data.pop_front();
283                         used_of_buffered_data = 0;
284                 } else {
285                         // We don't need the entire string; just use the first part of it.
286                         memcpy(buf, s.data() + used_of_buffered_data, max);
287                         used_of_buffered_data += max;
288                         ret += max;
289                         max = 0;
290                 }
291         }
292
293         return ret;
294 }
295
296 void HTTPD::Stream::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
297 {
298         mux->add_packet(pkt, pts, dts);
299 }
300
301 int HTTPD::Stream::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
302 {
303         HTTPD::Stream *stream = (HTTPD::Stream *)opaque;
304         return stream->write_packet(buf, buf_size);
305 }
306
307 int HTTPD::Stream::write_packet(uint8_t *buf, int buf_size)
308 {
309         unique_lock<mutex> lock(buffer_mutex);
310         buffered_data.emplace_back((char *)buf, buf_size);
311         has_buffered_data.notify_all(); 
312         return buf_size;
313 }
314