#include <assert.h>
+#include <mutex>
#include <string>
#include <vector>
using namespace std;
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, int time_base, int bit_rate)
- : 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);
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");
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()
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 (av_copy_packet(&pkt_copy, &pkt) < 0) {
+ fprintf(stderr, "av_copy_packet() failed\n");
+ exit(1);
+ }
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);
+ pkt_copy.duration = av_rescale_q(pkt.duration, 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);
+ pkt_copy.duration = av_rescale_q(pkt.duration, 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);
+ if (keyframe_signal_receiver) {
+ if (pkt.flags & AV_PKT_FLAG_KEY) {
+ av_write_frame(avctx, nullptr);
+ keyframe_signal_receiver->signal_keyframe();
+ }
+ }
+
+ {
+ lock_guard<mutex> lock(ctx_mu);
+ if (av_interleaved_write_frame(avctx, &pkt_copy) < 0) {
+ fprintf(stderr, "av_interleaved_write_frame() failed\n");
+ exit(1);
+ }
}
av_packet_unref(&pkt_copy);
}
-