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