5G-LENA nr-v3.3-159-ga6832aa7
The 5G/NR module for the ns-3 simulator
Loading...
Searching...
No Matches
traffic-generator-ngmn-voip.cc
1// Copyright (c) 2022 CTTC
2// Copyright (c) 2023 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3//
4// SPDX-License-Identifier: GPL-2.0-only
5
6#include "traffic-generator-ngmn-voip.h"
7
8#include "ns3/address.h"
9#include "ns3/boolean.h"
10#include "ns3/double.h"
11#include "ns3/log.h"
12#include "ns3/node.h"
13#include "ns3/nstime.h"
14#include "ns3/packet.h"
15#include "ns3/simulator.h"
16#include "ns3/socket-factory.h"
17#include "ns3/socket.h"
18#include "ns3/tcp-socket-factory.h"
19#include "ns3/trace-source-accessor.h"
20#include "ns3/udp-socket-factory.h"
21#include "ns3/uinteger.h"
22
23namespace ns3
24{
25
26NS_LOG_COMPONENT_DEFINE("TrafficGeneratorNgmnVoip");
27NS_OBJECT_ENSURE_REGISTERED(TrafficGeneratorNgmnVoip);
28
29TypeId
31{
32 static TypeId tid =
33 TypeId("ns3::TrafficGeneratorNgmnVoip")
34 .SetParent<TrafficGenerator>()
35 .SetGroupName("Applications")
36 .AddConstructor<TrafficGeneratorNgmnVoip>()
37 .AddAttribute("EncoderFrameLength",
38 "The encoder frame length in milliseconds. It is used for "
39 "the calculation of transition probabilities based on configured "
40 "voice activity factor (VAF).",
41 UintegerValue(20),
42 MakeUintegerAccessor(&TrafficGeneratorNgmnVoip::m_encoderFrameLength),
43 MakeUintegerChecker<uint32_t>())
44 .AddAttribute("MeanTalkSpurtDuration",
45 "Mean talk spurt duration in the "
46 "number of milliseconds.",
47 UintegerValue(2000),
48 MakeUintegerAccessor(&TrafficGeneratorNgmnVoip::m_meanTalkSpurtDuration),
49 MakeUintegerChecker<uint32_t>())
50 .AddAttribute("VoiceActivityFactor",
51 "Voice activity factor, determines "
52 "the ratio of active versus inactive state. Expressed as the ratio.",
53 DoubleValue(0.5),
54 MakeDoubleAccessor(&TrafficGeneratorNgmnVoip::m_voiceActivityFactor),
55 MakeDoubleChecker<double>(0.0, 0.99))
56 .AddAttribute("VoicePayload",
57 "The voice packet payload in number of bytes.",
58 UintegerValue(40),
59 MakeUintegerAccessor(&TrafficGeneratorNgmnVoip::m_activePayload),
60 MakeUintegerChecker<uint32_t>())
61 .AddAttribute("SIDPeriodicity",
62 "The periodicity of SIDs is 160 ms during silence",
63 UintegerValue(160),
64 MakeUintegerAccessor(&TrafficGeneratorNgmnVoip::m_SIDPeriodicity),
65 MakeUintegerChecker<uint32_t>())
66 .AddAttribute("SIDPayload",
67 "The payload of SIDs.",
68 UintegerValue(15),
69 MakeUintegerAccessor(&TrafficGeneratorNgmnVoip::m_SIDPayload),
70 MakeUintegerChecker<uint32_t>())
71 .AddAttribute("Remote",
72 "The address of the destination",
73 AddressValue(),
74 MakeAddressAccessor(&TrafficGenerator::SetRemote),
75 MakeAddressChecker())
76 .AddAttribute("Protocol",
77 "The type of protocol to use.",
78 TypeIdValue(TcpSocketFactory::GetTypeId()),
79 MakeTypeIdAccessor(&TrafficGenerator::SetProtocol),
80 MakeTypeIdChecker())
81 .AddTraceSource("Tx",
82 "A new packet is created and is sent",
83 MakeTraceSourceAccessor(&TrafficGenerator::m_txTrace),
84 "ns3::TrafficGenerator::TxTracedCallback");
85 return tid;
86}
87
88TrafficGeneratorNgmnVoip::TrafficGeneratorNgmnVoip()
90{
91 NS_LOG_FUNCTION(this);
92}
93
94TrafficGeneratorNgmnVoip::~TrafficGeneratorNgmnVoip()
95{
96 NS_LOG_FUNCTION(this);
97}
98
99void
100TrafficGeneratorNgmnVoip::StartApplication()
101{
102 NS_LOG_FUNCTION(this);
103 // calculate variables needed for the generation of the active and inactive
104 // probabilities
105 // The probability of transitioning from the active to the inactive state
106 m_a = ((double)m_encoderFrameLength / (double)m_meanTalkSpurtDuration);
107 // The probability of transitioning from the inactive to the active state
108 m_c = (m_a * m_voiceActivityFactor) / (1 - m_voiceActivityFactor);
109
110 UpdateState();
112}
113
114void
115TrafficGeneratorNgmnVoip::StopApplication()
116{
117 NS_LOG_FUNCTION(this);
118 TrafficGenerator::StopApplication();
119 NS_ASSERT(m_updateState.IsPending());
120 m_updateState.Cancel();
121}
122
123void
124TrafficGeneratorNgmnVoip::UpdateState()
125{
126 NS_LOG_FUNCTION(this);
127 // check what is the next state
128 if (m_state == INACTIVE_STATE)
129 {
130 double randomValue = m_fromInactiveToActive->GetValue();
131 // throw a coin and check if lower than the probability of transmission from inactive to
132 // active
133 //, switch to active state
134 if (randomValue < m_c)
135 {
136 // switch to active state
137 m_state = ACTIVE_STATE;
138 }
139 }
140 else if (m_state == ACTIVE_STATE)
141 {
142 double randomValue = m_fromActiveToInactive->GetValue();
143 // throw a coin and check if lower than the probability of transitions from active to
144 // inactive
145 if (randomValue < m_a)
146 {
147 // switch to inactive state
148 m_state = INACTIVE_STATE;
149 }
150 }
151 // The model is assumed updated at the speech encoder frame rate R=1/T, where T
152 // is the encoder frame duration (typically, 20ms)
153 m_updateState = Simulator::Schedule(MilliSeconds(m_encoderFrameLength),
154 &TrafficGeneratorNgmnVoip::UpdateState,
155 this);
156}
157
158void
159TrafficGeneratorNgmnVoip::GenerateNextPacketBurstSize()
160{
161 NS_LOG_FUNCTION(this);
162 SetPacketBurstSizeInBytes(UINT32_MAX);
163}
164
165uint32_t
166TrafficGeneratorNgmnVoip::GetNextPacketSize() const
167{
168 NS_LOG_FUNCTION(this);
169 if (m_state == ACTIVE_STATE)
170 {
171 return m_activePayload;
172 }
173 else
174 {
175 return m_SIDPayload;
176 }
177}
178
179Time
180TrafficGeneratorNgmnVoip::GetNextPacketTime() const
181{
182 NS_LOG_FUNCTION(this);
183 if (m_state == INACTIVE_STATE)
184 {
185 return MilliSeconds(m_SIDPeriodicity);
186 }
187 else
188 {
189 return Seconds((double)(m_activePayload * 8) / 12200);
190 }
191}
192
193void
194TrafficGeneratorNgmnVoip::DoDispose()
195{
196 NS_LOG_FUNCTION(this);
197 m_fromActiveToInactive = nullptr;
198 m_fromInactiveToActive = nullptr;
199 // chain up
200 TrafficGenerator::DoDispose();
201}
202
203void
204TrafficGeneratorNgmnVoip::DoInitialize()
205{
206 NS_LOG_FUNCTION(this);
207 m_fromActiveToInactive = CreateObject<UniformRandomVariable>();
208 m_fromActiveToInactive->SetAttribute("Min", DoubleValue(0));
209 m_fromActiveToInactive->SetAttribute("Max", DoubleValue(1));
210 m_fromInactiveToActive = CreateObject<UniformRandomVariable>();
211 m_fromInactiveToActive->SetAttribute("Min", DoubleValue(0));
212 m_fromInactiveToActive->SetAttribute("Max", DoubleValue(1));
213 // chain up
214 TrafficGenerator::DoInitialize();
215}
216
217int64_t
219{
220 m_fromActiveToInactive->SetStream(stream);
221 m_fromInactiveToActive->SetStream(stream + 1);
222
223 return 2;
224}
225
226} // Namespace ns3
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.
int64_t AssignStreams(int64_t stream) override
static TypeId GetTypeId()
Get the type ID.