blob: ba64f825922be28b5e68ba12227c586fcee0f761 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
kjellandera96e2d72016-02-04 23:52:28 -080011#ifndef WEBRTC_MEDIA_BASE_TESTUTILS_H_
12#define WEBRTC_MEDIA_BASE_TESTUTILS_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
14#include <string>
15#include <vector>
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000016
henrike@webrtc.org28e20752013-07-10 00:45:36 +000017#include "libyuv/compare.h"
tfarina5237aaf2015-11-10 23:44:30 -080018#include "webrtc/base/arraysize.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000019#include "webrtc/base/basictypes.h"
20#include "webrtc/base/sigslot.h"
21#include "webrtc/base/window.h"
kjellandera96e2d72016-02-04 23:52:28 -080022#include "webrtc/media/base/mediachannel.h"
23#include "webrtc/media/base/videocapturer.h"
24#include "webrtc/media/base/videocommon.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000027class ByteBuffer;
28class StreamInterface;
29}
30
31namespace cricket {
32
33// Returns size of 420 image with rounding on chroma for odd sizes.
34#define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2)
35// Returns size of ARGB image.
36#define ARGB_SIZE(w, h) (w * h * 4)
37
38template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
39 return std::vector<T>(a, a + s);
40}
tfarina5237aaf2015-11-10 23:44:30 -080041#define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
43struct RtpDumpPacket;
44class RtpDumpWriter;
45class VideoFrame;
46
47struct RawRtpPacket {
Peter Boström0c4e06b2015-10-07 12:23:21 +020048 void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBuffer* buf) const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050 // Check if this packet is the same as the specified packet except the
51 // sequence number and timestamp, which should be the same as the specified
52 // parameters.
Peter Boström0c4e06b2015-10-07 12:23:21 +020053 bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet,
54 uint16_t seq,
55 uint32_t ts,
56 uint32_t ssc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 int size() const { return 28; }
58
Peter Boström0c4e06b2015-10-07 12:23:21 +020059 uint8_t ver_to_cc;
60 uint8_t m_to_pt;
61 uint16_t sequence_number;
62 uint32_t timestamp;
63 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064 char payload[16];
65};
66
67struct RawRtcpPacket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000068 void WriteToByteBuffer(rtc::ByteBuffer* buf) const;
69 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 bool EqualsTo(const RawRtcpPacket& packet) const;
71
Peter Boström0c4e06b2015-10-07 12:23:21 +020072 uint8_t ver_to_count;
73 uint8_t type;
74 uint16_t length;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 char payload[16];
76};
77
78class RtpTestUtility {
79 public:
80 static size_t GetTestPacketCount();
81
82 // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets,
83 // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return
84 // true if successful.
Peter Boström0c4e06b2015-10-07 12:23:21 +020085 static bool WriteTestPackets(size_t count,
86 bool rtcp,
87 uint32_t rtp_ssrc,
88 RtpDumpWriter* writer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089
90 // Loop read the first count number of packets from the specified stream.
91 // Verify the elapsed time of the dump packets increase monotonically. If the
92 // stream is a RTP stream, verify the RTP sequence number, timestamp, and
93 // payload. If the stream is a RTCP stream, verify the RTCP header and
94 // payload.
Peter Boström0c4e06b2015-10-07 12:23:21 +020095 static bool VerifyTestPacketsFromStream(size_t count,
96 rtc::StreamInterface* stream,
97 uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098
99 // Verify the dump packet is the same as the raw RTP packet.
100 static bool VerifyPacket(const RtpDumpPacket* dump,
101 const RawRtpPacket* raw,
102 bool header_only);
103
Peter Boström0c4e06b2015-10-07 12:23:21 +0200104 static const uint32_t kDefaultSsrc = 1;
105 static const uint32_t kRtpTimestampIncrease = 90;
106 static const uint32_t kDefaultTimeIncrease = 30;
107 static const uint32_t kElapsedTimeInterval = 10;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108 static const RawRtpPacket kTestRawRtpPackets[];
109 static const RawRtcpPacket kTestRawRtcpPackets[];
110
111 private:
112 RtpTestUtility() {}
113};
114
115// Test helper for testing VideoCapturer implementations.
116class VideoCapturerListener : public sigslot::has_slots<> {
117 public:
118 explicit VideoCapturerListener(VideoCapturer* cap);
119
120 CaptureState last_capture_state() const { return last_capture_state_; }
121 int frame_count() const { return frame_count_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200122 uint32_t frame_fourcc() const { return frame_fourcc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123 int frame_width() const { return frame_width_; }
124 int frame_height() const { return frame_height_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200125 uint32_t frame_size() const { return frame_size_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 bool resolution_changed() const { return resolution_changed_; }
127
128 void OnStateChange(VideoCapturer* capturer, CaptureState state);
129 void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame);
130
131 private:
132 CaptureState last_capture_state_;
133 int frame_count_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200134 uint32_t frame_fourcc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 int frame_width_;
136 int frame_height_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200137 uint32_t frame_size_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000138 bool resolution_changed_;
139};
140
141class ScreencastEventCatcher : public sigslot::has_slots<> {
142 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000143 ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 uint32_t ssrc() const { return ssrc_; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000145 rtc::WindowEvent event() const { return ev_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200146 void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147 ssrc_ = ssrc;
148 ev_ = ev;
149 }
150 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200151 uint32_t ssrc_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000152 rtc::WindowEvent ev_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153};
154
155class VideoMediaErrorCatcher : public sigslot::has_slots<> {
156 public:
157 VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200158 uint32_t ssrc() const { return ssrc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 VideoMediaChannel::Error error() const { return error_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200160 void OnError(uint32_t ssrc, VideoMediaChannel::Error error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 ssrc_ = ssrc;
162 error_ = error;
163 }
164 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200165 uint32_t ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 VideoMediaChannel::Error error_;
167};
168
169// Returns the absolute path to a file in the testdata/ directory.
170std::string GetTestFilePath(const std::string& filename);
171
172// PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
173// sse is set to a small number for identical frames or sse == 0
174static inline double ComputePSNR(double sse, double count) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200175 return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse),
176 static_cast<uint64_t>(count));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177}
178
Peter Boström0c4e06b2015-10-07 12:23:21 +0200179static inline double ComputeSumSquareError(const uint8_t* org,
180 const uint8_t* rec,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 int size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183}
184
185// Loads the image with the specified prefix and size into |out|.
186bool LoadPlanarYuvTestImage(const std::string& prefix,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200187 int width,
188 int height,
189 uint8_t* out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190
191// Dumps the YUV image out to a file, for visual inspection.
192// PYUV tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200193void DumpPlanarYuvTestImage(const std::string& prefix,
194 const uint8_t* img,
195 int w,
196 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197
198// Dumps the ARGB image out to a file, for visual inspection.
199// ffplay tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200200void DumpPlanarArgbTestImage(const std::string& prefix,
201 const uint8_t* img,
202 int w,
203 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000204
205// Compare two I420 frames.
206bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
207
208// Checks whether |codecs| contains |codec|; checks using Codec::Matches().
209template <class C>
210bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) {
211 typename std::vector<C>::const_iterator it;
212 for (it = codecs.begin(); it != codecs.end(); ++it) {
213 if (it->Matches(codec)) {
214 return true;
215 }
216 }
217 return false;
218}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000219
220// Create Simulcast StreamParams with given |ssrcs| and |cname|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200221cricket::StreamParams CreateSimStreamParams(const std::string& cname,
222 const std::vector<uint32_t>& ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000223// Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
224// The number of |rtx_ssrcs| must match number of |ssrcs|.
225cricket::StreamParams CreateSimWithRtxStreamParams(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200226 const std::string& cname,
227 const std::vector<uint32_t>& ssrcs,
228 const std::vector<uint32_t>& rtx_ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000229
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230} // namespace cricket
231
kjellandera96e2d72016-02-04 23:52:28 -0800232#endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_