-#include "mux.h"
+#include "shared/mux.h"
+#include <algorithm>
#include <assert.h>
+#include <mutex>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <algorithm>
-#include <mutex>
#include <string>
#include <utility>
#include <vector>
#include <libavutil/rational.h>
}
-#include "defs.h"
-#include "flags.h"
-#include "metrics.h"
+#include "shared/metrics.h"
+#include "shared/mux_opts.h"
#include "shared/timebase.h"
using namespace std;
const AVFormatContext * const ctx;
};
-Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, int time_base, std::function<void(int64_t)> write_callback, WriteStrategy write_strategy, const vector<MuxMetrics *> &metrics)
+Mux::Mux(AVFormatContext *avctx, int width, int height, Codec video_codec, const string &video_extradata, const AVCodecParameters *audio_codecpar, AVColorSpace color_space, WithAudio with_audio, int time_base, function<void(int64_t)> write_callback, WriteStrategy write_strategy, const vector<MuxMetrics *> &metrics)
: write_strategy(write_strategy), avctx(avctx), write_callback(write_callback), metrics(metrics)
{
avstream_video = avformat_new_stream(avctx, nullptr);
avstream_video->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
if (video_codec == CODEC_H264) {
avstream_video->codecpar->codec_id = AV_CODEC_ID_H264;
- } else {
- assert(video_codec == CODEC_NV12);
+ } else if (video_codec == CODEC_NV12) {
avstream_video->codecpar->codec_id = AV_CODEC_ID_RAWVIDEO;
avstream_video->codecpar->codec_tag = avcodec_pix_fmt_to_codec_tag(AV_PIX_FMT_NV12);
+ } else {
+ assert(video_codec == CODEC_MJPEG);
+ avstream_video->codecpar->codec_id = AV_CODEC_ID_MJPEG;
}
avstream_video->codecpar->width = width;
avstream_video->codecpar->height = height;
avstream_video->codecpar->color_primaries = AVCOL_PRI_BT709; // RGB colorspace (inout_format.color_space).
avstream_video->codecpar->color_trc = AVCOL_TRC_IEC61966_2_1; // Gamma curve (inout_format.gamma_curve).
// YUV colorspace (output_ycbcr_format.luma_coefficients).
- if (global_flags.ycbcr_rec709_coefficients) {
- avstream_video->codecpar->color_space = AVCOL_SPC_BT709;
- } else {
- avstream_video->codecpar->color_space = AVCOL_SPC_SMPTE170M;
- }
+ avstream_video->codecpar->color_space = color_space;
avstream_video->codecpar->color_range = AVCOL_RANGE_MPEG; // Full vs. limited range (output_ycbcr_format.full_range).
avstream_video->codecpar->chroma_location = AVCHROMA_LOC_LEFT; // Chroma sample location. See chroma_offset_0[] in Mixer::subsample_chroma().
avstream_video->codecpar->field_order = AV_FIELD_PROGRESSIVE;
memcpy(avstream_video->codecpar->extradata, video_extradata.data(), video_extradata.size());
}
- 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};
- if (avcodec_parameters_copy(avstream_audio->codecpar, audio_codecpar) < 0) {
- fprintf(stderr, "avcodec_parameters_copy() failed\n");
- exit(1);
+ if (with_audio == WITH_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};
+ if (avcodec_parameters_copy(avstream_audio->codecpar, audio_codecpar) < 0) {
+ fprintf(stderr, "avcodec_parameters_copy() failed\n");
+ exit(1);
+ }
+ } else {
+ assert(with_audio == WITHOUT_AUDIO);
+ avstream_audio = nullptr;
}
AVDictionary *options = NULL;
lock_guard<mutex> lock(mu);
if (write_strategy == WriteStrategy::WRITE_BACKGROUND) {
packet_queue.push_back(QueuedPacket{ av_packet_clone(&pkt_copy), pts });
- if (plug_count == 0) packet_queue_ready.notify_all();
+ if (plug_count == 0)
+ packet_queue_ready.notify_all();
} else if (plug_count > 0) {
packet_queue.push_back(QueuedPacket{ av_packet_clone(&pkt_copy), pts });
} else {
int64_t old_pos = avctx->pb->pos;
if (av_interleaved_write_frame(avctx, const_cast<AVPacket *>(&pkt)) < 0) {
fprintf(stderr, "av_interleaved_write_frame() failed\n");
- exit(1);
+ abort();
}
avio_flush(avctx->pb);
for (MuxMetrics *metric : metrics) {
void Mux::thread_func()
{
+ pthread_setname_np(pthread_self(), "Mux");
+
unique_lock<mutex> lock(mu);
for ( ;; ) {
packet_queue_ready.wait(lock, [this]() {