X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=mux.cpp;h=b9860b943c402f08f8263de63ccd40aca3719c3b;hb=ab03e5e6f24b1651b4ca7df95e20aa5786939209;hp=d0c17fdfdc89665ab717db0fc182308f07751ae9;hpb=3173d8e9b8c66be0a6cb08d7a102297456f4e7a3;p=nageru diff --git a/mux.cpp b/mux.cpp index d0c17fd..b9860b9 100644 --- a/mux.cpp +++ b/mux.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -10,7 +11,7 @@ using namespace std; -Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodec *codec_audio, int time_base, int bit_rate, KeyFrameSignalReceiver *keyframe_signal_receiver) +Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecContext *audio_ctx, int time_base, KeyFrameSignalReceiver *keyframe_signal_receiver) : avctx(avctx), keyframe_signal_receiver(keyframe_signal_receiver) { AVCodec *codec_video = avcodec_find_encoder((video_codec == CODEC_H264) ? AV_CODEC_ID_H264 : AV_CODEC_ID_RAWVIDEO); @@ -43,24 +44,20 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const avstream_video->codec->color_range = AVCOL_RANGE_MPEG; // Full vs. limited range (output_ycbcr_format.full_range). avstream_video->codec->chroma_sample_location = AVCHROMA_LOC_LEFT; // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma(). avstream_video->codec->field_order = AV_FIELD_PROGRESSIVE; - if (avctx->oformat->flags & AVFMT_GLOBALHEADER) { - avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER; + + if (!video_extradata.empty()) { + avstream_video->codec->extradata = (uint8_t *)av_malloc(video_extradata.size()); + avstream_video->codec->extradata_size = video_extradata.size(); + memcpy(avstream_video->codec->extradata, video_extradata.data(), video_extradata.size()); } - avstream_audio = avformat_new_stream(avctx, codec_audio); + avstream_audio = avformat_new_stream(avctx, nullptr); if (avstream_audio == nullptr) { fprintf(stderr, "avformat_new_stream() failed\n"); exit(1); } avstream_audio->time_base = AVRational{1, time_base}; - avstream_audio->codec->bit_rate = bit_rate; - avstream_audio->codec->sample_rate = OUTPUT_FREQUENCY; - avstream_audio->codec->channels = 2; - avstream_audio->codec->channel_layout = AV_CH_LAYOUT_STEREO; - avstream_audio->codec->time_base = AVRational{1, time_base}; - if (avctx->oformat->flags & AVFMT_GLOBALHEADER) { - avstream_audio->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER; - } + avcodec_copy_context(avstream_audio->codec, audio_ctx); AVDictionary *options = NULL; vector> opts = MUX_OPTS; @@ -105,16 +102,16 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts) if (keyframe_signal_receiver) { if (pkt.flags & AV_PKT_FLAG_KEY) { - if (avctx->oformat->flags & AVFMT_ALLOW_FLUSH) { - av_write_frame(avctx, nullptr); - } + av_write_frame(avctx, nullptr); keyframe_signal_receiver->signal_keyframe(); } } { - lock_guard lock(ctx_mu); - if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) { + lock_guard lock(mu); + if (plug_count > 0) { + plugged_packets.push_back(av_packet_clone(&pkt_copy)); + } else if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) { fprintf(stderr, "av_interleaved_write_frame() failed\n"); exit(1); } @@ -122,3 +119,37 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts) av_packet_unref(&pkt_copy); } + +void Mux::plug() +{ + lock_guard lock(mu); + ++plug_count; +} + +void Mux::unplug() +{ + lock_guard lock(mu); + if (--plug_count > 0) { + return; + } + assert(plug_count >= 0); + + sort(plugged_packets.begin(), plugged_packets.end(), [](const AVPacket *a, const AVPacket *b) { + int64_t a_dts = (a->dts == AV_NOPTS_VALUE ? a->pts : a->dts); + int64_t b_dts = (b->dts == AV_NOPTS_VALUE ? b->pts : b->dts); + if (a_dts != b_dts) { + return a_dts < b_dts; + } else { + return a->pts < b->pts; + } + }); + + for (AVPacket *pkt : plugged_packets) { + if (av_interleaved_write_frame(avctx, pkt) < 0) { + fprintf(stderr, "av_interleaved_write_frame() failed\n"); + exit(1); + } + av_packet_free(&pkt); + } + plugged_packets.clear(); +}