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.

208 lines
7.3 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. #ifndef TALK_MEDIA_BASE_TESTUTILS_H_
  28. #define TALK_MEDIA_BASE_TESTUTILS_H_
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/basictypes.h"
  32. #include "talk/base/sigslot.h"
  33. #include "talk/base/window.h"
  34. #include "talk/media/base/mediachannel.h"
  35. #include "talk/media/base/videocapturer.h"
  36. #include "talk/media/base/videocommon.h"
  37. namespace talk_base {
  38. class ByteBuffer;
  39. class StreamInterface;
  40. }
  41. namespace cricket {
  42. // Returns size of 420 image with rounding on chroma for odd sizes.
  43. #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2)
  44. // Returns size of ARGB image.
  45. #define ARGB_SIZE(w, h) (w * h * 4)
  46. template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
  47. return std::vector<T>(a, a + s);
  48. }
  49. #define MAKE_VECTOR(a) cricket::MakeVector(a, ARRAY_SIZE(a))
  50. struct RtpDumpPacket;
  51. class RtpDumpWriter;
  52. class VideoFrame;
  53. struct RawRtpPacket {
  54. void WriteToByteBuffer(uint32 in_ssrc, talk_base::ByteBuffer* buf) const;
  55. bool ReadFromByteBuffer(talk_base::ByteBuffer* buf);
  56. // Check if this packet is the same as the specified packet except the
  57. // sequence number and timestamp, which should be the same as the specified
  58. // parameters.
  59. bool SameExceptSeqNumTimestampSsrc(
  60. const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const;
  61. int size() const { return 28; }
  62. uint8 ver_to_cc;
  63. uint8 m_to_pt;
  64. uint16 sequence_number;
  65. uint32 timestamp;
  66. uint32 ssrc;
  67. char payload[16];
  68. };
  69. struct RawRtcpPacket {
  70. void WriteToByteBuffer(talk_base::ByteBuffer* buf) const;
  71. bool ReadFromByteBuffer(talk_base::ByteBuffer* buf);
  72. bool EqualsTo(const RawRtcpPacket& packet) const;
  73. uint8 ver_to_count;
  74. uint8 type;
  75. uint16 length;
  76. char payload[16];
  77. };
  78. class RtpTestUtility {
  79. public:
  80. static size_t GetTestPacketCount();
  81. // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets,
  82. // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return
  83. // true if successful.
  84. static bool WriteTestPackets(
  85. size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer);
  86. // Loop read the first count number of packets from the specified stream.
  87. // Verify the elapsed time of the dump packets increase monotonically. If the
  88. // stream is a RTP stream, verify the RTP sequence number, timestamp, and
  89. // payload. If the stream is a RTCP stream, verify the RTCP header and
  90. // payload.
  91. static bool VerifyTestPacketsFromStream(
  92. size_t count, talk_base::StreamInterface* stream, uint32 ssrc);
  93. // Verify the dump packet is the same as the raw RTP packet.
  94. static bool VerifyPacket(const RtpDumpPacket* dump,
  95. const RawRtpPacket* raw,
  96. bool header_only);
  97. static const uint32 kDefaultSsrc = 1;
  98. static const uint32 kRtpTimestampIncrease = 90;
  99. static const uint32 kDefaultTimeIncrease = 30;
  100. static const uint32 kElapsedTimeInterval = 10;
  101. static const RawRtpPacket kTestRawRtpPackets[];
  102. static const RawRtcpPacket kTestRawRtcpPackets[];
  103. private:
  104. RtpTestUtility() {}
  105. };
  106. // Test helper for testing VideoCapturer implementations.
  107. class VideoCapturerListener : public sigslot::has_slots<> {
  108. public:
  109. explicit VideoCapturerListener(VideoCapturer* cap);
  110. CaptureState last_capture_state() const { return last_capture_state_; }
  111. int frame_count() const { return frame_count_; }
  112. uint32 frame_fourcc() const { return frame_fourcc_; }
  113. int frame_width() const { return frame_width_; }
  114. int frame_height() const { return frame_height_; }
  115. uint32 frame_size() const { return frame_size_; }
  116. bool resolution_changed() const { return resolution_changed_; }
  117. void OnStateChange(VideoCapturer* capturer, CaptureState state);
  118. void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame);
  119. private:
  120. CaptureState last_capture_state_;
  121. int frame_count_;
  122. uint32 frame_fourcc_;
  123. int frame_width_;
  124. int frame_height_;
  125. uint32 frame_size_;
  126. bool resolution_changed_;
  127. };
  128. class ScreencastEventCatcher : public sigslot::has_slots<> {
  129. public:
  130. ScreencastEventCatcher() : ssrc_(0), ev_(talk_base::WE_RESIZE) { }
  131. uint32 ssrc() const { return ssrc_; }
  132. talk_base::WindowEvent event() const { return ev_; }
  133. void OnEvent(uint32 ssrc, talk_base::WindowEvent ev) {
  134. ssrc_ = ssrc;
  135. ev_ = ev;
  136. }
  137. private:
  138. uint32 ssrc_;
  139. talk_base::WindowEvent ev_;
  140. };
  141. class VideoMediaErrorCatcher : public sigslot::has_slots<> {
  142. public:
  143. VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
  144. uint32 ssrc() const { return ssrc_; }
  145. VideoMediaChannel::Error error() const { return error_; }
  146. void OnError(uint32 ssrc, VideoMediaChannel::Error error) {
  147. ssrc_ = ssrc;
  148. error_ = error;
  149. }
  150. private:
  151. uint32 ssrc_;
  152. VideoMediaChannel::Error error_;
  153. };
  154. // Returns the absolute path to a file in the testdata/ directory.
  155. std::string GetTestFilePath(const std::string& filename);
  156. // PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
  157. // sse is set to a small number for identical frames or sse == 0
  158. double ComputePSNR(double sse, double size);
  159. double ComputeSumSquareError(const uint8 *org, const uint8 *rec,
  160. int size);
  161. // Loads the image with the specified prefix and size into |out|.
  162. bool LoadPlanarYuvTestImage(const std::string& prefix,
  163. int width, int height, uint8* out);
  164. // Dumps the YUV image out to a file, for visual inspection.
  165. // PYUV tool can be used to view dump files.
  166. void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
  167. int w, int h);
  168. // Dumps the ARGB image out to a file, for visual inspection.
  169. // ffplay tool can be used to view dump files.
  170. void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
  171. int w, int h);
  172. // Compare two I420 frames.
  173. bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
  174. } // namespace cricket
  175. #endif // TALK_MEDIA_BASE_TESTUTILS_H_