|
INET Framework for OMNeT++/OMNEST
|
#include <VoipStreamSender.h>
◆ VoipStreamSender()
| inet::VoipStreamSender::VoipStreamSender |
( |
| ) |
|
◆ ~VoipStreamSender()
| inet::VoipStreamSender::~VoipStreamSender |
( |
| ) |
|
38 cancelAndDelete(
timer);
◆ checkSilence()
| bool inet::VoipStreamSender::checkSilence |
( |
AVSampleFormat |
sampleFormat, |
|
|
void * |
_buf, |
|
|
int |
samples |
|
) |
| |
|
protectedvirtual |
368 switch (sampleFormat) {
369 case AV_SAMPLE_FMT_U8: {
370 uint8_t *buf = (uint8_t *)_buf;
371 for (i = 0; i < samples; ++i) {
372 int s = abs(
int(buf[i]) - 0x80);
379 case AV_SAMPLE_FMT_S16: {
380 int16_t *buf = (int16_t *)_buf;
381 for (i = 0; i < samples; ++i) {
389 case AV_SAMPLE_FMT_S32: {
390 int32_t *buf = (int32_t *)_buf;
392 for (i = 0; i < samples; ++i) {
402 throw cRuntimeError(
"Invalid sampleFormat: %d", sampleFormat);
Referenced by generatePacket().
◆ finish()
| void inet::VoipStreamSender::finish |
( |
| ) |
|
|
overrideprotectedvirtual |
◆ generatePacket()
| Packet * inet::VoipStreamSender::generatePacket |
( |
| ) |
|
|
protectedvirtual |
298 short int bytesPerInSample = av_get_bytes_per_sample(
pEncoderCtx->sample_fmt);
300 int inBytes = samples * bytesPerInSample;
302 const auto& vp = makeShared<VoipStreamPacket>();
305 av_init_packet(&opacket);
306 opacket.data =
nullptr;
308 AVFrame *frame = av_frame_alloc();
310 frame->nb_samples = samples;
312 frame->channel_layout = AV_CH_LAYOUT_MONO;
317 throw cRuntimeError(
"Error in avcodec_fill_audio_frame(): err=%d", ret);
321 ret = avcodec_encode_audio2(
pEncoderCtx, &opacket, frame, &gotPacket);
322 if (ret < 0 || gotPacket != 1)
323 throw cRuntimeError(
"avcodec_encode_audio() error: %d gotPacket: %d", ret, gotPacket);
329 Packet *pk =
new Packet();
331 pk->setName(
"SILENCE");
332 vp->setType(SILENCE);
335 vp->setDataLength(0);
338 pk->setName(
"VOICE");
340 vp->setDataLength(opacket.size);
343 const auto& voice = makeShared<BytesChunk>(opacket.data, opacket.size);
344 pk->insertAtFront(voice);
347 vp->setTimeStamp(
pktID);
351 vp->setSampleBits(
pEncoderCtx->bits_per_coded_sample);
354 pk->insertAtFront(vp);
358 av_free_packet(&opacket);
359 av_frame_free(&frame);
Referenced by handleMessage().
◆ handleMessage()
| void inet::VoipStreamSender::handleMessage |
( |
cMessage * |
msg | ) |
|
|
overrideprotectedvirtual |
150 if (msg->isSelfMessage()) {
171 packet = check_and_cast<Packet *>(msg);
◆ initialize()
| void inet::VoipStreamSender::initialize |
( |
int |
stage | ) |
|
|
overrideprotectedvirtual |
80 EV_INFO <<
"The packetTimeLength parameter is " <<
packetTimeLength * 1000.0 <<
"ms, ";
91 EV_DEBUG <<
"libavcodec: " << LIBAVCODEC_VERSION_MAJOR <<
"." << LIBAVCODEC_VERSION_MINOR <<
"." << LIBAVCODEC_VERSION_MICRO << endl;
92 EV_DEBUG <<
"libavformat: " << LIBAVFORMAT_VERSION_MAJOR <<
"." << LIBAVFORMAT_VERSION_MINOR <<
"." << LIBAVFORMAT_VERSION_MICRO << endl;
93 EV_DEBUG <<
"libavutil: " << LIBAVUTIL_VERSION_MAJOR <<
"." << LIBAVUTIL_VERSION_MINOR <<
"." << LIBAVUTIL_VERSION_MICRO << endl;
94 EV_DEBUG <<
"libavresample: " << LIBAVRESAMPLE_VERSION_MAJOR <<
"." << LIBAVRESAMPLE_VERSION_MINOR <<
"." << LIBAVRESAMPLE_VERSION_MICRO << endl;
98 NodeStatus *nodeStatus = node ? check_and_cast_nullable<NodeStatus *>(node->getSubmodule(
"status")) : nullptr;
99 bool isOperational = (!nodeStatus) || nodeStatus->getState() ==
NodeStatus::UP;
101 throw cRuntimeError(
"This module doesn't support starting in node DOWN state");
104 EV_TRACE <<
"VoIPSourceApp -> initialize(" << stage <<
")" << endl;
107 recordScalar(
"hackForCreateResultsFolder", 0);
109 destAddress = L3AddressResolver().resolve(par(
"destAddress"));
114 int timeToLive = par(
"timeToLive");
115 if (timeToLive != -1)
118 int dscp = par(
"dscp");
122 int tos = par(
"tos");
126 simtime_t startTime = par(
"startTime");
132 avcodec_register_all();
138 timer =
new cMessage(
"sendVoIP");
139 scheduleAt(startTime,
timer);
◆ numInitStages()
| virtual int inet::VoipStreamSender::numInitStages |
( |
| ) |
const |
|
inlineoverrideprotectedvirtual |
◆ openSoundFile()
| void inet::VoipStreamSender::openSoundFile |
( |
const char * |
name | ) |
|
|
protectedvirtual |
203 ret = avformat_open_input(&
pFormatCtx, name,
nullptr,
nullptr);
205 throw cRuntimeError(
"Audiofile '%s' open error: %d", name, ret);
207 ret = avformat_find_stream_info(
pFormatCtx,
nullptr);
209 throw cRuntimeError(
"Audiofile '%s' avformat_find_stream_info() error: %d", name, ret);
213 for (
unsigned int j = 0; j <
pFormatCtx->nb_streams; j++) {
214 if (
pFormatCtx->streams[j]->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
221 throw cRuntimeError(
"The file '%s' not contains any audio stream.", name);
228 throw cRuntimeError(
"Audiofile '%s' avcodec_find_decoder() error: decoder not found", name);
232 throw cRuntimeError(
"avcodec_open() error on file '%s': %d", name, ret);
237 throw cRuntimeError(
"error occured in avcodec_alloc_context3()");
246 throw cRuntimeError(
"Codec '%s' not found!",
codec);
251 throw cRuntimeError(
"could not open %s encoding codec!",
codec);
262 throw cRuntimeError(
"error in av_audio_resample_init()");
264 int inChannelLayout =
pCodecCtx->channel_layout == 0 ? av_get_default_channel_layout(
pCodecCtx->channels) :
pCodecCtx->channel_layout;
265 if (av_opt_set_int(
pReSampleCtx,
"in_channel_layout", inChannelLayout, 0))
266 throw cRuntimeError(
"error in option setting of 'in_channel_layout'");
268 throw cRuntimeError(
"error in option setting of 'in_sample_fmt'");
270 throw cRuntimeError(
"error in option setting of 'in_sample_rate'");
271 if (av_opt_set_int(
pReSampleCtx,
"out_channel_layout", AV_CH_LAYOUT_MONO, 0))
272 throw cRuntimeError(
"error in option setting of 'out_channel_layout'");
274 throw cRuntimeError(
"error in option setting of 'out_sample_fmt'");
276 throw cRuntimeError(
"error in option setting of 'out_sample_rate'");
277 if (av_opt_set_int(
pReSampleCtx,
"internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0))
278 throw cRuntimeError(
"error in option setting of 'internal_sample_fmt'");
282 throw cRuntimeError(
"Error opening context");
Referenced by initialize().
◆ readFrame()
| void inet::VoipStreamSender::readFrame |
( |
| ) |
|
|
protectedvirtual |
420 short int inBytesPerSample = av_get_bytes_per_sample(
pCodecCtx->sample_fmt);
421 short int outBytesPerSample = av_get_bytes_per_sample(
pEncoderCtx->sample_fmt);
443 avpkt.data =
nullptr;
445 av_init_packet(&avpkt);
446 ASSERT(avpkt.data ==
nullptr && avpkt.size == 0);
450 while (avpkt.size > 0) {
452 AVFrame *frame = av_frame_alloc();
454 int decoded = avcodec_decode_audio4(
pCodecCtx, frame, &gotFrame, &avpkt);
456 throw cRuntimeError(
"Error in avcodec_decode_audio4(), err=%d, gotFrame=%d", decoded, gotFrame);
458 avpkt.data += decoded;
459 avpkt.size -= decoded;
464 int dataSize = av_samples_get_buffer_size(
nullptr,
pCodecCtx->channels, frame->nb_samples,
pCodecCtx->sample_fmt, 1);
471 uint8_t **in_data = frame->extended_data;
472 int in_linesize = frame->linesize[0];
473 int in_nb_samples = frame->nb_samples;
475 uint8_t *out_data[AVRESAMPLE_MAX_CHANNELS] = {
481 ret = av_samples_fill_arrays(out_data, &out_linesize, tmpSamples, 1, maxOutSamples,
pEncoderCtx->sample_fmt, 0);
483 throw cRuntimeError(
"failed out_data fill arrays");
485 decoded = avresample_convert(
pReSampleCtx, out_data, out_linesize, decoded, in_data, in_linesize, in_nb_samples);
486 if (decoded <= 0 && avresample_get_delay(
pReSampleCtx) == 0) {
487 throw cRuntimeError(
"audio_resample() returns error");
500 av_frame_free(&frame);
501 av_free_packet(&avpkt);
Referenced by generatePacket().
◆ codec
| const char* inet::VoipStreamSender::codec = nullptr |
|
protected |
◆ compressedBitRate
| int inet::VoipStreamSender::compressedBitRate = 0 |
|
protected |
◆ destAddress
| L3Address inet::VoipStreamSender::destAddress |
|
protected |
◆ destPort
| int inet::VoipStreamSender::destPort = -1 |
|
protected |
◆ localPort
| int inet::VoipStreamSender::localPort = -1 |
|
protected |
◆ outFile
◆ packet
| AVPacket inet::VoipStreamSender::packet {} |
|
protected |
◆ packetTimeLength
| simtime_t inet::VoipStreamSender::packetTimeLength |
|
protected |
◆ pCodec
| AVCodec* inet::VoipStreamSender::pCodec = nullptr |
|
protected |
◆ pCodecCtx
| AVCodecContext* inet::VoipStreamSender::pCodecCtx = nullptr |
|
protected |
◆ pCodecEncoder
| AVCodec* inet::VoipStreamSender::pCodecEncoder = nullptr |
|
protected |
◆ pEncoderCtx
| AVCodecContext* inet::VoipStreamSender::pEncoderCtx = nullptr |
|
protected |
◆ pFormatCtx
| AVFormatContext* inet::VoipStreamSender::pFormatCtx = nullptr |
|
protected |
◆ pktID
| uint32_t inet::VoipStreamSender::pktID = 0 |
|
protected |
◆ pReSampleCtx
| AVAudioResampleContext* inet::VoipStreamSender::pReSampleCtx = nullptr |
|
protected |
◆ repeatCount
| int inet::VoipStreamSender::repeatCount = 0 |
|
protected |
◆ sampleBuffer
| Buffer inet::VoipStreamSender::sampleBuffer |
|
protected |
◆ sampleRate
| int inet::VoipStreamSender::sampleRate = 0 |
|
protected |
◆ samplesPerPacket
| int inet::VoipStreamSender::samplesPerPacket = 0 |
|
protected |
◆ socket
◆ soundFile
| const char* inet::VoipStreamSender::soundFile = nullptr |
|
protected |
◆ streamIndex
| int inet::VoipStreamSender::streamIndex = -1 |
|
protected |
◆ timer
| cMessage* inet::VoipStreamSender::timer = nullptr |
|
protected |
◆ traceFileName
| const char* inet::VoipStreamSender::traceFileName = nullptr |
|
protected |
◆ voipHeaderSize
| int inet::VoipStreamSender::voipHeaderSize = 0 |
|
protected |
◆ voipSilencePacketSize
| int inet::VoipStreamSender::voipSilencePacketSize = 0 |
|
protected |
◆ voipSilenceThreshold
| int inet::VoipStreamSender::voipSilenceThreshold = 0 |
|
protected |
The documentation for this class was generated from the following files:
void setOutputGate(cGate *toUdp)
Sets the gate on which to send to UDP.
Definition: UdpSocket.h:117
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:31
void setDscp(short dscp)
Sets the Ipv4 / Ipv6 dscp fields of packets sent from the UDP socket.
Definition: UdpSocket.cc:121
void setTimeToLive(int ttl)
Set the TTL (Ipv6: Hop Limit) field on sent packets.
Definition: UdpSocket.cc:112
void notifyRead(int length)
Definition: VoipStreamSender.h:78
AVAudioResampleContext * pReSampleCtx
Definition: VoipStreamSender.h:106
void bind(int localPort)
Bind the socket to a local port number.
Definition: UdpSocket.cc:34
Buffer sampleBuffer
Definition: VoipStreamSender.h:116
void sendTo(Packet *msg, L3Address destAddr, int destPort)
Sends a data packet to the given address and port.
Definition: UdpSocket.cc:69
char * readPtr()
Definition: VoipStreamSender.h:75
int samplesPerPacket
Definition: VoipStreamSender.h:114
virtual void readFrame()
Definition: VoipStreamSender.cc:418
AVFormatContext * pFormatCtx
Definition: VoipStreamSender.h:103
int localPort
Definition: VoipStreamSender.h:85
int voipHeaderSize
Definition: VoipStreamSender.h:89
int voipSilenceThreshold
Definition: VoipStreamSender.h:90
void clear(int framesize)
Definition: VoipStreamSender.cc:55
double min(const double a, const double b)
Returns the minimum of a and b.
Definition: SctpAssociation.h:261
void open(const char *resultFile, int sampleRate, short int sampleBits)
Definition: AudioOutFile.cc:43
AVCodec * pCodecEncoder
Definition: VoipStreamSender.h:108
int length() const
Definition: VoipStreamSender.h:73
simsignal_t packetSentSignal
Definition: Simsignals.cc:96
L3Address destAddress
Definition: VoipStreamSender.h:87
simtime_t packetTimeLength
Definition: VoipStreamSender.h:95
bool isOpen() const
Definition: AudioOutFile.h:35
AudioOutFile outFile
Definition: VoipStreamSender.h:100
char * writePtr()
Definition: VoipStreamSender.h:76
value< double, units::s > s
Definition: Units.h:1235
intscale< b, 1, 8 > B
Definition: Units.h:1168
bool empty() const
Definition: VoipStreamSender.h:74
int voipSilencePacketSize
Definition: VoipStreamSender.h:91
@ BUFSIZE
Definition: VoipStreamSender.h:61
AVCodec * pCodec
Definition: VoipStreamSender.h:105
int sampleRate
Definition: VoipStreamSender.h:92
UdpSocket socket
Definition: VoipStreamSender.h:111
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
AVCodecContext * pCodecCtx
Definition: VoipStreamSender.h:104
void write(void *inbuf, int inbytes)
Definition: AudioOutFile.cc:101
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
int round(double d)
Returns an integer that corresponds to rounded double parameter.
Definition: INETMath.h:143
int availableSpace() const
Definition: VoipStreamSender.h:77
double max(const double a, const double b)
Returns the maximum of a and b.
Definition: SctpAssociation.h:266
bool close()
Definition: AudioOutFile.cc:139
uint32_t pktID
Definition: VoipStreamSender.h:113
INET_API InitStage INITSTAGE_APPLICATION_LAYER
Initialization of applications.
void align()
Definition: VoipStreamSender.cc:408
@ UP
Definition: NodeStatus.h:28
virtual void openSoundFile(const char *name)
Definition: VoipStreamSender.cc:199
const char * soundFile
Definition: VoipStreamSender.h:96
const char * traceFileName
Definition: VoipStreamSender.h:99
int streamIndex
Definition: VoipStreamSender.h:112
void notifyWrote(int length)
Definition: VoipStreamSender.h:79
cMessage * timer
Definition: VoipStreamSender.h:118
AVPacket packet
Definition: VoipStreamSender.h:115
AVCodecContext * pEncoderCtx
Definition: VoipStreamSender.h:107
void setTos(short tos)
Sets the Ipv4 Type of Service / Ipv6 Traffic Class fields of packets sent from the UDP socket.
Definition: UdpSocket.cc:130
int destPort
Definition: VoipStreamSender.h:86
virtual Packet * generatePacket()
Definition: VoipStreamSender.cc:291
int repeatCount
Definition: VoipStreamSender.h:97
virtual bool checkSilence(AVSampleFormat sampleFormat, void *_buf, int samples)
Definition: VoipStreamSender.cc:363
const char * codec
Definition: VoipStreamSender.h:93
int compressedBitRate
Definition: VoipStreamSender.h:94