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/rtpdump.h"
33 #include "talk/media/base/videocapturer.h"
34 #include "talk/media/base/videoframe.h"
35 #include "webrtc/base/bytebuffer.h"
36 #include "webrtc/base/fileutils.h"
37 #include "webrtc/base/gunit.h"
38 #include "webrtc/base/pathutils.h"
39 #include "webrtc/base/stream.h"
40 #include "webrtc/base/stringutils.h"
41 #include "webrtc/base/testutils.h"
45 /////////////////////////////////////////////////////////////////////////
46 // Implementation of RawRtpPacket
47 /////////////////////////////////////////////////////////////////////////
48 void RawRtpPacket::WriteToByteBuffer(
49 uint32 in_ssrc, rtc::ByteBuffer *buf) const {
52 buf->WriteUInt8(ver_to_cc);
53 buf->WriteUInt8(m_to_pt);
54 buf->WriteUInt16(sequence_number);
55 buf->WriteUInt32(timestamp);
56 buf->WriteUInt32(in_ssrc);
57 buf->WriteBytes(payload, sizeof(payload));
60 bool RawRtpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
61 if (!buf) return false;
64 ret &= buf->ReadUInt8(&ver_to_cc);
65 ret &= buf->ReadUInt8(&m_to_pt);
66 ret &= buf->ReadUInt16(&sequence_number);
67 ret &= buf->ReadUInt32(×tamp);
68 ret &= buf->ReadUInt32(&ssrc);
69 ret &= buf->ReadBytes(payload, sizeof(payload));
73 bool RawRtpPacket::SameExceptSeqNumTimestampSsrc(
74 const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const {
75 return sequence_number == seq &&
77 ver_to_cc == packet.ver_to_cc &&
78 m_to_pt == packet.m_to_pt &&
80 0 == memcmp(payload, packet.payload, sizeof(payload));
83 /////////////////////////////////////////////////////////////////////////
84 // Implementation of RawRtcpPacket
85 /////////////////////////////////////////////////////////////////////////
86 void RawRtcpPacket::WriteToByteBuffer(rtc::ByteBuffer *buf) const {
89 buf->WriteUInt8(ver_to_count);
90 buf->WriteUInt8(type);
91 buf->WriteUInt16(length);
92 buf->WriteBytes(payload, sizeof(payload));
95 bool RawRtcpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
96 if (!buf) return false;
99 ret &= buf->ReadUInt8(&ver_to_count);
100 ret &= buf->ReadUInt8(&type);
101 ret &= buf->ReadUInt16(&length);
102 ret &= buf->ReadBytes(payload, sizeof(payload));
106 bool RawRtcpPacket::EqualsTo(const RawRtcpPacket& packet) const {
107 return ver_to_count == packet.ver_to_count &&
108 type == packet.type &&
109 length == packet.length &&
110 0 == memcmp(payload, packet.payload, sizeof(payload));
113 /////////////////////////////////////////////////////////////////////////
114 // Implementation of class RtpTestUtility
115 /////////////////////////////////////////////////////////////////////////
116 const RawRtpPacket RtpTestUtility::kTestRawRtpPackets[] = {
117 {0x80, 0, 0, 0, RtpTestUtility::kDefaultSsrc, "RTP frame 0"},
118 {0x80, 0, 1, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
119 {0x80, 0, 2, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
120 {0x80, 0, 3, 60, RtpTestUtility::kDefaultSsrc, "RTP frame 2"}
122 const RawRtcpPacket RtpTestUtility::kTestRawRtcpPackets[] = {
123 // The Version is 2, the Length is 2, and the payload has 8 bytes.
124 {0x80, 0, 2, "RTCP0000"},
125 {0x80, 0, 2, "RTCP0001"},
126 {0x80, 0, 2, "RTCP0002"},
127 {0x80, 0, 2, "RTCP0003"},
130 size_t RtpTestUtility::GetTestPacketCount() {
132 ARRAY_SIZE(kTestRawRtpPackets),
133 ARRAY_SIZE(kTestRawRtcpPackets));
136 bool RtpTestUtility::WriteTestPackets(
137 size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer) {
138 if (!writer || count > GetTestPacketCount()) return false;
141 uint32 elapsed_time_ms = 0;
142 for (size_t i = 0; i < count && result; ++i) {
145 kTestRawRtcpPackets[i].WriteToByteBuffer(&buf);
147 kTestRawRtpPackets[i].WriteToByteBuffer(rtp_ssrc, &buf);
150 RtpDumpPacket dump_packet(buf.Data(), buf.Length(), elapsed_time_ms, rtcp);
151 elapsed_time_ms += kElapsedTimeInterval;
152 result &= (rtc::SR_SUCCESS == writer->WritePacket(dump_packet));
157 bool RtpTestUtility::VerifyTestPacketsFromStream(
158 size_t count, rtc::StreamInterface* stream, uint32 ssrc) {
159 if (!stream) return false;
161 uint32 prev_elapsed_time = 0;
164 RtpDumpLoopReader reader(stream);
165 for (size_t i = 0; i < count && result; ++i) {
166 // Which loop and which index in the loop are we reading now.
167 size_t loop = i / GetTestPacketCount();
168 size_t index = i % GetTestPacketCount();
170 RtpDumpPacket packet;
171 result &= (rtc::SR_SUCCESS == reader.ReadPacket(&packet));
172 // Check the elapsed time of the dump packet.
173 result &= (packet.elapsed_time >= prev_elapsed_time);
174 prev_elapsed_time = packet.elapsed_time;
176 // Check the RTP or RTCP packet.
177 rtc::ByteBuffer buf(reinterpret_cast<const char*>(&packet.data[0]),
179 if (packet.is_rtcp()) {
181 RawRtcpPacket rtcp_packet;
182 result &= rtcp_packet.ReadFromByteBuffer(&buf);
183 result &= rtcp_packet.EqualsTo(kTestRawRtcpPackets[index]);
186 RawRtpPacket rtp_packet;
187 result &= rtp_packet.ReadFromByteBuffer(&buf);
188 result &= rtp_packet.SameExceptSeqNumTimestampSsrc(
189 kTestRawRtpPackets[index],
190 static_cast<uint16>(kTestRawRtpPackets[index].sequence_number +
191 loop * GetTestPacketCount()),
192 static_cast<uint32>(kTestRawRtpPackets[index].timestamp +
193 loop * kRtpTimestampIncrease),
202 bool RtpTestUtility::VerifyPacket(const RtpDumpPacket* dump,
203 const RawRtpPacket* raw,
205 if (!dump || !raw) return false;
208 raw->WriteToByteBuffer(RtpTestUtility::kDefaultSsrc, &buf);
211 size_t header_len = 0;
212 dump->GetRtpHeaderLen(&header_len);
213 return header_len == dump->data.size() &&
214 buf.Length() > dump->data.size() &&
215 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
217 return buf.Length() == dump->data.size() &&
218 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
222 // Implementation of VideoCaptureListener.
223 VideoCapturerListener::VideoCapturerListener(VideoCapturer* capturer)
224 : last_capture_state_(CS_STARTING),
230 resolution_changed_(false) {
231 capturer->SignalStateChange.connect(this,
232 &VideoCapturerListener::OnStateChange);
233 capturer->SignalFrameCaptured.connect(this,
234 &VideoCapturerListener::OnFrameCaptured);
237 void VideoCapturerListener::OnStateChange(VideoCapturer* capturer,
238 CaptureState result) {
239 last_capture_state_ = result;
242 void VideoCapturerListener::OnFrameCaptured(VideoCapturer* capturer,
243 const CapturedFrame* frame) {
245 if (1 == frame_count_) {
246 frame_fourcc_ = frame->fourcc;
247 frame_width_ = frame->width;
248 frame_height_ = frame->height;
249 frame_size_ = frame->data_size;
250 } else if (frame_width_ != frame->width || frame_height_ != frame->height) {
251 resolution_changed_ = true;
255 // Returns the absolute path to a file in the testdata/ directory.
256 std::string GetTestFilePath(const std::string& filename) {
257 // Locate test data directory.
258 rtc::Pathname path = testing::GetTalkDirectory();
259 EXPECT_FALSE(path.empty()); // must be run from inside "talk"
260 path.AppendFolder("media");
261 path.AppendFolder("testdata");
262 path.SetFilename(filename);
263 return path.pathname();
266 // Loads the image with the specified prefix and size into |out|.
267 bool LoadPlanarYuvTestImage(const std::string& prefix,
268 int width, int height, uint8* out) {
269 std::stringstream ss;
270 ss << prefix << "." << width << "x" << height << "_P420.yuv";
272 rtc::scoped_ptr<rtc::FileStream> stream(
273 rtc::Filesystem::OpenFile(rtc::Pathname(
274 GetTestFilePath(ss.str())), "rb"));
279 rtc::StreamResult res =
280 stream->ReadAll(out, I420_SIZE(width, height), NULL, NULL);
281 return (res == rtc::SR_SUCCESS);
284 // Dumps the YUV image out to a file, for visual inspection.
285 // PYUV tool can be used to view dump files.
286 void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
290 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_P420.yuv",
291 prefix.c_str(), w, h);
292 fs.Open(filename, "wb", NULL);
293 fs.Write(img, I420_SIZE(w, h), NULL, NULL);
296 // Dumps the ARGB image out to a file, for visual inspection.
297 // ffplay tool can be used to view dump files.
298 void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
302 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_ARGB.raw",
303 prefix.c_str(), w, h);
304 fs.Open(filename, "wb", NULL);
305 fs.Write(img, ARGB_SIZE(w, h), NULL, NULL);
308 bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1) {
309 const uint8* y0 = frame0->GetYPlane();
310 const uint8* u0 = frame0->GetUPlane();
311 const uint8* v0 = frame0->GetVPlane();
312 const uint8* y1 = frame1->GetYPlane();
313 const uint8* u1 = frame1->GetUPlane();
314 const uint8* v1 = frame1->GetVPlane();
316 for (size_t i = 0; i < frame0->GetHeight(); ++i) {
317 if (0 != memcmp(y0, y1, frame0->GetWidth())) {
320 y0 += frame0->GetYPitch();
321 y1 += frame1->GetYPitch();
324 for (size_t i = 0; i < frame0->GetChromaHeight(); ++i) {
325 if (0 != memcmp(u0, u1, frame0->GetChromaWidth())) {
328 if (0 != memcmp(v0, v1, frame0->GetChromaWidth())) {
331 u0 += frame0->GetUPitch();
332 v0 += frame0->GetVPitch();
333 u1 += frame1->GetUPitch();
334 v1 += frame1->GetVPitch();
340 cricket::StreamParams CreateSimStreamParams(
341 const std::string& cname, const std::vector<uint32>& ssrcs) {
342 cricket::StreamParams sp;
343 cricket::SsrcGroup sg(cricket::kSimSsrcGroupSemantics, ssrcs);
345 sp.ssrc_groups.push_back(sg);
350 // There should be an rtx_ssrc per ssrc.
351 cricket::StreamParams CreateSimWithRtxStreamParams(
352 const std::string& cname, const std::vector<uint32>& ssrcs,
353 const std::vector<uint32>& rtx_ssrcs) {
354 cricket::StreamParams sp = CreateSimStreamParams(cname, ssrcs);
355 for (size_t i = 0; i < ssrcs.size(); ++i) {
356 sp.ssrcs.push_back(rtx_ssrcs[i]);
357 std::vector<uint32> fid_ssrcs;
358 fid_ssrcs.push_back(ssrcs[i]);
359 fid_ssrcs.push_back(rtx_ssrcs[i]);
360 cricket::SsrcGroup fid_group(cricket::kFidSsrcGroupSemantics, fid_ssrcs);
361 sp.ssrc_groups.push_back(fid_group);
366 } // namespace cricket