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