blob: cb4146d7078cd6ea697cffd9699e7c4f5713b41f [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
28#ifndef TALK_MEDIA_BASE_TESTUTILS_H_
29#define TALK_MEDIA_BASE_TESTUTILS_H_
30
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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/media/base/mediachannel.h"
36#include "talk/media/base/videocapturer.h"
37#include "talk/media/base/videocommon.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000038#include "webrtc/base/basictypes.h"
39#include "webrtc/base/sigslot.h"
40#include "webrtc/base/window.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042namespace rtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043class ByteBuffer;
44class StreamInterface;
45}
46
47namespace cricket {
48
49// Returns size of 420 image with rounding on chroma for odd sizes.
50#define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2)
51// Returns size of ARGB image.
52#define ARGB_SIZE(w, h) (w * h * 4)
53
54template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
55 return std::vector<T>(a, a + s);
56}
57#define MAKE_VECTOR(a) cricket::MakeVector(a, ARRAY_SIZE(a))
58
59struct RtpDumpPacket;
60class RtpDumpWriter;
61class VideoFrame;
62
63struct RawRtpPacket {
Peter Boström0c4e06b2015-10-07 12:23:21 +020064 void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBuffer* buf) const;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000065 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066 // Check if this packet is the same as the specified packet except the
67 // sequence number and timestamp, which should be the same as the specified
68 // parameters.
Peter Boström0c4e06b2015-10-07 12:23:21 +020069 bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet,
70 uint16_t seq,
71 uint32_t ts,
72 uint32_t ssc) const;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073 int size() const { return 28; }
74
Peter Boström0c4e06b2015-10-07 12:23:21 +020075 uint8_t ver_to_cc;
76 uint8_t m_to_pt;
77 uint16_t sequence_number;
78 uint32_t timestamp;
79 uint32_t ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080 char payload[16];
81};
82
83struct RawRtcpPacket {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000084 void WriteToByteBuffer(rtc::ByteBuffer* buf) const;
85 bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086 bool EqualsTo(const RawRtcpPacket& packet) const;
87
Peter Boström0c4e06b2015-10-07 12:23:21 +020088 uint8_t ver_to_count;
89 uint8_t type;
90 uint16_t length;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 char payload[16];
92};
93
94class RtpTestUtility {
95 public:
96 static size_t GetTestPacketCount();
97
98 // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets,
99 // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return
100 // true if successful.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200101 static bool WriteTestPackets(size_t count,
102 bool rtcp,
103 uint32_t rtp_ssrc,
104 RtpDumpWriter* writer);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105
106 // Loop read the first count number of packets from the specified stream.
107 // Verify the elapsed time of the dump packets increase monotonically. If the
108 // stream is a RTP stream, verify the RTP sequence number, timestamp, and
109 // payload. If the stream is a RTCP stream, verify the RTCP header and
110 // payload.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200111 static bool VerifyTestPacketsFromStream(size_t count,
112 rtc::StreamInterface* stream,
113 uint32_t ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114
115 // Verify the dump packet is the same as the raw RTP packet.
116 static bool VerifyPacket(const RtpDumpPacket* dump,
117 const RawRtpPacket* raw,
118 bool header_only);
119
Peter Boström0c4e06b2015-10-07 12:23:21 +0200120 static const uint32_t kDefaultSsrc = 1;
121 static const uint32_t kRtpTimestampIncrease = 90;
122 static const uint32_t kDefaultTimeIncrease = 30;
123 static const uint32_t kElapsedTimeInterval = 10;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124 static const RawRtpPacket kTestRawRtpPackets[];
125 static const RawRtcpPacket kTestRawRtcpPackets[];
126
127 private:
128 RtpTestUtility() {}
129};
130
131// Test helper for testing VideoCapturer implementations.
132class VideoCapturerListener : public sigslot::has_slots<> {
133 public:
134 explicit VideoCapturerListener(VideoCapturer* cap);
135
136 CaptureState last_capture_state() const { return last_capture_state_; }
137 int frame_count() const { return frame_count_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200138 uint32_t frame_fourcc() const { return frame_fourcc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139 int frame_width() const { return frame_width_; }
140 int frame_height() const { return frame_height_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200141 uint32_t frame_size() const { return frame_size_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 bool resolution_changed() const { return resolution_changed_; }
143
144 void OnStateChange(VideoCapturer* capturer, CaptureState state);
145 void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame);
146
147 private:
148 CaptureState last_capture_state_;
149 int frame_count_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200150 uint32_t frame_fourcc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 int frame_width_;
152 int frame_height_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200153 uint32_t frame_size_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 bool resolution_changed_;
155};
156
157class ScreencastEventCatcher : public sigslot::has_slots<> {
158 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000159 ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200160 uint32_t ssrc() const { return ssrc_; }
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000161 rtc::WindowEvent event() const { return ev_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200162 void OnEvent(uint32_t ssrc, rtc::WindowEvent ev) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 ssrc_ = ssrc;
164 ev_ = ev;
165 }
166 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200167 uint32_t ssrc_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000168 rtc::WindowEvent ev_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169};
170
171class VideoMediaErrorCatcher : public sigslot::has_slots<> {
172 public:
173 VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200174 uint32_t ssrc() const { return ssrc_; }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 VideoMediaChannel::Error error() const { return error_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200176 void OnError(uint32_t ssrc, VideoMediaChannel::Error error) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 ssrc_ = ssrc;
178 error_ = error;
179 }
180 private:
Peter Boström0c4e06b2015-10-07 12:23:21 +0200181 uint32_t ssrc_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 VideoMediaChannel::Error error_;
183};
184
185// Returns the absolute path to a file in the testdata/ directory.
186std::string GetTestFilePath(const std::string& filename);
187
188// PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
189// sse is set to a small number for identical frames or sse == 0
190static inline double ComputePSNR(double sse, double count) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200191 return libyuv::SumSquareErrorToPsnr(static_cast<uint64_t>(sse),
192 static_cast<uint64_t>(count));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193}
194
Peter Boström0c4e06b2015-10-07 12:23:21 +0200195static inline double ComputeSumSquareError(const uint8_t* org,
196 const uint8_t* rec,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 int size) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199}
200
201// Loads the image with the specified prefix and size into |out|.
202bool LoadPlanarYuvTestImage(const std::string& prefix,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200203 int width,
204 int height,
205 uint8_t* out);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206
207// Dumps the YUV image out to a file, for visual inspection.
208// PYUV tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200209void DumpPlanarYuvTestImage(const std::string& prefix,
210 const uint8_t* img,
211 int w,
212 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213
214// Dumps the ARGB image out to a file, for visual inspection.
215// ffplay tool can be used to view dump files.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200216void DumpPlanarArgbTestImage(const std::string& prefix,
217 const uint8_t* img,
218 int w,
219 int h);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220
221// Compare two I420 frames.
222bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
223
224// Checks whether |codecs| contains |codec|; checks using Codec::Matches().
225template <class C>
226bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) {
227 typename std::vector<C>::const_iterator it;
228 for (it = codecs.begin(); it != codecs.end(); ++it) {
229 if (it->Matches(codec)) {
230 return true;
231 }
232 }
233 return false;
234}
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000235
236// Create Simulcast StreamParams with given |ssrcs| and |cname|.
Peter Boström0c4e06b2015-10-07 12:23:21 +0200237cricket::StreamParams CreateSimStreamParams(const std::string& cname,
238 const std::vector<uint32_t>& ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000239// Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
240// The number of |rtx_ssrcs| must match number of |ssrcs|.
241cricket::StreamParams CreateSimWithRtxStreamParams(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200242 const std::string& cname,
243 const std::vector<uint32_t>& ssrcs,
244 const std::vector<uint32_t>& rtx_ssrcs);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000245
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246} // namespace cricket
247
248#endif // TALK_MEDIA_BASE_TESTUTILS_H_