]> git.sesse.net Git - nageru/blob - print_latency.cpp
Move find_received_timestamp() into print_latency.h, so that multiple consumers can...
[nageru] / print_latency.cpp
1 #include "print_latency.h"
2
3 #include "flags.h"
4
5 #include <stdio.h>
6 #include <chrono>
7 #include <string>
8
9 using namespace std;
10 using namespace std::chrono;
11
12 ReceivedTimestamps find_received_timestamp(const vector<RefCountedFrame> &input_frames)
13 {
14         // Find min and max timestamp of all input frames that have a timestamp.
15         steady_clock::time_point min_ts = steady_clock::time_point::max(), max_ts = steady_clock::time_point::min();
16         for (const RefCountedFrame &input_frame : input_frames) {
17                 if (input_frame && input_frame->received_timestamp > steady_clock::time_point::min()) {
18                         min_ts = min(min_ts, input_frame->received_timestamp);
19                         max_ts = max(max_ts, input_frame->received_timestamp);
20                 }
21         }
22         return { min_ts, max_ts };
23 }
24
25 void print_latency(const string &header, const ReceivedTimestamps &received_ts, bool is_b_frame, int *frameno)
26 {
27         if (received_ts.max_ts == steady_clock::time_point::min())
28                 return;
29
30         // 101 is chosen so that it's prime, which is unlikely to get the same frame type every time.
31         if (global_flags.print_video_latency && (++*frameno % 101) == 0) {
32                 const steady_clock::time_point now = steady_clock::now();
33                 printf("%-60s %4.0f ms (lowest-latency input), %4.0f ms (highest-latency input)",
34                         header.c_str(),
35                         1e3 * std::chrono::duration<double>(now - received_ts.max_ts).count(),
36                         1e3 * std::chrono::duration<double>(now - received_ts.min_ts).count());
37
38                 if (is_b_frame) {
39                         printf("  [on B-frame; potential extra latency]\n");
40                 } else {
41                         printf("\n");
42                 }
43         }
44 }