Home | History | Annotate | Download | only in base
      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 
     28 #ifndef TALK_MEDIA_BASE_TESTUTILS_H_
     29 #define TALK_MEDIA_BASE_TESTUTILS_H_
     30 
     31 #include <string>
     32 #include <vector>
     33 
     34 #if !defined(DISABLE_YUV)
     35 #include "libyuv/compare.h"
     36 #endif
     37 #include "talk/media/base/mediachannel.h"
     38 #include "talk/media/base/videocapturer.h"
     39 #include "talk/media/base/videocommon.h"
     40 #include "webrtc/base/basictypes.h"
     41 #include "webrtc/base/sigslot.h"
     42 #include "webrtc/base/window.h"
     43 
     44 namespace rtc {
     45 class ByteBuffer;
     46 class StreamInterface;
     47 }
     48 
     49 namespace cricket {
     50 
     51 // Returns size of 420 image with rounding on chroma for odd sizes.
     52 #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2)
     53 // Returns size of ARGB image.
     54 #define ARGB_SIZE(w, h) (w * h * 4)
     55 
     56 template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
     57   return std::vector<T>(a, a + s);
     58 }
     59 #define MAKE_VECTOR(a) cricket::MakeVector(a, ARRAY_SIZE(a))
     60 
     61 struct RtpDumpPacket;
     62 class RtpDumpWriter;
     63 class VideoFrame;
     64 
     65 struct RawRtpPacket {
     66   void WriteToByteBuffer(uint32 in_ssrc, rtc::ByteBuffer* buf) const;
     67   bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
     68   // Check if this packet is the same as the specified packet except the
     69   // sequence number and timestamp, which should be the same as the specified
     70   // parameters.
     71   bool SameExceptSeqNumTimestampSsrc(
     72       const RawRtpPacket& packet, uint16 seq, uint32 ts, uint32 ssc) const;
     73   int size() const { return 28; }
     74 
     75   uint8 ver_to_cc;
     76   uint8 m_to_pt;
     77   uint16 sequence_number;
     78   uint32 timestamp;
     79   uint32 ssrc;
     80   char payload[16];
     81 };
     82 
     83 struct RawRtcpPacket {
     84   void WriteToByteBuffer(rtc::ByteBuffer* buf) const;
     85   bool ReadFromByteBuffer(rtc::ByteBuffer* buf);
     86   bool EqualsTo(const RawRtcpPacket& packet) const;
     87 
     88   uint8 ver_to_count;
     89   uint8 type;
     90   uint16 length;
     91   char payload[16];
     92 };
     93 
     94 class RtpTestUtility {
     95  public:
     96   static size_t GetTestPacketCount();
     97 
     98   // Write the first count number of kTestRawRtcpPackets or kTestRawRtpPackets,
     99   // depending on the flag rtcp. If it is RTP, use the specified SSRC. Return
    100   // true if successful.
    101   static bool WriteTestPackets(
    102       size_t count, bool rtcp, uint32 rtp_ssrc, RtpDumpWriter* writer);
    103 
    104   // Loop read the first count number of packets from the specified stream.
    105   // Verify the elapsed time of the dump packets increase monotonically. If the
    106   // stream is a RTP stream, verify the RTP sequence number, timestamp, and
    107   // payload. If the stream is a RTCP stream, verify the RTCP header and
    108   // payload.
    109   static bool VerifyTestPacketsFromStream(
    110       size_t count, rtc::StreamInterface* stream, uint32 ssrc);
    111 
    112   // Verify the dump packet is the same as the raw RTP packet.
    113   static bool VerifyPacket(const RtpDumpPacket* dump,
    114                            const RawRtpPacket* raw,
    115                            bool header_only);
    116 
    117   static const uint32 kDefaultSsrc = 1;
    118   static const uint32 kRtpTimestampIncrease = 90;
    119   static const uint32 kDefaultTimeIncrease = 30;
    120   static const uint32 kElapsedTimeInterval = 10;
    121   static const RawRtpPacket kTestRawRtpPackets[];
    122   static const RawRtcpPacket kTestRawRtcpPackets[];
    123 
    124  private:
    125   RtpTestUtility() {}
    126 };
    127 
    128 // Test helper for testing VideoCapturer implementations.
    129 class VideoCapturerListener : public sigslot::has_slots<> {
    130  public:
    131   explicit VideoCapturerListener(VideoCapturer* cap);
    132 
    133   CaptureState last_capture_state() const { return last_capture_state_; }
    134   int frame_count() const { return frame_count_; }
    135   uint32 frame_fourcc() const { return frame_fourcc_; }
    136   int frame_width() const { return frame_width_; }
    137   int frame_height() const { return frame_height_; }
    138   uint32 frame_size() const { return frame_size_; }
    139   bool resolution_changed() const { return resolution_changed_; }
    140 
    141   void OnStateChange(VideoCapturer* capturer, CaptureState state);
    142   void OnFrameCaptured(VideoCapturer* capturer, const CapturedFrame* frame);
    143 
    144  private:
    145   CaptureState last_capture_state_;
    146   int frame_count_;
    147   uint32 frame_fourcc_;
    148   int frame_width_;
    149   int frame_height_;
    150   uint32 frame_size_;
    151   bool resolution_changed_;
    152 };
    153 
    154 class ScreencastEventCatcher : public sigslot::has_slots<> {
    155  public:
    156   ScreencastEventCatcher() : ssrc_(0), ev_(rtc::WE_RESIZE) { }
    157   uint32 ssrc() const { return ssrc_; }
    158   rtc::WindowEvent event() const { return ev_; }
    159   void OnEvent(uint32 ssrc, rtc::WindowEvent ev) {
    160     ssrc_ = ssrc;
    161     ev_ = ev;
    162   }
    163  private:
    164   uint32 ssrc_;
    165   rtc::WindowEvent ev_;
    166 };
    167 
    168 class VideoMediaErrorCatcher : public sigslot::has_slots<> {
    169  public:
    170   VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
    171   uint32 ssrc() const { return ssrc_; }
    172   VideoMediaChannel::Error error() const { return error_; }
    173   void OnError(uint32 ssrc, VideoMediaChannel::Error error) {
    174     ssrc_ = ssrc;
    175     error_ = error;
    176   }
    177  private:
    178   uint32 ssrc_;
    179   VideoMediaChannel::Error error_;
    180 };
    181 
    182 // Returns the absolute path to a file in the testdata/ directory.
    183 std::string GetTestFilePath(const std::string& filename);
    184 
    185 // PSNR formula: psnr = 10 * log10 (Peak Signal^2 / mse)
    186 // sse is set to a small number for identical frames or sse == 0
    187 static inline double ComputePSNR(double sse, double count) {
    188 #if !defined(DISABLE_YUV)
    189   return libyuv::SumSquareErrorToPsnr(static_cast<uint64>(sse),
    190                                       static_cast<uint64>(count));
    191 #else
    192   if (sse <= 0.)
    193     sse = 65025.0 * count / pow(10., 128./10.);  // produces max PSNR of 128
    194   return 10.0 * log10(65025.0 * count / sse);
    195 #endif
    196 }
    197 
    198 static inline double ComputeSumSquareError(const uint8 *org, const uint8 *rec,
    199                                            int size) {
    200 #if !defined(DISABLE_YUV)
    201   return static_cast<double>(libyuv::ComputeSumSquareError(org, rec, size));
    202 #else
    203   double sse = 0.;
    204   for (int j = 0; j < size; ++j) {
    205     const int diff = static_cast<int>(org[j]) - static_cast<int>(rec[j]);
    206     sse += static_cast<double>(diff * diff);
    207   }
    208   return sse;
    209 #endif
    210 }
    211 
    212 // Loads the image with the specified prefix and size into |out|.
    213 bool LoadPlanarYuvTestImage(const std::string& prefix,
    214                             int width, int height, uint8* out);
    215 
    216 // Dumps the YUV image out to a file, for visual inspection.
    217 // PYUV tool can be used to view dump files.
    218 void DumpPlanarYuvTestImage(const std::string& prefix, const uint8* img,
    219                             int w, int h);
    220 
    221 // Dumps the ARGB image out to a file, for visual inspection.
    222 // ffplay tool can be used to view dump files.
    223 void DumpPlanarArgbTestImage(const std::string& prefix, const uint8* img,
    224                              int w, int h);
    225 
    226 // Compare two I420 frames.
    227 bool VideoFrameEqual(const VideoFrame* frame0, const VideoFrame* frame1);
    228 
    229 // Checks whether |codecs| contains |codec|; checks using Codec::Matches().
    230 template <class C>
    231 bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) {
    232   typename std::vector<C>::const_iterator it;
    233   for (it = codecs.begin(); it != codecs.end(); ++it) {
    234     if (it->Matches(codec)) {
    235       return true;
    236     }
    237   }
    238   return false;
    239 }
    240 
    241 // Create Simulcast StreamParams with given |ssrcs| and |cname|.
    242 cricket::StreamParams CreateSimStreamParams(
    243     const std::string& cname, const std::vector<uint32>& ssrcs);
    244 // Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
    245 // The number of |rtx_ssrcs| must match number of |ssrcs|.
    246 cricket::StreamParams CreateSimWithRtxStreamParams(
    247     const std::string& cname, const std::vector<uint32>& ssrcs,
    248     const std::vector<uint32>& rtx_ssrcs);
    249 
    250 }  // namespace cricket
    251 
    252 #endif  // TALK_MEDIA_BASE_TESTUTILS_H_
    253