You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "talk/media/base/testutils.h"
  28. #include <math.h>
  29. #ifdef HAVE_YUV
  30. #include "libyuv/compare.h"
  31. #endif
  32. #include "talk/base/bytebuffer.h"
  33. #include "talk/base/fileutils.h"
  34. #include "talk/base/gunit.h"
  35. #include "talk/base/pathutils.h"
  36. #include "talk/base/stream.h"
  37. #include "talk/base/stringutils.h"
  38. #include "talk/media/base/rtpdump.h"
  39. #include "talk/media/base/videocapturer.h"
  40. #include "talk/media/base/videoframe.h"
  41. namespace cricket {
  42. /////////////////////////////////////////////////////////////////////////
  43. // Implementation of RawRtpPacket
  44. /////////////////////////////////////////////////////////////////////////
  45. void RawRtpPacket::WriteToByteBuffer(
  46. uint32 in_ssrc, talk_base::ByteBuffer *buf) const {
  47. if (!buf) return;
  48. buf->WriteUInt8(ver_to_cc);
  49. buf->WriteUInt8(m_to_pt);
  50. buf->WriteUInt16(sequence_number);
  51. buf->WriteUInt32(timestamp);
  52. buf->WriteUInt32(in_ssrc);
  53. buf->WriteBytes(payload, sizeof(payload));
  54. }
  55. bool RawRtpPacket::ReadFromByteBuffer(talk_base::ByteBuffer* buf) {
  56. if (!buf) return false;
  57. bool ret = true;
  58. ret &= buf->ReadUInt8(&ver_to_cc);
  59. ret &= buf->ReadUInt8(&m_to_pt);
  60. ret &= buf->ReadUInt16(&sequence_number);
  61. ret &= buf->ReadUInt32(&timestamp);
  62. ret &= buf->ReadUInt32(&ssrc);
  63. ret &= buf->ReadBytes(payload, sizeof(payload));
  64. return ret;
  65. }
  66. bool RawRtpPacket::SameExceptSeqNumTimestampSsrc(
  67. const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const {
  68. return sequence_number == seq &&
  69. timestamp == ts &&
  70. ver_to_cc == packet.ver_to_cc &&
  71. m_to_pt == packet.m_to_pt &&
  72. ssrc == ssc &&
  73. 0 == memcmp(payload, packet.payload, sizeof(payload));
  74. }
  75. /////////////////////////////////////////////////////////////////////////
  76. // Implementation of RawRtcpPacket
  77. /////////////////////////////////////////////////////////////////////////
  78. void RawRtcpPacket::WriteToByteBuffer(talk_base::ByteBuffer *buf) const {
  79. if (!buf) return;
  80. buf->WriteUInt8(ver_to_count);
  81. buf->WriteUInt8(type);
  82. buf->WriteUInt16(length);
  83. buf->WriteBytes(payload, sizeof(payload));
  84. }
  85. bool RawRtcpPacket::ReadFromByteBuffer(talk_base::ByteBuffer* buf) {
  86. if (!buf) return false;
  87. bool ret = true;
  88. ret &= buf->ReadUInt8(&ver_to_count);
  89. ret &= buf->ReadUInt8(&type);
  90. ret &= buf->ReadUInt16(&length);
  91. ret &= buf->ReadBytes(payload, sizeof(payload));
  92. return ret;
  93. }
  94. bool RawRtcpPacket::EqualsTo(const RawRtcpPacket& packet) const {
  95. return ver_to_count == packet.ver_to_count &&
  96. type == packet.type &&
  97. length == packet.length &&
  98. 0 == memcmp(payload, packet.payload, sizeof(payload));
  99. }
  100. /////////////////////////////////////////////////////////////////////////
  101. // Implementation of class RtpTestUtility
  102. /////////////////////////////////////////////////////////////////////////
  103. const RawRtpPacket RtpTestUtility::kTestRawRtpPackets[] = {
  104. {0x80, 0, 0, 0, RtpTestUtility::kDefaultSsrc, "RTP frame 0"},
  105. {0x80, 0, 1, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
  106. {0x80, 0, 2, 30, RtpTestUtility::kDefaultSsrc, "RTP frame 1"},
  107. {0x80, 0, 3, 60, RtpTestUtility::kDefaultSsrc, "RTP frame 2"}
  108. };
  109. const RawRtcpPacket RtpTestUtility::kTestRawRtcpPackets[] = {
  110. // The Version is 2, the Length is 2, and the payload has 8 bytes.
  111. {0x80, 0, 2, "RTCP0000"},
  112. {0x80, 0, 2, "RTCP0001"},
  113. {0x80, 0, 2, "RTCP0002"},
  114. {0x80, 0, 2, "RTCP0003"},
  115. };
  116. size_t RtpTestUtility::GetTestPacketCount() {
  117. return talk_base::_min(
  118. ARRAY_SIZE(kTestRawRtpPackets),
  119. ARRAY_SIZE(kTestRawRtcpPackets));
  120. }
  121. bool RtpTestUtility::WriteTestPackets(
  122. size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer) {
  123. if (!writer || count > GetTestPacketCount()) return false;
  124. bool result = true;
  125. uint32 elapsed_time_ms = 0;
  126. for (size_t i = 0; i < count && result; ++i) {
  127. talk_base::ByteBuffer buf;
  128. if (rtcp) {
  129. kTestRawRtcpPackets[i].WriteToByteBuffer(&buf);
  130. } else {
  131. kTestRawRtpPackets[i].WriteToByteBuffer(rtp_ssrc, &buf);
  132. }
  133. RtpDumpPacket dump_packet(buf.Data(), buf.Length(), elapsed_time_ms, rtcp);
  134. elapsed_time_ms += kElapsedTimeInterval;
  135. result &= (talk_base::SR_SUCCESS == writer->WritePacket(dump_packet));
  136. }
  137. return result;
  138. }
  139. bool RtpTestUtility::VerifyTestPacketsFromStream(
  140. size_t count, talk_base::StreamInterface* stream, uint32 ssrc) {
  141. if (!stream) return false;
  142. uint32 prev_elapsed_time = 0;
  143. bool result = true;
  144. stream->Rewind();
  145. RtpDumpLoopReader reader(stream);
  146. for (size_t i = 0; i < count && result; ++i) {
  147. // Which loop and which index in the loop are we reading now.
  148. size_t loop = i / GetTestPacketCount();
  149. size_t index = i % GetTestPacketCount();
  150. RtpDumpPacket packet;
  151. result &= (talk_base::SR_SUCCESS == reader.ReadPacket(&packet));
  152. // Check the elapsed time of the dump packet.
  153. result &= (packet.elapsed_time >= prev_elapsed_time);
  154. prev_elapsed_time = packet.elapsed_time;
  155. // Check the RTP or RTCP packet.
  156. talk_base::ByteBuffer buf(reinterpret_cast<const char*>(&packet.data[0]),
  157. packet.data.size());
  158. if (packet.is_rtcp()) {
  159. // RTCP packet.
  160. RawRtcpPacket rtcp_packet;
  161. result &= rtcp_packet.ReadFromByteBuffer(&buf);
  162. result &= rtcp_packet.EqualsTo(kTestRawRtcpPackets[index]);
  163. } else {
  164. // RTP packet.
  165. RawRtpPacket rtp_packet;
  166. result &= rtp_packet.ReadFromByteBuffer(&buf);
  167. result &= rtp_packet.SameExceptSeqNumTimestampSsrc(
  168. kTestRawRtpPackets[index],
  169. kTestRawRtpPackets[index].sequence_number +
  170. loop * GetTestPacketCount(),
  171. kTestRawRtpPackets[index].timestamp + loop * kRtpTimestampIncrease,
  172. ssrc);
  173. }
  174. }
  175. stream->Rewind();
  176. return result;
  177. }
  178. bool RtpTestUtility::VerifyPacket(const RtpDumpPacket* dump,
  179. const RawRtpPacket* raw,
  180. bool header_only) {
  181. if (!dump || !raw) return false;
  182. talk_base::ByteBuffer buf;
  183. raw->WriteToByteBuffer(RtpTestUtility::kDefaultSsrc, &buf);
  184. if (header_only) {
  185. size_t header_len = 0;
  186. dump->GetRtpHeaderLen(&header_len);
  187. return header_len == dump->data.size() &&
  188. buf.Length() > dump->data.size() &&
  189. 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
  190. } else {
  191. return buf.Length() == dump->data.size() &&
  192. 0 == memcmp(buf.Data(), &dump->data[0], dump->data.size());
  193. }
  194. }
  195. // Implementation of VideoCaptureListener.
  196. VideoCapturerListener::VideoCapturerListener(VideoCapturer* capturer)
  197. : last_capture_state_(CS_STARTING),
  198. frame_count_(0),
  199. frame_fourcc_(0),
  200. frame_width_(0),
  201. frame_height_(0),
  202. frame_size_(0),
  203. resolution_changed_(false) {
  204. capturer->SignalStateChange.connect(this,
  205. &VideoCapturerListener::OnStateChange);
  206. capturer->SignalFrameCaptured.connect(this,
  207. &VideoCapturerListener::OnFrameCaptured);
  208. }
  209. void VideoCapturerListener::OnStateChange(VideoCapturer* capturer,
  210. CaptureState result) {
  211. last_capture_state_ = result;
  212. }
  213. void VideoCapturerListener::OnFrameCaptured(VideoCapturer* capturer,
  214. const CapturedFrame* frame) {
  215. ++frame_count_;
  216. if (1 == frame_count_) {
  217. frame_fourcc_ = frame->fourcc;
  218. frame_width_ = frame->width;
  219. frame_height_ = frame->height;
  220. frame_size_ = frame->data_size;
  221. } else if (frame_width_ != frame->width || frame_height_ != frame->height) {
  222. resolution_changed_ = true;
  223. }
  224. }
  225. // Returns the absolute path to a file in the testdata/ directory.
  226. std::string GetTestFilePath(const std::string& filename) {
  227. // Locate test data directory.
  228. talk_base::Pathname path = GetTalkDirectory();
  229. EXPECT_FALSE(path.empty()); // must be run from inside "talk"
  230. path.AppendFolder("media");
  231. path.AppendFolder("testdata");
  232. path.SetFilename(filename);
  233. return path.pathname();
  234. }
  235. // PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
  236. // sse is set to a small number for identical frames or sse == 0
  237. double ComputePSNR(double sse, double size) {
  238. if (sse <= 0.)
  239. sse = 65025.0 * size / pow(10., 128./10.); // produces max PSNR of 128
  240. return 10.0 * log10(65025.0 * size / sse);
  241. }
  242. double ComputeSumSquareError(const uint8 *org, const uint8 *rec, int size) {
  243. #ifdef HAVE_YUV
  244. return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
  245. #else
  246. double sse = 0.;
  247. for (int j = 0; j < size; ++j) {
  248. const int diff = static_cast<int>(org[j]) - static_cast<int>(rec[j]);
  249. sse += static_cast<double>(diff * diff);
  250. }
  251. return sse;
  252. #endif
  253. }
  254. // Loads the image with the specified prefix and size into |out|.
  255. bool LoadPlanarYuvTestImage(const std::string& prefix,
  256. int width, int height, uint8* out) {
  257. std::stringstream ss;
  258. ss << prefix << "." << width << "x" << height << "_P420.yuv";
  259. talk_base::scoped_ptr<talk_base::FileStream> stream(
  260. talk_base::Filesystem::OpenFile(talk_base::Pathname(
  261. GetTestFilePath(ss.str())), "rb"));
  262. if (!stream) {
  263. return false;
  264. }
  265. talk_base::StreamResult res =
  266. stream->ReadAll(out, I420_SIZE(width, height), NULL, NULL);
  267. return (res == talk_base::SR_SUCCESS);
  268. }
  269. // Dumps the YUV image out to a file, for visual inspection.
  270. // PYUV tool can be used to view dump files.
  271. void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
  272. int w, int h) {
  273. talk_base::FileStream fs;
  274. char filename[256];
  275. talk_base::sprintfn(filename, sizeof(filename), "%s.%dx%d_P420.yuv",
  276. prefix.c_str(), w, h);
  277. fs.Open(filename, "wb", NULL);
  278. fs.Write(img, I420_SIZE(w, h), NULL, NULL);
  279. }
  280. // Dumps the ARGB image out to a file, for visual inspection.
  281. // ffplay tool can be used to view dump files.
  282. void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
  283. int w, int h) {
  284. talk_base::FileStream fs;
  285. char filename[256];
  286. talk_base::sprintfn(filename, sizeof(filename), "%s.%dx%d_ARGB.raw",
  287. prefix.c_str(), w, h);
  288. fs.Open(filename, "wb", NULL);
  289. fs.Write(img, ARGB_SIZE(w, h), NULL, NULL);
  290. }
  291. bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1) {
  292. const uint8* y0 = frame0->GetYPlane();
  293. const uint8* u0 = frame0->GetUPlane();
  294. const uint8* v0 = frame0->GetVPlane();
  295. const uint8* y1 = frame1->GetYPlane();
  296. const uint8* u1 = frame1->GetUPlane();
  297. const uint8* v1 = frame1->GetVPlane();
  298. for (size_t i = 0; i < frame0->GetHeight(); ++i) {
  299. if (0 != memcmp(y0, y1, frame0->GetWidth())) {
  300. return false;
  301. }
  302. y0 += frame0->GetYPitch();
  303. y1 += frame1->GetYPitch();
  304. }
  305. for (size_t i = 0; i < frame0->GetChromaHeight(); ++i) {
  306. if (0 != memcmp(u0, u1, frame0->GetChromaWidth())) {
  307. return false;
  308. }
  309. if (0 != memcmp(v0, v1, frame0->GetChromaWidth())) {
  310. return false;
  311. }
  312. u0 += frame0->GetUPitch();
  313. v0 += frame0->GetVPitch();
  314. u1 += frame1->GetUPitch();
  315. v1 += frame1->GetVPitch();
  316. }
  317. return true;
  318. }
  319. } // namespace cricket