1 #include "print_latency.h"
11 using namespace std::chrono;
13 ReceivedTimestamps find_received_timestamp(const vector<RefCountedFrame> &input_frames)
15 // Find min and max timestamp of all input frames that have a timestamp.
16 steady_clock::time_point min_ts = steady_clock::time_point::max(), max_ts = steady_clock::time_point::min();
17 for (const RefCountedFrame &input_frame : input_frames) {
18 if (input_frame && input_frame->received_timestamp > steady_clock::time_point::min()) {
19 min_ts = min(min_ts, input_frame->received_timestamp);
20 max_ts = max(max_ts, input_frame->received_timestamp);
23 return { min_ts, max_ts };
26 void LatencyHistogram::init(const string &measuring_point)
28 histogram_lowest_latency_input.init_geometric(0.001, 10.0, 30);
29 histogram_highest_latency_input.init_geometric(0.001, 10.0, 30);
30 histogram_lowest_latency_input_bframe.init_geometric(0.001, 10.0, 30);
31 histogram_highest_latency_input_bframe.init_geometric(0.001, 10.0, 30);
33 global_metrics.add("latency_seconds",
34 {{ "measuring_point", measuring_point }, { "input", "lowest_latency" }, { "frame_type", "i/p" }},
35 &histogram_lowest_latency_input);
36 global_metrics.add("latency_seconds",
37 {{ "measuring_point", measuring_point }, { "input", "highest_latency" }, { "frame_type", "i/p" }},
38 &histogram_highest_latency_input);
39 global_metrics.add("latency_seconds",
40 {{ "measuring_point", measuring_point }, { "input", "lowest_latency" }, { "frame_type", "b" }},
41 &histogram_lowest_latency_input_bframe);
42 global_metrics.add("latency_seconds",
43 {{ "measuring_point", measuring_point }, { "input", "highest_latency" }, { "frame_type", "b" }},
44 &histogram_highest_latency_input_bframe);
47 void print_latency(const string &header, const ReceivedTimestamps &received_ts, bool is_b_frame, int *frameno, LatencyHistogram *histogram)
49 if (received_ts.max_ts == steady_clock::time_point::min())
52 const steady_clock::time_point now = steady_clock::now();
53 duration<double> lowest_latency = now - received_ts.max_ts;
54 duration<double> highest_latency = now - received_ts.min_ts;
57 histogram->histogram_lowest_latency_input_bframe.count_event(lowest_latency.count());
58 histogram->histogram_highest_latency_input_bframe.count_event(highest_latency.count());
60 histogram->histogram_lowest_latency_input.count_event(lowest_latency.count());
61 histogram->histogram_highest_latency_input.count_event(highest_latency.count());
64 // 101 is chosen so that it's prime, which is unlikely to get the same frame type every time.
65 if (global_flags.print_video_latency && (++*frameno % 101) == 0) {
66 printf("%-60s %4.0f ms (lowest-latency input), %4.0f ms (highest-latency input)",
67 header.c_str(), 1e3 * lowest_latency.count(), 1e3 * highest_latency.count());
70 printf(" [on B-frame; potential extra latency]\n");