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

#include <VoIPStreamReceiver.h>

Inheritance diagram for inet::VoIPStreamReceiver:
inet::ILifecycle

Classes

class  Connection
 

Public Member Functions

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

Protected Member Functions

virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void finish () override
 
virtual void createConnection (VoIPStreamPacket *vp)
 
virtual void checkSourceAndParameters (VoIPStreamPacket *vp)
 
virtual void closeConnection ()
 
virtual void decodePacket (VoIPStreamPacket *vp)
 

Protected Attributes

int localPort = -1
 
simtime_t playoutDelay
 
const char * resultFile = nullptr
 
UDPSocket socket
 
Connection curConn
 

Static Protected Attributes

static simsignal_t rcvdPkSignal = registerSignal("rcvdPk")
 
static simsignal_t dropPkSignal = registerSignal("dropPk")
 
static simsignal_t lostSamplesSignal = registerSignal("lostSamples")
 
static simsignal_t lostPacketsSignal = registerSignal("lostPackets")
 
static simsignal_t packetHasVoiceSignal = registerSignal("packetHasVoice")
 
static simsignal_t connStateSignal = registerSignal("connState")
 
static simsignal_t delaySignal = registerSignal("delay")
 

Constructor & Destructor Documentation

inet::VoIPStreamReceiver::VoIPStreamReceiver ( )
inline
51 {}
inet::VoIPStreamReceiver::~VoIPStreamReceiver ( )
38 {
40 }
virtual void closeConnection()
Definition: VoIPStreamReceiver.cc:201

Member Function Documentation

void inet::VoIPStreamReceiver::checkSourceAndParameters ( VoIPStreamPacket *  vp)
protectedvirtual

Referenced by handleMessage().

181 {
182  ASSERT(!curConn.offline);
183 
184  UDPDataIndication *udpCtrl = check_and_cast<UDPDataIndication *>(vp->getControlInfo());
185  if (curConn.srcAddr != udpCtrl->getSrcAddr()
186  || curConn.srcPort != udpCtrl->getSrcPort()
187  || curConn.destAddr != udpCtrl->getDestAddr()
188  || curConn.destPort != udpCtrl->getDestPort()
189  || vp->getSsrc() != curConn.ssrc)
190  throw cRuntimeError("Voice packet received from third party during a voice session (concurrent voice sessions not supported)");
191 
192  if (vp->getCodec() != curConn.codec
193  || vp->getSampleBits() != curConn.sampleBits
194  || vp->getSampleRate() != curConn.sampleRate
195  || vp->getSamplesPerPacket() != curConn.samplesPerPacket
196  || vp->getTransmitBitrate() != curConn.transmitBitrate
197  )
198  throw cRuntimeError("Cannot change voice encoding parameters a during session");
199 }
int srcPort
Definition: VoIPStreamReceiver.h:95
enum AVCodecID codec
Definition: VoIPStreamReceiver.h:82
int samplesPerPacket
Definition: VoIPStreamReceiver.h:85
int sampleRate
Definition: VoIPStreamReceiver.h:84
bool offline
Definition: VoIPStreamReceiver.h:78
int transmitBitrate
Definition: VoIPStreamReceiver.h:86
L3Address destAddr
Definition: VoIPStreamReceiver.h:96
Connection curConn
Definition: VoIPStreamReceiver.h:107
short sampleBits
Definition: VoIPStreamReceiver.h:83
L3Address srcAddr
Definition: VoIPStreamReceiver.h:94
uint32_t ssrc
Definition: VoIPStreamReceiver.h:81
int destPort
Definition: VoIPStreamReceiver.h:97
void inet::VoIPStreamReceiver::closeConnection ( )
protectedvirtual

Referenced by finish(), and ~VoIPStreamReceiver().

202 {
203  if (!curConn.offline) {
204  curConn.offline = true;
205  avcodec_close(curConn.decCtx);
206  avcodec_free_context(&curConn.decCtx);
208  emit(connStateSignal, -1L); // so that sum() yields the number of active sessions
209  }
210 }
bool offline
Definition: VoIPStreamReceiver.h:78
bool close()
Definition: AudioOutFile.cc:149
AVCodecContext * decCtx
Definition: VoIPStreamReceiver.h:91
Connection curConn
Definition: VoIPStreamReceiver.h:107
static simsignal_t connStateSignal
Definition: VoIPStreamReceiver.h:114
AudioOutFile outFile
Definition: VoIPStreamReceiver.h:93
void inet::VoIPStreamReceiver::createConnection ( VoIPStreamPacket *  vp)
protectedvirtual

