1 /*
2  *  Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef MEDIA_BASE_TESTUTILS_H_
12 #define MEDIA_BASE_TESTUTILS_H_
13 
14 #include <string>
15 #include <vector>
16 
17 #include "media/base/mediachannel.h"
18 #include "media/base/videocapturer.h"
19 #include "media/base/videocommon.h"
20 #include "rtc_base/arraysize.h"
21 #include "rtc_base/basictypes.h"
22 #include "rtc_base/sigslot.h"
23 #include "rtc_base/window.h"
24 
25 namespace rtc {
26 class ByteBufferReader;
27 class ByteBufferWriter;
28 class StreamInterface;
29 }
30 
31 namespace webrtc {
32 class VideoFrame;
33 }
34 
35 namespace cricket {
36 
37 // Returns size of 420 image with rounding on chroma for odd sizes.
38 #define I420_SIZE(w, h) (w * h + (((w + 1) / 2) * ((h + 1) / 2)) * 2)
39 // Returns size of ARGB image.
40 #define ARGB_SIZE(w, h) (w * h * 4)
41 
MakeVector(const T a[],size_t s)42 template <class T> inline std::vector<T> MakeVector(const T a[], size_t s) {
43   return std::vector<T>(a, a + s);
44 }
45 #define MAKE_VECTOR(a) cricket::MakeVector(a, arraysize(a))
46 
47 struct RtpDumpPacket;
48 class RtpDumpWriter;
49 
50 struct RawRtpPacket {
51   void WriteToByteBuffer(uint32_t in_ssrc, rtc::ByteBufferWriter* buf) const;
52   bool ReadFromByteBuffer(rtc::ByteBufferReader* buf);
53   // Check if this packet is the same as the specified packet except the
54   // sequence number and timestamp, which should be the same as the specified
55   // parameters.
56   bool SameExceptSeqNumTimestampSsrc(const RawRtpPacket& packet,
57                                      uint16_t seq,
58                                      uint32_t ts,
59                                      uint32_t ssc) const;
sizeRawRtpPacket60   int size() const { return 28; }
61 
62   uint8_t ver_to_cc;
63   uint8_t m_to_pt;
64   uint16_t sequence_number;
65   uint32_t timestamp;
66   uint32_t ssrc;
67   char payload[16];
68 };
69 
70 struct RawRtcpPacket {
71   void WriteToByteBuffer(rtc::ByteBufferWriter* buf) const;
72   bool ReadFromByteBuffer(rtc::ByteBufferReader* buf);
73   bool EqualsTo(const RawRtcpPacket& packet) const;
74 
75   uint8_t ver_to_count;
76   uint8_t type;
77   uint16_t length;
78   char payload[16];
79 };
80 
81 // Test helper for testing VideoCapturer implementations.
82 class VideoCapturerListener
83     : public sigslot::has_slots<>,
84       public rtc::VideoSinkInterface<webrtc::VideoFrame> {
85  public:
86   explicit VideoCapturerListener(VideoCapturer* cap);
87   ~VideoCapturerListener();
88 
last_capture_state()89   CaptureState last_capture_state() const { return last_capture_state_; }
frame_count()90   int frame_count() const { return frame_count_; }
frame_width()91   int frame_width() const { return frame_width_; }
frame_height()92   int frame_height() const { return frame_height_; }
resolution_changed()93   bool resolution_changed() const { return resolution_changed_; }
94 
95   void OnStateChange(VideoCapturer* capturer, CaptureState state);
96   void OnFrame(const webrtc::VideoFrame& frame) override;
97 
98  private:
99   VideoCapturer* capturer_;
100   CaptureState last_capture_state_;
101   int frame_count_;
102   int frame_width_;
103   int frame_height_;
104   bool resolution_changed_;
105 };
106 
107 class VideoMediaErrorCatcher : public sigslot::has_slots<> {
108  public:
VideoMediaErrorCatcher()109   VideoMediaErrorCatcher() : ssrc_(0), error_(VideoMediaChannel::ERROR_NONE) { }
ssrc()110   uint32_t ssrc() const { return ssrc_; }
error()111   VideoMediaChannel::Error error() const { return error_; }
OnError(uint32_t ssrc,VideoMediaChannel::Error error)112   void OnError(uint32_t ssrc, VideoMediaChannel::Error error) {
113     ssrc_ = ssrc;
114     error_ = error;
115   }
116  private:
117   uint32_t ssrc_;
118   VideoMediaChannel::Error error_;
119 };
120 
121 // Checks whether |codecs| contains |codec|; checks using Codec::Matches().
122 template <class C>
ContainsMatchingCodec(const std::vector<C> & codecs,const C & codec)123 bool ContainsMatchingCodec(const std::vector<C>& codecs, const C& codec) {
124   typename std::vector<C>::const_iterator it;
125   for (it = codecs.begin(); it != codecs.end(); ++it) {
126     if (it->Matches(codec)) {
127       return true;
128     }
129   }
130   return false;
131 }
132 
133 // Create Simulcast StreamParams with given |ssrcs| and |cname|.
134 cricket::StreamParams CreateSimStreamParams(const std::string& cname,
135                                             const std::vector<uint32_t>& ssrcs);
136 // Create Simulcast stream with given |ssrcs| and |rtx_ssrcs|.
137 // The number of |rtx_ssrcs| must match number of |ssrcs|.
138 cricket::StreamParams CreateSimWithRtxStreamParams(
139     const std::string& cname,
140     const std::vector<uint32_t>& ssrcs,
141     const std::vector<uint32_t>& rtx_ssrcs);
142 
143 // Create StreamParams with single primary SSRC and corresponding FlexFEC SSRC.
144 cricket::StreamParams CreatePrimaryWithFecFrStreamParams(
145     const std::string& cname,
146     uint32_t primary_ssrc,
147     uint32_t flexfec_ssrc);
148 
149 }  // namespace cricket
150 
151 #endif  // MEDIA_BASE_TESTUTILS_H_
152