void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
void encode_last_audio();
- const AVCodec *get_codec() { return ctx->codec; }
+ const AVCodecContext *get_ctx() { return ctx; }
private:
void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
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 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);
avstream_video->codec->flags = AV_CODEC_FLAG_GLOBAL_HEADER;
}
- 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<pair<string, string>> opts = MUX_OPTS;
};
// Takes ownership of avctx. <keyframe_signal_receiver> can be nullptr.
- 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(AVFormatContext *avctx, int width, int height, Codec video_codec, const AVCodecContext *audio_ctx, int time_base, KeyFrameSignalReceiver *keyframe_signal_receiver);
~Mux();
void add_packet(const AVPacket &pkt, int64_t pts, int64_t dts);
exit(1);
}
- file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, file_audio_encoder->get_codec(), TIMEBASE, DEFAULT_AUDIO_OUTPUT_BIT_RATE, nullptr));
+ file_mux.reset(new Mux(avctx, frame_width, frame_height, Mux::CODEC_H264, file_audio_encoder->get_ctx(), TIMEBASE, nullptr));
}
void QuickSyncEncoderImpl::encode_thread_func()
assert(oformat != nullptr);
avctx->oformat = oformat;
- string codec_name;
- int bit_rate;
-
- if (global_flags.stream_audio_codec_name.empty()) {
- codec_name = AUDIO_OUTPUT_CODEC_NAME;
- bit_rate = DEFAULT_AUDIO_OUTPUT_BIT_RATE;
- } else {
- codec_name = global_flags.stream_audio_codec_name;
- bit_rate = global_flags.stream_audio_codec_bitrate;
- }
-
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);
}
avctx->flags = AVFMT_FLAG_CUSTOM_IO;
- AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
- if (codec_audio == nullptr) {
- fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
- exit(1);
- }
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, codec_audio, time_base, bit_rate, this));
+ stream_mux.reset(new Mux(avctx, width, height, video_codec, stream_audio_encoder->get_ctx(), time_base, this));
stream_mux_writing_header = false;
httpd->set_header(stream_mux_header);
stream_mux_header.clear();