blob: 6d814efe6db9c70235ca996b057ac03d44ea0b35 [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 {
jbauchf1f87202016-03-30 06:43:37 -070027class ByteBufferReader;
28class ByteBufferWriter;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029class StreamInterface;
30}
31
32namespace 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
39template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
40 return std::vector<T>(a, a + s);
41}
tfarina5237aaf2015-11-10 23:44:30 -080042#define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a))
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
44struct RtpDumpPacket;
45class RtpDumpWriter;
46class VideoFrame;
47
48struct RawRtpPacket {
jbauchf1f87202016-03-30 06:43:37 -070049 void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBufferWriter* buf) const;
50 bool ReadFromByteBuffer(rtc::ByteBufferReader* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051 // 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öm0c4e06b2015-10-07 12:23:21 +020054 bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet,
55 uint16_t seq,
56 uint32_t ts,
57 uint32_t ssc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 int size() const { return 28; }
59
Peter Boström0c4e06b2015-10-07 12:23:21 +020060 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.org28e20752013-07-10 00:45:36 +000065 char payload[16];
66};
67
68struct RawRtcpPacket {
jbauchf1f87202016-03-30 06:43:37 -070069 void WriteToByteBuffer(rtc::ByteBufferWriter* buf) const;
70 bool ReadFromByteBuffer(rtc::ByteBufferReader* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071 bool EqualsTo(const RawRtcpPacket& packet) const;
72
Peter Boström0c4e06b2015-10-07 12:23:21 +020073 uint8_t ver_to_count;
74 uint8_t type;
75 uint16_t length;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000076 char payload[16];
77};
78
79class 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öm0c4e06b2015-10-07 12:23:21 +020086 static bool WriteTestPackets(size_t count,
87 bool rtcp,
88 uint32_t rtp_ssrc,
89 RtpDumpWriter* writer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
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öm0c4e06b2015-10-07 12:23:21 +020096 static bool VerifyTestPacketsFromStream(size_t count,
97 rtc::StreamInterface* stream,
98 uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099
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öm0c4e06b2015-10-07 12:23:21 +0200105 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.org28e20752013-07-10 00:45:36 +0000109 static const RawRtpPacket kTestRawRtpPackets[];
110 static const RawRtcpPacket kTestRawRtcpPackets[];
111
112 private:
113 RtpTestUtility() {}
114};
115
116// Test helper for testing VideoCapturer implementations.
117class 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öm0c4e06b2015-10-07 12:23:21 +0200123 uint32_t frame_fourcc() const { return frame_fourcc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 int frame_width() const { return frame_width_; }
125 int frame_height() const { return frame_height_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200126 uint32_t frame_size() const { return frame_size_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 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öm0c4e06b2015-10-07 12:23:21 +0200135 uint32_t frame_fourcc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 int frame_width_;
137 int frame_height_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200138 uint32_t frame_size_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 bool resolution_changed_;
140};
141
142class ScreencastEventCatcher : public sigslot::has_slots<> {
143 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000144 ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200145 uint32_t ssrc() const { return ssrc_; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000146 rtc::WindowEvent event() const { return ev_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200147 void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 ssrc_ = ssrc;
149 ev_ = ev;
150 }
151 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200152 uint32_t ssrc_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000153 rtc::WindowEvent ev_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154};
155
156class VideoMediaErrorCatcher : public sigslot::has_slots<> {
157 public:
158 VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200159 uint32_t ssrc() const { return ssrc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 VideoMediaChannel::Error error() const { return error_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200161 void OnError(uint32_t ssrc, VideoMediaChannel::Error error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000162 ssrc_ = ssrc;
163 error_ = error;
164 }
165 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200166 uint32_t ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 VideoMediaChannel::Error error_;
168};
169
170// Returns the absolute path to a file in the testdata/ directory.
171std::string GetTestFilePath(const std::string& filename);
172
173// PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
174// sse is set to a small number for identical frames or sse == 0
175static inline double ComputePSNR(double sse, double count) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200176 return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse),
177 static_cast<uint64_t>(count));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178}
179
Peter Boström0c4e06b2015-10-07 12:23:21 +0200180static inline double ComputeSumSquareError(const uint8_t* org,
181 const uint8_t* rec,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 int size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000184}
185
186// Loads the image with the specified prefix and size into |out|.
187bool LoadPlanarYuvTestImage(const std::string& prefix,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200188 int width,
189 int height,
190 uint8_t* out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000191
192// Dumps the YUV image out to a file, for visual inspection.
193// PYUV tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200194void DumpPlanarYuvTestImage(const std::string& prefix,
195 const uint8_t* img,
196 int w,
197 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198
199// Dumps the ARGB image out to a file, for visual inspection.
200// ffplay tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200201void DumpPlanarArgbTestImage(const std::string& prefix,
202 const uint8_t* img,
203 int w,
204 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205
206// Compare two I420 frames.
207bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
208
209// Checks whether |codecs| contains |codec|; checks using Codec::Matches().
210template <class C>
211bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) {
212 typename std::vector<C>::const_iterator it;
213 for (it = codecs.begin(); it != codecs.end(); ++it) {
214 if (it->Matches(codec)) {
215 return true;
216 }
217 }
218 return false;
219}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000220
221// Create Simulcast StreamParams with given |ssrcs| and |cname|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200222cricket::StreamParams CreateSimStreamParams(const std::string& cname,
223 const std::vector<uint32_t>& ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000224// Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
225// The number of |rtx_ssrcs| must match number of |ssrcs|.
226cricket::StreamParams CreateSimWithRtxStreamParams(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200227 const std::string& cname,
228 const std::vector<uint32_t>& ssrcs,
229 const std::vector<uint32_t>& rtx_ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000230
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231} // namespace cricket
232
kjellandera96e2d72016-02-04 23:52:28 -0800233#endif // WEBRTC_MEDIA_BASE_TESTUTILS_H_