A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tgax-video-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006,2007 INRIA
3 * Copyright (c) 2016 Magister Solutions
4 * Copyright (c) 2025 Tom Henderson
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 * Borrows from examples/wireless/wifi-ap.cc for overall Wi-Fi network setup pattern
9 * Borrows from src/applications/examples/three-gpp-http-example.cc for overall simulation flow
10 */
11
12/**
13 * @file tgax-video-example.cc
14 * @brief A simple buffered video streaming traffic generator example over Wi-Fi
15 *
16 * This example demonstrates how to set up a basic ns-3 simulation with video streaming
17 * traffic over a Wi-Fi network. It includes support for:
18 *
19 * 1. Buffered video streaming (BV1-BV6): Different bit rates from 2Mbps to 15.6Mbps
20 * 2. Multicast video streaming (MC1-MC2): 3Mbps and 6Mbps multicast
21 * 3. Custom video parameters: User-defined Weibull parameters and bit rate
22 *
23 * The simulation consists of:
24 * - A simple Wi-Fi network with one AP (Access Point) and one or more STAs (Stations)
25 * - Video traffic flowing from the AP to the STA(s)
26 * - Application-level tracing to observe video frames being generated and sent
27 *
28 * The video traffic model follows IEEE 802.11-14/0571r12 TGAX evaluation methodology:
29 * - Video frame sizes are drawn from a Weibull distribution
30 * - Network latency is modeled using a Gamma distribution (mean ~14.8ms)
31 * - Multiple predefined profiles for different video quality levels
32 *
33 * To run with default settings (BV1, 2Mbps): ./ns3 run tgax-video-example
34 * To run with higher bit rate: ./ns3 run "tgax-video-example --model=BV3"
35 * To run with multicast: ./ns3 run "tgax-video-example --model=MC1"
36 * To run with TCP: ./ns3 run "tgax-video-example --protocol=tcp"
37 *
38 * To disable verbose logging: ./ns3 run "tgax-video-example --verbose=false"
39 */
40
41#include "ns3/applications-module.h"
42#include "ns3/core-module.h"
43#include "ns3/internet-module.h"
44#include "ns3/mobility-module.h"
45#include "ns3/network-module.h"
46#include "ns3/wifi-module.h"
47
48using namespace ns3;
49
50NS_LOG_COMPONENT_DEFINE("TgaxVideoExample");
51
52/**
53 * Callback invoked when a video packet is transmitted
54 * @param packet The transmitted packet
55 * @param latency The network latency applied to the packet
56 */
57void VideoPacketSent(Ptr<const Packet> packet, Time latency);
58
59/**
60 * Callback invoked when a video frame is generated
61 * @param frameSize The size of the generated frame in bytes
62 */
63void VideoFrameGenerated(uint32_t frameSize);
64
65/**
66 * Callback invoked when the PacketSink receives a packet
67 * @param context The context string
68 * @param packet The received packet
69 * @param address The sender's address
70 */
71void PacketReceived(std::string context, Ptr<const Packet> packet, const Address& address);
72
73int
74main(int argc, char* argv[])
75{
76 Time duration{Seconds(10)}; // Simulation time in seconds
77 std::string model{"BV1"}; // Traffic model: BV1-BV6, MC1-MC2, or Custom
78 std::string protocol{"udp"}; // Protocol: udp or tcp
79 bool verbose = true; // Enable/disable verbose logging
80
81 CommandLine cmd(__FILE__);
82 cmd.Usage("Buffered video streaming example");
83 cmd.AddValue("duration", "Duration of traffic flow, in seconds", duration);
84 cmd.AddValue("model",
85 "Traffic model to use (BV1, BV2, BV3, BV4, BV5, BV6, MC1, MC2, or Custom). "
86 "Default: BV1",
87 model);
88 cmd.AddValue("protocol", "Protocol to use (udp or tcp). Default: udp", protocol);
89 cmd.AddValue("verbose",
90 "Enable verbose logging of TgaxVideoTraffic, PacketSink, and this program",
91 verbose);
92 cmd.Parse(argc, argv);
93
94 if (verbose)
95 {
97 "TgaxVideoExample",
100 "TgaxVideoTraffic",
103 "PacketSink",
105 }
106
107 // Determine if this is a multicast scenario
108 bool isMulticast = (model == "MC1" || model == "MC2");
109 uint32_t numStations = isMulticast ? 2 : 1;
110
111 NodeContainer wifiNodes;
112 wifiNodes.Create(1 + numStations); // AP + STAs
113
114 Ptr<Node> apNode = wifiNodes.Get(0);
115 NodeContainer staNodes;
116 for (uint32_t i = 0; i < numStations; ++i)
117 {
118 staNodes.Add(wifiNodes.Get(1 + i));
119 }
120
123 phy.SetChannel(channel.Create());
124
127 wifi.SetStandard(WIFI_STANDARD_80211ax);
128
131
132 // Configure AP
133 Ssid ssid = Ssid("video-network");
134 mac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
135 apDevices = wifi.Install(phy, mac, apNode);
136
137 // Configure STAs
138 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
139 staDevices = wifi.Install(phy, mac, staNodes);
140
142 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
143 "MinX",
144 DoubleValue(0.0),
145 "MinY",
146 DoubleValue(0.0),
147 "DeltaX",
148 DoubleValue(5.0),
149 "DeltaY",
150 DoubleValue(5.0),
151 "GridWidth",
152 UintegerValue(3),
153 "LayoutType",
154 StringValue("RowFirst"));
155 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
156 mobility.Install(wifiNodes);
157
159 internet.Install(wifiNodes);
160
162 ipv4.SetBase("10.1.1.0", "255.255.255.0");
163
164 NetDeviceContainer allDevices;
165 allDevices.Add(apDevices);
166 allDevices.Add(staDevices);
167 Ipv4InterfaceContainer ipv4Interfaces = ipv4.Assign(allDevices);
168
169 NS_LOG_INFO("AP address: " << ipv4Interfaces.GetAddress(0));
170 for (uint32_t i = 0; i < numStations; ++i)
171 {
172 NS_LOG_INFO("STA " << i << " address: " << ipv4Interfaces.GetAddress(1 + i));
173 }
174
175 std::string protocolFactory;
176 if (protocol == "udp")
177 {
178 protocolFactory = "ns3::UdpSocketFactory";
179 }
180 else if (protocol == "tcp")
181 {
182 protocolFactory = "ns3::TcpSocketFactory";
183 }
184 else
185 {
186 NS_FATAL_ERROR("Invalid protocol: " << protocol << ". Use 'udp' or 'tcp'.");
187 }
188
189 uint16_t remotePort = 5000;
190 std::string remoteAddress;
191 if (isMulticast)
192 {
193 // For multicast, use a multicast address
194 remoteAddress = "239.192.100.1";
195
196 // Add multicast route on AP
197 Ipv4StaticRoutingHelper staticRouting;
198 Ptr<Ipv4> ipv4Proto = apNode->GetObject<Ipv4>();
199 Ptr<Ipv4StaticRouting> routing = staticRouting.GetStaticRouting(ipv4Proto);
200 routing->AddHostRouteTo(Ipv4Address(remoteAddress.c_str()),
201 ipv4Proto->GetInterfaceForDevice(apNode->GetDevice(0)),
202 0);
203 }
204 else
205 {
206 // For unicast, use the first STA's address
207 remoteAddress = "10.1.1.2";
208 }
209
211
212 // Configure video parameters based on model
213 // Traffic models from IEEE 802.11-14/0571r12:
214 // BV1: 2Mbps, BV2: 4Mbps, BV3: 6Mbps, BV4: 8Mbps, BV5: 10Mbps, BV6: 15.6Mbps
215 // MC1: 3Mbps multicast, MC2: 6Mbps multicast
216 sourceHelper.SetAttribute("Protocol", TypeIdValue(TypeId::LookupByName(protocolFactory)));
217 sourceHelper.SetAttribute("TrafficModelClassIdentifier", StringValue(model));
218
219 Address remoteAddr = InetSocketAddress(Ipv4Address(remoteAddress.c_str()), remotePort);
220 sourceHelper.SetAttribute("Remote", AddressValue(remoteAddr));
221
222 // Install video source on AP node (downlink traffic)
223 auto sourceApps = sourceHelper.Install(apNode);
224 sourceApps.Start(Seconds(1.0));
225 sourceApps.Stop(Seconds(1.0) + duration);
226
227 // Install packet sinks on STAs
228 PacketSinkHelper sinkHelper(protocolFactory,
230 auto sinkApps = sinkHelper.Install(staNodes);
231 sinkApps.Start(Seconds(0.0));
232 sinkApps.Stop(Seconds(2.0) + duration);
233
234 NS_LOG_INFO("PacketSink application installed on STA node(s)");
235
236 // Connect to video TX with latency trace
238 "/NodeList/*/ApplicationList/*/$ns3::TgaxVideoTraffic/TxWithLatency",
240
241 // Connect to video frame generation trace
243 "/NodeList/*/ApplicationList/*/$ns3::TgaxVideoTraffic/VideoFrameGenerated",
245
246 // Connect to RX trace
247 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
249
250 // Get expected bit rate for the model
251 std::map<std::string, double> modelBitRates = {{"BV1", 2.0},
252 {"BV2", 4.0},
253 {"BV3", 6.0},
254 {"BV4", 8.0},
255 {"BV5", 10.0},
256 {"BV6", 15.6},
257 {"MC1", 3.0},
258 {"MC2", 6.0},
259 {"Custom", 2.0}};
260
261 double expectedBitRate = modelBitRates.count(model) ? modelBitRates[model] : 2.0;
262
263 NS_LOG_INFO("Starting simulation for traffic duration: " << duration.As(Time::S));
264 NS_LOG_INFO("Traffic model: " << model);
265 NS_LOG_INFO("Protocol: " << protocol);
266 NS_LOG_INFO("Expected bit rate: " << expectedBitRate << " Mbps");
267 if (isMulticast)
268 {
269 NS_LOG_INFO("Multicast mode: delivering to " << numStations << " receivers");
270 }
271
272 Simulator::Stop(Seconds(2) + duration);
274
275 // Get statistics from packet sink applications
276 uint64_t totalBytesReceived = 0;
277 for (uint32_t i = 0; i < sinkApps.GetN(); ++i)
278 {
280 if (sink)
281 {
282 uint32_t bytesRx = sink->GetTotalRx();
283 NS_LOG_INFO("STA " << i << " received: " << bytesRx << " bytes");
284 totalBytesReceived += bytesRx;
285 }
286 }
287
288 // Calculate approximate expected bytes
289 // expectedBitRate is in Mbps, duration is in seconds
290 double expectedBytes = (expectedBitRate * 1e6 / 8) * duration.GetSeconds();
291 NS_LOG_INFO("Total bytes received: " << totalBytesReceived);
292 NS_LOG_INFO("Approximate expected bytes per receiver: " << expectedBytes);
293
294 // Calculate measured bit rate
295 double measuredBitRate =
296 static_cast<double>(totalBytesReceived * 8) / (duration.GetSeconds() * 1e6);
297 if (isMulticast)
298 {
299 measuredBitRate /= numStations; // Per-receiver rate
300 }
301 NS_LOG_INFO("Measured bit rate: " << measuredBitRate << " Mbps");
302
304
305 return 0;
306}
307
308void
310{
311 NS_LOG_INFO("Video TX: Packet size (bytes): " << packet->GetSize()
312 << " Latency: " << latency.As(Time::US));
313}
314
315void
317{
318 NS_LOG_INFO("Video Frame Generated: " << frameSize << " bytes");
319}
320
321void
322PacketReceived(std::string context, Ptr<const Packet> packet, const Address& address)
323{
324 NS_LOG_INFO("Packet RX: Size(bytes): " << packet->GetSize());
325}
a polymophic address class
Definition address.h:114
AttributeValue implementation for Address.
Definition address.h:329
A helper to make it easier to instantiate an application on a set of nodes.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class that adds ns3::Ipv4StaticRouting objects.
Ptr< Ipv4StaticRouting > GetStaticRouting(Ptr< Ipv4 > ipv4) const
Try and find the static routing protocol as either the main routing protocol or in the list of routin...
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
AttributeValue implementation for Ssid.
Definition ssid.h:85
Hold variables of type string.
Definition string.h:45
static TypeId GetTypeId()
Get the type ID.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:408
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:398
@ US
microsecond
Definition nstime.h:108
@ S
second
Definition nstime.h:106
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:870
AttributeValue implementation for TypeId.
Definition type-id.h:649
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:970
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:946
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1273
@ WIFI_STANDARD_80211ax
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:279
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:605
LogLevel
Logging severity classes and levels.
Definition log.h:86
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:110
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:111
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:112
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:96
staDevices
Definition third.py:87
ssid
Definition third.py:82
channel
Definition third.py:77
mac
Definition third.py:81
wifi
Definition third.py:84
apDevices
Definition third.py:90
mobility
Definition third.py:92
phy
Definition third.py:78
bool verbose
void PacketReceived(std::string context, Ptr< const Packet > packet, const Address &address)
Callback invoked when the PacketSink receives a packet.
void VideoFrameGenerated(uint32_t frameSize)
Callback invoked when a video frame is generated.
void PacketReceived(std::string context, Ptr< const Packet > packet, const Address &address)
Callback invoked when the PacketSink receives a packet.
void VideoPacketSent(Ptr< const Packet > packet, Time latency)
Callback invoked when a video packet is transmitted.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44