aboutsummaryrefslogtreecommitdiffstats
path: root/src/odr-audioenc.cpp
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2025-08-31 18:57:19 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2025-08-31 18:57:19 +0200
commit73d0a6134a9060a9f3cf15b5ce76a5f2747fb1f2 (patch)
tree33a71a379faa2c27b569ce5c43f3ccfc42085886 /src/odr-audioenc.cpp
parentc4f0ba1c9c909f7c1ac697068197b681cd567beb (diff)
parentd31dad0b24775cf84d308b94c3fbd0187c476c14 (diff)
downloadODR-AudioEnc-73d0a6134a9060a9f3cf15b5ce76a5f2747fb1f2.tar.gz
ODR-AudioEnc-73d0a6134a9060a9f3cf15b5ce76a5f2747fb1f2.tar.bz2
ODR-AudioEnc-73d0a6134a9060a9f3cf15b5ce76a5f2747fb1f2.zip
Merge PR #38 with logging improvements from rmens
Diffstat (limited to 'src/odr-audioenc.cpp')
-rw-r--r--src/odr-audioenc.cpp155
1 files changed, 74 insertions, 81 deletions
diff --git a/src/odr-audioenc.cpp b/src/odr-audioenc.cpp
index de3843c..98e9c34 100644
--- a/src/odr-audioenc.cpp
+++ b/src/odr-audioenc.cpp
@@ -239,13 +239,13 @@ static int prepare_aac_encoder(
case 1: mode = MODE_1; break;
case 2: mode = MODE_2; break;
default:
- fprintf(stderr, "Unsupported channels number %d\n", channels);
+ etiLog.level(error) << "Unsupported channels number " << channels;
return 1;
}
if (aacEncOpen(encoder, 0x01|0x02|0x04, channels) != AACENC_OK) {
- fprintf(stderr, "Unable to open encoder\n");
+ etiLog.level(error) << "Unable to open encoder";
return 1;
}
@@ -263,35 +263,34 @@ static int prepare_aac_encoder(
}
}
- fprintf(stderr, "Using %d subchannels. AAC type: %s%s%s. channels=%d, sample_rate=%d\n",
- subchannel_index,
- *aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "",
- *aot == AOT_DABPLUS_SBR ? "HE-AAC" : "",
- *aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "",
- channels, sample_rate);
+ etiLog.level(info) << "Using " << subchannel_index << " subchannels. AAC type: "
+ << (*aot == AOT_DABPLUS_PS ? "HE-AAC v2" : "")
+ << (*aot == AOT_DABPLUS_SBR ? "HE-AAC" : "")
+ << (*aot == AOT_DABPLUS_AAC_LC ? "AAC-LC" : "")
+ << ". channels=" << channels << ", sample_rate=" << sample_rate;
if (aacEncoder_SetParam(*encoder, AACENC_AOT, *aot) != AACENC_OK) {
- fprintf(stderr, "Unable to set the AOT\n");
+ etiLog.level(error) << "Unable to set the AOT";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_SAMPLERATE, sample_rate) != AACENC_OK) {
- fprintf(stderr, "Unable to set the sample rate\n");
+ etiLog.level(error) << "Unable to set the sample rate";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_CHANNELMODE, mode) != AACENC_OK) {
- fprintf(stderr, "Unable to set the channel mode\n");
+ etiLog.level(error) << "Unable to set the channel mode";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_CHANNELORDER, 1) != AACENC_OK) {
- fprintf(stderr, "Unable to set the wav channel order\n");
+ etiLog.level(error) << "Unable to set the wav channel order";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_GRANULE_LENGTH, 960) != AACENC_OK) {
- fprintf(stderr, "Unable to set the granule length\n");
+ etiLog.level(error) << "Unable to set the granule length";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_TRANSMUX, TT_DABPLUS) != AACENC_OK) {
- fprintf(stderr, "Unable to set the RAW transmux\n");
+ etiLog.level(error) << "Unable to set the RAW transmux";
return 1;
}
@@ -302,33 +301,33 @@ static int prepare_aac_encoder(
}*/
- fprintf(stderr, "AAC bitrate set to: %d\n", subchannel_index*8000);
+ etiLog.level(info) << "AAC bitrate set to: " << (subchannel_index*8000);
if (aacEncoder_SetParam(*encoder, AACENC_BITRATE, subchannel_index*8000) != AACENC_OK) {
- fprintf(stderr, "Unable to set the bitrate\n");
+ etiLog.level(error) << "Unable to set the bitrate";
return 1;
}
if (aacEncoder_SetParam(*encoder, AACENC_AFTERBURNER, afterburner) != AACENC_OK) {
- fprintf(stderr, "Unable to set the afterburner mode\n");
+ etiLog.level(error) << "Unable to set the afterburner mode";
return 1;
}
if (!afterburner) {
- fprintf(stderr, "Warning: Afterburned disabled!\n");
+ etiLog.level(warn) << "Warning: Afterburner disabled!";
}
if (bandwidth > 0) {
- fprintf(stderr, "Setting bandwidth is %d\n", bandwidth);
+ etiLog.level(info) << "Setting bandwidth to " << bandwidth;
if (aacEncoder_SetParam(*encoder, AACENC_BANDWIDTH, bandwidth) != AACENC_OK) {
- fprintf(stderr, "Unable to set bandwidth mode\n");
+ etiLog.level(error) << "Unable to set bandwidth mode";
return 1;
}
}
if (aacEncEncode(*encoder, nullptr, nullptr, nullptr, nullptr) != AACENC_OK) {
- fprintf(stderr, "Unable to initialize the encoder\n");
+ etiLog.level(error) << "Unable to initialize the encoder";
return 1;
}
const uint32_t bw = aacEncoder_GetParam(*encoder, AACENC_BANDWIDTH);
- fprintf(stderr, "Bandwidth is %d\n", bw);
+ etiLog.level(info) << "Bandwidth is " << bw;
return 0;
}
@@ -464,7 +463,7 @@ public:
vector<string> edi_output_uris;
void *rs_handler = nullptr;
- AACENC_InfoStruct info = { 0 };
+ AACENC_InfoStruct aac_info = { 0 };
int aot = AOT_NONE;
string decode_wavfilename;
@@ -529,11 +528,11 @@ int AudioEnc::run()
#endif
if (num_inputs == 0) {
- fprintf(stderr, "No input defined!\n");
+ etiLog.level(error) << "No input defined!";
return 1;
}
else if (num_inputs > 1) {
- fprintf(stderr, "You must define only one possible input, not several!\n");
+ etiLog.level(error) << "You must define only one possible input, not several!";
return 1;
}
@@ -545,13 +544,12 @@ int AudioEnc::run()
int subchannel_index = bitrate / 8;
if (subchannel_index < 1 || subchannel_index > 24) {
- fprintf(stderr, "Bad subchannel index: %d, must be between 1 and 24. Try other bitrate.\n",
- subchannel_index);
+ etiLog.level(error) << "Bad subchannel index: " << subchannel_index << ", must be between 1 and 24. Try other bitrate.";
return 1;
}
if ( ! (sample_rate == 32000 || sample_rate == 48000)) {
- fprintf(stderr, "Invalid sample rate. Possible values are: 32000, 48000.\n");
+ etiLog.level(error) << "Invalid sample rate. Possible values are: 32000, 48000.";
return 1;
}
}
@@ -561,25 +559,25 @@ int AudioEnc::run()
}
if ( ! (sample_rate == 24000 || sample_rate == 48000)) {
- fprintf(stderr, "Invalid sample rate. Possible values are: 24000, 48000.\n");
+ etiLog.level(error) << "Invalid sample rate. Possible values are: 24000, 48000.";
return 1;
}
}
if (padlen < 0 or padlen > 255) {
- fprintf(stderr, "Invalid PAD length specified\n");
+ etiLog.level(error) << "Invalid PAD length specified";
return 1;
}
if (output_uris.empty() and edi_output_uris.empty()) {
- fprintf(stderr, "No output defined\n");
+ etiLog.level(error) << "No output defined";
return 1;
}
for (const auto& uri : output_uris) {
if (uri == "-") {
if (file_output) {
- fprintf(stderr, "You can't write to more than one file!\n");
+ etiLog.level(error) << "You can't write to more than one file!";
return 1;
}
file_output = make_shared<Output::File>(stdout);
@@ -597,7 +595,7 @@ int AudioEnc::run()
}
else { // We assume it's a file name
if (file_output) {
- fprintf(stderr, "You can't write to more than one file!\n");
+ etiLog.level(error) << "You can't write to more than one file!";
return 1;
}
file_output = make_shared<Output::File>(uri.c_str());
@@ -624,11 +622,11 @@ int AudioEnc::run()
}
}
else {
- fprintf(stderr, "Invalid EDI URL host!\n");
+ etiLog.level(error) << "Invalid EDI URL host!";
}
}
else {
- fprintf(stderr, "Invalid EDI protocol!\n");
+ etiLog.level(error) << "Invalid EDI protocol!";
}
}
@@ -653,10 +651,10 @@ int AudioEnc::run()
if (padlen != 0 and not pad_ident.empty()) {
pad_intf.open(pad_ident);
- fprintf(stderr, "PAD socket opened\n");
+ etiLog.level(info) << "PAD socket opened";
}
else {
- fprintf(stderr, "PAD disabled because neither PAD length nor PAD identifier given\n");
+ etiLog.level(info) << "PAD disabled because neither PAD length nor PAD identifier given";
}
vec_u8 input_buf;
@@ -665,20 +663,18 @@ int AudioEnc::run()
int subchannel_index = bitrate / 8;
if (prepare_aac_encoder(&encoder, subchannel_index, channels,
sample_rate, afterburner, bandwidth, &aot) != 0) {
- fprintf(stderr, "Encoder preparation failed\n");
+ etiLog.level(error) << "Encoder preparation failed";
return 1;
}
- if (aacEncInfo(encoder, &info) != AACENC_OK) {
- fprintf(stderr, "Unable to get the encoder info\n");
+ if (aacEncInfo(encoder, &aac_info) != AACENC_OK) {
+ etiLog.level(error) << "Unable to get the encoder info";
return 1;
}
// Each DAB+ frame will need input_size audio bytes
- const int input_size = channels * BYTES_PER_SAMPLE * info.frameLength;
- fprintf(stderr, "DAB+ Encoding: framelen=%d (%dB)\n",
- info.frameLength,
- input_size);
+ const int input_size = channels * BYTES_PER_SAMPLE * aac_info.frameLength;
+ etiLog.level(info) << "DAB+ Encoding: framelen=" << aac_info.frameLength << " (" << input_size << "B)";
input_buf.resize(input_size);
@@ -705,8 +701,7 @@ int AudioEnc::run()
dab_channel_mode = 'm'; // Default to mono
}
else {
- fprintf(stderr, "Unsupported channels number %d\n",
- channels);
+ etiLog.level(error) << "Unsupported channels number " << channels;
return 1;
}
}
@@ -725,14 +720,14 @@ int AudioEnc::run()
}
if (err) {
- fprintf(stderr, "libtoolame-dab init failed: %d\n", err);
+ etiLog.level(error) << "libtoolame-dab init failed: " << err;
return err;
}
input_buf.resize(channels * 1152 * BYTES_PER_SAMPLE);
if (not decode_wavfilename.empty()) {
- fprintf(stderr, "--decode not supported for DAB\n");
+ etiLog.level(error) << "--decode not supported for DAB";
return 1;
}
}
@@ -744,7 +739,7 @@ int AudioEnc::run()
stats_publisher.reset(s);
}
catch (const runtime_error& e) {
- fprintf(stderr, "Failed to initialise Stats Publisher: %s", e.what());
+ etiLog.level(error) << "Failed to initialise Stats Publisher: " << e.what();
if (s != nullptr) {
delete s;
}
@@ -780,7 +775,7 @@ int AudioEnc::run()
input = initialise_input();
}
catch (const runtime_error& e) {
- fprintf(stderr, "Initialising input triggered exception: %s\n", e.what());
+ etiLog.level(error) << "Initialising input triggered exception: " << e.what();
return 1;
}
@@ -799,18 +794,18 @@ int AudioEnc::run()
case encoder_selection_t::toolame_dab:
outbuf_size = 4092;
outbuf.resize(outbuf_size);
- fprintf(stderr, "Setting outbuf size to %zu\n", outbuf.size());
+ etiLog.level(info) << "Setting outbuf size to " << outbuf.size();
break;
}
vector<uint8_t> pad_buf(padlen + 1);
if (restart_on_fault) {
- fprintf(stderr, "Autorestart has been deprecated and will be removed in the future!\n");
+ etiLog.level(warn) << "Autorestart has been deprecated and will be removed in the future!";
this_thread::sleep_for(chrono::seconds(2));
}
- fprintf(stderr, "Starting encoding\n");
+ etiLog.level(info) << "Starting encoding";
int retval = 0;
int send_error_count = 0;
@@ -849,7 +844,7 @@ int AudioEnc::run()
copy(pad_data.begin(), pad_data.end(), pad_buf.begin());
}
else {
- fprintf(stderr, "Incorrect PAD length received: %zu expected %d\n", pad_data.size(), padlen + 1);
+ etiLog.level(error) << "Incorrect PAD length received: " << pad_data.size() << " expected " << (padlen + 1);
break;
}
}
@@ -876,13 +871,13 @@ int AudioEnc::run()
*/
if (input->fault_detected()) {
- fprintf(stderr, "Detected fault in input!\n");
+ etiLog.level(warn) << "Detected fault in input!";
if (restart_on_fault) {
fault_counter++;
if (fault_counter >= MAX_FAULTS_ALLOWED) {
- fprintf(stderr, "Maximum number of input faults reached, aborting");
+ etiLog.level(error) << "Maximum number of input faults reached, aborting";
retval = 5;
break;
}
@@ -891,7 +886,7 @@ int AudioEnc::run()
input = initialise_input();
}
catch (const runtime_error& e) {
- fprintf(stderr, "Initialising input triggered exception: %s\n", e.what());
+ etiLog.level(error) << "Initialising input triggered exception: " << e.what();
retval = 5;
break;
}
@@ -905,7 +900,7 @@ int AudioEnc::run()
}
if (not input->read_source(input_buf.size())) {
- fprintf(stderr, "End of input reached\n");
+ etiLog.level(info) << "End of input reached";
retval = 0;
break;
}
@@ -929,7 +924,7 @@ int AudioEnc::run()
const auto elapsed = chrono::duration_cast<chrono::seconds>(
now - timepoint_last_received_sample);
if (elapsed.count() > 60) {
- fprintf(stderr, "Underruns for 60s, aborting!\n");
+ etiLog.level(error) << "Underruns for 60s, aborting!";
return 1;
}
}
@@ -960,13 +955,13 @@ int AudioEnc::run()
if (bytes_from_queue < read_bytes) {
// queue timeout occurred
- fprintf(stderr, "Detected fault in input! No data in time.\n");
+ etiLog.level(warn) << "Detected fault in input! No data in time.";
if (restart_on_fault) {
fault_counter++;
if (fault_counter >= MAX_FAULTS_ALLOWED) {
- fprintf(stderr, "Maximum number of input faults reached, aborting");
+ etiLog.level(error) << "Maximum number of input faults reached, aborting";
retval = 5;
break;
}
@@ -975,7 +970,7 @@ int AudioEnc::run()
input = initialise_input();
}
catch (const runtime_error& e) {
- fprintf(stderr, "Initialising input triggered exception: %s\n", e.what());
+ etiLog.level(error) << "Initialising input triggered exception: " << e.what();
return 1;
}
@@ -1014,7 +1009,7 @@ int AudioEnc::run()
bool success = write_icy_to_file(text, icytext_file, icytext_dlplus);
if (not success) {
- fprintf(stderr, "Failed to write ICY Text\n");
+ etiLog.level(warn) << "Failed to write ICY Text";
}
}
@@ -1071,8 +1066,7 @@ int AudioEnc::run()
measured_silence_ms += frame_time_msec;
if (measured_silence_ms > 1000*silence_timeout) {
- fprintf(stderr, "Silence detected for %d seconds, aborting.\n",
- silence_timeout);
+ etiLog.level(info) << "Silence detected for " << silence_timeout << " seconds, aborting.";
retval = 2;
break;
}
@@ -1124,10 +1118,10 @@ int AudioEnc::run()
if ((err = aacEncEncode(encoder, &in_buf, &out_buf, &in_args, &out_args))
!= AACENC_OK) {
if (err == AACENC_ENCODE_EOF) {
- fprintf(stderr, "encoder error: EOF reached\n");
+ etiLog.level(info) << "encoder error: EOF reached";
break;
}
- fprintf(stderr, "Encoding failed (%d)\n", err);
+ etiLog.level(error) << "Encoding failed (" << err << ")";
retval = 3;
break;
}
@@ -1154,7 +1148,7 @@ int AudioEnc::run()
}
}
else {
- fprintf(stderr, "INTERNAL ERROR! invalid number of channels\n");
+ etiLog.level(error) << "INTERNAL ERROR! invalid number of channels";
}
if (read_bytes) {
@@ -1170,7 +1164,7 @@ int AudioEnc::run()
decoder->decode_frame(outbuf.data(), numOutBytes);
}
catch (runtime_error &e) {
- fprintf(stderr, "Decoding failed with: %s\n", e.what());
+ etiLog.level(error) << "Decoding failed with: " << e.what();
return 1;
}
}
@@ -1184,8 +1178,7 @@ int AudioEnc::run()
// Our timing code depends on this
if (calls != enc_calls_per_output) {
- fprintf(stderr, "INTERNAL ERROR! calls=%d, expected %d\n",
- calls, enc_calls_per_output);
+ etiLog.level(error) << "INTERNAL ERROR! calls=" << calls << ", expected " << enc_calls_per_output;
}
calls = 0;
@@ -1221,7 +1214,7 @@ int AudioEnc::run()
bool success = send_frame(frame.data(), frame.size(), peak_left, peak_right);
if (not success) {
- fprintf(stderr, "Send error !\n");
+ etiLog.level(error) << "Send error !";
send_error_count ++;
}
}
@@ -1235,7 +1228,7 @@ int AudioEnc::run()
}
if (send_error_count > 10) {
- fprintf(stderr, "Send failed ten times, aborting!\n");
+ etiLog.level(error) << "Send failed ten times, aborting!";
retval = 4;
break;
}
@@ -1278,7 +1271,7 @@ int AudioEnc::run()
fflush(stdout);
} while (read_bytes > 0);
- fprintf(stderr, "\n");
+ // Final newline removed - etiLog provides its own line endings
return retval;
}
@@ -1486,7 +1479,7 @@ int main(int argc, char *argv[])
audio_enc.dab_channel_mode == "d" or
audio_enc.dab_channel_mode == "j" or
audio_enc.dab_channel_mode == "m")) {
- fprintf(stderr, "Invalid DAB channel mode\n");
+ etiLog.level(error) << "Invalid DAB channel mode";
usage(argv[0]);
return 1;
}
@@ -1502,7 +1495,7 @@ int main(int argc, char *argv[])
/* The 32 character length restriction is arbitrary, but guarantees
* that the EDI packet will not grow too large */
if (audio_enc.identifier.size() > 32) {
- fprintf(stderr, "Output Identifier too long!\n");
+ etiLog.level(error) << "Output Identifier too long!";
usage(argv[0]);
return 1;
}
@@ -1554,7 +1547,7 @@ int main(int argc, char *argv[])
}
break;
case 10:
- fprintf(stderr, "WARNING: the --vlc-gain option has been deprecated in favour of --audio-gain\n");
+ etiLog.level(warn) << "WARNING: the --vlc-gain option has been deprecated in favour of --audio-gain";
// fallthrough
case 'g':
audio_enc.gain_dB = std::stod(optarg);
@@ -1574,7 +1567,7 @@ int main(int argc, char *argv[])
#if HAVE_JACK
audio_enc.jack_name = optarg;
#else
- fprintf(stderr, "JACK disabled at compile time!\n");
+ etiLog.level(error) << "JACK disabled at compile time!";
return 1;
#endif
break;
@@ -1605,7 +1598,7 @@ int main(int argc, char *argv[])
audio_enc.die_on_silence = true;
}
else {
- fprintf(stderr, "Invalid silence timeout (%d) given!\n", audio_enc.silence_timeout);
+ etiLog.level(error) << "Invalid silence timeout (" << audio_enc.silence_timeout << ") given!";
return 1;
}
@@ -1631,7 +1624,7 @@ int main(int argc, char *argv[])
break;
#else
case 'v':
- fprintf(stderr, "VLC input not enabled at compile time!\n");
+ etiLog.level(error) << "VLC input not enabled at compile time!";
return 1;
#endif
case 'V':
@@ -1667,7 +1660,7 @@ int main(int argc, char *argv[])
return audio_enc.run();
}
catch (const std::runtime_error& e) {
- fprintf(stderr, "ODR-AudioEnc failed to start: %s\n", e.what());
+ etiLog.level(error) << "ODR-AudioEnc failed to start: " << e.what();
return 1;
}
}