-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcReceiver.py
55 lines (45 loc) · 1.52 KB
/
mcReceiver.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
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import pyaudio
import wave
import time
import pickle
# PyAudio configuration
SIZE_PACKAGE = 1024
CHUNK = 1024
CHANNELS = 1
RATE = 10240
INPUT = True
FORMAT = pyaudio.paInt16
CHUNK_COUNT = 0
p = pyaudio.PyAudio()
stream = p.open(format=8,
channels=1,
rate=44100,
output=True)
class MulticastPingPong(DatagramProtocol):
def __init__(self):
self.CHUNK_COUNT = 0
def startProtocol(self):
"""
Called after protocol has started listening.
"""
# Set the TTL=1 so multicast will NOT cross router hops:
self.transport.setTTL(1)
# Join a specific multicast group:
self.transport.joinGroup("228.0.0.5")
def datagramReceived(self, datagram, address):
#print "Datagram %s received from %s" % (repr(datagram), repr(address))
if datagram == "Server: Ping":
print "Joined multicast group sucessfully at 228.0.0.5 "
else:
count, stream_data = pickle.loads(datagram)
print "data received {}".format(type(stream_data))
print "Playing chunk {}".format(count)
stream.write(stream_data)
self.CHUNK_COUNT += 1
# We use listenMultiple=True so that we can run MulticastServer.py and
# MulticastClient.py on same machine:
reactor.listenMulticast(8005, MulticastPingPong(),
listenMultiple=True)
reactor.run()