Referenced by handleMessage().

142 {
143  ASSERT(curConn.offline);
144 
145  UDPDataIndication *udpCtrl = check_and_cast<UDPDataIndication *>(vp->getControlInfo());
146 
147  curConn.srcAddr = udpCtrl->getSrcAddr();
148  curConn.srcPort = udpCtrl->getSrcPort();
149  curConn.destAddr = udpCtrl->getDestAddr();
150  curConn.destPort = udpCtrl->getDestPort();
151  curConn.seqNo = vp->getSeqNo() - 1;
152  curConn.timeStamp = vp->getTimeStamp();
153  curConn.ssrc = vp->getSsrc();
154  curConn.codec = (enum AVCodecID)(vp->getCodec());
155  curConn.sampleBits = vp->getSampleBits();
156  curConn.sampleRate = vp->getSampleRate();
157  curConn.transmitBitrate = vp->getTransmitBitrate();
158  curConn.samplesPerPacket = vp->getSamplesPerPacket();
159  curConn.lastPacketFinish = simTime() + playoutDelay;
160 
161  curConn.pCodecDec = avcodec_find_decoder(curConn.codec);
162  if (curConn.pCodecDec == nullptr)
163  throw cRuntimeError("Codec %d not found", curConn.codec);
164 
165  curConn.decCtx = avcodec_alloc_context3(curConn.pCodecDec);
167  curConn.decCtx->sample_rate = curConn.sampleRate;
168  curConn.decCtx->channels = 1;
169  curConn.decCtx->bits_per_coded_sample = curConn.sampleBits;
170 
171  int ret = avcodec_open2(curConn.decCtx, curConn.pCodecDec, nullptr);
172  if (ret < 0)
173  throw cRuntimeError("could not open decoding codec %d (%s): err=%d", curConn.codec, curConn.pCodecDec->name, ret);
174 
176  curConn.offline = false;
177  emit(connStateSignal, 1);
178 }
int srcPort
Definition: VoIPStreamReceiver.h:95
enum AVCodecID codec
Definition: VoIPStreamReceiver.h:82
int samplesPerPacket
Definition: VoIPStreamReceiver.h:85
int sampleRate
Definition: VoIPStreamReceiver.h:84
bool offline
Definition: VoIPStreamReceiver.h:78
int transmitBitrate
Definition: VoIPStreamReceiver.h:86
simtime_t lastPacketFinish
Definition: VoIPStreamReceiver.h:87
const char * resultFile
Definition: VoIPStreamReceiver.h:103
uint32_t timeStamp
Definition: VoIPStreamReceiver.h:80
AVCodecContext * decCtx
Definition: VoIPStreamReceiver.h:91
void openAudio(const char *fileName)
Definition: VoIPStreamReceiver.cc:99
L3Address destAddr
Definition: VoIPStreamReceiver.h:96
simtime_t playoutDelay
Definition: VoIPStreamReceiver.h:102
Connection curConn
Definition: VoIPStreamReceiver.h:107
short sampleBits
Definition: VoIPStreamReceiver.h:83
uint16_t seqNo
Definition: VoIPStreamReceiver.h:79
static simsignal_t connStateSignal
Definition: VoIPStreamReceiver.h:114
AVCodec * pCodecDec
Definition: VoIPStreamReceiver.h:92
L3Address srcAddr
Definition: VoIPStreamReceiver.h:94
uint32_t ssrc
Definition: VoIPStreamReceiver.h:81
int destPort
Definition: VoIPStreamReceiver.h:97
void inet::VoIPStreamReceiver::decodePacket ( VoIPStreamPacket *  vp)
protectedvirtual

Referenced by handleMessage().

