6#include "traffic-generator-3gpp-generic-video.h"
10#include "ns3/inet-socket-address.h"
12#include "ns3/nstime.h"
13#include "ns3/simulator.h"
14#include "ns3/tcp-socket-factory.h"
15#include "ns3/trace-source-accessor.h"
16#include "ns3/uinteger.h"
21NS_LOG_COMPONENT_DEFINE(
"TrafficGenerator3gppGenericVideo");
22NS_OBJECT_ENSURE_REGISTERED(TrafficGenerator3gppGenericVideo);
28 TypeId(
"ns3::TrafficGenerator3gppGenericVideo")
30 .SetGroupName(
"Applications")
32 .AddAttribute(
"DataRate",
33 "The desired data rate in Mbps.",
35 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_dataRate),
36 MakeDoubleChecker<double>())
37 .AddAttribute(
"MinDataRate",
38 "The minimum desired data rate in Mbps.",
40 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_minDataRate),
41 MakeDoubleChecker<double>())
42 .AddAttribute(
"MaxDataRate",
43 "The maximum desired data rate in Mbps.",
45 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_maxDataRate),
46 MakeDoubleChecker<double>())
48 "LowerThresholdForDecreasingSlowly",
49 "The lower packet loss bound for decreasing the video traffic slowly.",
52 &TrafficGenerator3gppGenericVideo::m_lowerThresholdForDecreasingSlowly),
53 MakeDoubleChecker<double>())
55 "LowerThresholdForDecreasingQuickly",
56 "The lower packet loss bound for decreasing the video traffic quickly.",
59 &TrafficGenerator3gppGenericVideo::m_lowerThresholdForDecreasingQuickly),
60 MakeDoubleChecker<double>())
61 .AddAttribute(
"UpperThresholdForIncreasing",
62 "The upper packet loss bound for increasing the video traffic.",
65 &TrafficGenerator3gppGenericVideo::m_upperThresholdForIncreasing),
66 MakeDoubleChecker<double>())
68 "IncreaseDataRateMultiplier",
69 "The multiplier when increasing the video traffic volume, e.g., 3 to increase 3 "
70 "times. Used to decrease fps or data rate.",
72 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_increaseDataRateMultiplier),
73 MakeDoubleChecker<double>())
75 "DecreaseDataRateSlowlyMultiplier",
76 "The multiplier when decreasing the video traffic volume slowly, e.g, 0.75, to "
77 "decrease 25%. Used to decrease fps or data rate.",
80 &TrafficGenerator3gppGenericVideo::m_decreaseDataRateSlowlyMultiplier),
81 MakeDoubleChecker<double>())
83 "DecreaseDataRateQuicklyMultiplier",
84 "The multiplier when decreasing the video traffic volume quickly, e.g. 0.2 to "
85 "decrease 5 times. Used to decrease fps or data rate.",
88 &TrafficGenerator3gppGenericVideo::m_decreaseDataRateQuicklyMultiplier),
89 MakeDoubleChecker<double>())
91 "Frame generation rate (per second). E.g. typical value cold be 60fps.",
93 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_fps),
94 MakeUintegerChecker<uint32_t>())
95 .AddAttribute(
"MinFps",
96 "The minimum frame generation rate (per second). ",
98 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_minFps),
99 MakeUintegerChecker<uint32_t>())
100 .AddAttribute(
"MaxFps",
101 "The maximum frame generation rate (per second). ",
103 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_maxFps),
104 MakeUintegerChecker<uint32_t>())
106 "StdRatioPacketSize",
107 "STD ratio wrt the mean packet size. "
108 "See Table 5.1.1.1-1 of 3GPP TR 38.838 V17.0.0 (2021-12)."
109 "Typical values are 10.5% and 3%.",
111 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_stdRatioPacketSize),
112 MakeDoubleChecker<double>(0, 1))
114 "MinRatioPacketSize",
115 "Min ratio wrt the mean packet size. "
116 "See Table 5.1.1.1-1 of 3GPP TR 38.838 V17.0.0 (2021-12)."
117 "Typical values are 50% and 91%.",
119 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_minRatioPacketSize),
120 MakeDoubleChecker<double>(0, 1))
122 "MaxRatioPacketSize",
123 "Max ratio wrt the mean packet size. "
124 "See Table 5.1.1.1-1 of 3GPP TR 38.838 V17.0.0 (2021-12)."
125 "Typical values are 150% and 109%.",
127 MakeDoubleAccessor(&TrafficGenerator3gppGenericVideo::m_maxRatioPacketSize),
128 MakeDoubleChecker<double>(1, 2))
129 .AddAttribute(
"MeanPacketArrivalJitter",
130 "The mean of packet arrival jitter in milliseconds.",
132 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_meanJitter),
133 MakeUintegerChecker<uint32_t>())
134 .AddAttribute(
"StdPacketArrivalJitter",
135 "The STD of packet arrival jitter in milliseconds.",
137 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_stdJitter),
138 MakeUintegerChecker<uint32_t>())
139 .AddAttribute(
"BoundJitter",
140 "The periodicity in milliseconds.",
142 MakeUintegerAccessor(&TrafficGenerator3gppGenericVideo::m_boundJitter),
143 MakeUintegerChecker<uint32_t>())
144 .AddAttribute(
"Remote",
145 "The address of the destination",
148 MakeAddressChecker())
149 .AddAttribute(
"Protocol",
150 "The type of protocol to use.",
151 TypeIdValue(TcpSocketFactory::GetTypeId()),
156 "Type of the algorithm for the codec adaptation",
157 EnumValue(LoopbackAlgType::ADJUST_IPA_TIME),
158 MakeEnumAccessor<LoopbackAlgType>(
160 &TrafficGenerator3gppGenericVideo::GetLoopbackAlgType),
161 MakeEnumChecker(TrafficGenerator3gppGenericVideo::ADJUST_IPA_TIME,
163 TrafficGenerator3gppGenericVideo::ADJUST_PACKET_SIZE,
165 TrafficGenerator3gppGenericVideo::ADJUST_FPS,
167 TrafficGenerator3gppGenericVideo::WO,
169 TrafficGenerator3gppGenericVideo::ADJUST_PACKET_SIZE_UP_AGG,
171 .AddTraceSource(
"Tx",
172 "A new packet is created and is sent",
173 MakeTraceSourceAccessor(&TrafficGenerator::m_txTrace),
174 "ns3::TrafficGenerator::TxTracedCallback")
177 "Traffic parameters have been updated accordingly "
178 "the loopback algorithm, and notified through this trace.",
179 MakeTraceSourceAccessor(&TrafficGenerator3gppGenericVideo::m_paramsTrace),
180 "ns3::TrafficGenerator3gppGenericVideo::ParamsTracedCallback");
185TrafficGenerator3gppGenericVideo::GetLoopbackAlgType()
const
187 return m_loopbackAlgType;
193 m_loopbackAlgType = loopbackAlgType;
196TrafficGenerator3gppGenericVideo::TrafficGenerator3gppGenericVideo()
199 NS_LOG_FUNCTION(
this);
202TrafficGenerator3gppGenericVideo::~TrafficGenerator3gppGenericVideo()
204 NS_LOG_FUNCTION(
this);
208TrafficGenerator3gppGenericVideo::StartApplication()
210 NS_LOG_FUNCTION(
this);
213 InetSocketAddress address = InetSocketAddress::ConvertFrom(GetPeer());
214 m_port = address.GetPort();
216 m_paramsTrace(Simulator::Now(),
228TrafficGenerator3gppGenericVideo::PacketBurstSent()
230 NS_LOG_FUNCTION(
this);
233 NS_ABORT_MSG(
"This function should not be called for the video + audio/data traffic");
237TrafficGenerator3gppGenericVideo::GenerateNextPacketBurstSize()
239 NS_LOG_FUNCTION(
this);
240 SetPacketBurstSizeInPackets(1);
244TrafficGenerator3gppGenericVideo::GetNextPacketTime()
const
246 NS_LOG_FUNCTION(
this);
247 double packetJitter = 0;
250 packetJitter = m_packetJitter->GetValue();
251 if (packetJitter <= m_boundJitter && packetJitter > -m_boundJitter)
257 NS_LOG_DEBUG(
"Generated packet jitter is out of configured bounds. Generated value:"
262 double packetTimeMs = (1e3 * 1 / (double)m_fps) + packetJitter;
263 NS_ASSERT(packetTimeMs);
264 NS_LOG_DEBUG(
"Next packet time in Milliseconds: " << packetTimeMs);
265 return Seconds(1e-3 * packetTimeMs);
269TrafficGenerator3gppGenericVideo::GetNextPacketSize()
const
271 NS_LOG_FUNCTION(
this);
272 uint32_t packetSize = 0;
275 packetSize = m_packetSize->GetValue();
276 if (packetSize <= m_maxRatioPacketSize * m_meanPacketSize &&
277 packetSize > m_minRatioPacketSize * m_meanPacketSize)
283 NS_LOG_DEBUG(
"Generated packet size is out of configured bounbds. Generated value:"
291TrafficGenerator3gppGenericVideo::DoInitialize()
293 NS_LOG_FUNCTION(
this);
294 m_meanPacketSize = (m_dataRate * 1e6) / (m_fps) / 8;
297 m_packetSize = CreateObject<NormalRandomVariable>();
298 m_packetSize->SetAttribute(
"Mean", DoubleValue(m_meanPacketSize));
299 m_packetSize->SetAttribute(
"Variance", DoubleValue(m_stdRatioPacketSize * m_meanPacketSize));
301 m_packetJitter = CreateObject<NormalRandomVariable>();
302 m_packetJitter->SetAttribute(
"Mean", DoubleValue(m_meanJitter));
303 m_packetJitter->SetAttribute(
"Variance", DoubleValue(m_stdJitter));
304 m_packetJitter->SetAttribute(
"Bound", DoubleValue(m_boundJitter));
306 TrafficGenerator::DoInitialize();
310TrafficGenerator3gppGenericVideo::ReceiveLoopbackInformation(
double packetLoss,
311 uint32_t packetReceived,
312 double windowInSeconds,
314 Time packetDelayJitter)
316 NS_LOG_FUNCTION(
this);
318 if (!m_stopEvent.IsPending())
320 NS_LOG_WARN(
"The application stopped working ignore this function call...");
324 if (Simulator::Now() - m_startTime < Seconds(windowInSeconds))
329 double tempDataRate = m_dataRate;
330 uint32_t tempFps = m_fps;
331 double tempMeanPacketSize = m_meanPacketSize;
334 double txPacketLossEstimation =
335 std::max(0.0, std::min(1.0, 1 - ((
double)packetReceived / (m_fps * windowInSeconds))));
337 NS_LOG_INFO(
"Packets received:" << packetReceived
338 <<
", packets expected: " << m_fps * windowInSeconds
339 <<
", packet loss estimation:" << txPacketLossEstimation);
344 packetLoss = txPacketLossEstimation;
346 if (m_loopbackAlgType == ADJUST_IPA_TIME)
348 if (packetLoss > m_lowerThresholdForDecreasingSlowly)
351 m_dataRate = std::max(m_dataRate * m_decreaseDataRateSlowlyMultiplier, m_minDataRate);
352 m_fps = std::max(m_fps * m_decreaseDataRateSlowlyMultiplier, m_minFps);
354 else if (packetLoss < m_upperThresholdForIncreasing)
356 m_dataRate = std::min(m_dataRate * m_increaseDataRateMultiplier, m_maxDataRate);
357 m_fps = std::min(m_fps * m_increaseDataRateMultiplier, m_maxFps);
361 NS_LOG_INFO(
"Packet loss is in an accepted range to not change anything");
364 else if (m_loopbackAlgType == ADJUST_PACKET_SIZE)
366 if (packetLoss > m_lowerThresholdForDecreasingSlowly &&
367 packetLoss < m_lowerThresholdForDecreasingQuickly)
370 m_dataRate = std::max(m_dataRate * m_decreaseDataRateSlowlyMultiplier, m_minDataRate);
372 else if (packetLoss >= m_lowerThresholdForDecreasingQuickly)
375 m_dataRate = std::max(m_dataRate * m_decreaseDataRateQuicklyMultiplier, m_minDataRate);
377 else if (packetLoss < m_upperThresholdForIncreasing)
380 m_dataRate = std::min(m_dataRate * m_increaseDataRateMultiplier, m_maxDataRate);
384 NS_LOG_INFO(
"Packet loss is in an acceptable range to not change anything");
387 else if (m_loopbackAlgType == ADJUST_PACKET_SIZE_UP_AGG)
389 if (packetLoss > m_lowerThresholdForDecreasingSlowly &&
390 packetLoss < m_lowerThresholdForDecreasingQuickly)
393 m_dataRate = std::max(m_dataRate * m_decreaseDataRateSlowlyMultiplier, m_minDataRate);
395 else if (packetLoss >= m_lowerThresholdForDecreasingQuickly)
398 m_dataRate = std::max(m_dataRate * m_decreaseDataRateQuicklyMultiplier, m_minDataRate);
400 else if (packetLoss < m_upperThresholdForIncreasing)
403 m_dataRate = std::min(m_dataRate * 1.5, m_maxDataRate);
407 NS_LOG_INFO(
"Packet loss is in an acceptable range to not change anything");
410 else if (m_loopbackAlgType == ADJUST_FPS)
412 if (packetLoss > m_lowerThresholdForDecreasingSlowly)
415 m_fps = std::max(m_fps * m_decreaseDataRateSlowlyMultiplier, m_minFps);
417 else if (packetLoss < m_upperThresholdForIncreasing)
419 m_fps = std::min(m_fps * m_increaseDataRateMultiplier, m_maxFps);
423 NS_LOG_INFO(
"Packet loss is in an accepted range to not change anything");
448 m_meanPacketSize = (m_dataRate * 1e6) / (m_fps) / 8;
450 m_packetSize->SetAttribute(
"Mean", DoubleValue(m_meanPacketSize));
451 m_packetSize->SetAttribute(
"Variance", DoubleValue(m_stdRatioPacketSize * m_meanPacketSize));
453 m_paramsTrace(Simulator::Now(),
462 if (tempDataRate != m_dataRate || tempFps != m_fps || tempMeanPacketSize != m_meanPacketSize)
465 NS_LOG_DEBUG(
"Old data rate: " << tempDataRate <<
" new data rate: " << m_dataRate);
466 NS_LOG_DEBUG(
"Old fps: " << tempFps <<
" new fps: " << m_fps);
467 NS_LOG_DEBUG(
"Old mean packet size: "
468 << tempMeanPacketSize <<
" new mean packet size: " << m_meanPacketSize);
475 m_packetSize->SetStream(stream);
476 m_packetJitter->SetStream(stream + 1);
void SetLoopbackAlgType(LoopbackAlgType loopbackAlgType)
Set loopback algorithm type.
LoopbackAlgType
Different loopback algorithm types.
static TypeId GetTypeId()
Get the type ID.
int64_t AssignStreams(int64_t stream) override
void SetRemote(Address remote)
Sets the remote address.
bool SendPacketBurst()
Send another packet burst, which can be e.g., a file, or a video frame.
void SetProtocol(TypeId protocol)
Sets the protocol.