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