-
Notifications
You must be signed in to change notification settings - Fork 2
/
gums.py
215 lines (171 loc) · 4.79 KB
/
gums.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
"""
gums, Grande Unicast Multicast Sender
"""
import argparse
import os
import socket
import sys
import time
from functools import partial
from new_reader import reader
DGRAM_SIZE = 1316
DEFAULT_MULTICAST = "235.35.3.5:3535"
MAJOR = "0"
MINOR = "0"
MAINTAINENCE = "31"
def version():
"""
version prints version as a string
"""
return f"{MAJOR}.{MINOR}.{MAINTAINENCE}"
class GumS:
"""
GumS class is the UDP Unicast/Multicast Sender
"""
def __init__(self, addr=None, mttl=1, bind_addr="0.0.0.0"):
self.dest_ip, self.dest_port = addr.rsplit(":", 1)
self.src_ip = bind_addr.rsplit(":", 1)[0]
self.src_port = 0
self.ttl = mttl
self.dest_grp = (self.dest_ip, int(self.dest_port))
self.sock = self.mk_sock()
self.sock.bind((self.src_ip, self.src_port))
def is_multicast(self):
"""
is_multicast tests the first byte of an ipv4 address
to see if it is in the multicast range.
"""
net_id = int(self.dest_ip.split(".", 1)[0])
if net_id in range(224, 240):
return True
return False
def mk_sock(self):
"""
mk_sock makes a udp socket, self.sock
and sets a few opts.
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, "SO_REUSEPORT"):
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
return sock
def iter_dgrams(self, vid):
"""
iter_dgrams iterates over the video and sends
self.dgram_size chunks of video to the socket.
"""
million = 1024 * 1024
start_time = time.time()
now = time.time
total_bytes = 0
with reader(vid) as gum:
for dgram in iter(partial(gum.read, DGRAM_SIZE), b""):
self.sock.sendto(dgram, self.dest_grp)
total_bytes += len(dgram)
elapsed = now() - start_time
rate = (total_bytes / million) / elapsed
print(
f"\t{total_bytes/million:0.2f} MB sent in {elapsed:5.2f} seconds. {rate:3.2f} MB/Sec",
end="\r",
file=sys.stderr,
)
print("\n", file=sys.stderr)
def send_stream(self, vid):
"""
send_stream sets multicast ttl if needed,
prints socket address info,
calls self.iter_dgrams,
and closes the socket
"""
proto = "udp://"
pre = "Unicast"
if self.is_multicast():
self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self.ttl)
proto = proto + "@"
pre = "Multicast"
src_ip, src_port = self.sock.getsockname()
print(
f"\n\t{pre} Stream\n\t{proto}{self.dest_ip}:{self.dest_port}",
file=sys.stderr,
)
print(f"\n\tSource\n\t{src_ip}:{src_port}\n", file=sys.stderr)
self.iter_dgrams(vid)
self.sock.close()
def parse_args():
"""
parse_args parse command line args
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input",
default=None,
help="""like "/home/a/vid.ts"
or "udp://@235.35.3.5:3535"
or "https://futzu.com/xaa.ts"
""",
)
parser.add_argument(
"-a",
"--addr",
default=DEFAULT_MULTICAST,
help='Destination IP:Port like "227.1.3.10:4310"',
)
parser.add_argument(
"-b",
"--bind_addr",
default="0.0.0.0",
help='Local IP to bind to like "192.168.1.34". Default is 0.0.0.0',
)
parser.add_argument(
"-t",
"--ttl",
default=1,
help="Multicast TTL 1 - 255",
)
parser.add_argument(
"-v",
"--version",
action="store_const",
default=False,
const=True,
help="Show version",
)
return parser.parse_args()
def fork():
"""
fork
"""
pid = os.fork()
if pid > 0:
sys.exit(0)
def daemonize():
"""
The Steven's double fork
"""
fork()
fork()
def cli():
"""
cli adds command line args
passes them to a Gums instance
and calls self.send_stream
in just one function call
Use like this
import gums
if __name__ == "__main__":
gums.cli()
"""
args = parse_args()
if args.version:
print(version())
sys.exit()
# daemonize()
ttl = int(args.ttl).to_bytes(1, byteorder="big")
dest_addr = args.addr
gummie = GumS(dest_addr, ttl, args.bind_addr)
gummie.send_stream(args.input)
sys.exit()
if __name__ == "__main__":
cli()