]> git.sesse.net Git - nageru/blobdiff - mux.cpp
Put __restrict on the right place (the pointer; the pointee cannot be restricted...
[nageru] / mux.cpp
diff --git a/mux.cpp b/mux.cpp
index 82d42abe705c75e2af79259252a68e5494e6763f..eff336f5799d01706fd7f9c4a2a0fc99356303f6 100644 (file)
--- a/mux.cpp
+++ b/mux.cpp
@@ -9,8 +9,8 @@
 
 using namespace std;
 
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int time_base)
-       : avctx(avctx)
+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)
+       : 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);
        avstream_video = avformat_new_stream(avctx, codec_video);
@@ -46,18 +46,13 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int t
                avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
        }
 
-       AVCodec *codec_audio = avcodec_find_encoder_by_name(AUDIO_OUTPUT_CODEC_NAME);
-       if (codec_audio == nullptr) {
-               fprintf(stderr, "ERROR: Could not find codec '%s'\n", AUDIO_OUTPUT_CODEC_NAME);
-               exit(1);
-       }
        avstream_audio = avformat_new_stream(avctx, codec_audio);
        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 = AUDIO_OUTPUT_BIT_RATE;
+       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;
@@ -75,6 +70,9 @@ Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int t
                fprintf(stderr, "avformat_write_header() failed\n");
                exit(1);
        }
+
+       // Make sure the header is written before the constructor exits.
+       avio_flush(avctx->pb);
 }
 
 Mux::~Mux()
@@ -87,12 +85,6 @@ Mux::~Mux()
 
 void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
 {
-       if (!seen_keyframe && !(pkt.stream_index == 0 && (pkt.flags & AV_PKT_FLAG_KEY))) {
-               // Wait until we see the first (video) key frame.
-               return;
-       }
-       seen_keyframe = true;
-
        AVPacket pkt_copy;
        av_copy_packet(&pkt_copy, &pkt);
        if (pkt.stream_index == 0) {
@@ -105,6 +97,15 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
                assert(false);
        }
 
+       if (keyframe_signal_receiver) {
+               if (pkt.flags & AV_PKT_FLAG_KEY) {
+                       if (avctx->oformat->flags & AVFMT_ALLOW_FLUSH) {
+                               av_write_frame(avctx, nullptr);
+                       }
+                       keyframe_signal_receiver->signal_keyframe();
+               }
+       }
+
        if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
                fprintf(stderr, "av_interleaved_write_frame() failed\n");
                exit(1);
@@ -112,4 +113,3 @@ void Mux::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
 
        av_packet_unref(&pkt_copy);
 }
-