INET Framework for OMNeT++/OMNEST
inet::SimpleVoipSender Class Reference

Implements a simple VoIP source. More...

#include <SimpleVoipSender.h>

Inheritance diagram for inet::SimpleVoipSender:
inet::LifecycleUnsupported inet::ILifecycle

Public Member Functions

virtual ~SimpleVoipSender ()
 
 SimpleVoipSender ()
 
- Public Member Functions inherited from inet::LifecycleUnsupported
virtual bool handleOperationStage (LifecycleOperation *operation, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Member Functions

void talkspurt (simtime_t dur)
 
void selectTalkOrSilenceInterval ()
 
void sendVoIPPacket ()
 
virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 

Private Attributes

UdpSocket socket
 
simtime_t stopTime
 
simtime_t packetizationInterval
 
int localPort = -1
 
int destPort = -1
 
int talkPacketSize = 0
 
L3Address destAddress
 
cMessage * selfSender = nullptr
 
cMessage * selfSource = nullptr
 
simtime_t silenceDuration
 
simtime_t talkspurtDuration
 
int packetID = -1
 
int talkspurtID = -1
 
int talkspurtNumPackets = 0
 
bool isTalk = false
 

Detailed Description

Implements a simple VoIP source.

See the NED file for more information.

Constructor & Destructor Documentation

◆ ~SimpleVoipSender()

inet::SimpleVoipSender::~SimpleVoipSender ( )
virtual
25 {
26  cancelAndDelete(selfSender);
27  cancelAndDelete(selfSource);
28 }

◆ SimpleVoipSender()

inet::SimpleVoipSender::SimpleVoipSender ( )
21 {
22 }

Member Function Documentation

◆ handleMessage()

void inet::SimpleVoipSender::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
75 {
76  if (msg->isSelfMessage()) {
77  if (msg == selfSender)
79  else
81  }
82  else
83  throw cRuntimeError("Unknown incoming message: '%s' on gate '%s'", msg->getClassName(), msg->getArrivalGate()->getFullName());
84 }

◆ initialize()

void inet::SimpleVoipSender::initialize ( int  stage)
overrideprotectedvirtual
31 {
32  EV_TRACE << "VoIP Sender initialize: stage " << stage << endl;
33 
34  cSimpleModule::initialize(stage);
35 
36  // avoid multiple initializations
37  if (stage == INITSTAGE_LOCAL) {
39  silenceDuration = 0;
40  selfSource = new cMessage("selfSource");
41  isTalk = false;
42  talkspurtID = 0;
44  packetID = 0;
45  talkPacketSize = par("talkPacketSize");
46  packetizationInterval = par("packetizationInterval");
47  selfSender = new cMessage("selfSender");
48  localPort = par("localPort");
49  destPort = par("destPort");
50  }
51  else if (stage == INITSTAGE_APPLICATION_LAYER) {
52  cModule *node = findContainingNode(this);
53  NodeStatus *nodeStatus = node ? check_and_cast_nullable<NodeStatus *>(node->getSubmodule("status")) : nullptr;
54  bool isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
55  if (!isOperational)
56  throw cRuntimeError("This module doesn't support starting in node DOWN state");
57 
58  socket.setOutputGate(gate("socketOut"));
60 
61  EV_INFO << "VoIPSender::initialize - binding to port: local:" << localPort << " , dest:" << destPort << endl;
62 
63  // calculating traffic starting time
64  simtime_t startTime(par("startTime"));
65  stopTime = par("stopTime");
66  if (stopTime >= SIMTIME_ZERO && stopTime < startTime)
67  throw cRuntimeError("Invalid startTime/stopTime settings: startTime %g s greater than stopTime %g s", SIMTIME_DBL(startTime), SIMTIME_DBL(stopTime));
68 
69  scheduleAt(startTime, selfSource);
70  EV_INFO << "\t starting traffic in " << startTime << " s" << endl;
71  }
72 }

◆ numInitStages()

virtual int inet::SimpleVoipSender::numInitStages ( ) const
inlineoverrideprotectedvirtual
50 { return NUM_INIT_STAGES; }

◆ selectTalkOrSilenceInterval()

void inet::SimpleVoipSender::selectTalkOrSilenceInterval ( )
protected
107 {
108  simtime_t now = simTime();
109  if (stopTime >= SIMTIME_ZERO && now >= stopTime)
110  return;
111 
112  if (isTalk) {
113  silenceDuration = par("silenceDuration");
114  EV_DEBUG << "SILENCE: " << "Duration: " << silenceDuration << " seconds\n\n";
115  simtime_t endSilence = now + silenceDuration;
116  if (stopTime >= SIMTIME_ZERO && endSilence > stopTime)
117  endSilence = stopTime;
118  scheduleAt(endSilence, selfSource);
119  isTalk = false;
120  }
121  else {
122  talkspurtDuration = par("talkspurtDuration");
123  EV_DEBUG << "TALKSPURT: " << talkspurtID << " Duration: " << talkspurtDuration << " seconds\n\n";
124  simtime_t endTalk = now + talkspurtDuration;
125  if (stopTime >= SIMTIME_ZERO && endTalk > stopTime) {
126  endTalk = stopTime;
127  talkspurtDuration = stopTime - now;
128  }
130  scheduleAt(endTalk, selfSource);
131  isTalk = true;
132  }
133 }

Referenced by handleMessage().

◆ sendVoIPPacket()

void inet::SimpleVoipSender::sendVoIPPacket ( )
protected
136 {
138  destAddress = L3AddressResolver().resolve(par("destAddress"));
139 
140  Packet *packet = new Packet("VoIP");
141  const auto& voice = makeShared<SimpleVoipPacket>();
142  voice->setTalkspurtID(talkspurtID - 1);
143  voice->setTalkspurtNumPackets(talkspurtNumPackets);
144  voice->setPacketID(packetID);
145  voice->setVoipTimestamp(simTime() - packetizationInterval); // start time of voice in this packet
146  voice->setVoiceDuration(packetizationInterval);
147  voice->setTotalLengthField(talkPacketSize);
148  voice->setChunkLength(B(talkPacketSize));
149  packet->insertAtBack(voice);
150 
151  EV_INFO << "TALKSPURT " << talkspurtID - 1 << " sending packet " << packetID << "\n";
152 
153  socket.sendTo(packet, destAddress, destPort);
154  ++packetID;
155 
157  scheduleAfter(packetizationInterval, selfSender);
158 }

Referenced by handleMessage().

◆ talkspurt()

void inet::SimpleVoipSender::talkspurt ( simtime_t  dur)
protected
87 {
88  simtime_t curTime = simTime();
89  simtime_t startTime = curTime;
90  if (selfSender->isScheduled()) {
91  // silence was too short, detected overlapping talkspurts
92  simtime_t delta = selfSender->getArrivalTime() - curTime;
93  startTime += delta;
94  dur -= SIMTIME_DBL(delta);
95  cancelEvent(selfSender);
96  }
97 
98  talkspurtID++;
99  packetID = 0;
101  EV_DEBUG << "TALKSPURT " << talkspurtID - 1 << " will be sent " << talkspurtNumPackets << " packets\n\n";
102 
103  scheduleAt(startTime + packetizationInterval, selfSender);
104 }

Referenced by selectTalkOrSilenceInterval().

Member Data Documentation

◆ destAddress

L3Address inet::SimpleVoipSender::destAddress
private

Referenced by sendVoIPPacket().

◆ destPort

int inet::SimpleVoipSender::destPort = -1
private

Referenced by initialize(), and sendVoIPPacket().

◆ isTalk

bool inet::SimpleVoipSender::isTalk = false
private

◆ localPort

int inet::SimpleVoipSender::localPort = -1
private

Referenced by initialize().

◆ packetID

int inet::SimpleVoipSender::packetID = -1
private

◆ packetizationInterval

simtime_t inet::SimpleVoipSender::packetizationInterval
private

◆ selfSender

cMessage* inet::SimpleVoipSender::selfSender = nullptr
private

◆ selfSource

cMessage* inet::SimpleVoipSender::selfSource = nullptr
private

◆ silenceDuration

simtime_t inet::SimpleVoipSender::silenceDuration
private

◆ socket

UdpSocket inet::SimpleVoipSender::socket
private

Referenced by initialize(), and sendVoIPPacket().

◆ stopTime

simtime_t inet::SimpleVoipSender::stopTime
private

◆ talkPacketSize

int inet::SimpleVoipSender::talkPacketSize = 0
private

Referenced by initialize(), and sendVoIPPacket().

◆ talkspurtDuration

simtime_t inet::SimpleVoipSender::talkspurtDuration
private

◆ talkspurtID

int inet::SimpleVoipSender::talkspurtID = -1
private

◆ talkspurtNumPackets

int inet::SimpleVoipSender::talkspurtNumPackets = 0
private

The documentation for this class was generated from the following files:
inet::UdpSocket::setOutputGate
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UdpSocket.h:117
inet::findContainingNode
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:31
inet::SimpleVoipSender::destPort
int destPort
Definition: SimpleVoipSender.h:31
inet::SimpleVoipSender::packetID
int packetID
Definition: SimpleVoipSender.h:40
inet::UdpSocket::bind
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
inet::SimpleVoipSender::localPort
int localPort
Definition: SimpleVoipSender.h:30
inet::UdpSocket::sendTo
void sendTo(Packet *msg, L3Address destAddr, int destPort)
Sends a data packet to the given address and port.
Definition: UdpSocket.cc:69
inet::SimpleVoipSender::selfSender
cMessage * selfSender
Definition: SimpleVoipSender.h:36
inet::SimpleVoipSender::talkspurtNumPackets
int talkspurtNumPackets
Definition: SimpleVoipSender.h:42
inet::SimpleVoipSender::stopTime
simtime_t stopTime
Definition: SimpleVoipSender.h:28
inet::SimpleVoipSender::selfSource
cMessage * selfSource
Definition: SimpleVoipSender.h:37
inet::L3Address::isUnspecified
bool isUnspecified() const
Definition: L3Address.cc:138
inet::SimpleVoipSender::packetizationInterval
simtime_t packetizationInterval
Definition: SimpleVoipSender.h:29
inet::SimpleVoipSender::isTalk
bool isTalk
Definition: SimpleVoipSender.h:43
inet::units::units::B
intscale< b, 1, 8 > B
Definition: Units.h:1168
inet::SimpleVoipSender::talkPacketSize
int talkPacketSize
Definition: SimpleVoipSender.h:32
inet::SimpleVoipSender::talkspurtID
int talkspurtID
Definition: SimpleVoipSender.h:41
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::SimpleVoipSender::talkspurt
void talkspurt(simtime_t dur)
Definition: SimpleVoipSender.cc:86
inet::SimpleVoipSender::talkspurtDuration
simtime_t talkspurtDuration
Definition: SimpleVoipSender.h:39
inet::SimpleVoipSender::silenceDuration
simtime_t silenceDuration
Definition: SimpleVoipSender.h:38
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::SimpleVoipSender::socket
UdpSocket socket
Definition: SimpleVoipSender.h:25
inet::INITSTAGE_APPLICATION_LAYER
INET_API InitStage INITSTAGE_APPLICATION_LAYER
Initialization of applications.
inet::SimpleVoipSender::sendVoIPPacket
void sendVoIPPacket()
Definition: SimpleVoipSender.cc:135
inet::NodeStatus::UP
@ UP
Definition: NodeStatus.h:28
inet::SimpleVoipSender::destAddress
L3Address destAddress
Definition: SimpleVoipSender.h:33
inet::SimpleVoipSender::selectTalkOrSilenceInterval
void selectTalkOrSilenceInterval()
Definition: SimpleVoipSender.cc:106