X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=rsencoder.cpp;h=719d548bde48dcaa073181c761137e59a60993b4;hb=6a75d93c88d8a94e63e0814c5e049572b887fa85;hp=3026dd4d2373fdc552fbe1660ab88dbf37ce818e;hpb=02120e9414a6e613b17d6284891593271659b5d9;p=greproxy diff --git a/rsencoder.cpp b/rsencoder.cpp index 3026dd4..719d548 100644 --- a/rsencoder.cpp +++ b/rsencoder.cpp @@ -5,9 +5,7 @@ #include #include #include -extern "C" { -#include -} +#include #include "reorderer.h" #include "rsencoder.h" @@ -16,6 +14,12 @@ extern "C" { #include using namespace std; + +RSEncoder::RSEncoder(Sender *sender) + : sender(sender), + rs(RS_PAYLOAD_SIZE, RS_GROUP_SIZE) +{ +} void RSEncoder::send_packet(uint16_t proto, const std::string &data, int incoming_seq) { @@ -45,7 +49,9 @@ void RSEncoder::finish_group() max_length = max(max_length, packet_history[i].data.size()); } - vector padded_packets; + string padded_packets; + padded_packets.reserve((max_length + 4) * packet_history.size()); + // TODO: RS_PAD for (int i = 0; i < packet_history.size(); ++i) { string p; p.resize(max_length + 4); @@ -55,37 +61,22 @@ void RSEncoder::finish_group() uint16_t len_be = htons(packet_history[i].data.size()); memcpy(&p[2], &len_be, sizeof(uint16_t)); memcpy(&p[4], packet_history[i].data.data(), packet_history[i].data.size()); - padded_packets.push_back(p); + padded_packets += p; } - // Now construct RS packets. - vector rs_packets; - for (int i = 0; i < RS_PARITY_SIZE; ++i) { - string p; - p.resize(max_length + 4); - memset(&p[0], 0, max_length + 4); - rs_packets.push_back(p); - } - string data, parity; - data.resize(RS_PAYLOAD_SIZE); - parity.resize(RS_PARITY_SIZE); - for (int i = 0; i < max_length + 4; ++i) { - for (int j = 0; j < packet_history.size(); ++j) { - data[j] = packet_history[j].data[i]; - } - encode_rs_8(reinterpret_cast(&data[0]), - reinterpret_cast(&parity[0]), - RS_PAD); - for (int j = 0; j < RS_PARITY_SIZE; ++j) { - rs_packets[j][i] = parity[j]; - } - } + // Now construct and send RS packets. + rs.encode(reinterpret_cast(padded_packets.data()), + padded_packets.size(), + [&](size_t packet_num, size_t num_packets, const fecpp::byte data[], size_t size) { + // The first N packets are just the original ones; ignore them. + if (packet_num < RS_PAYLOAD_SIZE) { + return; + } - // Actually send the RS packets. - int start_seq = packet_history[0].seq - 1; - for (int i = 0; i < RS_PARITY_SIZE; ++i) { - sender->send_packet(0xffff, rs_packets[i], start_seq - i); - } + const char *sdata = reinterpret_cast(data); + int start_seq = packet_history[0].seq - 1; + sender->send_packet(0xffff, string(sdata, size), start_seq - (packet_num - RS_PAYLOAD_SIZE)); + }); packet_history.clear(); }