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_CODECS_TEST_PACKET_MANIPULATOR_H_
12 #define WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_PACKET_MANIPULATOR_H_
13 
14 #include <stdlib.h>
15 
16 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
17 #include "webrtc/system_wrappers/include/critical_section_wrapper.h"
18 #include "webrtc/test/testsupport/packet_reader.h"
19 
20 namespace webrtc {
21 namespace test {
22 
23 // Which mode the packet loss shall be performed according to.
24 enum PacketLossMode {
25   // Drops packets with a configured probability independently for each packet
26   kUniform,
27   // Drops packets similar to uniform but when a packet is being dropped,
28   // the number of lost packets in a row is equal to the configured burst
29   // length.
30   kBurst
31 };
32 // Returns a string representation of the enum value.
33 const char* PacketLossModeToStr(PacketLossMode e);
34 
35 // Contains configurations related to networking and simulation of
36 // scenarios caused by network interference.
37 struct NetworkingConfig {
NetworkingConfigNetworkingConfig38   NetworkingConfig()
39       : packet_size_in_bytes(1500),
40         max_payload_size_in_bytes(1440),
41         packet_loss_mode(kUniform),
42         packet_loss_probability(0.0),
43         packet_loss_burst_length(1) {}
44 
45   // Packet size in bytes. Default: 1500 bytes.
46   size_t packet_size_in_bytes;
47 
48   // Encoder specific setting of maximum size in bytes of each payload.
49   // Default: 1440 bytes.
50   size_t max_payload_size_in_bytes;
51 
52   // Packet loss mode. Two different packet loss models are supported:
53   // uniform or burst. This setting has no effect unless
54   // packet_loss_probability is >0.
55   // Default: uniform.
56   PacketLossMode packet_loss_mode;
57 
58   // Packet loss probability. A value between 0.0 and 1.0 that defines the
59   // probability of a packet being lost. 0.1 means 10% and so on.
60   // Default: 0 (no loss).
61   double packet_loss_probability;
62 
63   // Packet loss burst length. Defines how many packets will be lost in a burst
64   // when a packet has been decided to be lost. Must be >=1. Default: 1.
65   int packet_loss_burst_length;
66 };
67 
68 // Class for simulating packet loss on the encoded frame data.
69 // When a packet loss has occurred in a frame, the remaining data in that
70 // frame is lost (even if burst length is only a single packet).
71 // TODO(kjellander): Support discarding only individual packets in the frame
72 // when CL 172001 has been submitted. This also requires a correct
73 // fragmentation header to be passed to the decoder.
74 //
75 // To get a repeatable packet drop pattern, re-initialize the random seed
76 // using InitializeRandomSeed before each test run.
77 class PacketManipulator {
78  public:
~PacketManipulator()79   virtual ~PacketManipulator() {}
80 
81   // Manipulates the data of the encoded_image to simulate parts being lost
82   // during transport.
83   // If packets are dropped from frame data, the completedFrame field will be
84   // set to false.
85   // Returns the number of packets being dropped.
86   virtual int ManipulatePackets(webrtc::EncodedImage* encoded_image) = 0;
87 };
88 
89 class PacketManipulatorImpl : public PacketManipulator {
90  public:
91   PacketManipulatorImpl(PacketReader* packet_reader,
92                         const NetworkingConfig& config,
93                         bool verbose);
94   virtual ~PacketManipulatorImpl();
95   int ManipulatePackets(webrtc::EncodedImage* encoded_image) override;
96   virtual void InitializeRandomSeed(unsigned int seed);
97 
98  protected:
99   // Returns a uniformly distributed random value between 0.0 and 1.0
100   virtual double RandomUniform();
101 
102  private:
103   PacketReader* packet_reader_;
104   const NetworkingConfig& config_;
105   // Used to simulate a burst over several frames.
106   int active_burst_packets_;
107   CriticalSectionWrapper* critsect_;
108   unsigned int random_seed_;
109   bool verbose_;
110 };
111 
112 }  // namespace test
113 }  // namespace webrtc
114 
115 #endif  // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_PACKET_MANIPULATOR_H_
116