Warning: This is an old version. The latest stable version is Version 10.0.0.

Code Example: Encode and Decode

In this example shows how to use the API of otacast to implement encoding and decoding.

The example will walk through the basic functionality of class encoder and class decoder, and discuss how to use them.

 1#include <algorithm>
 2#include <cstdlib>
 3#include <iostream>
 4#include <vector>
 5
 6#include <otacast/decoder.hpp>
 7#include <otacast/encoder.hpp>
 8#include <otacast/version.hpp>
 9
10int main()
11{
12    // Create the encoder/decoder objects with a specific finite field
13    otacast::encoder encoder;
14    otacast::decoder decoder;
15
16    std::size_t block_bytes = 1024 * 1000 * 500; // 500 MB
17    std::size_t symbol_bytes = 1500;             // 1.5 kB
18    auto width = otacast::codec_width::_32;
19
20    encoder.configure(width, block_bytes, symbol_bytes);
21    decoder.configure(width, block_bytes, symbol_bytes);
22
23    // Allocate some data to decode
24    std::vector<uint8_t> data_in(encoder.block_bytes());
25
26    // For the example - fill data_in with random data
27    std::generate(data_in.begin(), data_in.end(), rand);
28
29    // Assign the data buffer to the encoder so that we may start
30    // to produce encoded symbols from it
31    encoder.set_symbols_storage(data_in.data());
32
33    // Define a data buffer where the symbols should be decoded
34    std::vector<uint8_t> data_out(decoder.block_bytes());
35    decoder.set_symbols_storage(data_out.data());
36
37    // Initiate a zero-vector for the offset
38    std::vector<uint8_t> symbol(encoder.symbol_bytes());
39
40    // The seed can be anything, here it's basically a packet sequence number.
41    // Will be used to count transmissions as well
42    std::size_t transmissions = 0;
43
44    // Count number of packets received
45    std::size_t received = 0;
46
47    // Set up some packet loss
48    auto loss_probability = 10;
49
50    while (!decoder.is_complete())
51    {
52        // Generate an encoded packet using the number of transmissions as seed
53        auto offset = encoder.encode(symbol.data(), transmissions);
54
55        // We see if the packet is lost
56        if (rand() % 100 >= loss_probability)
57        {
58            // Decode the encoded packet using the number of transmissions as
59            // seed
60            decoder.decode(symbol.data(), transmissions, offset);
61            received++;
62        }
63        transmissions++;
64    }
65
66    // Calculate the overhead
67    auto data_received = received * decoder.symbol_bytes();
68    auto overhead = (data_received / (double)decoder.block_bytes()) - 1;
69
70    // Check if everything went as it should
71    if (data_in == data_out)
72    {
73        std::cout << "Decoding finished successfully!" << std::endl;
74        std::cout << "Overhead: " << overhead * 100 << "%" << std::endl;
75    }
76    else
77    {
78        std::cout << "Something went wrong!" << std::endl;
79    }
80
81    return 0;
82}