• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef C2_E2E_TEST_COMMON_H_
6 #define C2_E2E_TEST_COMMON_H_
7 
8 #include <fstream>
9 #include <ios>
10 #include <string>
11 #include <vector>
12 
13 namespace android {
14 
15 // The enumeration of video codec profile. This would be better to align with
16 // VideoCodecProfile enum in Chromium so we could use the identical test stream
17 // data arguments for both ARC end-to-end and Chromium tests.
18 enum VideoCodecProfile {
19     VIDEO_CODEC_PROFILE_UNKNOWN = -1,
20     VIDEO_CODEC_PROFILE_MIN = VIDEO_CODEC_PROFILE_UNKNOWN,
21     H264PROFILE_MIN = 0,
22     H264PROFILE_BASELINE = H264PROFILE_MIN,
23     H264PROFILE_MAIN = 1,
24     H264PROFILE_EXTENDED = 2,
25     H264PROFILE_HIGH = 3,
26     H264PROFILE_HIGH10PROFILE = 4,
27     H264PROFILE_HIGH422PROFILE = 5,
28     H264PROFILE_HIGH444PREDICTIVEPROFILE = 6,
29     H264PROFILE_SCALABLEBASELINE = 7,
30     H264PROFILE_SCALABLEHIGH = 8,
31     H264PROFILE_STEREOHIGH = 9,
32     H264PROFILE_MULTIVIEWHIGH = 10,
33     H264PROFILE_MAX = H264PROFILE_MULTIVIEWHIGH,
34     VP8PROFILE_MIN = 11,
35     VP8PROFILE_ANY = VP8PROFILE_MIN,
36     VP8PROFILE_MAX = VP8PROFILE_ANY,
37     VP9PROFILE_MIN = 12,
38     VP9PROFILE_PROFILE0 = VP9PROFILE_MIN,
39     VP9PROFILE_PROFILE1 = 13,
40     VP9PROFILE_PROFILE2 = 14,
41     VP9PROFILE_PROFILE3 = 15,
42     VP9PROFILE_MAX = VP9PROFILE_PROFILE3,
43 };
44 
45 // The enum class of video codec type.
46 enum class VideoCodecType {
47     UNKNOWN,
48     H264,
49     VP8,
50     VP9,
51 };
52 
53 // Structure to store resolution.
54 struct Size {
SizeSize55     Size() : width(0), height(0) {}
SizeSize56     Size(int w, int h) : width(w), height(h) {}
IsEmptySize57     bool IsEmpty() const { return width <= 0 || height <= 0; }
58 
59     int width;
60     int height;
61 };
62 
63 class InputFile {
64 public:
65     explicit InputFile(std::string file_path);
66     InputFile(std::string file_path, std::ios_base::openmode openmode);
67 
68     // Check if the file is valid.
69     bool IsValid() const;
70     // Get the size of the file.
71     size_t GetLength();
72     // Set position to the beginning of the file.
73     void Rewind();
74 
75 protected:
76     std::ifstream file_;
77 };
78 
79 // Wrapper of std::ifstream for reading binary file.
80 class InputFileStream : public InputFile {
81 public:
82     explicit InputFileStream(std::string file_path);
83 
84     // Read the given number of bytes to the buffer. Return the number of bytes
85     // read or -1 on error.
86     size_t Read(char* buffer, size_t size);
87 };
88 
89 // Wrapper of std::ifstream for reading ASCII file.
90 class InputFileASCII : public InputFile {
91 public:
92     explicit InputFileASCII(std::string file_path);
93 
94     // Read one line from the file. Return false if EOF.
95     bool ReadLine(std::string* line);
96 };
97 
98 // The helper class to calculate FPS.
99 class FPSCalculator {
100 public:
101     // Record the time interval of output buffers. Return false if is invalid.
102     // This should be called per output buffer ready callback.
103     bool RecordFrameTimeDiff();
104 
105     // Calucalate FPS value.
106     double CalculateFPS() const;
107 
108 private:
109     static constexpr double kMovingAvgWindowUs = 1000000;
110     static constexpr double kRegardedPercentile = 95;
111 
112     // Return the statistics for the moving average over a window over the
113     // cumulative sum. Basically, moves a window from: [0, window] to
114     // [sum - window, sum] over the cumulative sum, over ((sum - window)/average)
115     // steps, and returns the average value over each window.
116     // This method is used to average time-diff data over a window of a constant
117     // time.
118     std::vector<double> MovingAvgOverSum() const;
119 
120     std::vector<double> frame_time_diffs_us_;
121     int64_t last_frame_time_us_ = 0;
122 };
123 
124 // Helper function to get VideoCodecType from |profile|.
125 VideoCodecType VideoCodecProfileToType(VideoCodecProfile profile);
126 
127 // Split the string |src| by the delimiter |delim|.
128 std::vector<std::string> SplitString(const std::string& src, char delim);
129 
130 // Get monotonic timestamp for now in microseconds.
131 int64_t GetNowUs();
132 
133 // Get Mime type name from video codec type.
134 const char* GetMimeType(VideoCodecType type);
135 
136 }  // namespace android
137 #endif  // C2_E2E_TEST_COMMON_H_
138