forked from saghul/sipsimple-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsipsimple_hello_world.py
65 lines (52 loc) · 2.1 KB
/
sipsimple_hello_world.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
from application.notification import NotificationCenter
from threading import Event
from sipsimple.account import AccountManager
from sipsimple.application import SIPApplication
from sipsimple.storage import FileStorage
from sipsimple.core import SIPURI, ToHeader
from sipsimple.lookup import DNSLookup, DNSLookupError
from sipsimple.session import Session
from sipsimple.streams import AudioStream
from sipsimple.threading.green import run_in_green_thread
class SimpleCallApplication(SIPApplication):
def __init__(self):
SIPApplication.__init__(self)
self.ended = Event()
self.callee = None
self.session = None
notification_center = NotificationCenter()
notification_center.add_observer(self)
def call(self, callee):
self.callee = callee
self.start(FileStorage('test-config'))
@run_in_green_thread
def _NH_SIPApplicationDidStart(self, notification):
self.callee = ToHeader(SIPURI.parse(self.callee))
try:
routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait()
except DNSLookupError, e:
print 'DNS lookup failed: %s' % str(e)
else:
account = AccountManager().default_account
self.session = Session(account)
self.session.connect(self.callee, routes, [AudioStream()])
def _NH_SIPSessionGotRingIndication(self, notification):
print 'Ringing!'
def _NH_SIPSessionDidStart(self, notification):
print 'Session started!'
def _NH_SIPSessionDidFail(self, notification):
print 'Failed to connect'
self.stop()
def _NH_SIPSessionDidEnd(self, notification):
print 'Session ended'
self.stop()
def _NH_SIPApplicationDidEnd(self, notification):
self.ended.set()
# place an audio call to the specified URI
application = SimpleCallApplication()
application.call("sip:[email protected]")
print "Placing call, press Enter to quit the program"
raw_input()
if application.session:
application.session.end()
application.ended.wait()