213 {
214  switch (vp->getType()) {
215  case VOICE:
216  emit(packetHasVoiceSignal, 1);
217  break;
218 
219  case SILENCE:
220  emit(packetHasVoiceSignal, 0);
221  break;
222 
223  default:
224  throw cRuntimeError("The received VoIPStreamPacket has unknown type %d", vp->getType());
225  }
226  uint16_t newSeqNo = vp->getSeqNo();
227  if (newSeqNo > curConn.seqNo + 1)
228  emit(lostPacketsSignal, newSeqNo - (curConn.seqNo + 1));
229 
230  if (simTime() > curConn.lastPacketFinish) {
231  int lostSamples = ceil(SIMTIME_DBL((simTime() - curConn.lastPacketFinish) * curConn.sampleRate));
232  ASSERT(lostSamples > 0);
233  EV_INFO << "Lost " << lostSamples << " samples\n";
234  emit(lostSamplesSignal, lostSamples);
235  curConn.writeLostSamples(lostSamples);
236  curConn.lastPacketFinish += lostSamples * (1.0 / curConn.sampleRate);
237  FINGERPRINT_ADD_EXTRA_DATA(lostSamples);
238  }
239  emit(delaySignal, curConn.lastPacketFinish - vp->getCreationTime());
240  curConn.seqNo = newSeqNo;
241 
242  int len = vp->getBytes().getDataArraySize();
243  uint8_t buff[len];
244  vp->getBytes().copyDataToBuffer(buff, len);
245  curConn.writeAudioFrame(buff, len);
246  FINGERPRINT_ADD_EXTRA_DATA2((const char *)buff, len);
247 }
static simsignal_t lostPacketsSignal
Definition: VoIPStreamReceiver.h:112
static simsignal_t delaySignal
Definition: VoIPStreamReceiver.h:115
static simsignal_t packetHasVoiceSignal
Definition: VoIPStreamReceiver.h:113
int sampleRate
Definition: VoIPStreamReceiver.h:84
uint16_t len
Definition: TCP_NSC.cc:85
void writeAudioFrame(uint8_t *buf, int len)
Definition: VoIPStreamReceiver.cc:114
simtime_t lastPacketFinish
Definition: VoIPStreamReceiver.h:87
Connection curConn
Definition: VoIPStreamReceiver.h:107
uint16_t seqNo
Definition: VoIPStreamReceiver.h:79
#define FINGERPRINT_ADD_EXTRA_DATA2(x, y)
Definition: INETDefs.h:88
void writeLostSamples(int sampleCount)
Definition: VoIPStreamReceiver.cc:104
#define FINGERPRINT_ADD_EXTRA_DATA(x)
Definition: INETDefs.h:87
static simsignal_t lostSamplesSignal
Definition: VoIPStreamReceiver.h:111
void inet::VoIPStreamReceiver::finish ( )
overrideprotectedvirtual
250 {
251  EV_TRACE << "Sink finish()" << endl;
252  closeConnection();
253 }
virtual void closeConnection()
Definition: VoIPStreamReceiver.cc:201
void inet::VoIPStreamReceiver::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
74 {
75  if (msg->getKind() == UDP_I_ERROR) {
76  delete msg;
77  return;
78  }
79 
80  VoIPStreamPacket *vp = check_and_cast<VoIPStreamPacket *>(msg);
81  bool ok = true;
82  if (curConn.offline)
83  createConnection(vp);
84  else {
86  ok = vp->getSeqNo() > curConn.seqNo && vp->getTimeStamp() > curConn.timeStamp;
87  }
88 
89  if (ok) {
90  emit(rcvdPkSignal, vp);
91  decodePacket(vp);
92  }
93  else
94  emit(dropPkSignal, msg);
95 
96  delete msg;
97 }
bool offline
Definition: VoIPStreamReceiver.h:78
uint32_t timeStamp
Definition: VoIPStreamReceiver.h:80
Definition: UDPControlInfo_m.h:84
virtual void checkSourceAndParameters(VoIPStreamPacket *vp)
Definition: VoIPStreamReceiver.cc:180
Connection curConn
Definition: VoIPStreamReceiver.h:107
static simsignal_t dropPkSignal
Definition: VoIPStreamReceiver.h:110
static simsignal_t rcvdPkSignal
Definition: VoIPStreamReceiver.h:109
uint16_t seqNo
Definition: VoIPStreamReceiver.h:79
virtual void decodePacket(VoIPStreamPacket *vp)
Definition: VoIPStreamReceiver.cc:212
virtual void createConnection(VoIPStreamPacket *vp)
Definition: VoIPStreamReceiver.cc:141
bool inet::VoIPStreamReceiver::handleOperationStage ( LifecycleOperation operation,
int  stage,
IDoneCallback doneCallback 
)
overridevirtual

