7#ifndef OPUS_ENC_MAX_BUFFER_SIZE
8#define OPUS_ENC_MAX_BUFFER_SIZE 2048
11#ifndef OPUS_DEC_MAX_BUFFER_SIZE
12#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
138 const char *
mime()
override {
return "audio/opus"; }
160 LOGE(
"Sample rate not supported: %d",
cfg.sample_rate);
167 size_t size = opus_decoder_get_size(
cfg.channels);
171 int err = opus_decoder_init(
dec,
cfg.sample_rate,
cfg.channels);
173 if (err != OPUS_OK) {
174 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
175 opus_strerror(err),
cfg.sample_rate,
cfg.channels);
201 size_t write(
const uint8_t *data,
size_t len)
override {
204 LOGD(
"OpusAudioDecoder::write: %d", (
int)len);
205 int in_band_forward_error_correction = 0;
206 int frame_count =
cfg.max_buffer_size /
cfg.channels /
sizeof(opus_int16);
208 opus_decode(
dec, (uint8_t *)data, len, (opus_int16 *)
outbuf.data(),
209 frame_count, in_band_forward_error_correction);
210 if (out_samples < 0) {
211 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
212 }
else if (out_samples > 0) {
214 int out_bytes = out_samples *
cfg.channels *
sizeof(int16_t);
215 LOGD(
"opus-decode: %d", out_bytes);
216 int open = out_bytes;
219 int to_write = std::min(open,
cfg.max_buffer_write_size);
220 int written =
p_print->write(
outbuf.data() + processed, to_write);
222 processed += written;
228 operator bool()
override {
return active; }
236 OpusDecoder *
dec =
nullptr;
241 const uint32_t
valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
246 if (valid == rate)
return true;
274 const char *
mime()
override {
return "audio/opus"; }
291 enc = opus_encoder_create(
cfg.sample_rate,
cfg.channels,
cfg.application,
293 if (err != OPUS_OK) {
294 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
295 opus_strerror(err),
cfg.sample_rate,
cfg.channels);
322 opus_encoder_destroy(
enc);
328 size_t write(
const uint8_t *data,
size_t len)
override {
330 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
333 for (
int j = 0; j < len; j++) {
344 if (
enc ==
nullptr)
return 0;
345 opus_int32 samples = 0;
346 if (opus_encoder_ctl(
enc, OPUS_GET_LOOKAHEAD(&samples)) != OPUS_OK) {
349 return samples > 0 ? samples : 0;
355 int bytesPerSample =
cfg.channels * (int)
sizeof(int16_t);
356 if (bytesPerSample <= 0)
return 0;
365 OpusEncoder *
enc =
nullptr;
383 if (
frame.size() > 0) {
387 uint8_t packet[packet_len];
389 int frames =
frame.size() /
cfg.channels /
sizeof(int16_t);
390 LOGD(
"opus_encode - frame_size: %d", frames);
391 int len = opus_encode(
enc, (opus_int16 *)
frame.data(), frames, packet,
394 LOGE(
"opus_encode: %s", opus_strerror(len));
395 }
else if (len > 0) {
396 LOGD(
"opus-encode: %d", len);
397 int eff =
p_print->write(packet, len);
399 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
407 switch (
cfg.frame_sizes_ms_x2) {
408 case OPUS_FRAMESIZE_2_5_MS:
409 return sampling_rate / 400;
410 case OPUS_FRAMESIZE_5_MS:
411 return sampling_rate / 200;
412 case OPUS_FRAMESIZE_10_MS:
413 return sampling_rate / 100;
414 case OPUS_FRAMESIZE_20_MS:
415 return sampling_rate / 50;
416 case OPUS_FRAMESIZE_40_MS:
417 return sampling_rate / 25;
418 case OPUS_FRAMESIZE_60_MS:
419 return 3 * sampling_rate / 50;
420 case OPUS_FRAMESIZE_80_MS:
421 return 4 * sampling_rate / 50;
422 case OPUS_FRAMESIZE_100_MS:
423 return 5 * sampling_rate / 50;
424 case OPUS_FRAMESIZE_120_MS:
425 return 6 * sampling_rate / 50;
427 return sampling_rate / 100;
432 if (
cfg.bitrate >= 0 &&
433 opus_encoder_ctl(
enc, OPUS_SET_BITRATE(
cfg.bitrate)) != OPUS_OK) {
434 LOGE(
"invalid bitrate: %d",
cfg.bitrate);
437 if (
cfg.force_channel >= 0 &&
438 opus_encoder_ctl(
enc, OPUS_SET_FORCE_CHANNELS(
cfg.force_channel)) !=
440 LOGE(
"invalid force_channel: %d",
cfg.force_channel);
444 opus_encoder_ctl(
enc, OPUS_SET_VBR(
cfg.vbr)) != OPUS_OK) {
445 LOGE(
"invalid vbr: %d",
cfg.vbr);
448 if (
cfg.vbr_constraint >= 0 &&
449 opus_encoder_ctl(
enc, OPUS_SET_VBR_CONSTRAINT(
cfg.vbr_constraint)) !=
451 LOGE(
"invalid vbr_constraint: %d",
cfg.vbr_constraint);
454 if (
cfg.complexity >= 0 &&
455 opus_encoder_ctl(
enc, OPUS_SET_COMPLEXITY(
cfg.complexity)) != OPUS_OK) {
456 LOGE(
"invalid complexity: %d",
cfg.complexity);
459 if (
cfg.max_bandwidth >= 0 &&
460 opus_encoder_ctl(
enc, OPUS_SET_MAX_BANDWIDTH(
cfg.max_bandwidth)) !=
462 LOGE(
"invalid max_bandwidth: %d",
cfg.max_bandwidth);
465 if (
cfg.signal >= 0 &&
466 opus_encoder_ctl(
enc, OPUS_SET_SIGNAL(
cfg.signal)) != OPUS_OK) {
467 LOGE(
"invalid signal: %d",
cfg.signal);
470 if (
cfg.inband_fec >= 0 &&
471 opus_encoder_ctl(
enc, OPUS_SET_INBAND_FEC(
cfg.inband_fec)) != OPUS_OK) {
472 LOGE(
"invalid inband_fec: %d",
cfg.inband_fec);
475 if (
cfg.packet_loss_perc >= 0 &&
477 enc, OPUS_SET_PACKET_LOSS_PERC(
cfg.packet_loss_perc)) != OPUS_OK) {
478 LOGE(
"invalid pkt_loss: %d",
cfg.packet_loss_perc);
481 if (
cfg.lsb_depth >= 0 &&
482 opus_encoder_ctl(
enc, OPUS_SET_LSB_DEPTH(
cfg.lsb_depth)) != OPUS_OK) {
483 LOGE(
"invalid lsb_depth: %d",
cfg.lsb_depth);
486 if (
cfg.prediction_disabled >= 0 &&
487 opus_encoder_ctl(
enc, OPUS_SET_PREDICTION_DISABLED(
488 cfg.prediction_disabled)) != OPUS_OK) {
489 LOGE(
"invalid pred_disabled: %d",
cfg.prediction_disabled);
492 if (
cfg.use_dtx >= 0 &&
493 opus_encoder_ctl(
enc, OPUS_SET_DTX(
cfg.use_dtx)) != OPUS_OK) {
494 LOGE(
"invalid use_dtx: %d",
cfg.use_dtx);
497 if (
cfg.frame_sizes_ms_x2 > 0 &&
498 opus_encoder_ctl(
enc, OPUS_SET_EXPERT_FRAME_DURATION(
499 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
500 LOGE(
"invalid frame_sizes_ms_x2: %d",
cfg.frame_sizes_ms_x2);
#define OPUS_DEC_MAX_BUFFER_SIZE
Definition CodecOpus.h:12
#define OPUS_ENC_MAX_BUFFER_SIZE
Definition CodecOpus.h:8