+
+void VideoEncoder::open_output_stream()
+{
+ 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, &VideoEncoder::write_packet_thunk, nullptr);
+
+ Mux::Codec video_codec;
+ if (global_flags.uncompressed_video_to_http) {
+ video_codec = Mux::CODEC_NV12;
+ } else {
+ video_codec = Mux::CODEC_H264;
+ }
+
+ avctx->flags = AVFMT_FLAG_CUSTOM_IO;
+
+ string video_extradata;
+ if (global_flags.x264_video_to_http) {
+ video_extradata = x264_encoder->get_global_headers();
+ }
+
+ int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
+ stream_mux_writing_header = true;
+ stream_mux.reset(new Mux(avctx, width, height, video_codec, video_extradata, stream_audio_encoder->get_ctx(), time_base, this));
+ stream_mux_writing_header = false;
+ httpd->set_header(stream_mux_header);
+ stream_mux_header.clear();
+}
+
+void VideoEncoder::close_output_stream()
+{
+ stream_mux.reset();
+}
+
+int VideoEncoder::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
+{
+ VideoEncoder *video_encoder = (VideoEncoder *)opaque;
+ return video_encoder->write_packet(buf, buf_size);
+}
+
+int VideoEncoder::write_packet(uint8_t *buf, int buf_size)
+{
+ if (stream_mux_writing_header) {
+ stream_mux_header.append((char *)buf, buf_size);
+ } else {
+ httpd->add_data((char *)buf, buf_size, stream_mux_writing_keyframes);
+ stream_mux_writing_keyframes = false;
+ }
+ return buf_size;
+}
+