Skip to content

Commit 3e7cb15

Browse files
ntocmassiot
authored andcommitted
upipe_avcodec_encode: update flow def latency
1 parent 6bdcaed commit 3e7cb15

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

lib/upipe-av/upipe_avcodec_encode.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ struct upipe_avcenc {
170170
struct uchain urefs_in_use;
171171
/** last incoming pts (in avcodec timebase) */
172172
int64_t avcpts;
173+
/** last PTS */
174+
uint64_t last_pts;
173175
/** last DTS */
174176
uint64_t last_dts;
175177
/** last DTS (system time) */
@@ -180,6 +182,8 @@ struct upipe_avcenc {
180182
uint64_t input_pts;
181183
/** last input PTS (system time) */
182184
uint64_t input_pts_sys;
185+
/** latency in the input flow */
186+
uint64_t input_latency;
183187

184188
/** frame counter */
185189
uint64_t counter;
@@ -532,6 +536,14 @@ static void upipe_avcenc_build_flow_def(struct upipe *upipe)
532536
UBASE_FATAL(upipe, uref_ts_flow_set_sub_ancillary(flow_def, 1, 0))
533537
}
534538

539+
/* find latency */
540+
if (upipe_avcenc->input_pts != UINT64_MAX) {
541+
uint64_t latency = upipe_avcenc->input_pts - upipe_avcenc->last_pts;
542+
upipe_notice_va(upipe, "latency: %" PRIu64 " ms",
543+
1000 * latency / UCLOCK_FREQ);
544+
uref_clock_set_latency(flow_def, upipe_avcenc->input_latency + latency);
545+
}
546+
535547
upipe_avcenc_store_flow_def(upipe, flow_def);
536548
}
537549

@@ -655,6 +667,7 @@ static void upipe_avcenc_output_pkt(struct upipe *upipe,
655667

656668
uref_clock_rebase_dts_orig(uref);
657669
uref_clock_set_rate(uref, upipe_avcenc->drift_rate);
670+
uref_clock_get_pts_prog(uref, &upipe_avcenc->last_pts);
658671

659672
upipe_avcenc->last_dts = dts;
660673
upipe_avcenc->last_dts_sys = dts_sys;
@@ -1090,6 +1103,8 @@ static bool upipe_avcenc_handle(struct upipe *upipe, struct uref *uref,
10901103
AVCodecContext *context = upipe_avcenc->context;
10911104
const char *def;
10921105
if (unlikely(uref != NULL && ubase_check(uref_flow_get_def(uref, &def)))) {
1106+
upipe_avcenc->input_latency = 0;
1107+
uref_clock_get_latency(uref, &upipe_avcenc->input_latency);
10931108
upipe_avcenc_store_flow_def(upipe, NULL);
10941109
uref_free(upipe_avcenc->flow_def_requested);
10951110
upipe_avcenc->flow_def_requested = NULL;
@@ -2091,11 +2106,13 @@ static struct upipe *upipe_avcenc_alloc(struct upipe_mgr *mgr,
20912106

20922107
ulist_init(&upipe_avcenc->urefs_in_use);
20932108
upipe_avcenc->avcpts = AVCPTS_INIT;
2109+
upipe_avcenc->last_pts = UINT64_MAX;
20942110
upipe_avcenc->last_dts = UINT64_MAX;
20952111
upipe_avcenc->last_dts_sys = UINT64_MAX;
20962112
upipe_avcenc->drift_rate.num = upipe_avcenc->drift_rate.den = 1;
20972113
upipe_avcenc->input_pts = UINT64_MAX;
20982114
upipe_avcenc->input_pts_sys = UINT64_MAX;
2115+
upipe_avcenc->input_latency = 0;
20992116

21002117
upipe_throw_ready(upipe);
21012118
upipe_avcenc_build_flow_def_attr(upipe);

0 commit comments

Comments
 (0)