Records audio into a file.
More...
#include <AudioOutFile.h>
|
| void | addAudioStream (enum AVCodecID codec_id, int sampleRate, short int sampleBits) |
| |
Records audio into a file.
◆ AudioOutFile()
| inet::AudioOutFile::AudioOutFile |
( |
| ) |
|
|
inline |
◆ ~AudioOutFile()
| inet::AudioOutFile::~AudioOutFile |
( |
| ) |
|
◆ addAudioStream()
| void inet::AudioOutFile::addAudioStream |
( |
enum AVCodecID |
codec_id, |
|
|
int |
sampleRate, |
|
|
short int |
sampleBits |
|
) |
| |
|
protected |
26 AVStream *st = avformat_new_stream(
oc,
nullptr);
29 throw cRuntimeError(
"Could not alloc stream\n");
31 AVCodecContext *
c = st->codec;
32 c->codec_id = codec_id;
33 c->codec_type = AVMEDIA_TYPE_AUDIO;
36 c->bit_rate = sampleRate * sampleBits;
37 c->sample_rate = sampleRate;
38 c->sample_fmt = AV_SAMPLE_FMT_S16;
Referenced by open().
◆ close()
| bool inet::AudioOutFile::close |
( |
| ) |
|
◆ isOpen()
| bool inet::AudioOutFile::isOpen |
( |
| ) |
const |
|
inline |
◆ open()
| void inet::AudioOutFile::open |
( |
const char * |
resultFile, |
|
|
int |
sampleRate, |
|
|
short int |
sampleBits |
|
) |
| |
50 AVOutputFormat *fmt = av_guess_format(
nullptr, resultFile,
nullptr);
52 EV_WARN <<
"Could not deduce output format from file extension: using WAV.\n";
53 fmt = av_guess_format(
"wav",
nullptr,
nullptr);
56 throw cRuntimeError(
"Could not find suitable output format for filename '%s'", resultFile);
60 oc = avformat_alloc_context();
62 throw cRuntimeError(
"Memory error at avformat_alloc_context()");
65 snprintf(
oc->filename,
sizeof(
oc->filename),
"%s", resultFile);
69 if (fmt->audio_codec != AV_CODEC_ID_NONE)
72 av_dump_format(
oc, 0, resultFile, 1);
80 AVCodec *avcodec = avcodec_find_encoder(
c->codec_id);
82 throw cRuntimeError(
"Codec %d not found",
c->codec_id);
85 if (avcodec_open2(
c, avcodec,
nullptr) < 0)
86 throw cRuntimeError(
"Could not open codec %d",
c->codec_id);
90 if (!(fmt->flags & AVFMT_NOFILE)) {
91 if (avio_open(&
oc->pb, resultFile, AVIO_FLAG_WRITE) < 0)
92 throw cRuntimeError(
"Could not open '%s'", resultFile);
96 auto err = avformat_write_header(
oc,
nullptr);
98 throw cRuntimeError(
"Could not write header to '%s'", resultFile);
Referenced by inet::VoipStreamReceiver::Connection::openAudio(), and inet::VoipStreamSender::openSoundFile().
◆ write()
| void inet::AudioOutFile::write |
( |
void * |
inbuf, |
|
|
int |
inbytes |
|
) |
| |
106 short int bytesPerInSample = av_get_bytes_per_sample(
c->sample_fmt);
107 int samples = pktBytes / bytesPerInSample;
110 av_init_packet(&pkt);
113 AVFrame *frame = av_frame_alloc();
115 frame->nb_samples = samples;
117 frame->channel_layout = AV_CH_LAYOUT_MONO;
118 frame->sample_rate =
c->sample_rate;
120 int ret = avcodec_fill_audio_frame(frame, 1,
c->sample_fmt, (
const uint8_t *)(decBuf), pktBytes, 1);
122 throw cRuntimeError(
"Error in avcodec_fill_audio_frame(): err=%d", ret);
126 ret = avcodec_encode_audio2(
c, &pkt, frame, &gotPacket);
127 if (ret < 0 || gotPacket != 1)
128 throw cRuntimeError(
"avcodec_encode_audio() error: %d gotPacket: %d", ret, gotPacket);
133 ret = av_interleaved_write_frame(
oc, &pkt);
135 throw cRuntimeError(
"Error while writing audio frame: %d", ret);
136 av_frame_free(&frame);
Referenced by inet::VoipStreamSender::generatePacket().
◆ audio_st
| AVStream* inet::AudioOutFile::audio_st |
|
protected |
◆ oc
| AVFormatContext* inet::AudioOutFile::oc |
|
protected |
◆ opened
| bool inet::AudioOutFile::opened |
|
protected |
The documentation for this class was generated from the following files: