henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame^] | 2 | * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame^] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_BASE_TESTUTILS_H_ |
| 12 | #define WEBRTC_MEDIA_BASE_TESTUTILS_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 16 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 17 | #include "libyuv/compare.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 18 | #include "webrtc/base/arraysize.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 19 | #include "webrtc/base/basictypes.h" |
| 20 | #include "webrtc/base/sigslot.h" |
| 21 | #include "webrtc/base/window.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 22 | #include "webrtc/media/base/mediachannel.h" |
| 23 | #include "webrtc/media/base/videocapturer.h" |
| 24 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 26 | namespace rtc { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | class ByteBuffer; |
| 28 | class StreamInterface; |
| 29 | } |
| 30 | |
| 31 | namespace cricket { |
| 32 | |
| 33 | // Returns size of 420 image with rounding on chroma for odd sizes. |
| 34 | #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2) |
| 35 | // Returns size of ARGB image. |
| 36 | #define ARGB_SIZE(w, h) (w * h * 4) |
| 37 | |
| 38 | template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) { |
| 39 | return std::vector<T>(a, a + s); |
| 40 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 41 | #define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
| 43 | struct RtpDumpPacket; |
| 44 | class RtpDumpWriter; |
| 45 | class VideoFrame; |
| 46 | |
| 47 | struct RawRtpPacket { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 48 | void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBuffer* buf) const; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 49 | bool ReadFromByteBuffer(rtc::ByteBuffer* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 50 | // Check if this packet is the same as the specified packet except the |
| 51 | // sequence number and timestamp, which should be the same as the specified |
| 52 | // parameters. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 53 | bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet, |
| 54 | uint16_t seq, |
| 55 | uint32_t ts, |
| 56 | uint32_t ssc) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 57 | int size() const { return 28; } |
| 58 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 59 | uint8_t ver_to_cc; |
| 60 | uint8_t m_to_pt; |
| 61 | uint16_t sequence_number; |
| 62 | uint32_t timestamp; |
| 63 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 64 | char payload[16]; |
| 65 | }; |
| 66 | |
| 67 | struct RawRtcpPacket { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 68 | void WriteToByteBuffer(rtc::ByteBuffer* buf) const; |
| 69 | bool ReadFromByteBuffer(rtc::ByteBuffer* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 70 | bool EqualsTo(const RawRtcpPacket& packet) const; |
| 71 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 72 | uint8_t ver_to_count; |
| 73 | uint8_t type; |
| 74 | uint16_t length; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | char payload[16]; |
| 76 | }; |
| 77 | |
| 78 | class RtpTestUtility { |
| 79 | public: |
| 80 | static size_t GetTestPacketCount(); |
| 81 | |
| 82 | // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets, |
| 83 | // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return |
| 84 | // true if successful. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 85 | static bool WriteTestPackets(size_t count, |
| 86 | bool rtcp, |
| 87 | uint32_t rtp_ssrc, |
| 88 | RtpDumpWriter* writer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 89 | |
| 90 | // Loop read the first count number of packets from the specified stream. |
| 91 | // Verify the elapsed time of the dump packets increase monotonically. If the |
| 92 | // stream is a RTP stream, verify the RTP sequence number, timestamp, and |
| 93 | // payload. If the stream is a RTCP stream, verify the RTCP header and |
| 94 | // payload. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 95 | static bool VerifyTestPacketsFromStream(size_t count, |
| 96 | rtc::StreamInterface* stream, |
| 97 | uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | |
| 99 | // Verify the dump packet is the same as the raw RTP packet. |
| 100 | static bool VerifyPacket(const RtpDumpPacket* dump, |
| 101 | const RawRtpPacket* raw, |
| 102 | bool header_only); |
| 103 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 104 | static const uint32_t kDefaultSsrc = 1; |
| 105 | static const uint32_t kRtpTimestampIncrease = 90; |
| 106 | static const uint32_t kDefaultTimeIncrease = 30; |
| 107 | static const uint32_t kElapsedTimeInterval = 10; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | static const RawRtpPacket kTestRawRtpPackets[]; |
| 109 | static const RawRtcpPacket kTestRawRtcpPackets[]; |
| 110 | |
| 111 | private: |
| 112 | RtpTestUtility() {} |
| 113 | }; |
| 114 | |
| 115 | // Test helper for testing VideoCapturer implementations. |
| 116 | class VideoCapturerListener : public sigslot::has_slots<> { |
| 117 | public: |
| 118 | explicit VideoCapturerListener(VideoCapturer* cap); |
| 119 | |
| 120 | CaptureState last_capture_state() const { return last_capture_state_; } |
| 121 | int frame_count() const { return frame_count_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 122 | uint32_t frame_fourcc() const { return frame_fourcc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 123 | int frame_width() const { return frame_width_; } |
| 124 | int frame_height() const { return frame_height_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 125 | uint32_t frame_size() const { return frame_size_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 126 | bool resolution_changed() const { return resolution_changed_; } |
| 127 | |
| 128 | void OnStateChange(VideoCapturer* capturer, CaptureState state); |
| 129 | void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame); |
| 130 | |
| 131 | private: |
| 132 | CaptureState last_capture_state_; |
| 133 | int frame_count_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 134 | uint32_t frame_fourcc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 135 | int frame_width_; |
| 136 | int frame_height_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 137 | uint32_t frame_size_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 138 | bool resolution_changed_; |
| 139 | }; |
| 140 | |
| 141 | class ScreencastEventCatcher : public sigslot::has_slots<> { |
| 142 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 143 | ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 144 | uint32_t ssrc() const { return ssrc_; } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 145 | rtc::WindowEvent event() const { return ev_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 146 | void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 147 | ssrc_ = ssrc; |
| 148 | ev_ = ev; |
| 149 | } |
| 150 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 151 | uint32_t ssrc_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 152 | rtc::WindowEvent ev_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | class VideoMediaErrorCatcher : public sigslot::has_slots<> { |
| 156 | public: |
| 157 | VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 158 | uint32_t ssrc() const { return ssrc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 159 | VideoMediaChannel::Error error() const { return error_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 160 | void OnError(uint32_t ssrc, VideoMediaChannel::Error error) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 161 | ssrc_ = ssrc; |
| 162 | error_ = error; |
| 163 | } |
| 164 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 165 | uint32_t ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 166 | VideoMediaChannel::Error error_; |
| 167 | }; |
| 168 | |
| 169 | // Returns the absolute path to a file in the testdata/ directory. |
| 170 | std::string GetTestFilePath(const std::string& filename); |
| 171 | |
| 172 | // PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse) |
| 173 | // sse is set to a small number for identical frames or sse == 0 |
| 174 | static inline double ComputePSNR(double sse, double count) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 175 | return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse), |
| 176 | static_cast<uint64_t>(count)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 179 | static inline double ComputeSumSquareError(const uint8_t* org, |
| 180 | const uint8_t* rec, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 181 | int size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 182 | return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Loads the image with the specified prefix and size into |out|. |
| 186 | bool LoadPlanarYuvTestImage(const std::string& prefix, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 187 | int width, |
| 188 | int height, |
| 189 | uint8_t* out); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 190 | |
| 191 | // Dumps the YUV image out to a file, for visual inspection. |
| 192 | // PYUV tool can be used to view dump files. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 193 | void DumpPlanarYuvTestImage(const std::string& prefix, |
| 194 | const uint8_t* img, |
| 195 | int w, |
| 196 | int h); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 197 | |
| 198 | // Dumps the ARGB image out to a file, for visual inspection. |
| 199 | // ffplay tool can be used to view dump files. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 200 | void DumpPlanarArgbTestImage(const std::string& prefix, |
| 201 | const uint8_t* img, |
| 202 | int w, |
| 203 | int h); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 204 | |
| 205 | // Compare two I420 frames. |
| 206 | bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1); |
| 207 | |
| 208 | // Checks whether |codecs| contains |codec|; checks using Codec::Matches(). |
| 209 | template <class C> |
| 210 | bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) { |
| 211 | typename std::vector<C>::const_iterator it; |
| 212 | for (it = codecs.begin(); it != codecs.end(); ++it) { |
| 213 | if (it->Matches(codec)) { |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
| 218 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 219 | |
| 220 | // Create Simulcast StreamParams with given |ssrcs| and |cname|. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 221 | cricket::StreamParams CreateSimStreamParams(const std::string& cname, |
| 222 | const std::vector<uint32_t>& ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 223 | // Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|. |
| 224 | // The number of |rtx_ssrcs| must match number of |ssrcs|. |
| 225 | cricket::StreamParams CreateSimWithRtxStreamParams( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 226 | const std::string& cname, |
| 227 | const std::vector<uint32_t>& ssrcs, |
| 228 | const std::vector<uint32_t>& rtx_ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 229 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | } // namespace cricket |
| 231 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 232 | #endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_ |