From 0af2e779b2438d9ca1bc1d4f49e52bb3789907e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bodn=C3=A1r=20Zsombor?= <bodzsoaa@sch.bme.hu> Date: Sat, 6 Feb 2021 18:17:50 +0100 Subject: [PATCH] Move SendFrame out of MuebTransmitterPrivate --- src/muebtransmitter.cc | 59 ++++++++++++++++++++++++++++++++++++++- src/muebtransmitter_p.h | 61 ----------------------------------------- 2 files changed, 58 insertions(+), 62 deletions(-) diff --git a/src/muebtransmitter.cc b/src/muebtransmitter.cc index 02e13d7..5b571b1 100644 --- a/src/muebtransmitter.cc +++ b/src/muebtransmitter.cc @@ -18,7 +18,64 @@ void MuebTransmitter::SendFrame(libmueb::Frame frame) { } frame.convertTo(QImage::Format_RGB888); - d->SendFrame(frame); + + QByteArray reduced_compressed_frame; + // Frame color reduction and compression + if (d->configuration_.color_depth() < 5) { + reduced_compressed_frame = QtConcurrent::blockingMappedReduced<QByteArray>( + frame.constBits(), frame.constBits() + frame.sizeInBytes(), + /* Reference: + * http://threadlocalmutex.com/?p=48 + * http://threadlocalmutex.com/?page_id=60 + */ + [d](const uchar& color) -> uchar { + if (d->configuration_.color_depth() == 3) { + return (color * 225 + 4096) >> 13; + } else if (d->configuration_.color_depth() == 4) { + return (color * 15 + 135) >> 8; + } + + return color; + }, + [d](QByteArray& compressed_colors, const uchar& color) { + static bool msb{true}; + + // Compress 2 color components into 1 byte + if (msb) { + compressed_colors.append(color << d->configuration_.factor()); + } else { + compressed_colors.back() = compressed_colors.back() | color; + } + + msb = !msb; + }, + QtConcurrent::OrderedReduce | QtConcurrent::SequentialReduce); + } + // No compression + else { + reduced_compressed_frame.setRawData( + reinterpret_cast<const char*>(frame.bits()), frame.sizeInBytes()); + } + + if (d->configuration_.max_packet_number() == 1) { + reduced_compressed_frame.insert(0, d->configuration_.protocol_type()) + .insert(1, static_cast<char>(0) /*packet number*/); + + d->datagram_.setData(reduced_compressed_frame); + d->socket_.writeDatagram(d->datagram_); + } else { + for (std::uint8_t i = 0; i < d->configuration_.max_packet_number(); ++i) { + QByteArray data; + data.append(d->configuration_.protocol_type()) + .append(i /*packet number*/) + .append(reduced_compressed_frame.sliced( + i * d->configuration_.packet_payload_size(), + d->configuration_.packet_payload_size())); + + d->datagram_.setData(data); + d->socket_.writeDatagram(d->datagram_); + } + } } MuebTransmitter& MuebTransmitter::Instance() { diff --git a/src/muebtransmitter_p.h b/src/muebtransmitter_p.h index 71fb818..19e4a95 100644 --- a/src/muebtransmitter_p.h +++ b/src/muebtransmitter_p.h @@ -25,67 +25,6 @@ class MuebTransmitterPrivate { .arg(configuration_.broadcast_animation_port()); } - void SendFrame(libmueb::Frame frame) { - QByteArray reduced_compressed_frame; - // Frame color reduction and compression - if (configuration_.color_depth() < 5) { - reduced_compressed_frame = - QtConcurrent::blockingMappedReduced<QByteArray>( - frame.constBits(), frame.constBits() + frame.sizeInBytes(), - /* Reference: - * http://threadlocalmutex.com/?p=48 - * http://threadlocalmutex.com/?page_id=60 - */ - [this](const uchar& color) -> uchar { - if (configuration_.color_depth() == 3) { - return (color * 225 + 4096) >> 13; - } else if (configuration_.color_depth() == 4) { - return (color * 15 + 135) >> 8; - } - - return color; - }, - [this](QByteArray& compressed_colors, const uchar& color) { - static bool msb{true}; - - // Compress 2 color components into 1 byte - if (msb) { - compressed_colors.append(color << configuration_.factor()); - } else { - compressed_colors.back() = compressed_colors.back() | color; - } - - msb = !msb; - }, - QtConcurrent::OrderedReduce | QtConcurrent::SequentialReduce); - } - // No compression - else { - reduced_compressed_frame.setRawData( - reinterpret_cast<const char*>(frame.bits()), frame.sizeInBytes()); - } - - if (configuration_.max_packet_number() == 1) { - reduced_compressed_frame.insert(0, configuration_.protocol_type()) - .insert(1, static_cast<char>(0) /*packet number*/); - - datagram_.setData(reduced_compressed_frame); - socket_.writeDatagram(datagram_); - } else { - for (std::uint8_t i = 0; i < configuration_.max_packet_number(); ++i) { - QByteArray data; - data.append(configuration_.protocol_type()) - .append(i /*packet number*/) - .append(reduced_compressed_frame.sliced( - i * configuration_.packet_payload_size(), - configuration_.packet_payload_size())); - - datagram_.setData(data); - socket_.writeDatagram(datagram_); - } - } - } - Configuration configuration_; QUdpSocket socket_; QNetworkDatagram datagram_; -- GitLab