3 #include "AudioCodecs/AudioEncoded.h"
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
32 int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
33 int max_buffer_write_size = 512;
71 max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
141 bool begin(OpusSettings settings) {
145 notifyAudioChange(cfg);
149 bool begin()
override {
152 LOGE(
"Sample rate not supported: %d", cfg.
sample_rate);
155 outbuf.resize(cfg.max_buffer_size);
156 assert(outbuf.data() !=
nullptr);
161 size_t size = opus_decoder_get_size(cfg.
channels);
163 assert(decbuf.data() !=
nullptr);
164 dec = (OpusDecoder*)decbuf.data();
168 if (err != OPUS_OK) {
169 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
177 void end()
override {
180 opus_decoder_destroy(dec);
196 size_t write(
const void *in_ptr,
size_t in_size)
override {
197 if (!active || p_print ==
nullptr)
return 0;
199 LOGD(
"OpusAudioDecoder::write: %d", (
int)in_size);
200 int in_band_forward_error_correction = 0;
201 int frame_count = cfg.max_buffer_size / cfg.
channels /
sizeof(opus_int16);
202 int out_samples = opus_decode(
203 dec, (uint8_t *)in_ptr, in_size, (opus_int16 *)outbuf.data(),
204 frame_count, in_band_forward_error_correction);
205 if (out_samples < 0) {
206 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
207 }
else if (out_samples > 0) {
209 int out_bytes = out_samples * cfg.
channels *
sizeof(int16_t);
210 LOGD(
"opus-decode: %d", out_bytes);
211 int open = out_bytes;
214 int to_write = std::min(open, cfg.max_buffer_write_size);
215 int written = p_print->write(outbuf.data()+processed, to_write);
217 processed += written;
223 operator bool()
override {
return active; }
226 Print *p_print =
nullptr;
230 Vector<uint8_t> outbuf{0};
231 Vector<uint8_t> decbuf{0};
232 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
234 bool isValidRate(
int rate){
235 for (
auto &valid : valid_rates){
236 if (valid==rate)
return true;
261 const char *
mime()
override {
return "audio/opus"; }
276 assert(frame.data() !=
nullptr);
277 enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
278 if (err != OPUS_OK) {
279 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
280 opus_strerror(err), cfg.sample_rate, cfg.channels);
283 is_open = settings();
292 bool begin(OpusEncoderSettings settings) {
302 opus_encoder_destroy(enc);
307 size_t write(
const void *in_ptr,
size_t in_size)
override {
308 if (!is_open || p_print ==
nullptr)
return 0;
309 LOGD(
"OpusAudioEncoder::write: %d", (
int)in_size);
312 uint8_t *p_byte = (uint8_t *)in_ptr;
313 for (
int j = 0; j < in_size; j++) {
314 encodeByte(p_byte[j]);
319 operator bool()
override {
return is_open; }
321 bool isOpen() {
return is_open; }
324 Print *p_print =
nullptr;
325 OpusEncoder *enc =
nullptr;
326 OpusEncoderSettings cfg;
327 bool is_open =
false;
328 Vector<uint8_t> frame{0};
331 void encodeByte(uint8_t data) {
333 frame[frame_pos++] = data;
336 if (frame_pos >= frame.size()) {
343 if (frame.size() > 0) {
345 int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
346 uint8_t packet[packet_len];
348 int frames = frame.size() / cfg.channels /
sizeof(int16_t);
349 LOGD(
"opus_encode - frame_size: %d", frames);
350 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
353 LOGE(
"opus_encode: %s", opus_strerror(len));
354 }
else if (len > 0) {
355 LOGD(
"opus-encode: %d", len);
356 int eff = p_print->write(packet, len);
358 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
366 switch (cfg.frame_sizes_ms_x2) {
367 case OPUS_FRAMESIZE_2_5_MS:
368 return sampling_rate / 400;
369 case OPUS_FRAMESIZE_5_MS:
370 return sampling_rate / 200;
371 case OPUS_FRAMESIZE_10_MS:
372 return sampling_rate / 100;
373 case OPUS_FRAMESIZE_20_MS:
374 return sampling_rate / 50;
375 case OPUS_FRAMESIZE_40_MS:
376 return sampling_rate / 25;
377 case OPUS_FRAMESIZE_60_MS:
378 return 3 * sampling_rate / 50;
379 case OPUS_FRAMESIZE_80_MS:
380 return 4 * sampling_rate / 50;
381 case OPUS_FRAMESIZE_100_MS:
382 return 5 * sampling_rate / 50;
383 case OPUS_FRAMESIZE_120_MS:
384 return 6 * sampling_rate / 50;
386 return sampling_rate / 100;
391 if (cfg.bitrate >= 0 &&
392 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
393 LOGE(
"invalid bitrate: %d", cfg.bitrate);
396 if (cfg.force_channel >= 0 &&
397 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
399 LOGE(
"invalid force_channel: %d", cfg.force_channel);
403 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
404 LOGE(
"invalid vbr: %d", cfg.vbr);
407 if (cfg.vbr_constraint >= 0 &&
408 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
410 LOGE(
"invalid vbr_constraint: %d", cfg.vbr_constraint);
413 if (cfg.complexity >= 0 &&
414 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
415 LOGE(
"invalid complexity: %d", cfg.complexity);
418 if (cfg.max_bandwidth >= 0 &&
419 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
421 LOGE(
"invalid max_bandwidth: %d", cfg.max_bandwidth);
424 if (cfg.singal >= 0 &&
425 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.singal)) != OPUS_OK) {
426 LOGE(
"invalid singal: %d", cfg.singal);
429 if (cfg.inband_fec >= 0 &&
430 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
431 LOGE(
"invalid inband_fec: %d", cfg.inband_fec);
434 if (cfg.packet_loss_perc >= 0 &&
436 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
437 LOGE(
"invalid pkt_loss: %d", cfg.packet_loss_perc);
440 if (cfg.lsb_depth >= 0 &&
441 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
442 LOGE(
"invalid lsb_depth: %d", cfg.lsb_depth);
445 if (cfg.prediction_disabled >= 0 &&
446 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
447 cfg.prediction_disabled)) != OPUS_OK) {
448 LOGE(
"invalid pred_disabled: %d", cfg.prediction_disabled);
451 if (cfg.use_dtx >= 0 &&
452 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
453 LOGE(
"invalid use_dtx: %d", cfg.use_dtx);
456 if (cfg.frame_sizes_ms_x2 > 0 &&
457 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
458 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
459 LOGE(
"invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);