blob: a76ec5de24c59156e793e814c6f84b69f05667e0 [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#include "talk/media/base/testutils.h"
29
buildbot@webrtc.org72e44852014-09-03 00:43:48 +000030#if defined(OSX) && defined(FLUTE_IN_CHROME_BUILD)
31#include <mach-o/dyld.h> // For NSGetExecutablePath.
32#endif
33
henrike@webrtc.org28e20752013-07-10 00:45:36 +000034#include <math.h>
35
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000036#include "talk/media/base/rtpdump.h"
37#include "talk/media/base/videocapturer.h"
38#include "talk/media/base/videoframe.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000039#include "webrtc/base/bytebuffer.h"
40#include "webrtc/base/fileutils.h"
41#include "webrtc/base/gunit.h"
42#include "webrtc/base/pathutils.h"
43#include "webrtc/base/stream.h"
44#include "webrtc/base/stringutils.h"
45#include "webrtc/base/testutils.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
47namespace cricket {
48
49/////////////////////////////////////////////////////////////////////////
50// Implementation of RawRtpPacket
51/////////////////////////////////////////////////////////////////////////
52void RawRtpPacket::WriteToByteBuffer(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000053 uint32 in_ssrc, rtc::ByteBuffer *buf) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000054 if (!buf) return;
55
56 buf->WriteUInt8(ver_to_cc);
57 buf->WriteUInt8(m_to_pt);
58 buf->WriteUInt16(sequence_number);
59 buf->WriteUInt32(timestamp);
60 buf->WriteUInt32(in_ssrc);
61 buf->WriteBytes(payload, sizeof(payload));
62}
63
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000064bool RawRtpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 if (!buf) return false;
66
67 bool ret = true;
68 ret &= buf->ReadUInt8(&ver_to_cc);
69 ret &= buf->ReadUInt8(&m_to_pt);
70 ret &= buf->ReadUInt16(&sequence_number);
71 ret &= buf->ReadUInt32(&timestamp);
72 ret &= buf->ReadUInt32(&ssrc);
73 ret &= buf->ReadBytes(payload, sizeof(payload));
74 return ret;
75}
76
77bool RawRtpPacket::SameExceptSeqNumTimestampSsrc(
78 const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const {
79 return sequence_number == seq &&
80 timestamp == ts &&
81 ver_to_cc == packet.ver_to_cc &&
82 m_to_pt == packet.m_to_pt &&
83 ssrc == ssc &&
84 0 == memcmp(payload, packet.payload, sizeof(payload));
85}
86
87/////////////////////////////////////////////////////////////////////////
88// Implementation of RawRtcpPacket
89/////////////////////////////////////////////////////////////////////////
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090void RawRtcpPacket::WriteToByteBuffer(rtc::ByteBuffer *buf) const {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 if (!buf) return;
92
93 buf->WriteUInt8(ver_to_count);
94 buf->WriteUInt8(type);
95 buf->WriteUInt16(length);
96 buf->WriteBytes(payload, sizeof(payload));
97}
98
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000099bool RawRtcpPacket::ReadFromByteBuffer(rtc::ByteBuffer* buf) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100 if (!buf) return false;
101
102 bool ret = true;
103 ret &= buf->ReadUInt8(&ver_to_count);
104 ret &= buf->ReadUInt8(&type);
105 ret &= buf->ReadUInt16(&length);
106 ret &= buf->ReadBytes(payload, sizeof(payload));
107 return ret;
108}
109
110bool RawRtcpPacket::EqualsTo(const RawRtcpPacket& packet) const {
111 return ver_to_count == packet.ver_to_count &&
112 type == packet.type &&
113 length == packet.length &&
114 0 == memcmp(payload, packet.payload, sizeof(payload));
115}
116
117/////////////////////////////////////////////////////////////////////////
118// Implementation of class RtpTestUtility
119/////////////////////////////////////////////////////////////////////////
120const RawRtpPacket RtpTestUtility::kTestRawRtpPackets[] = {
121 {0x80, 0, 0, 0, RtpTestUtility::kDefaultSsrc, "RTP frame 0"},
122 {0x80, 0, 1, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
123 {0x80, 0, 2, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
124 {0x80, 0, 3, 60, RtpTestUtility::kDefaultSsrc, "RTP frame 2"}
125};
126const RawRtcpPacket RtpTestUtility::kTestRawRtcpPackets[] = {
127 // The Version is 2, the Length is 2, and the payload has 8 bytes.
128 {0x80, 0, 2, "RTCP0000"},
129 {0x80, 0, 2, "RTCP0001"},
130 {0x80, 0, 2, "RTCP0002"},
131 {0x80, 0, 2, "RTCP0003"},
132};
133
134size_t RtpTestUtility::GetTestPacketCount() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000135 return rtc::_min(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 ARRAY_SIZE(kTestRawRtpPackets),
137 ARRAY_SIZE(kTestRawRtcpPackets));
138}
139
140bool RtpTestUtility::WriteTestPackets(
141 size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer) {
142 if (!writer || count > GetTestPacketCount()) return false;
143
144 bool result = true;
145 uint32 elapsed_time_ms = 0;
146 for (size_t i = 0; i < count && result; ++i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000147 rtc::ByteBuffer buf;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148 if (rtcp) {
149 kTestRawRtcpPackets[i].WriteToByteBuffer(&buf);
150 } else {
151 kTestRawRtpPackets[i].WriteToByteBuffer(rtp_ssrc, &buf);
152 }
153
154 RtpDumpPacket dump_packet(buf.Data(), buf.Length(), elapsed_time_ms, rtcp);
155 elapsed_time_ms += kElapsedTimeInterval;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000156 result &= (rtc::SR_SUCCESS == writer->WritePacket(dump_packet));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return result;
159}
160
161bool RtpTestUtility::VerifyTestPacketsFromStream(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000162 size_t count, rtc::StreamInterface* stream, uint32 ssrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 if (!stream) return false;
164
165 uint32 prev_elapsed_time = 0;
166 bool result = true;
167 stream->Rewind();
168 RtpDumpLoopReader reader(stream);
169 for (size_t i = 0; i < count && result; ++i) {
170 // Which loop and which index in the loop are we reading now.
171 size_t loop = i / GetTestPacketCount();
172 size_t index = i % GetTestPacketCount();
173
174 RtpDumpPacket packet;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000175 result &= (rtc::SR_SUCCESS == reader.ReadPacket(&packet));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 // Check the elapsed time of the dump packet.
177 result &= (packet.elapsed_time >= prev_elapsed_time);
178 prev_elapsed_time = packet.elapsed_time;
179
180 // Check the RTP or RTCP packet.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000181 rtc::ByteBuffer buf(reinterpret_cast<const char*>(&packet.data[0]),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 packet.data.size());
183 if (packet.is_rtcp()) {
184 // RTCP packet.
185 RawRtcpPacket rtcp_packet;
186 result &= rtcp_packet.ReadFromByteBuffer(&buf);
187 result &= rtcp_packet.EqualsTo(kTestRawRtcpPackets[index]);
188 } else {
189 // RTP packet.
190 RawRtpPacket rtp_packet;
191 result &= rtp_packet.ReadFromByteBuffer(&buf);
192 result &= rtp_packet.SameExceptSeqNumTimestampSsrc(
193 kTestRawRtpPackets[index],
henrike@webrtc.org28654cb2013-07-22 21:07:49 +0000194 static_cast<uint16>(kTestRawRtpPackets[index].sequence_number +
195 loop * GetTestPacketCount()),
196 static_cast<uint32>(kTestRawRtpPackets[index].timestamp +
197 loop * kRtpTimestampIncrease),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 ssrc);
199 }
200 }
201
202 stream->Rewind();
203 return result;
204}
205
206bool RtpTestUtility::VerifyPacket(const RtpDumpPacket* dump,
207 const RawRtpPacket* raw,
208 bool header_only) {
209 if (!dump || !raw) return false;
210
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000211 rtc::ByteBuffer buf;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000212 raw->WriteToByteBuffer(RtpTestUtility::kDefaultSsrc, &buf);
213
214 if (header_only) {
215 size_t header_len = 0;
216 dump->GetRtpHeaderLen(&header_len);
217 return header_len == dump->data.size() &&
218 buf.Length() > dump->data.size() &&
219 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
220 } else {
221 return buf.Length() == dump->data.size() &&
222 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
223 }
224}
225
226// Implementation of VideoCaptureListener.
227VideoCapturerListener::VideoCapturerListener(VideoCapturer* capturer)
228 : last_capture_state_(CS_STARTING),
229 frame_count_(0),
230 frame_fourcc_(0),
231 frame_width_(0),
232 frame_height_(0),
233 frame_size_(0),
234 resolution_changed_(false) {
235 capturer->SignalStateChange.connect(this,
236 &VideoCapturerListener::OnStateChange);
237 capturer->SignalFrameCaptured.connect(this,
238 &VideoCapturerListener::OnFrameCaptured);
239}
240
241void VideoCapturerListener::OnStateChange(VideoCapturer* capturer,
242 CaptureState result) {
243 last_capture_state_ = result;
244}
245
246void VideoCapturerListener::OnFrameCaptured(VideoCapturer* capturer,
247 const CapturedFrame* frame) {
248 ++frame_count_;
249 if (1 == frame_count_) {
250 frame_fourcc_ = frame->fourcc;
251 frame_width_ = frame->width;
252 frame_height_ = frame->height;
253 frame_size_ = frame->data_size;
254 } else if (frame_width_ != frame->width || frame_height_ != frame->height) {
255 resolution_changed_ = true;
256 }
257}
258
buildbot@webrtc.org72e44852014-09-03 00:43:48 +0000259// Returns the path to the running executable or an empty path.
260// TODO(thorcarpenter): Consolidate with FluteClient::get_executable_dir.
261#ifdef FLUTE_IN_CHROME_BUILD
262inline rtc::Pathname GetExecutablePath() {
263 const int32 kMaxExePathSize = 255;
264#ifdef WIN32
265 TCHAR exe_path_buffer[kMaxExePathSize];
266 DWORD copied_length = GetModuleFileName(NULL, // NULL = Current process
267 exe_path_buffer, kMaxExePathSize);
268 if (0 == copied_length) {
269 LOG(LS_ERROR) << "Copied length is zero";
270 return rtc::Pathname();
271 }
272 if (kMaxExePathSize == copied_length) {
273 LOG(LS_ERROR) << "Buffer too small";
274 return rtc::Pathname();
275 }
276#ifdef UNICODE
277 std::wstring wdir(exe_path_buffer);
278 std::string dir_tmp(wdir.begin(), wdir.end());
279 rtc::Pathname path(dir_tmp);
280#else // UNICODE
281 rtc::Pathname path(exe_path_buffer);
282#endif // UNICODE
283#elif defined(OSX) || defined(LINUX)
284 char exe_path_buffer[kMaxExePathSize];
285#ifdef OSX
286 uint32_t copied_length = kMaxExePathSize - 1;
287 if (_NSGetExecutablePath(exe_path_buffer, &copied_length) == -1) {
288 LOG(LS_ERROR) << "Buffer too small";
289 return rtc::Pathname();
290 }
291#elif defined LINUX
292 int32 copied_length = kMaxExePathSize - 1;
293 const char* kProcExeFmt = "/proc/%d/exe";
294 char proc_exe_link[40];
295 snprintf(proc_exe_link, sizeof(proc_exe_link), kProcExeFmt, getpid());
296 copied_length = readlink(proc_exe_link, exe_path_buffer, copied_length);
297 if (copied_length == -1) {
298 LOG_ERR(LS_ERROR) << "Error reading link " << proc_exe_link;
299 return rtc::Pathname();
300 }
301 if (copied_length == kMaxExePathSize - 1) {
302 LOG(LS_ERROR) << "Probably truncated result when reading link "
303 << proc_exe_link;
304 return rtc::Pathname();
305 }
306 exe_path_buffer[copied_length] = '\0';
307#endif // LINUX
308 rtc::Pathname path(exe_path_buffer);
309#else // Android/iOS etc
310 rtc::Pathname path;
311#endif // OSX || LINUX
312 return path;
313}
314#endif
315
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000316// Returns the absolute path to a file in the testdata/ directory.
317std::string GetTestFilePath(const std::string& filename) {
318 // Locate test data directory.
buildbot@webrtc.org72e44852014-09-03 00:43:48 +0000319#ifdef FLUTE_IN_CHROME_BUILD
320 rtc::Pathname path = GetExecutablePath();
321 EXPECT_FALSE(path.empty());
322 path.AppendPathname("../../magicflute/");
323 path.AppendFolder("talk");
324#else
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000325 rtc::Pathname path = testing::GetTalkDirectory();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000326 EXPECT_FALSE(path.empty()); // must be run from inside "talk"
buildbot@webrtc.org72e44852014-09-03 00:43:48 +0000327#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000328 path.AppendFolder("media");
329 path.AppendFolder("testdata");
330 path.SetFilename(filename);
331 return path.pathname();
332}
333
334// Loads the image with the specified prefix and size into |out|.
335bool LoadPlanarYuvTestImage(const std::string& prefix,
336 int width, int height, uint8* out) {
337 std::stringstream ss;
338 ss << prefix << "." << width << "x" << height << "_P420.yuv";
339
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000340 rtc::scoped_ptr<rtc::FileStream> stream(
341 rtc::Filesystem::OpenFile(rtc::Pathname(
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000342 GetTestFilePath(ss.str())), "rb"));
343 if (!stream) {
344 return false;
345 }
346
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000347 rtc::StreamResult res =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348 stream->ReadAll(out, I420_SIZE(width, height), NULL, NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000349 return (res == rtc::SR_SUCCESS);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000350}
351
352// Dumps the YUV image out to a file, for visual inspection.
353// PYUV tool can be used to view dump files.
354void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
355 int w, int h) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000356 rtc::FileStream fs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000357 char filename[256];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000358 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_P420.yuv",
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 prefix.c_str(), w, h);
360 fs.Open(filename, "wb", NULL);
361 fs.Write(img, I420_SIZE(w, h), NULL, NULL);
362}
363
364// Dumps the ARGB image out to a file, for visual inspection.
365// ffplay tool can be used to view dump files.
366void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
367 int w, int h) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000368 rtc::FileStream fs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000369 char filename[256];
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000370 rtc::sprintfn(filename, sizeof(filename), "%s.%dx%d_ARGB.raw",
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 prefix.c_str(), w, h);
372 fs.Open(filename, "wb", NULL);
373 fs.Write(img, ARGB_SIZE(w, h), NULL, NULL);
374}
375
376bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1) {
377 const uint8* y0 = frame0->GetYPlane();
378 const uint8* u0 = frame0->GetUPlane();
379 const uint8* v0 = frame0->GetVPlane();
380 const uint8* y1 = frame1->GetYPlane();
381 const uint8* u1 = frame1->GetUPlane();
382 const uint8* v1 = frame1->GetVPlane();
383
384 for (size_t i = 0; i < frame0->GetHeight(); ++i) {
385 if (0 != memcmp(y0, y1, frame0->GetWidth())) {
386 return false;
387 }
388 y0 += frame0->GetYPitch();
389 y1 += frame1->GetYPitch();
390 }
391
392 for (size_t i = 0; i < frame0->GetChromaHeight(); ++i) {
393 if (0 != memcmp(u0, u1, frame0->GetChromaWidth())) {
394 return false;
395 }
396 if (0 != memcmp(v0, v1, frame0->GetChromaWidth())) {
397 return false;
398 }
399 u0 += frame0->GetUPitch();
400 v0 += frame0->GetVPitch();
401 u1 += frame1->GetUPitch();
402 v1 += frame1->GetVPitch();
403 }
404
405 return true;
406}
407
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000408cricket::StreamParams CreateSimStreamParams(
409 const std::string& cname, const std::vector<uint32>& ssrcs) {
410 cricket::StreamParams sp;
411 cricket::SsrcGroup sg(cricket::kSimSsrcGroupSemantics, ssrcs);
412 sp.ssrcs = ssrcs;
413 sp.ssrc_groups.push_back(sg);
414 sp.cname = cname;
415 return sp;
416}
417
418// There should be an rtx_ssrc per ssrc.
419cricket::StreamParams CreateSimWithRtxStreamParams(
420 const std::string& cname, const std::vector<uint32>& ssrcs,
421 const std::vector<uint32>& rtx_ssrcs) {
422 cricket::StreamParams sp = CreateSimStreamParams(cname, ssrcs);
423 for (size_t i = 0; i < ssrcs.size(); ++i) {
424 sp.ssrcs.push_back(rtx_ssrcs[i]);
425 std::vector<uint32> fid_ssrcs;
426 fid_ssrcs.push_back(ssrcs[i]);
427 fid_ssrcs.push_back(rtx_ssrcs[i]);
428 cricket::SsrcGroup fid_group(cricket::kFidSsrcGroupSemantics, fid_ssrcs);
429 sp.ssrc_groups.push_back(fid_group);
430 }
431 return sp;
432}
433
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434} // namespace cricket