File: messages.py

package info (click to toggle)
python-snapcast 2.3.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: python: 1,564; makefile: 9
file content (176 lines) | stat: -rw-r--r-- 3,758 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
""" Snapcast messages. """

from construct import (ULInt8, ULInt16, ULInt32, ULInt64,
                       Embed, Struct, Enum, Array,
                       PascalString, Switch, Container)

ENCODING = 'utf-8'
BASE_SIZE = 26

# pylint: disable=bad-continuation,invalid-name

timestamp = Embed(Struct('time',
    ULInt32('secs'),
    ULInt32('usecs')
))


snaptype = Enum(ULInt16('type'),
    Base=0,
    Header=1,
    WireChunk=2,
    SampleFormat=3,
    ServerSettings=4,
    Time=5,
    Request=6,
    Ack=7,
    Command=8,
    Hello=9,
    Map=10,
    String=11
)


basemessage = Struct('base',
    snaptype,
    ULInt16('id'),
    ULInt16('refer'),
    Struct('sent', timestamp),
    Struct('recv', timestamp),
    ULInt32('payload_length'),
)


mapmessage = Struct('map',
    ULInt16('num'),
    Array(lambda ctx: ctx.num, Struct('map',
        PascalString('field', length_field=ULInt16('length')),
        PascalString('value', length_field=ULInt16('length'))
    ))
)


stringmessage = Struct('string',
    PascalString('string', length_field=ULInt16('string_length'))
)


request = Struct('request',
    Struct('request_type', Embed(snaptype))
)


server_settings = Struct('settings',
    ULInt32('buffer_ms'),
    ULInt32('latency'),
    ULInt16('volume'),
    ULInt8('mute')
)


sample_format = Struct('sample',
    ULInt32('rate'),
    ULInt16('bits'),
    ULInt16('channels'),
    ULInt16('sample_size'),
    ULInt16('frame_size')
)


header = Struct('header',
    PascalString('codec', length_field=ULInt16('codec_length')),
    PascalString('header', length_field=ULInt32('header_length'))
)


time = Struct('header',
    ULInt64('latency')
)


chunk = Struct('chunk',
    Struct('timestamp', timestamp),
    PascalString('chunk', length_field=ULInt32('chunk_length'))
)


hello = mapmessage
command = stringmessage
ack = stringmessage


packet = Struct('packet',
    Embed(basemessage),
    Switch('payload', lambda ctx: ctx.type,
        {
            'Header': header,
            'Time': time,
            'WireChunk': chunk,
            'SampleFormat': sample_format,
            'ServerSettings': server_settings,
            'Request': request,
            'Hello': hello,
            'Command': command,
            'Ack': ack
        }
    )
)

# pylint: enable=bad-continuation,invalid-name


def message(message_type, payload, payload_length):
    """ Build a message. """
    return packet.build(
        Container(
            type=message_type,
            id=1,
            refer=0,
            sent=Container(
                secs=0,
                usecs=0
            ),
            recv=Container(
                secs=0,
                usecs=0
            ),
            payload_length=payload_length,
            payload=payload
        )
    )


def map_helper(data):
    """ Build a map message. """
    as_list = []
    length = 2
    for field, value in data.items():
        as_list.append(Container(field=bytes(field, ENCODING),
                                 value=bytes(value, ENCODING)))
        length += len(field) + len(value) + 4
    return (Container(
        num=len(as_list),
        map=as_list
    ), length)


def hello_packet(hostname, mac, version):
    """ Build a hello message. """
    return message('Hello', *map_helper({
        'HostName': hostname,
        'MAC': mac,
        'Version': version
    }))


def request_packet(request_type):
    """ Build a request message. """
    return message('Request', Container(request_type=request_type), 2)


def command_packet(cmd):
    """ Build a command message. """
    return message('Command',
                   Container(string_length=len(cmd),
                             string=bytes(cmd, ENCODING)),
                   len(cmd) + 2)