Skip to content

Commit

Permalink
Added time.sleeps to non blocking while loops to lower CPU requirements.
Browse files Browse the repository at this point in the history
  • Loading branch information
tayler6000 committed Aug 14, 2020
1 parent 1308bbc commit b84c10f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
14 changes: 10 additions & 4 deletions pyVoIP/RTP.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def __init__(self):

def read(self, length=160):
while self.rebuilding: #This acts functionally as a lock while the buffer is being rebuilt.
print("Rebuilding")
time.sleep(0.01)
self.bufferLock.acquire()
packet = self.buffer.read(length)
if len(packet)<length:
Expand Down Expand Up @@ -249,8 +249,14 @@ def stop(self):
self.sin.close()
self.sout.close()

def read(self, length=160):
return self.pmin.read(length)
def read(self, length=160, blocking=False):
if not blocking:
return self.pmin.read(length)
packet = self.pmin.read(length)
while packet == bytes(length) and self.NSD:
time.sleep(0.01)
packet = self.pmin.read(length)
return packet

def write(self, data):
self.pmout.write(self.outOffset, data)
Expand All @@ -262,7 +268,7 @@ def recv(self):
packet = self.sin.recv(8192)
self.parsePacket(packet)
except BlockingIOError:
pass
time.sleep(0.01)
except RTPParseError as e:
print(str(e))
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions pyVoIP/SIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def recv(self):
else:
print("TODO: Add 400 Error on non processable request")
except BlockingIOError:
pass
time.sleep(0.01)
except SIPParseError as e:
if "SIP Version" in str(e):
request = self.genSIPVersionNotSupported(message)
Expand Down Expand Up @@ -712,7 +712,7 @@ def register(self):
self.out.sendto(regRequest.encode('utf8'), (self.server, self.port))

response = SIPMessage(self.s.recv(8192))

print(response.summary())
if response.status != SIPStatus(401):
if response.status == SIPStatus(500):
self.recvLock.release()
Expand Down
20 changes: 10 additions & 10 deletions pyVoIP/VoIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(self, phone, request, session_id, myIP, rtpPortLow, rtpPortHigh):
self.dtmfLock = Lock()
self.dtmf = io.StringIO()

self.RTPClients = []

self.connections = 0
self.audioPorts = 0
self.videoPorts = 0
Expand Down Expand Up @@ -68,7 +70,7 @@ def __init__(self, phone, request, session_id, myIP, rtpPortLow, rtpPortHigh):
print("Unable to assign ports for RTP.")
return

self.RTPClients = []


for i in request.body['m']:
assoc = {}
Expand Down Expand Up @@ -111,7 +113,6 @@ def getDTMF(self, length=1):
packet = self.dtmf.read(length)
self.dtmfLock.release()
return packet


def answer(self):
if self.state != CallState.RINGING:
Expand Down Expand Up @@ -152,17 +153,16 @@ def writeAudio(self, data):
for x in self.RTPClients:
x.write(data)

def readAudio(self, length = 160):
def readAudio(self, length=160, blocking=False):
if len(self.RTPClients) == 1:
return self.RTPClients[0].read(length, blocking)
data = []
for x in self.RTPClients:
data.append(x.read(length))
if len(data) == 1:
return data[0]
else:
nd = audioop.add(data.pop(0), data.pop(0), 1) #Mix audio from different sources before returning
for d in data:
nd = audioop.add(nd, d, 1)
return nd
nd = audioop.add(data.pop(0), data.pop(0), 1) #Mix audio from different sources before returning
for d in data:
nd = audioop.add(nd, d, 1)
return nd


class VoIPPhone():
Expand Down
2 changes: 1 addition & 1 deletion pyVoIP/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__all__ = ['SIP', 'RTP', 'VoIP']

version_info = (0, 5, 0, 'Beta')
version_info = (0, 5, 2, '')

__version__ = ".".join([str(x) for x in version_info])

Expand Down

0 comments on commit b84c10f

Please sign in to comment.