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 { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 27 | class ByteBufferReader; |
| 28 | class ByteBufferWriter; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | class StreamInterface; |
| 30 | } |
| 31 | |
| 32 | namespace cricket { |
| 33 | |
| 34 | // Returns size of 420 image with rounding on chroma for odd sizes. |
| 35 | #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2) |
| 36 | // Returns size of ARGB image. |
| 37 | #define ARGB_SIZE(w, h) (w * h * 4) |
| 38 | |
| 39 | template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) { |
| 40 | return std::vector<T>(a, a + s); |
| 41 | } |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 42 | #define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a)) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | |
| 44 | struct RtpDumpPacket; |
| 45 | class RtpDumpWriter; |
| 46 | class VideoFrame; |
| 47 | |
| 48 | struct RawRtpPacket { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 49 | void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBufferWriter* buf) const; |
| 50 | bool ReadFromByteBuffer(rtc::ByteBufferReader* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 51 | // Check if this packet is the same as the specified packet except the |
| 52 | // sequence number and timestamp, which should be the same as the specified |
| 53 | // parameters. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 54 | bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet, |
| 55 | uint16_t seq, |
| 56 | uint32_t ts, |
| 57 | uint32_t ssc) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | int size() const { return 28; } |
| 59 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 60 | uint8_t ver_to_cc; |
| 61 | uint8_t m_to_pt; |
| 62 | uint16_t sequence_number; |
| 63 | uint32_t timestamp; |
| 64 | uint32_t ssrc; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 65 | char payload[16]; |
| 66 | }; |
| 67 | |
| 68 | struct RawRtcpPacket { |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 69 | void WriteToByteBuffer(rtc::ByteBufferWriter* buf) const; |
| 70 | bool ReadFromByteBuffer(rtc::ByteBufferReader* buf); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 71 | bool EqualsTo(const RawRtcpPacket& packet) const; |
| 72 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 73 | uint8_t ver_to_count; |
| 74 | uint8_t type; |
| 75 | uint16_t length; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 76 | char payload[16]; |
| 77 | }; |
| 78 | |
| 79 | class RtpTestUtility { |
| 80 | public: |
| 81 | static size_t GetTestPacketCount(); |
| 82 | |
| 83 | // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets, |
| 84 | // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return |
| 85 | // true if successful. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 86 | static bool WriteTestPackets(size_t count, |
| 87 | bool rtcp, |
| 88 | uint32_t rtp_ssrc, |
| 89 | RtpDumpWriter* writer); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | |
| 91 | // Loop read the first count number of packets from the specified stream. |
| 92 | // Verify the elapsed time of the dump packets increase monotonically. If the |
| 93 | // stream is a RTP stream, verify the RTP sequence number, timestamp, and |
| 94 | // payload. If the stream is a RTCP stream, verify the RTCP header and |
| 95 | // payload. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 96 | static bool VerifyTestPacketsFromStream(size_t count, |
| 97 | rtc::StreamInterface* stream, |
| 98 | uint32_t ssrc); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | |
| 100 | // Verify the dump packet is the same as the raw RTP packet. |
| 101 | static bool VerifyPacket(const RtpDumpPacket* dump, |
| 102 | const RawRtpPacket* raw, |
| 103 | bool header_only); |
| 104 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 105 | static const uint32_t kDefaultSsrc = 1; |
| 106 | static const uint32_t kRtpTimestampIncrease = 90; |
| 107 | static const uint32_t kDefaultTimeIncrease = 30; |
| 108 | static const uint32_t kElapsedTimeInterval = 10; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 109 | static const RawRtpPacket kTestRawRtpPackets[]; |
| 110 | static const RawRtcpPacket kTestRawRtcpPackets[]; |
| 111 | |
| 112 | private: |
| 113 | RtpTestUtility() {} |
| 114 | }; |
| 115 | |
| 116 | // Test helper for testing VideoCapturer implementations. |
| 117 | class VideoCapturerListener : public sigslot::has_slots<> { |
| 118 | public: |
| 119 | explicit VideoCapturerListener(VideoCapturer* cap); |
| 120 | |
| 121 | CaptureState last_capture_state() const { return last_capture_state_; } |
| 122 | int frame_count() const { return frame_count_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 123 | uint32_t frame_fourcc() const { return frame_fourcc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 124 | int frame_width() const { return frame_width_; } |
| 125 | int frame_height() const { return frame_height_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 126 | uint32_t frame_size() const { return frame_size_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | bool resolution_changed() const { return resolution_changed_; } |
| 128 | |
| 129 | void OnStateChange(VideoCapturer* capturer, CaptureState state); |
| 130 | void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame); |
| 131 | |
| 132 | private: |
| 133 | CaptureState last_capture_state_; |
| 134 | int frame_count_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 135 | uint32_t frame_fourcc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 136 | int frame_width_; |
| 137 | int frame_height_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 138 | uint32_t frame_size_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | bool resolution_changed_; |
| 140 | }; |
| 141 | |
| 142 | class ScreencastEventCatcher : public sigslot::has_slots<> { |
| 143 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 144 | ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 145 | uint32_t ssrc() const { return ssrc_; } |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 146 | rtc::WindowEvent event() const { return ev_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 147 | void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | ssrc_ = ssrc; |
| 149 | ev_ = ev; |
| 150 | } |
| 151 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 152 | uint32_t ssrc_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 153 | rtc::WindowEvent ev_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | class VideoMediaErrorCatcher : public sigslot::has_slots<> { |
| 157 | public: |
| 158 | VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 159 | uint32_t ssrc() const { return ssrc_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 160 | VideoMediaChannel::Error error() const { return error_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 161 | void OnError(uint32_t ssrc, VideoMediaChannel::Error error) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 162 | ssrc_ = ssrc; |
| 163 | error_ = error; |
| 164 | } |
| 165 | private: |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 166 | uint32_t ssrc_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 167 | VideoMediaChannel::Error error_; |
| 168 | }; |
| 169 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 170 | // Checks whether |codecs| contains |codec|; checks using Codec::Matches(). |
| 171 | template <class C> |
| 172 | bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) { |
| 173 | typename std::vector<C>::const_iterator it; |
| 174 | for (it = codecs.begin(); it != codecs.end(); ++it) { |
| 175 | if (it->Matches(codec)) { |
| 176 | return true; |
| 177 | } |
| 178 | } |
| 179 | return false; |
| 180 | } |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 181 | |
| 182 | // Create Simulcast StreamParams with given |ssrcs| and |cname|. |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 183 | cricket::StreamParams CreateSimStreamParams(const std::string& cname, |
| 184 | const std::vector<uint32_t>& ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 185 | // Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|. |
| 186 | // The number of |rtx_ssrcs| must match number of |ssrcs|. |
| 187 | cricket::StreamParams CreateSimWithRtxStreamParams( |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 188 | const std::string& cname, |
| 189 | const std::vector<uint32_t>& ssrcs, |
| 190 | const std::vector<uint32_t>& rtx_ssrcs); |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 191 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 192 | } // namespace cricket |
| 193 | |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 194 | #endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_ |