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