henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004 Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 28 | #ifndef WEBRTC_MEDIA_BASE_TESTUTILS_H_ |
| 29 | #define WEBRTC_MEDIA_BASE_TESTUTILS_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 33 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 34 | #include "libyuv/compare.h" |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 35 | #include "webrtc/base/arraysize.h" |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 36 | #include "webrtc/base/basictypes.h" |
| 37 | #include "webrtc/base/sigslot.h" |
| 38 | #include "webrtc/base/window.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 39 | #include "webrtc/media/base/mediachannel.h" |
| 40 | #include "webrtc/media/base/videocapturer.h" |
| 41 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 43 | namespace rtc { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 44 | class ByteBuffer; |
| 45 | class StreamInterface; |
| 46 | } |
| 47 | |
| 48 | namespace cricket { |
| 49 | |
| 50 | // Returns size of 420 image with rounding on chroma for odd sizes. |
| 51 | #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2) |
| 52 | // Returns size of ARGB image. |
| 53 | #define ARGB_SIZE(w, h) (w * h * 4) |
| 54 | |
| 55 | template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) { |
| 56 | return std::vector<T>(a, a + s); |
| 57 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 58 | #define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | |
| 60 | struct RtpDumpPacket; |
| 61 | class RtpDumpWriter; |
| 62 | class VideoFrame; |
| 63 | |
| 64 | struct RawRtpPacket { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 65 | void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBuffer* buf) const; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 66 | bool ReadFromByteBuffer(rtc::ByteBuffer* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 67 | // Check if this packet is the same as the specified packet except the |
| 68 | // sequence number and timestamp, which should be the same as the specified |
| 69 | // parameters. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 70 | bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet, |
| 71 | uint16_t seq, |
| 72 | uint32_t ts, |
| 73 | uint32_t ssc) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 74 | int size() const { return 28; } |
| 75 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 76 | uint8_t ver_to_cc; |
| 77 | uint8_t m_to_pt; |
| 78 | uint16_t sequence_number; |
| 79 | uint32_t timestamp; |
| 80 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | char payload[16]; |
| 82 | }; |
| 83 | |
| 84 | struct RawRtcpPacket { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 85 | void WriteToByteBuffer(rtc::ByteBuffer* buf) const; |
| 86 | bool ReadFromByteBuffer(rtc::ByteBuffer* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | bool EqualsTo(const RawRtcpPacket& packet) const; |
| 88 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 89 | uint8_t ver_to_count; |
| 90 | uint8_t type; |
| 91 | uint16_t length; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | char payload[16]; |
| 93 | }; |
| 94 | |
| 95 | class RtpTestUtility { |
| 96 | public: |
| 97 | static size_t GetTestPacketCount(); |
| 98 | |
| 99 | // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets, |
| 100 | // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return |
| 101 | // true if successful. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 102 | static bool WriteTestPackets(size_t count, |
| 103 | bool rtcp, |
| 104 | uint32_t rtp_ssrc, |
| 105 | RtpDumpWriter* writer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | |
| 107 | // Loop read the first count number of packets from the specified stream. |
| 108 | // Verify the elapsed time of the dump packets increase monotonically. If the |
| 109 | // stream is a RTP stream, verify the RTP sequence number, timestamp, and |
| 110 | // payload. If the stream is a RTCP stream, verify the RTCP header and |
| 111 | // payload. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 112 | static bool VerifyTestPacketsFromStream(size_t count, |
| 113 | rtc::StreamInterface* stream, |
| 114 | uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | |
| 116 | // Verify the dump packet is the same as the raw RTP packet. |
| 117 | static bool VerifyPacket(const RtpDumpPacket* dump, |
| 118 | const RawRtpPacket* raw, |
| 119 | bool header_only); |
| 120 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 121 | static const uint32_t kDefaultSsrc = 1; |
| 122 | static const uint32_t kRtpTimestampIncrease = 90; |
| 123 | static const uint32_t kDefaultTimeIncrease = 30; |
| 124 | static const uint32_t kElapsedTimeInterval = 10; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | static const RawRtpPacket kTestRawRtpPackets[]; |
| 126 | static const RawRtcpPacket kTestRawRtcpPackets[]; |
| 127 | |
| 128 | private: |
| 129 | RtpTestUtility() {} |
| 130 | }; |
| 131 | |
| 132 | // Test helper for testing VideoCapturer implementations. |
| 133 | class VideoCapturerListener : public sigslot::has_slots<> { |
| 134 | public: |
| 135 | explicit VideoCapturerListener(VideoCapturer* cap); |
| 136 | |
| 137 | CaptureState last_capture_state() const { return last_capture_state_; } |
| 138 | int frame_count() const { return frame_count_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 139 | uint32_t frame_fourcc() const { return frame_fourcc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 140 | int frame_width() const { return frame_width_; } |
| 141 | int frame_height() const { return frame_height_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 142 | uint32_t frame_size() const { return frame_size_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | bool resolution_changed() const { return resolution_changed_; } |
| 144 | |
| 145 | void OnStateChange(VideoCapturer* capturer, CaptureState state); |
| 146 | void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame); |
| 147 | |
| 148 | private: |
| 149 | CaptureState last_capture_state_; |
| 150 | int frame_count_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 151 | uint32_t frame_fourcc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 152 | int frame_width_; |
| 153 | int frame_height_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 154 | uint32_t frame_size_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 155 | bool resolution_changed_; |
| 156 | }; |
| 157 | |
| 158 | class ScreencastEventCatcher : public sigslot::has_slots<> { |
| 159 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 160 | ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 161 | uint32_t ssrc() const { return ssrc_; } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 162 | rtc::WindowEvent event() const { return ev_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 163 | void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 164 | ssrc_ = ssrc; |
| 165 | ev_ = ev; |
| 166 | } |
| 167 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 168 | uint32_t ssrc_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 169 | rtc::WindowEvent ev_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | class VideoMediaErrorCatcher : public sigslot::has_slots<> { |
| 173 | public: |
| 174 | VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 175 | uint32_t ssrc() const { return ssrc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 176 | VideoMediaChannel::Error error() const { return error_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 177 | void OnError(uint32_t ssrc, VideoMediaChannel::Error error) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 178 | ssrc_ = ssrc; |
| 179 | error_ = error; |
| 180 | } |
| 181 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 182 | uint32_t ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | VideoMediaChannel::Error error_; |
| 184 | }; |
| 185 | |
| 186 | // Returns the absolute path to a file in the testdata/ directory. |
| 187 | std::string GetTestFilePath(const std::string& filename); |
| 188 | |
| 189 | // PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse) |
| 190 | // sse is set to a small number for identical frames or sse == 0 |
| 191 | static inline double ComputePSNR(double sse, double count) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 192 | return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse), |
| 193 | static_cast<uint64_t>(count)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 196 | static inline double ComputeSumSquareError(const uint8_t* org, |
| 197 | const uint8_t* rec, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 198 | int size) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 199 | return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Loads the image with the specified prefix and size into |out|. |
| 203 | bool LoadPlanarYuvTestImage(const std::string& prefix, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 204 | int width, |
| 205 | int height, |
| 206 | uint8_t* out); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 207 | |
| 208 | // Dumps the YUV image out to a file, for visual inspection. |
| 209 | // PYUV tool can be used to view dump files. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 210 | void DumpPlanarYuvTestImage(const std::string& prefix, |
| 211 | const uint8_t* img, |
| 212 | int w, |
| 213 | int h); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | |
| 215 | // Dumps the ARGB image out to a file, for visual inspection. |
| 216 | // ffplay tool can be used to view dump files. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 217 | void DumpPlanarArgbTestImage(const std::string& prefix, |
| 218 | const uint8_t* img, |
| 219 | int w, |
| 220 | int h); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 221 | |
| 222 | // Compare two I420 frames. |
| 223 | bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1); |
| 224 | |
| 225 | // Checks whether |codecs| contains |codec|; checks using Codec::Matches(). |
| 226 | template <class C> |
| 227 | bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) { |
| 228 | typename std::vector<C>::const_iterator it; |
| 229 | for (it = codecs.begin(); it != codecs.end(); ++it) { |
| 230 | if (it->Matches(codec)) { |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 236 | |
| 237 | // Create Simulcast StreamParams with given |ssrcs| and |cname|. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 238 | cricket::StreamParams CreateSimStreamParams(const std::string& cname, |
| 239 | const std::vector<uint32_t>& ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 240 | // Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|. |
| 241 | // The number of |rtx_ssrcs| must match number of |ssrcs|. |
| 242 | cricket::StreamParams CreateSimWithRtxStreamParams( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 243 | const std::string& cname, |
| 244 | const std::vector<uint32_t>& ssrcs, |
| 245 | const std::vector<uint32_t>& rtx_ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 246 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 247 | } // namespace cricket |
| 248 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame^] | 249 | #endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_ |