+void HTTPD::Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
+{
+ AVPacket pkt_copy;
+ av_copy_packet(&pkt_copy, &pkt);
+ if (pkt.stream_index == 0) {
+ pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_video->time_base);
+ pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_video->time_base);
+ } else if (pkt.stream_index == 1) {
+ pkt_copy.pts = av_rescale_q(pts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
+ pkt_copy.dts = av_rescale_q(dts, AVRational{1, TIMEBASE}, avstream_audio->time_base);
+ } else {
+ assert(false);
+ }
+
+ if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
+ fprintf(stderr, "av_interleaved_write_frame() failed\n");
+ exit(1);
+ }
+}
+
+HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height)
+{
+ AVFormatContext *avctx = avformat_alloc_context();
+ avctx->oformat = oformat;
+ uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
+ avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &HTTPD::Stream::write_packet_thunk, nullptr);
+ avctx->flags = AVFMT_FLAG_CUSTOM_IO;
+
+ mux.reset(new Mux(avctx, width, height));
+}
+