• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2012 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 WEBRTC_MODULES_VIDEO_CODING_TEST_TEST_CALLBACKS_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_TEST_TEST_CALLBACKS_H_
13 
14 /*
15  * Declaration of general callbacks that are used throughout VCM's offline tests
16  */
17 
18 
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include <fstream>
23 #include <list>
24 
25 #include "webrtc/modules/interface/module_common_types.h"
26 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
27 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
28 #include "webrtc/modules/video_coding/main/test/test_util.h"
29 #include "webrtc/system_wrappers/interface/trace.h"
30 
31 namespace webrtc
32 {
33 class RTPPayloadRegistry;
34 class RtpDump;
35 
36 // Send Side - Packetization callback - send an encoded frame to the VCMReceiver
37 class VCMEncodeCompleteCallback: public VCMPacketizationCallback
38 {
39 public:
40     // Constructor input: file in which encoded data will be written
41     VCMEncodeCompleteCallback(FILE* encodedFile);
42     virtual ~VCMEncodeCompleteCallback();
43     // Register transport callback
44     void RegisterTransportCallback(VCMPacketizationCallback* transport);
45     // Process encoded data received from the encoder, pass stream to the
46     // VCMReceiver module
47     int32_t SendData(const FrameType frameType,
48                            const uint8_t payloadType,
49                            const uint32_t timeStamp,
50                            int64_t capture_time_ms,
51                            const uint8_t* payloadData,
52                            const uint32_t payloadSize,
53                            const RTPFragmentationHeader& fragmentationHeader,
54                            const RTPVideoHeader* videoHdr);
55     // Register exisitng VCM. Currently - encode and decode under same module.
RegisterReceiverVCM(VideoCodingModule * vcm)56     void RegisterReceiverVCM(VideoCodingModule *vcm) {_VCMReceiver = vcm;}
57     // Return size of last encoded frame data (all frames in the sequence)
58     // Good for only one call - after which will reset value
59     // (to allow detection of frame drop)
60     float EncodedBytes();
61     // Return encode complete (true/false)
62     bool EncodeComplete();
63     // Inform callback of codec used
SetCodecType(RtpVideoCodecTypes codecType)64     void SetCodecType(RtpVideoCodecTypes codecType)
65     {_codecType = codecType;}
66     // Inform callback of frame dimensions
SetFrameDimensions(int32_t width,int32_t height)67     void SetFrameDimensions(int32_t width, int32_t height)
68     {
69         _width = width;
70         _height = height;
71     }
72     // Initialize callback data
73     void Initialize();
74     void ResetByteCount();
75 
76     // Conversion function for payload type (needed for the callback function)
77 
78 private:
79     FILE*              _encodedFile;
80     float              _encodedBytes;
81     VideoCodingModule* _VCMReceiver;
82     FrameType          _frameType;
83     uint16_t     _seqNo;
84     bool               _encodeComplete;
85     int32_t      _width;
86     int32_t      _height;
87     RtpVideoCodecTypes _codecType;
88 
89 }; // end of VCMEncodeCompleteCallback
90 
91 // Send Side - Packetization callback - packetize an encoded frame via the
92 // RTP module
93 class VCMRTPEncodeCompleteCallback: public VCMPacketizationCallback
94 {
95 public:
VCMRTPEncodeCompleteCallback(RtpRtcp * rtp)96     VCMRTPEncodeCompleteCallback(RtpRtcp* rtp) :
97         _encodedBytes(0),
98         _encodeComplete(false),
99         _RTPModule(rtp) {}
100 
~VCMRTPEncodeCompleteCallback()101     virtual ~VCMRTPEncodeCompleteCallback() {}
102     // Process encoded data received from the encoder, pass stream to the
103     // RTP module
104     int32_t SendData(const FrameType frameType,
105                            const uint8_t payloadType,
106                            const uint32_t timeStamp,
107                            int64_t capture_time_ms,
108                            const uint8_t* payloadData,
109                            const uint32_t payloadSize,
110                            const RTPFragmentationHeader& fragmentationHeader,
111                            const RTPVideoHeader* videoHdr);
112     // Return size of last encoded frame. Value good for one call
113     // (resets to zero after call to inform test of frame drop)
114     float EncodedBytes();
115     // Return encode complete (true/false)
116     bool EncodeComplete();
117     // Inform callback of codec used
SetCodecType(RtpVideoCodecTypes codecType)118     void SetCodecType(RtpVideoCodecTypes codecType)
119     {_codecType = codecType;}
120 
121     // Inform callback of frame dimensions
SetFrameDimensions(int16_t width,int16_t height)122     void SetFrameDimensions(int16_t width, int16_t height)
123     {
124         _width = width;
125         _height = height;
126     }
127 
128 private:
129     float              _encodedBytes;
130     FrameType          _frameType;
131     bool               _encodeComplete;
132     RtpRtcp*           _RTPModule;
133     int16_t      _width;
134     int16_t      _height;
135     RtpVideoCodecTypes _codecType;
136 }; // end of VCMEncodeCompleteCallback
137 
138 // Decode Complete callback
139 // Writes the decoded frames to a given file.
140 class VCMDecodeCompleteCallback: public VCMReceiveCallback
141 {
142 public:
VCMDecodeCompleteCallback(FILE * decodedFile)143     VCMDecodeCompleteCallback(FILE* decodedFile) :
144         _decodedFile(decodedFile), _decodedBytes(0) {}
~VCMDecodeCompleteCallback()145     virtual ~VCMDecodeCompleteCallback() {}
146     // Write decoded frame into file
147     int32_t FrameToRender(webrtc::I420VideoFrame& videoFrame);
148     int32_t DecodedBytes();
149 private:
150     FILE*               _decodedFile;
151     uint32_t      _decodedBytes;
152 }; // end of VCMDecodeCompleCallback class
153 
154 // Transport callback
155 // Called by the RTP Sender - simulates sending packets through a network to the
156 // RTP receiver. User can set network conditions as: RTT, packet loss,
157 // burst length and jitter.
158 class RTPSendCompleteCallback: public Transport
159 {
160 public:
161     // Constructor input: (receive side) rtp module to send encoded data to
162     RTPSendCompleteCallback(Clock* clock,
163                             const char* filename = NULL);
164     virtual ~RTPSendCompleteCallback();
165 
SetRtpModule(RtpRtcp * rtp_module)166     void SetRtpModule(RtpRtcp* rtp_module) { _rtp = rtp_module; }
167     // Send Packet to receive side RTP module
168     virtual int SendPacket(int channel, const void *data, int len);
169     // Send RTCP Packet to receive side RTP module
170     virtual int SendRTCPPacket(int channel, const void *data, int len);
171     // Set percentage of channel loss in the network
172     void SetLossPct(double lossPct);
173     // Set average size of burst loss
174     void SetBurstLength(double burstLength);
175     // Set network delay in the network
SetNetworkDelay(uint32_t networkDelayMs)176     void SetNetworkDelay(uint32_t networkDelayMs)
177                         {_networkDelayMs = networkDelayMs;};
178     // Set Packet jitter delay
SetJitterVar(uint32_t jitterVar)179     void SetJitterVar(uint32_t jitterVar)
180                       {_jitterVar = jitterVar;};
181     // Return send count
SendCount()182     int SendCount() {return _sendCount; }
183     // Return accumulated length in bytes of transmitted packets
TotalSentLength()184     uint32_t TotalSentLength() {return _totalSentLength;}
185 protected:
186     // Randomly decide whether to drop packets, based on the channel model
187     bool PacketLoss();
188     // Random uniform loss model
189     bool UnifomLoss(double lossPct);
190 
191     Clock*                  _clock;
192     uint32_t          _sendCount;
193     RTPPayloadRegistry* rtp_payload_registry_;
194     RtpReceiver* rtp_receiver_;
195     RtpRtcp*                _rtp;
196     double                  _lossPct;
197     double                  _burstLength;
198     uint32_t          _networkDelayMs;
199     double                  _jitterVar;
200     bool                    _prevLossState;
201     uint32_t          _totalSentLength;
202     std::list<RtpPacket*>   _rtpPackets;
203     RtpDump*                _rtpDump;
204 };
205 
206 // Request re-transmission of packets (NACK)
207 class PacketRequester: public VCMPacketRequestCallback
208 {
209 public:
PacketRequester(RtpRtcp & rtp)210     PacketRequester(RtpRtcp& rtp) :
211         _rtp(rtp) {}
212     int32_t ResendPackets(const uint16_t* sequenceNumbers,
213             uint16_t length);
214 private:
215     webrtc::RtpRtcp& _rtp;
216 };
217 
218 // Key frame request
219 class KeyFrameReqTest: public VCMFrameTypeCallback
220 {
221 public:
222     int32_t RequestKeyFrame();
223 };
224 
225 
226 // VCM statistics
227 class SendStatsTest: public webrtc::VCMSendStatisticsCallback
228 {
229 public:
SendStatsTest()230     SendStatsTest() : _framerate(15), _bitrate(500) {}
231     int32_t SendStatistics(const uint32_t bitRate,
232             const uint32_t frameRate);
set_framerate(uint32_t frameRate)233     void set_framerate(uint32_t frameRate) {_framerate = frameRate;}
set_bitrate(uint32_t bitrate)234     void set_bitrate(uint32_t bitrate) {_bitrate = bitrate;}
235 private:
236     uint32_t _framerate;
237     uint32_t _bitrate;
238 };
239 
240 // Protection callback - allows the VCM (media optimization) to inform the RTP
241 // module of the required protection(FEC rates/settings and NACK mode).
242 class VideoProtectionCallback: public VCMProtectionCallback
243 {
244 public:
245     VideoProtectionCallback();
246     virtual ~VideoProtectionCallback();
RegisterRtpModule(RtpRtcp * rtp)247     void RegisterRtpModule(RtpRtcp* rtp) {_rtp = rtp;}
248     int32_t ProtectionRequest(
249         const FecProtectionParams* delta_fec_params,
250         const FecProtectionParams* key_fec_params,
251         uint32_t* sent_video_rate_bps,
252         uint32_t* sent_nack_rate_bps,
253         uint32_t* sent_fec_rate_bps);
254     FecProtectionParams DeltaFecParameters() const;
255     FecProtectionParams KeyFecParameters() const;
256 private:
257     RtpRtcp* _rtp;
258     FecProtectionParams delta_fec_params_;
259     FecProtectionParams key_fec_params_;
260 };
261 }  // namespace webrtc
262 #endif
263