blob: c894f7fb4eea3c9fd27ecaf1c5c30c3af3bd0578 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
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
kjellandera96e2d72016-02-04 23:52:28 -080028#ifndef WEBRTC_MEDIA_BASE_TESTUTILS_H_
29#define WEBRTC_MEDIA_BASE_TESTUTILS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000030
31#include <string>
32#include <vector>
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000033
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include "libyuv/compare.h"
tfarina5237aaf2015-11-10 23:44:30 -080035#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "webrtc/base/basictypes.h"
37#include "webrtc/base/sigslot.h"
38#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/media/base/mediachannel.h"
40#include "webrtc/media/base/videocapturer.h"
41#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000043namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044class ByteBuffer;
45class StreamInterface;
46}
47
48namespace 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
55template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
56 return std::vector<T>(a, a + s);
57}
tfarina5237aaf2015-11-10 23:44:30 -080058#define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
60struct RtpDumpPacket;
61class RtpDumpWriter;
62class VideoFrame;
63
64struct RawRtpPacket {
Peter Boström0c4e06b2015-10-07 12:23:21 +020065 void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBuffer* buf) const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000066 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067 // 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öm0c4e06b2015-10-07 12:23:21 +020070 bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet,
71 uint16_t seq,
72 uint32_t ts,
73 uint32_t ssc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000074 int size() const { return 28; }
75
Peter Boström0c4e06b2015-10-07 12:23:21 +020076 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.org28e20752013-07-10 00:45:36 +000081 char payload[16];
82};
83
84struct RawRtcpPacket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000085 void WriteToByteBuffer(rtc::ByteBuffer* buf) const;
86 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 bool EqualsTo(const RawRtcpPacket& packet) const;
88
Peter Boström0c4e06b2015-10-07 12:23:21 +020089 uint8_t ver_to_count;
90 uint8_t type;
91 uint16_t length;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092 char payload[16];
93};
94
95class 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öm0c4e06b2015-10-07 12:23:21 +0200102 static bool WriteTestPackets(size_t count,
103 bool rtcp,
104 uint32_t rtp_ssrc,
105 RtpDumpWriter* writer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106
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öm0c4e06b2015-10-07 12:23:21 +0200112 static bool VerifyTestPacketsFromStream(size_t count,
113 rtc::StreamInterface* stream,
114 uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000115
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öm0c4e06b2015-10-07 12:23:21 +0200121 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.org28e20752013-07-10 00:45:36 +0000125 static const RawRtpPacket kTestRawRtpPackets[];
126 static const RawRtcpPacket kTestRawRtcpPackets[];
127
128 private:
129 RtpTestUtility() {}
130};
131
132// Test helper for testing VideoCapturer implementations.
133class 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öm0c4e06b2015-10-07 12:23:21 +0200139 uint32_t frame_fourcc() const { return frame_fourcc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000140 int frame_width() const { return frame_width_; }
141 int frame_height() const { return frame_height_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200142 uint32_t frame_size() const { return frame_size_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143 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öm0c4e06b2015-10-07 12:23:21 +0200151 uint32_t frame_fourcc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 int frame_width_;
153 int frame_height_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200154 uint32_t frame_size_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 bool resolution_changed_;
156};
157
158class ScreencastEventCatcher : public sigslot::has_slots<> {
159 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000160 ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 uint32_t ssrc() const { return ssrc_; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000162 rtc::WindowEvent event() const { return ev_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200163 void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 ssrc_ = ssrc;
165 ev_ = ev;
166 }
167 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200168 uint32_t ssrc_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000169 rtc::WindowEvent ev_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000170};
171
172class VideoMediaErrorCatcher : public sigslot::has_slots<> {
173 public:
174 VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200175 uint32_t ssrc() const { return ssrc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 VideoMediaChannel::Error error() const { return error_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200177 void OnError(uint32_t ssrc, VideoMediaChannel::Error error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 ssrc_ = ssrc;
179 error_ = error;
180 }
181 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200182 uint32_t ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 VideoMediaChannel::Error error_;
184};
185
186// Returns the absolute path to a file in the testdata/ directory.
187std::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
191static inline double ComputePSNR(double sse, double count) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200192 return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse),
193 static_cast<uint64_t>(count));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000194}
195
Peter Boström0c4e06b2015-10-07 12:23:21 +0200196static inline double ComputeSumSquareError(const uint8_t* org,
197 const uint8_t* rec,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 int size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200}
201
202// Loads the image with the specified prefix and size into |out|.
203bool LoadPlanarYuvTestImage(const std::string& prefix,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200204 int width,
205 int height,
206 uint8_t* out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000207
208// Dumps the YUV image out to a file, for visual inspection.
209// PYUV tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200210void DumpPlanarYuvTestImage(const std::string& prefix,
211 const uint8_t* img,
212 int w,
213 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214
215// Dumps the ARGB image out to a file, for visual inspection.
216// ffplay tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200217void DumpPlanarArgbTestImage(const std::string& prefix,
218 const uint8_t* img,
219 int w,
220 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000221
222// Compare two I420 frames.
223bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
224
225// Checks whether |codecs| contains |codec|; checks using Codec::Matches().
226template <class C>
227bool 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.org5bc25c42013-12-05 00:24:06 +0000236
237// Create Simulcast StreamParams with given |ssrcs| and |cname|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200238cricket::StreamParams CreateSimStreamParams(const std::string& cname,
239 const std::vector<uint32_t>& ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000240// Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
241// The number of |rtx_ssrcs| must match number of |ssrcs|.
242cricket::StreamParams CreateSimWithRtxStreamParams(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200243 const std::string& cname,
244 const std::vector<uint32_t>& ssrcs,
245 const std::vector<uint32_t>& rtx_ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000246
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000247} // namespace cricket
248
kjellandera96e2d72016-02-04 23:52:28 -0800249#endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_