Perform one stage of a lifecycle operation.

Processing may be done entirely within this method, or may be a longer process that involves nonzero simulation time or several events, and is triggered by this method call.

Return value: true = "done"; false = "not yet done, will invoke doneCallback when done"

Implements inet::ILifecycle.

256 {
257  Enter_Method_Silent();
258  throw cRuntimeError("Unsupported lifecycle operation '%s'", operation->getClassName());
259  //return true;
260 }
void inet::VoIPStreamReceiver::initialize ( int  stage)
overrideprotectedvirtual
43 {
44  cSimpleModule::initialize(stage);
45 
46  if (stage == INITSTAGE_LOCAL) {
47  // Hack for create results folder
48  recordScalar("hackForCreateResultsFolder", 0);
49 
50  // Say Hello to the world
51  EV_TRACE << "VoIPSinkApp initialize()" << endl;
52 
53  // read parameters
54  localPort = par("localPort");
55  resultFile = par("resultFile");
56  playoutDelay = par("playoutDelay");
57 
58  // initialize avcodec library
59  av_register_all();
60  }
61  else if (stage == INITSTAGE_APPLICATION_LAYER) {
62  bool isOperational;
63  NodeStatus *nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
64  isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
65  if (!isOperational)
66  throw cRuntimeError("This module doesn't support starting in node DOWN state");
67 
68  socket.setOutputGate(gate("udpOut"));
70  }
71 }
const char * resultFile
Definition: VoIPStreamReceiver.h:103
int localPort
Definition: VoIPStreamReceiver.h:101
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UDPSocket.h:110
simtime_t playoutDelay
Definition: VoIPStreamReceiver.h:102
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
UDPSocket socket
Definition: VoIPStreamReceiver.h:105
void bind(int localPort)
Bind the socket to a local port number.
Definition: UDPSocket.cc:53
Initialization of applications.
Definition: InitStages.h:106
Definition: NodeStatus.h:40
virtual int inet::VoIPStreamReceiver::numInitStages ( ) const
inlineoverrideprotectedvirtual
58 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116

Member Data Documentation

simsignal_t inet::VoIPStreamReceiver::connStateSignal = registerSignal("connState")
staticprotected
Connection inet::VoIPStreamReceiver::curConn
protected
simsignal_t inet::VoIPStreamReceiver::delaySignal = registerSignal("delay")
staticprotected

Referenced by decodePacket().

simsignal_t inet::VoIPStreamReceiver::dropPkSignal = registerSignal("dropPk")
staticprotected

Referenced by handleMessage().

int inet::VoIPStreamReceiver::localPort = -1
protected

Referenced by initialize().

simsignal_t inet::VoIPStreamReceiver::lostPacketsSignal = registerSignal("lostPackets")
staticprotected

Referenced by decodePacket().

simsignal_t inet::VoIPStreamReceiver::lostSamplesSignal = registerSignal("lostSamples")
staticprotected

Referenced by decodePacket().

simsignal_t inet::VoIPStreamReceiver::packetHasVoiceSignal = registerSignal("packetHasVoice")
staticprotected

Referenced by decodePacket().

simtime_t inet::VoIPStreamReceiver::playoutDelay
protected

Referenced by createConnection(), and initialize().

simsignal_t inet::VoIPStreamReceiver::rcvdPkSignal = registerSignal("rcvdPk")
staticprotected

Referenced by handleMessage().

const char* inet::VoIPStreamReceiver::resultFile = nullptr
protected

Referenced by createConnection(), and initialize().

UDPSocket inet::VoIPStreamReceiver::socket
protected

Referenced by initialize().


The documentation for this class was generated from the following files: