Skip to content

Commit

Permalink
[menu] high-dpi scaling, #25
Browse files Browse the repository at this point in the history
  • Loading branch information
ffiirree committed Apr 12, 2023
1 parent 7d37090 commit 84ad351
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 151 deletions.
32 changes: 16 additions & 16 deletions src/media/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ int Decoder::run()

int Decoder::run_f()
{
LOG(INFO) << fmt::format("[ DECODER] [{:>10}] STARTED", name_);
LOG(INFO) << "STARTED";

// reset the buffer
video_buffer_.clear();
Expand All @@ -287,11 +287,11 @@ int Decoder::run_f()

while (running_) {
if (video_buffer_.full()) {
LOG(WARNING) << fmt::format("[ DECODER] [V] [{:>10}] buffer is full, drop a packet", name_);
LOG(WARNING) <<"[V] buffer is full, drop the packet";
}

if (audio_buffer_.full()) {
LOG(WARNING) << fmt::format("[ DECODER] [A] [{:>10}] buffer is full, drop a packet", name_);
LOG(WARNING) << "[A] buffer is full, drop the packet";
}

// read
Expand All @@ -302,11 +302,11 @@ int Decoder::run_f()
// This will enter draining mode.
// [draining] 2. Call avcodec_receive_frame() (decoding) or avcodec_receive_packet() (encoding) in a loop until AVERROR_EOF is returned.
// The functions will not return AVERROR(EAGAIN), unless you forgot to enter draining mode.
LOG(INFO) << fmt::format("[ DECODER] [{:>10}] EOF => PUT NULL PACKET TO ENTER DRAINING MODE", name_);
LOG(INFO) << "EOF => PUT NULL PACKET TO ENTER DRAINING MODE";
eof_ |= DEMUXING_EOF;
}
else if (ret < 0) {
LOG(ERROR) << fmt::format("[ DECODER] [{:>10}] READ FRAME FAILED", name_);
LOG(ERROR) << "failed to read frame.";
running_ = false;
break;
}
Expand All @@ -321,20 +321,20 @@ int Decoder::run_f()
break;
}
else if (ret == AVERROR_EOF) {
LOG(INFO) << fmt::format("[ DECODER] [V] [{:>10}] EOF", name_);
LOG(INFO) << "[V] EOF";
eof_ |= VDECODING_EOF;
break;
}
else if (ret < 0) {
running_ = false;
LOG(INFO) << fmt::format("[ DECODER] [V] [{:>10}] DECODING ERROR", name_);
LOG(INFO) << "[V] DECODING ERROR";
return ret;
}

decoded_frame_->pts += VIDEO_OFFSET_TIME;

DLOG(INFO) << fmt::format("[ DECODER] [V] [{:>10}] frame = {:>5d}, pts = {:>9d}",
name_, video_decoder_ctx_->frame_number, decoded_frame_->pts);
DLOG(INFO) << fmt::format("[V] frame = {:>5d}, pts = {:>13d}",
video_decoder_ctx_->frame_number, decoded_frame_->pts);

video_buffer_.push(
[=](AVFrame* frame) {
Expand All @@ -355,20 +355,20 @@ int Decoder::run_f()
break;
}
else if (ret == AVERROR_EOF) {
LOG(INFO) << fmt::format("[ DECODER] [A] [{:>10}] EOF", name_);
LOG(INFO) <<"[A] EOF";
eof_ |= ADECODING_EOF;
break;
}
else if (ret < 0) {
running_ = false;
LOG(INFO) << fmt::format("[ DECODER] [A] [{:>10}] DECODING ERROR", name_);
LOG(INFO) << "[A] DECODING ERROR";
return ret;
}

decoded_frame_->pts += AUDIO_OFFSET_TIME;

DLOG(INFO) << fmt::format("[ DECODER] [A] [{:>10}] frame = {:>5d}, pts = {:>9d}, samples = {:>5d}, muted = {}",
name_, audio_decoder_ctx_->frame_number, decoded_frame_->pts, decoded_frame_->nb_samples, muted_);
DLOG(INFO) << fmt::format("[A] frame = {:>5d}, pts = {:>9d}, samples = {:>5d}, muted = {}",
audio_decoder_ctx_->frame_number, decoded_frame_->pts, decoded_frame_->nb_samples, muted_);

if (muted_) {
av_samples_set_silence(
Expand All @@ -391,10 +391,10 @@ int Decoder::run_f()
} // while(running_)

if (video_stream_idx_ >= 0) {
LOG(INFO) << fmt::format("[ DECODER] [V] [{:>10}] frames = {:>5d}",
name_, video_decoder_ctx_->frame_number);
LOG(INFO) << fmt::format("[V] decoded frames = {:>5d}",
video_decoder_ctx_->frame_number);
}
LOG(INFO) << fmt::format("[ DECODER] [{:>10}] EXITED", name_);
LOG(INFO) << "EXITED";

return 0;
}
Expand Down
42 changes: 20 additions & 22 deletions src/media/dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ int Dispatcher::create_sink_filter(const Consumer<AVFrame>* encoder, AVFilterCon
int Dispatcher::create_video_src(const Producer<AVFrame>* decoder, AVFilterContext** ctx)
{
if (!decoder->ready()) {
LOG(ERROR) << "[DISPATCHER] decoder is not ready";
LOG(ERROR) << "[DISPATCHER] [V] decoder is not ready";
return -1;
}
LOG(INFO) << "[DISPATCHER] [V] create video src: " << decoder->format_str(AVMEDIA_TYPE_VIDEO);

const AVFilter* buffersrc = avfilter_get_by_name("buffer");
if (!buffersrc) {
LOG(ERROR) << "avfilter_get_by_name";
LOG(ERROR) << "[DISPATCHER] [V] can not find video 'buffersrc' filter.";
return -1;
}

if (avfilter_graph_create_filter(ctx, buffersrc, "v_src", decoder->format_str(AVMEDIA_TYPE_VIDEO).c_str(), nullptr, video_graph_) < 0) {
LOG(ERROR) << "avfilter_graph_create_filter";
LOG(ERROR) << "[DISPATCHER] [V] failed to create 'buffer'.";
return -1;
}

Expand All @@ -57,19 +57,19 @@ int Dispatcher::create_video_sink(const Consumer<AVFrame>* encoder, AVFilterCont
{
const AVFilter* buffersink = avfilter_get_by_name("buffersink");
if (!buffersink) {
LOG(ERROR) << "can not find 'buffersink'.";
LOG(ERROR) << "[DISPATCHER] [V] can not find 'buffersink'.";
return -1;
}

if (avfilter_graph_create_filter(ctx, buffersink, "v_sink", nullptr, nullptr, video_graph_) < 0) {
LOG(ERROR) << "failed to create 'buffersink'.";
LOG(ERROR) << "[DISPATCHER] [V] failed to create 'buffersink'.";
return -1;
}

enum AVPixelFormat pix_fmt = ((AVPixelFormat)encoder->format(AVMEDIA_TYPE_VIDEO) == AV_PIX_FMT_NONE) ? AV_PIX_FMT_YUV420P : (AVPixelFormat)encoder->format(AVMEDIA_TYPE_VIDEO);
enum AVPixelFormat pix_fmts[] = { pix_fmt, AV_PIX_FMT_NONE };
if (av_opt_set_int_list(*ctx, "pix_fmts", pix_fmts, AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN) < 0) {
LOG(ERROR) << "av_opt_set_int_list";
LOG(ERROR) << "[DISPATCHER] [V] av_opt_set_int_list";
return -1;
}

Expand All @@ -81,19 +81,19 @@ int Dispatcher::create_video_sink(const Consumer<AVFrame>* encoder, AVFilterCont
int Dispatcher::create_audio_src(const Producer<AVFrame>* decoder, AVFilterContext** ctx)
{
if (!decoder->ready()) {
LOG(ERROR) << "[DISPATCHER] decoder is not ready";
LOG(ERROR) << "[DISPATCHER] [A] decoder is not ready.";
return -1;
}
LOG(INFO) << "[DISPATCHER] [A] createn audio src: " << decoder->format_str(AVMEDIA_TYPE_AUDIO);

const AVFilter* buffersrc = avfilter_get_by_name("abuffer");
if (!buffersrc) {
LOG(ERROR) << "can not find 'abuffer'.";
LOG(ERROR) << "[DISPATCHER] [A] can not find 'abuffer'.";
return -1;
}

if (avfilter_graph_create_filter(ctx, buffersrc, "a_src", decoder->format_str(AVMEDIA_TYPE_AUDIO).c_str(), nullptr, audio_graph_) < 0) {
LOG(ERROR) << "filter to create 'abuffer'";
LOG(ERROR) << "[DISPATCHER] [A] filter to create 'abuffer'";
return -1;
}

Expand All @@ -104,12 +104,12 @@ int Dispatcher::create_audio_sink(const Consumer<AVFrame>* encoder, AVFilterCont
{
const AVFilter* abuffersink = avfilter_get_by_name("abuffersink");
if (!abuffersink) {
LOG(ERROR) << "can not find 'abuffersink'.";
LOG(ERROR) << "[DISPATCHER] [A] can not find 'abuffersink'.";
return -1;
}

if (avfilter_graph_create_filter(ctx, abuffersink, "a_sink", nullptr, nullptr, audio_graph_) < 0) {
LOG(ERROR) << "failed to create 'abuffersink'";
LOG(ERROR) << "[DISPATCHER] [A] failed to create 'abuffersink'.";
return -1;
}

Expand Down Expand Up @@ -179,8 +179,6 @@ int Dispatcher::create_filter_graph(const std::string_view& video_graph_desc, co

int Dispatcher::create_filter_graph_for(std::vector<ProducerContext>& producer_ctxs, const std::string& desc, enum AVMediaType type)
{
LOG(INFO) << "[DISPATCHER] create filter graph : " << desc;

AVFilterGraph* graph = nullptr;
switch (type)
{
Expand Down Expand Up @@ -226,6 +224,8 @@ int Dispatcher::create_filter_graph_for(std::vector<ProducerContext>& producer_c
}
}
else {
LOG(INFO) << "[DISPATCHER] create filter graph : " << desc;

AVFilterInOut* inputs = nullptr, * outputs = nullptr;
defer(avfilter_inout_free(&inputs); avfilter_inout_free(&outputs));

Expand Down Expand Up @@ -367,8 +367,8 @@ bool Dispatcher::is_valid_pts(int64_t pts)

int Dispatcher::dispatch_fn(enum AVMediaType mt)
{
LOG(INFO) << "[DISPATCHER] started";
defer(LOG(INFO) << "[DISPATCHER] exited");
LOG(INFO) << fmt::format("[{}] STARTED", av_get_media_type_string(mt)[0]);
defer(LOG(INFO) << fmt::format("[{}] EXITED", av_get_media_type_string(mt)[0]));

AVFrame* frame = av_frame_alloc();
defer(av_frame_free(&frame));
Expand Down Expand Up @@ -398,7 +398,7 @@ int Dispatcher::dispatch_fn(enum AVMediaType mt)
sleepy = false;

if (!is_valid_pts(av_rescale_q(frame->pts, frame_tb, OS_TIME_BASE_Q))) {
LOG(INFO) << fmt::format("[DISPATCHER] [{}] drop frame {} ({} - {})",
LOG(INFO) << fmt::format("[{}] drop frame {} ({} - {})",
av_get_media_type_string(mt)[0], av_rescale_q(frame->pts, frame_tb, OS_TIME_BASE_Q), resumed_pts_, paused_pts_);
continue;
}
Expand All @@ -407,7 +407,7 @@ int Dispatcher::dispatch_fn(enum AVMediaType mt)

// send the frame to graph
if (av_buffersrc_add_frame_flags(src_ctx, frame, AV_BUFFERSRC_FLAG_PUSH) < 0) {
LOG(ERROR) << "av_buffersrc_add_frame_flags";
LOG(ERROR) << fmt::format("[{}] failed to send the frame to filter graph.", av_get_media_type_string(mt)[0]);
running_ = false;
break;
}
Expand All @@ -426,13 +426,13 @@ int Dispatcher::dispatch_fn(enum AVMediaType mt)
continue;
}
else if (ret == AVERROR_EOF) {
LOG(INFO) << fmt::format("[DISPATCHER] [{}] EOF", std::toupper(av_get_media_type_string(mt)[0]));
LOG(INFO) << fmt::format("[{}] EOF",av_get_media_type_string(mt)[0]);
consumer_eof = true;
// through and send null packet
av_frame_unref(frame);
}
else if (ret < 0) {
LOG(ERROR) << fmt::format("[DISPATCHER] [{}] failed to get frame.", std::toupper(av_get_media_type_string(mt)[0]));
LOG(ERROR) << fmt::format("[{}] failed to get frame.", av_get_media_type_string(mt)[0]);
running_ = false;
break;
}
Expand Down Expand Up @@ -477,8 +477,6 @@ int64_t Dispatcher::paused_time()

int Dispatcher::reset()
{
LOG(INFO) << fmt::format("[DISPATCHER] reseted at {}", escaped_us());

pause();
ready_ = false;

Expand Down Expand Up @@ -544,7 +542,7 @@ int Dispatcher::reset()

locked_ = false;

LOG(INFO) << "[DISPATCHER] RESETED";
LOG(INFO) << "[DISPATCHER] RESET";
return 0;
}

Expand Down
Loading

0 comments on commit 84ad351

Please sign in to comment.