More slight refactoring
This commit is contained in:
parent
bad666456c
commit
faeece7f1d
|
@ -25,18 +25,6 @@ from random import shuffle
|
|||
##
|
||||
## If you can get this working using PJSUA2, a pull request would be greatly appreciated.
|
||||
|
||||
# This feels real hacky but I'm not sure there's a better way of creating a generic object that
|
||||
# you can add properties to.
|
||||
class State(object):
|
||||
pass
|
||||
state=State()
|
||||
|
||||
# Configuration
|
||||
state.LOG_LEVEL=int(getenv('TT_LOG_LEVEL', 0))
|
||||
#TT_MEDIA_SOURCE and TT_LISTEN_PORT can be configured via env. variables
|
||||
state.source=getenv('TT_MEDIA_SOURCE', '/opt/media/')
|
||||
state.port=int(getenv('TT_LISTEN_PORT', 5062))
|
||||
# End configuration
|
||||
|
||||
# Application scaffolding
|
||||
# logger functions
|
||||
|
@ -66,10 +54,19 @@ def sighandle(_signo, _stack_frame):
|
|||
state.running=False
|
||||
pass
|
||||
|
||||
# Classes
|
||||
# Utility classes, used basically as enums or generics
|
||||
class State(object):
|
||||
running=False
|
||||
class PJStates:
|
||||
init=0
|
||||
deinit=1
|
||||
class SIPStates:
|
||||
ringing=180
|
||||
answer=200
|
||||
|
||||
state=State()
|
||||
|
||||
# Classes
|
||||
# Account Callback class
|
||||
class AccountCb(pj.AccountCallback):
|
||||
def __init__(self, account=None):
|
||||
|
@ -122,8 +119,9 @@ class CallCb(pj.CallCallback):
|
|||
Log(4, "event-media-state-change", "Media State transitioned to INACTIVE")
|
||||
|
||||
# Main logic functions
|
||||
def PJInit():
|
||||
def PJControl(action):
|
||||
global state
|
||||
if action == PJStates.init:
|
||||
state.lib=pj.Lib()
|
||||
state.cfg_ua=pj.UAConfig()
|
||||
state.cfg_md=pj.MediaConfig()
|
||||
|
@ -148,14 +146,7 @@ def PJInit():
|
|||
cb=AccountCb()
|
||||
)
|
||||
state.uri="sip:%s:%s" % (state.transport.info().host, state.transport.info().port)
|
||||
|
||||
def WaitLoop():
|
||||
global state
|
||||
while state.running:
|
||||
sleep(0.2)
|
||||
|
||||
def PjDeinit():
|
||||
global state
|
||||
elif action == PJStates.deinit:
|
||||
state.lib.hangup_all()
|
||||
# allow time for cleanup before destroying objects
|
||||
state.lib.handle_events(timeout=250)
|
||||
|
@ -170,6 +161,11 @@ def PjDeinit():
|
|||
Log(1, "deinit", "pjsip error when clearing down: %s" % str(e), error=True)
|
||||
pass
|
||||
|
||||
def WaitLoop():
|
||||
global state
|
||||
while state.running:
|
||||
sleep(0.2)
|
||||
|
||||
def MediaLoadPlaylist():
|
||||
Log(3, "playlist-load", "loading playlist files")
|
||||
global state
|
||||
|
@ -183,8 +179,12 @@ def MediaLoadPlaylist():
|
|||
"load playlist from %s, got %s files" % (state.source, len(state.playlist)))
|
||||
|
||||
def main():
|
||||
Log(1, "init", "initialising trashtalker")
|
||||
global state
|
||||
Log(1, "init", "initialising trashtalker")
|
||||
state.LOG_LEVEL=int(getenv('TT_LOG_LEVEL', 0))
|
||||
#TT_MEDIA_SOURCE and TT_LISTEN_PORT can be configured via env. variables
|
||||
state.source=getenv('TT_MEDIA_SOURCE', '/opt/media/')
|
||||
state.port=int(getenv('TT_LISTEN_PORT', 55060))
|
||||
state.running=True
|
||||
signal(SIGHUP, sighandle)
|
||||
signal(SIGINT, sighandle)
|
||||
|
@ -196,7 +196,7 @@ def main():
|
|||
Log(2, "playlist-load", "exception encountered while loading playlist from path %s" % state.source, error=True)
|
||||
raise Exception("Unable to load playlist")
|
||||
try:
|
||||
PJInit()
|
||||
PJControl(PJStates.init)
|
||||
except:
|
||||
Log(2, "pj-init", "Unable to initialise pjsip library; please check media path and SIP listening port are correct", error=True)
|
||||
raise Exception("Unable to initialise pjsip library; please check media path and SIP listening port are correct")
|
||||
|
@ -211,7 +211,7 @@ def main():
|
|||
state.running=False
|
||||
pass
|
||||
Log(1, "deinit", "main loop exited, shutting down")
|
||||
PjDeinit()
|
||||
PJControl(PJStates.deinit)
|
||||
Log(1, "deinit-complete", "trashtalker has shut down")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
Loading…
Reference in New Issue