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