File: codec2_udp_sink.py

package info (click to toggle)
gr-satellites 5.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,836 kB
  • sloc: python: 29,546; cpp: 5,448; ansic: 1,247; sh: 118; makefile: 24
file content (78 lines) | stat: -rw-r--r-- 2,469 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright 2019, 2022 Daniel Estevez <daniel@destevez.net>
#
# This file is part of gr-satellites
#
# SPDX-License-Identifier: GPL-3.0-or-later
#

from gnuradio import gr, blocks


if gr.api_version() != '9':
    from gnuradio import network

from ...utils.options_block import options_block
from ...grtypes import byte_t


class codec2_udp_sink(gr.hier_block2, options_block):
    """
    Hierarchical block for Codec2 UDP output

    The input are PDUs with Codec2 data

    The Codec2 data is sent to by PDU, a single frame (7 bytes)
    per packet for lowest latency.

    Args:
      ip: Detination IP (string)
      port: Destination UDP port (int)
    """
    def __init__(self, ip=None, port=None, options=None):
        gr.hier_block2.__init__(
            self,
            'codec2_udp_sink',
            gr.io_signature(0, 0, 0),
            gr.io_signature(0, 0, 0))
        options_block.__init__(self, options)
        self.message_port_register_hier_in('in')

        if ip is None:
            ip = self.options.codec2_ip
        if port is None:
            port = self.options.codec2_port

        self.pdu2tag = blocks.pdu_to_tagged_stream(byte_t, 'packet_len')
        payload_bytes = 7
        # The UDP sink has been moved in GNU Radio 3.10
        if gr.api_version() == '9':
            self.udp = blocks.udp_sink(
                gr.sizeof_char*1, ip, port, payload_bytes, False)
        else:
            udp_header = 0  # no header
            # The new UDP sink requires at least 8 bytes of payload.
            # We use 14 bytes, which is 2 codec2 frames.
            payload_bytes = 14
            self.udp = network.udp_sink(gr.sizeof_char, 1,
                                        ip, port, udp_header,
                                        payload_bytes, False)
        self.msg_connect((self, 'in'), (self.pdu2tag, 'pdus'))
        self.connect(self.pdu2tag, self.udp)

    _default_ip = '127.0.0.1'
    _default_port = 7000

    @classmethod
    def add_options(cls, parser):
        """
        Adds Codec2 UDP output specific options to the argparse parser
        """
        parser.add_argument(
            '--codec2_ip', type=str, default=cls._default_ip,
            help='Codec2 output IP [default=%(default)r]')
        parser.add_argument(
            '--codec2_port', type=int, default=cls._default_port,
            help='Codec2 output UDP port [default=%(default)r]')