Warning: This is a development version. The latest stable version is Version 4.0.0.
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
otacast.Encoder() and otacast.Decoder(), and discuss how to use them.
1#!/usr/bin/env python
2# encoding: utf-8
3
4"""
5In this example shows how to use the API of OTAcast to implement encoding and
6decoding.
7"""
8
9# License for Commercial Usage
10# Distributed under the "OTACAST EVALUATION LICENSE 1.3"
11# Licensees holding a valid commercial license may use this project in
12# accordance with the standard license agreement terms provided with the
13# Software (see accompanying file LICENSE.rst or
14# https://www.steinwurf.com/license), unless otherwise different terms and
15# conditions are agreed in writing between Licensee and Steinwurf ApS in which
16# case the license will be regulated by that separate written agreement.
17# License for Non-Commercial Usage
18# Distributed under the "OTACAST RESEARCH LICENSE 1.2"
19# Licensees holding a valid research license may use this project in accordance
20# with the license agreement terms provided with the Software
21# See accompanying file LICENSE.rst or https://www.steinwurf.com/license
22
23import otacast
24import os
25
26
27def main():
28
29 # Instantiate the encoder and decoder
30 encoder = otacast.Encoder()
31 decoder = otacast.Decoder()
32
33 # The size of the block determines the number of bytes that the encoder and
34 # decoder is working on. If you are encoding a file, the block bytes should
35 # be the file size in bytes.
36 block_bytes = 1024 * 10000 # 10 MB
37 # The symbol bytes determine the size of the produced symbols.
38 # In most cases it makes sense to make this value fit the MTU size of the
39 # network.
40 symbol_bytes = 1500
41 # The width manages a tradeoff between code
42 # effectiveness and computational complexity.
43 # A higher width causes higher code effectiveness, but also a higher memory
44 # footprint and computational complexity.
45 width = otacast.CodecWidth._8
46
47 # Configure the encoder and decoder
48 encoder.configure(width, block_bytes, symbol_bytes)
49 decoder.configure(width, block_bytes, symbol_bytes)
50
51 # Let's generate some random data to code
52 block_in = bytearray(os.urandom(block_bytes))
53 encoder.set_symbols_storage(block_in)
54
55 # Create some storage for the decoder to use
56 block_out = bytearray(decoder.block_bytes)
57 decoder.set_symbols_storage(block_out)
58
59 # This bytearray will contain the generated symbols.
60 symbol = bytearray(decoder.symbol_bytes)
61
62 # The seed can be anything, here it's basically a packet sequence number.
63 seed = 0
64 while not decoder.is_complete():
65 # Generate an encoded packet
66 offset = encoder.encode(symbol, seed)
67 # Decode the encoded packet
68 decoder.decode(symbol, seed, offset)
69 seed += 1
70
71 # Check if the decoding went as it should.
72 if block_in == block_out:
73 print("Decoding finished successfully!")
74 else:
75 print("Something went wrong!")
76
77
78if __name__ == "__main__":
79 main()