3 * Copyright 2004 Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #include "talk/media/base/testutils.h"
32 #include "talk/media/base/executablehelpers.h"
33 #include "talk/media/base/rtpdump.h"
34 #include "talk/media/base/videocapturer.h"
35 #include "talk/media/base/videoframe.h"
36 #include "webrtc/base/bytebuffer.h"
37 #include "webrtc/base/fileutils.h"
38 #include "webrtc/base/gunit.h"
39 #include "webrtc/base/pathutils.h"
40 #include "webrtc/base/stream.h"
41 #include "webrtc/base/stringutils.h"
42 #include "webrtc/base/testutils.h"
46 /////////////////////////////////////////////////////////////////////////
47 // Implementation of RawRtpPacket
48 /////////////////////////////////////////////////////////////////////////
49 void RawRtpPacket::WriteToByteBuffer(
50 uint32 in_ssrc, rtc::ByteBuffer *buf) const {
53 buf->WriteUInt8(ver_to_cc);
54 buf->WriteUInt8(m_to_pt);
55 buf->WriteUInt16(sequence_number);
56 buf->WriteUInt32(timestamp);
57 buf->WriteUInt32(in_ssrc);
58 buf->WriteBytes(payload, sizeof(payload));
61 bool RawRtpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
62 if (!buf) return false;
65 ret &= buf->ReadUInt8(&ver_to_cc);
66 ret &= buf->ReadUInt8(&m_to_pt);
67 ret &= buf->ReadUInt16(&sequence_number);
68 ret &= buf->ReadUInt32(×tamp);
69 ret &= buf->ReadUInt32(&ssrc);
70 ret &= buf->ReadBytes(payload, sizeof(payload));
74 bool RawRtpPacket::SameExceptSeqNumTimestampSsrc(
75 const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const {
76 return sequence_number == seq &&
78 ver_to_cc == packet.ver_to_cc &&
79 m_to_pt == packet.m_to_pt &&
81 0 == memcmp(payload, packet.payload, sizeof(payload));
84 /////////////////////////////////////////////////////////////////////////
85 // Implementation of RawRtcpPacket
86 /////////////////////////////////////////////////////////////////////////
87 void RawRtcpPacket::WriteToByteBuffer(rtc::ByteBuffer *buf) const {
90 buf->WriteUInt8(ver_to_count);
91 buf->WriteUInt8(type);
92 buf->WriteUInt16(length);
93 buf->WriteBytes(payload, sizeof(payload));
96 bool RawRtcpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
97 if (!buf) return false;
100 ret &= buf->ReadUInt8(&ver_to_count);
101 ret &= buf->ReadUInt8(&type);
102 ret &= buf->ReadUInt16(&length);
103 ret &= buf->ReadBytes(payload, sizeof(payload));
107 bool RawRtcpPacket::EqualsTo(const RawRtcpPacket& packet) const {
108 return ver_to_count == packet.ver_to_count &&
109 type == packet.type &&
110 length == packet.length &&
111 0 == memcmp(payload, packet.payload, sizeof(payload));
114 /////////////////////////////////////////////////////////////////////////
115 // Implementation of class RtpTestUtility
116 /////////////////////////////////////////////////////////////////////////
117 const RawRtpPacket RtpTestUtility::kTestRawRtpPackets[] = {
118 {0x80, 0, 0, 0, RtpTestUtility::kDefaultSsrc, "RTP frame 0"},
119 {0x80, 0, 1, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
120 {0x80, 0, 2, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
121 {0x80, 0, 3, 60, RtpTestUtility::kDefaultSsrc, "RTP frame 2"}
123 const RawRtcpPacket RtpTestUtility::kTestRawRtcpPackets[] = {
124 // The Version is 2, the Length is 2, and the payload has 8 bytes.
125 {0x80, 0, 2, "RTCP0000"},
126 {0x80, 0, 2, "RTCP0001"},
127 {0x80, 0, 2, "RTCP0002"},
128 {0x80, 0, 2, "RTCP0003"},
131 size_t RtpTestUtility::GetTestPacketCount() {
133 ARRAY_SIZE(kTestRawRtpPackets),
134 ARRAY_SIZE(kTestRawRtcpPackets));
137 bool RtpTestUtility::WriteTestPackets(
138 size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer) {
139 if (!writer || count > GetTestPacketCount()) return false;
142 uint32 elapsed_time_ms = 0;
143 for (size_t i = 0; i < count && result; ++i) {
146 kTestRawRtcpPackets[i].WriteToByteBuffer(&buf);
148 kTestRawRtpPackets[i].WriteToByteBuffer(rtp_ssrc, &buf);
151 RtpDumpPacket dump_packet(buf.Data(), buf.Length(), elapsed_time_ms, rtcp);
152 elapsed_time_ms += kElapsedTimeInterval;
153 result &= (rtc::SR_SUCCESS == writer->WritePacket(dump_packet));
158 bool RtpTestUtility::VerifyTestPacketsFromStream(
159 size_t count, rtc::StreamInterface* stream, uint32 ssrc) {
160 if (!stream) return false;
162 uint32 prev_elapsed_time = 0;
165 RtpDumpLoopReader reader(stream);
166 for (size_t i = 0; i < count && result; ++i) {
167 // Which loop and which index in the loop are we reading now.
168 size_t loop = i / GetTestPacketCount();
169 size_t index = i % GetTestPacketCount();
171 RtpDumpPacket packet;
172 result &= (rtc::SR_SUCCESS == reader.ReadPacket(&packet));
173 // Check the elapsed time of the dump packet.
174 result &= (packet.elapsed_time >= prev_elapsed_time);
175 prev_elapsed_time = packet.elapsed_time;
177 // Check the RTP or RTCP packet.
178 rtc::ByteBuffer buf(reinterpret_cast<const char*>(&packet.data[0]),
180 if (packet.is_rtcp()) {
182 RawRtcpPacket rtcp_packet;
183 result &= rtcp_packet.ReadFromByteBuffer(&buf);
184 result &= rtcp_packet.EqualsTo(kTestRawRtcpPackets[index]);
187 RawRtpPacket rtp_packet;
188 result &= rtp_packet.ReadFromByteBuffer(&buf);
189 result &= rtp_packet.SameExceptSeqNumTimestampSsrc(
190 kTestRawRtpPackets[index],
191 static_cast<uint16>(kTestRawRtpPackets[index].sequence_number +
192 loop * GetTestPacketCount()),
193 static_cast<uint32>(kTestRawRtpPackets[index].timestamp +
194 loop * kRtpTimestampIncrease),
203 bool RtpTestUtility::VerifyPacket(const RtpDumpPacket* dump,
204 const RawRtpPacket* raw,
206 if (!dump || !raw) return false;
209 raw->WriteToByteBuffer(RtpTestUtility::kDefaultSsrc, &buf);
212 size_t header_len = 0;
213 dump->GetRtpHeaderLen(&header_len);
214 return header_len == dump->data.size() &&
215 buf.Length() > dump->data.size() &&
216 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
218 return buf.Length() == dump->data.size() &&
219 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
223 // Implementation of VideoCaptureListener.
224 VideoCapturerListener::VideoCapturerListener(VideoCapturer* capturer)
225 : last_capture_state_(CS_STARTING),
231 resolution_changed_(false) {
232 capturer->SignalStateChange.connect(this,
233 &VideoCapturerListener::OnStateChange);
234 capturer->SignalFrameCaptured.connect(this,
235 &VideoCapturerListener::OnFrameCaptured);
238 void VideoCapturerListener::OnStateChange(VideoCapturer* capturer,
239 CaptureState result) {
240 last_capture_state_ = result;
243 void VideoCapturerListener::OnFrameCaptured(VideoCapturer* capturer,
244 const CapturedFrame* frame) {
246 if (1 == frame_count_) {
247 frame_fourcc_ = frame->fourcc;
248 frame_width_ = frame->width;
249 frame_height_ = frame->height;
250 frame_size_ = frame->data_size;
251 } else if (frame_width_ != frame->width || frame_height_ != frame->height) {
252 resolution_changed_ = true;
256 // Returns the absolute path to a file in the testdata/ directory.
257 std::string GetTestFilePath(const std::string& filename) {
258 // Locate test data directory.
260 rtc::Pathname path = rtc::GetExecutablePath();
261 EXPECT_FALSE(path.empty());
262 path.AppendPathname("../../talk/");
264 rtc::Pathname path = testing::GetTalkDirectory();
265 EXPECT_FALSE(path.empty()); // must be run from inside "talk"
267 path.AppendFolder("media/testdata/");
268 path.SetFilename(filename);
269 return path.pathname();
272 // Loads the image with the specified prefix and size into |out|.
273 bool LoadPlanarYuvTestImage(const std::string& prefix,
274 int width, int height, uint8* out) {
275 std::stringstream ss;
276 ss << prefix << "." << width << "x" << height << "_P420.yuv";
278 rtc::scoped_ptr<rtc::FileStream> stream(
279 rtc::Filesystem::OpenFile(rtc::Pathname(
280 GetTestFilePath(ss.str())), "rb"));
285 rtc::StreamResult res =
286 stream->ReadAll(out, I420_SIZE(width, height), NULL, NULL);
287 return (res == rtc::SR_SUCCESS);
290 // Dumps the YUV image out to a file, for visual inspection.
291 // PYUV tool can be used to view dump files.
292 void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
296 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_P420.yuv",
297 prefix.c_str(), w, h);
298 fs.Open(filename, "wb", NULL);
299 fs.Write(img, I420_SIZE(w, h), NULL, NULL);
302 // Dumps the ARGB image out to a file, for visual inspection.
303 // ffplay tool can be used to view dump files.
304 void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
308 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_ARGB.raw",
309 prefix.c_str(), w, h);
310 fs.Open(filename, "wb", NULL);
311 fs.Write(img, ARGB_SIZE(w, h), NULL, NULL);
314 bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1) {
315 const uint8* y0 = frame0->GetYPlane();
316 const uint8* u0 = frame0->GetUPlane();
317 const uint8* v0 = frame0->GetVPlane();
318 const uint8* y1 = frame1->GetYPlane();
319 const uint8* u1 = frame1->GetUPlane();
320 const uint8* v1 = frame1->GetVPlane();
322 for (size_t i = 0; i < frame0->GetHeight(); ++i) {
323 if (0 != memcmp(y0, y1, frame0->GetWidth())) {
326 y0 += frame0->GetYPitch();
327 y1 += frame1->GetYPitch();
330 for (size_t i = 0; i < frame0->GetChromaHeight(); ++i) {
331 if (0 != memcmp(u0, u1, frame0->GetChromaWidth())) {
334 if (0 != memcmp(v0, v1, frame0->GetChromaWidth())) {
337 u0 += frame0->GetUPitch();
338 v0 += frame0->GetVPitch();
339 u1 += frame1->GetUPitch();
340 v1 += frame1->GetVPitch();
346 cricket::StreamParams CreateSimStreamParams(
347 const std::string& cname, const std::vector<uint32>& ssrcs) {
348 cricket::StreamParams sp;
349 cricket::SsrcGroup sg(cricket::kSimSsrcGroupSemantics, ssrcs);
351 sp.ssrc_groups.push_back(sg);
356 // There should be an rtx_ssrc per ssrc.
357 cricket::StreamParams CreateSimWithRtxStreamParams(
358 const std::string& cname, const std::vector<uint32>& ssrcs,
359 const std::vector<uint32>& rtx_ssrcs) {
360 cricket::StreamParams sp = CreateSimStreamParams(cname, ssrcs);
361 for (size_t i = 0; i < ssrcs.size(); ++i) {
362 sp.ssrcs.push_back(rtx_ssrcs[i]);
363 std::vector<uint32> fid_ssrcs;
364 fid_ssrcs.push_back(ssrcs[i]);
365 fid_ssrcs.push_back(rtx_ssrcs[i]);
366 cricket::SsrcGroup fid_group(cricket::kFidSsrcGroupSemantics, fid_ssrcs);
367 sp.ssrc_groups.push_back(fid_group);
372 } // namespace cricket