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//
12// tb_external_transport.h
13//
14
15#ifndef WEBRTC_VIDEO_ENGINE_TEST_AUTOTEST_INTERFACE_TB_EXTERNAL_TRANSPORT_H_
16#define WEBRTC_VIDEO_ENGINE_TEST_AUTOTEST_INTERFACE_TB_EXTERNAL_TRANSPORT_H_
17
18#include <list>
19#include <map>
20
21#include "webrtc/common_types.h"
22
23namespace webrtc
24{
25class CriticalSectionWrapper;
26class EventWrapper;
27class ThreadWrapper;
28class ViENetwork;
29}
30
31enum RandomLossModel {
32  kNoLoss,
33  kUniformLoss,
34  kGilbertElliotLoss
35};
36struct NetworkParameters {
37  int packet_loss_rate;
38  int burst_length;  // Only applicable for kGilbertElliotLoss.
39  int mean_one_way_delay;
40  int std_dev_one_way_delay;
41  RandomLossModel loss_model;
42  NetworkParameters():
43    packet_loss_rate(0), burst_length(0), mean_one_way_delay(0),
44        std_dev_one_way_delay(0), loss_model(kNoLoss) {}
45};
46
47// Allows to subscribe for callback when a frame is started being sent.
48class SendFrameCallback
49{
50public:
51    // Called once per frame (when a new RTP timestamp is detected) when the
52    // first data packet of the frame is being sent using the
53    // TbExternalTransport.SendPacket method.
54    virtual void FrameSent(unsigned int rtp_timestamp) = 0;
55protected:
56    SendFrameCallback() {}
57    virtual ~SendFrameCallback() {}
58};
59
60// Allows to subscribe for callback when the first packet of a frame is
61// received.
62class ReceiveFrameCallback
63{
64public:
65    // Called once per frame (when a new RTP timestamp is detected)
66    // during the processing of the RTP packet queue in
67    // TbExternalTransport::ViEExternalTransportProcess.
68    virtual void FrameReceived(unsigned int rtp_timestamp) = 0;
69protected:
70    ReceiveFrameCallback() {}
71    virtual ~ReceiveFrameCallback() {}
72};
73
74// External transport implementation for testing purposes.
75// A packet loss probability must be set in order to drop packets from the data
76// being sent to this class.
77// Will never drop packets from the first frame of a video sequence.
78class TbExternalTransport : public webrtc::Transport
79{
80public:
81    typedef std::map<unsigned int, unsigned int> SsrcChannelMap;
82
83    TbExternalTransport(webrtc::ViENetwork& vieNetwork,
84                        int sender_channel,
85                        TbExternalTransport::SsrcChannelMap* receive_channels);
86    ~TbExternalTransport(void);
87
88    virtual int SendPacket(int channel, const void *data, int len);
89    virtual int SendRTCPPacket(int channel, const void *data, int len);
90
91    // Should only be called before/after traffic is being processed.
92    // Only one observer can be set (multiple calls will overwrite each other).
93    virtual void RegisterSendFrameCallback(SendFrameCallback* callback);
94
95    // Should only be called before/after traffic is being processed.
96    // Only one observer can be set (multiple calls will overwrite each other).
97    virtual void RegisterReceiveFrameCallback(ReceiveFrameCallback* callback);
98
99    // The network parameters of the link. Regarding packet losses, packets
100    // belonging to the first frame (same RTP timestamp) will never be dropped.
101    void SetNetworkParameters(const NetworkParameters& network_parameters);
102    void SetSSRCFilter(uint32_t SSRC);
103
104    void ClearStats();
105    // |packet_counters| is a map which counts the number of packets sent per
106    // payload type.
107    void GetStats(int32_t& numRtpPackets,
108                  int32_t& numDroppedPackets,
109                  int32_t& numRtcpPackets,
110                  std::map<uint8_t, int>* packet_counters);
111
112    void SetTemporalToggle(unsigned char layers);
113    void EnableSSRCCheck();
114    unsigned int ReceivedSSRC();
115
116    void EnableSequenceNumberCheck();
117    unsigned short GetFirstSequenceNumber();
118
119    bool EmptyQueue() const;
120
121protected:
122    static bool ViEExternalTransportRun(void* object);
123    bool ViEExternalTransportProcess();
124private:
125    // TODO(mikhal): Break these out to classes.
126    static int GaussianRandom(int mean_ms, int standard_deviation_ms);
127    bool UniformLoss(int loss_rate);
128    bool GilbertElliotLoss(int loss_rate, int burst_length);
129    int64_t NowMs();
130
131    enum
132    {
133        KMaxPacketSize = 1650
134    };
135    enum
136    {
137        KMaxWaitTimeMs = 100
138    };
139    typedef struct
140    {
141        int8_t packetBuffer[KMaxPacketSize];
142        int32_t length;
143        int32_t channel;
144        int64_t receiveTime;
145    } VideoPacket;
146
147    int sender_channel_;
148    SsrcChannelMap* receive_channels_;
149    webrtc::ViENetwork& _vieNetwork;
150    webrtc::ThreadWrapper& _thread;
151    webrtc::EventWrapper& _event;
152    webrtc::CriticalSectionWrapper& _crit;
153    webrtc::CriticalSectionWrapper& _statCrit;
154
155    NetworkParameters network_parameters_;
156    int32_t _rtpCount;
157    int32_t _rtcpCount;
158    int32_t _dropCount;
159    // |packet_counters| is a map which counts the number of packets sent per
160    // payload type.
161    std::map<uint8_t, int> packet_counters_;
162
163    std::list<VideoPacket*> _rtpPackets;
164    std::list<VideoPacket*> _rtcpPackets;
165
166    SendFrameCallback* _send_frame_callback;
167    ReceiveFrameCallback* _receive_frame_callback;
168
169    unsigned char _temporalLayers;
170    unsigned short _seqNum;
171    unsigned short _sendPID;
172    unsigned char _receivedPID;
173    bool _switchLayer;
174    unsigned char _currentRelayLayer;
175    unsigned int _lastTimeMs;
176
177    bool _checkSSRC;
178    uint32_t _lastSSRC;
179    bool _filterSSRC;
180    uint32_t _SSRC;
181    bool _checkSequenceNumber;
182    uint16_t _firstSequenceNumber;
183
184    // Keep track of the first RTP timestamp so we don't do packet loss on
185    // the first frame.
186    uint32_t _firstRTPTimestamp;
187    // Track RTP timestamps so we invoke callbacks properly (if registered).
188    uint32_t _lastSendRTPTimestamp;
189    uint32_t _lastReceiveRTPTimestamp;
190    int64_t last_receive_time_;
191    bool previous_drop_;
192};
193
194#endif  // WEBRTC_VIDEO_ENGINE_TEST_AUTOTEST_INTERFACE_TB_EXTERNAL_TRANSPORT_H_
195