2 #include <microhttpd.h>
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>
25 struct MHD_Connection;
30 HTTPD::HTTPD(int width, int height)
31 : width(width), height(height)
35 void HTTPD::start(int port)
37 MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
40 &answer_to_connection_thunk, this,
41 MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this,
45 void HTTPD::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, PacketDestination destination)
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);
53 if (file_mux && destination != DESTINATION_HTTP_ONLY) {
54 file_mux->add_packet(pkt, pts, dts);
58 void HTTPD::open_output_file(const string &filename)
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());
65 string url = "file:" + filename;
66 int ret = avio_open2(&avctx->pb, url.c_str(), AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL);
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));
73 file_mux.reset(new Mux(avctx, width, height, Mux::CODEC_H264, TIMEBASE));
76 void HTTPD::close_output_file()
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)
86 HTTPD *httpd = (HTTPD *)cls;
87 return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
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)
95 AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
96 assert(oformat != nullptr);
98 int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
99 HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height, time_base);
101 unique_lock<mutex> lock(streams_mutex);
102 streams.insert(stream);
106 // Does not strictly have to be equal to MUX_BUFFER_SIZE.
107 MHD_Response *response = MHD_create_response_from_callback(
108 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
109 int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
110 //MHD_destroy_response(response);
115 void HTTPD::free_stream(void *cls)
117 // FIXME: When is this actually called, if ever?
118 // Also, shouldn't we remove it from streams?
119 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
123 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
125 HTTPD *httpd = (HTTPD *)cls;
126 return httpd->request_completed(connection, con_cls, toe);
129 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
131 if (con_cls == nullptr) {
132 // Request was never set up.
135 HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
137 unique_lock<mutex> lock(streams_mutex);
139 streams.erase(stream);
143 HTTPD::Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int time_base)
146 AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO);
147 avstream_video = avformat_new_stream(avctx, codec_video);
148 if (avstream_video == nullptr) {
149 fprintf(stderr, "avformat_new_stream() failed\n");
152 avstream_video->time_base = AVRational{1, time_base};
153 avstream_video->codec->codec_type = AVMEDIA_TYPE_VIDEO;
154 if (video_codec == CODEC_H264) {
155 avstream_video->codec->codec_id = AV_CODEC_ID_H264;
157 assert(video_codec == CODEC_NV12);
158 avstream_video->codec->codec_id = AV_CODEC_ID_RAWVIDEO;
159 avstream_video->codec->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
161 avstream_video->codec->width = width;
162 avstream_video->codec->height = height;
163 avstream_video->codec->time_base = AVRational{1, time_base};
164 avstream_video->codec->ticks_per_frame = 1; // or 2?
166 // Colorspace details. Closely correspond to settings in EffectChain_finalize,
167 // as noted in each comment.
168 // Note that the H.264 stream also contains this information and depending on the
169 // mux, this might simply get ignored. See sps_rbsp().
170 avstream_video->codec->color_primaries = AVCOL_PRI_BT709; // RGB colorspace (inout_format.color_space).
171 avstream_video->codec->color_trc = AVCOL_TRC_UNSPECIFIED; // Gamma curve (inout_format.gamma_curve).
172 avstream_video->codec->colorspace = AVCOL_SPC_SMPTE170M; // YUV colorspace (output_ycbcr_format.luma_coefficients).
173 avstream_video->codec->color_range = AVCOL_RANGE_MPEG; // Full vs. limited range (output_ycbcr_format.full_range).
174 avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT; // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
175 avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE;
176 if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
177 avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
180 AVCodec *codec_audio = avcodec_find_encoder_by_name(AUDIO_OUTPUT_CODEC_NAME);
181 if (codec_audio == nullptr) {
182 fprintf(stderr, "ERROR: Could not find codec '%s'\n", AUDIO_OUTPUT_CODEC_NAME);
185 avstream_audio = avformat_new_stream(avctx, codec_audio);
186 if (avstream_audio == nullptr) {
187 fprintf(stderr, "avformat_new_stream() failed\n");
190 avstream_audio->time_base = AVRational{1, time_base};
191 avstream_audio->codec->bit_rate = AUDIO_OUTPUT_BIT_RATE;
192 avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY;
193 avstream_audio->codec->channels = 2;
194 avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO;
195 avstream_audio->codec->time_base = AVRational{1, time_base};
196 if (avctx->oformat->flags & AVFMT_GLOBALHEADER) {
197 avstream_audio->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
200 AVDictionary *options = NULL;
201 vector<pair<string, string>> opts = MUX_OPTS;
202 for (pair<string, string> opt : opts) {
203 av_dict_set(&options, opt.first.c_str(), opt.second.c_str(), 0);
205 if (avformat_write_header(avctx, &options) < 0) {
206 fprintf(stderr, "avformat_write_header() failed\n");
213 av_write_trailer(avctx);
214 av_free(avctx->pb->buffer);
216 avformat_free_context(avctx);
219 void HTTPD::Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
221 if (!seen_keyframe && !(pkt.stream_index == 0 && (pkt.flags & AV_PKT_FLAG_KEY))) {
222 // Wait until we see the first (video) key frame.
225 seen_keyframe = true;
228 av_copy_packet(&pkt_copy, &pkt);
229 if (pkt.stream_index == 0) {
230 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
231 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
232 } else if (pkt.stream_index == 1) {
233 pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
234 pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
239 if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
240 fprintf(stderr, "av_interleaved_write_frame() failed\n");
244 av_packet_unref(&pkt_copy);
247 HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_base)
249 AVFormatContext *avctx = avformat_alloc_context();
250 avctx->oformat = oformat;
251 uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
252 avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &HTTPD::Stream::write_packet_thunk, nullptr);
254 Mux::Codec video_codec;
255 if (global_flags.uncompressed_video_to_http) {
256 video_codec = Mux::CODEC_NV12;
258 video_codec = Mux::CODEC_H264;
261 avctx->flags = AVFMT_FLAG_CUSTOM_IO;
263 mux.reset(new Mux(avctx, width, height, video_codec, time_base));
266 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
268 HTTPD::Stream *stream = (HTTPD::Stream *)cls;
269 return stream->reader_callback(pos, buf, max);
272 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
274 unique_lock<mutex> lock(buffer_mutex);
275 has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
278 while (max > 0 && !buffered_data.empty()) {
279 const string &s = buffered_data.front();
280 assert(s.size() > used_of_buffered_data);
281 size_t len = s.size() - used_of_buffered_data;
283 // Consume the entire (rest of the) string.
284 memcpy(buf, s.data() + used_of_buffered_data, len);
288 buffered_data.pop_front();
289 used_of_buffered_data = 0;
291 // We don't need the entire string; just use the first part of it.
292 memcpy(buf, s.data() + used_of_buffered_data, max);
294 used_of_buffered_data += max;
303 void HTTPD::Stream::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
305 mux->add_packet(pkt, pts, dts);
308 int HTTPD::Stream::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
310 HTTPD::Stream *stream = (HTTPD::Stream *)opaque;
311 return stream->write_packet(buf, buf_size);
314 int HTTPD::Stream::write_packet(uint8_t *buf, int buf_size)
316 unique_lock<mutex> lock(buffer_mutex);
317 buffered_data.emplace_back((char *)buf, buf_size);
318 has_buffered_data.notify_all();