From d617d4550eeb3a174ff2eb8d0bca2844af2cd9e7 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 1 Oct 2019 15:17:09 +0200 Subject: Use new EDI input API with callback --- src/input/Edi.cpp | 33 +++++++++++++++------------------ src/input/Edi.h | 2 ++ 2 files changed, 17 insertions(+), 18 deletions(-) (limited to 'src/input') diff --git a/src/input/Edi.cpp b/src/input/Edi.cpp index 4a499e1..f4b5e25 100644 --- a/src/input/Edi.cpp +++ b/src/input/Edi.cpp @@ -47,7 +47,7 @@ constexpr size_t TCP_BLOCKSIZE = 2048; Edi::Edi(const std::string& name, const dab_input_edi_config_t& config) : RemoteControllable(name), m_tcp_receive_server(TCP_BLOCKSIZE), - m_sti_writer(), + m_sti_writer(bind(&Edi::m_new_sti_frame_callback, this, placeholders::_1)), m_sti_decoder(m_sti_writer, VERBOSE), m_max_frames_overrun(config.buffer_size), m_num_frames_prebuffering(config.prebuffering), @@ -306,8 +306,6 @@ size_t Edi::readFrame(uint8_t *buffer, size_t size, std::time_t seconds, int utc void Edi::m_run() { while (m_running) { - bool work_done = false; - switch (m_input_used) { case InputUsed::UDP: { @@ -318,7 +316,9 @@ void Edi::m_run() } if (not packet.buffer.empty()) { m_sti_decoder.push_packet(packet.buffer); - work_done = true; + } + else { + this_thread::sleep_for(chrono::milliseconds(12)); } } break; @@ -327,27 +327,24 @@ void Edi::m_run() auto packet = m_tcp_receive_server.receive(); if (not packet.empty()) { m_sti_decoder.push_bytes(packet); - work_done = true; + } + else { + this_thread::sleep_for(chrono::milliseconds(12)); } } break; default: throw logic_error("unimplemented input"); } + } +} - const auto sti = m_sti_writer.getFrame(); - if (not sti.frame.empty()) { - // We should not wait here, because we want the complete input buffering - // happening inside m_frames. Using the blocking function is only a protection - // against runaway memory usage if something goes wrong in the consumer. - m_frames.push_wait_if_full(move(sti), m_max_frames_overrun * 2); - work_done = true; - } - - if (not work_done) { - // Avoid fast loop - this_thread::sleep_for(chrono::milliseconds(12)); - } +void Edi::m_new_sti_frame_callback(EdiDecoder::sti_frame_t&& sti) { + if (not sti.frame.empty()) { + // We should not wait here, because we want the complete input buffering + // happening inside m_frames. Using the blocking function is only a protection + // against runaway memory usage if something goes wrong in the consumer. + m_frames.push_wait_if_full(move(sti), m_max_frames_overrun * 2); } } diff --git a/src/input/Edi.h b/src/input/Edi.h index 6f8c013..ed4d7cf 100644 --- a/src/input/Edi.h +++ b/src/input/Edi.h @@ -82,6 +82,8 @@ class Edi : public InputBase, public RemoteControllable { protected: void m_run(); + void m_new_sti_frame_callback(EdiDecoder::sti_frame_t&& frame); + std::mutex m_mutex; enum class InputUsed { Invalid, UDP, TCP }; -- cgit v1.2.3