SipSessionExample
Version 12 (Adrian Georgescu, 06/16/2011 12:51 pm)
1 | 1 | Adrian Georgescu | |
---|---|---|---|
2 | 12 | Adrian Georgescu | h2. Sample Code |
3 | 1 | Adrian Georgescu | |
4 | 12 | Adrian Georgescu | |
5 | 12 | Adrian Georgescu | This sample code implements a minimalist VoIP client. After [[SipInstallation|installing the SDK]], copy and paste this code into your Python interpreter to run it. |
6 | 12 | Adrian Georgescu | |
7 | 12 | Adrian Georgescu | <pre> |
8 | 10 | Adrian Georgescu | #/usr/bin/python |
9 | 1 | Adrian Georgescu | |
10 | 1 | Adrian Georgescu | from application.notification import NotificationCenter |
11 | 1 | Adrian Georgescu | |
12 | 1 | Adrian Georgescu | from sipsimple.account import AccountManager |
13 | 1 | Adrian Georgescu | from sipsimple.application import SIPApplication |
14 | 10 | Adrian Georgescu | from sipsimple.core import SIPURI, ToHeader |
15 | 7 | Patrick Simmons | from sipsimple.lookup import DNSLookup, DNSLookupError |
16 | 10 | Adrian Georgescu | from sipsimple.storage import FileStorage |
17 | 1 | Adrian Georgescu | from sipsimple.session import Session |
18 | 1 | Adrian Georgescu | from sipsimple.streams import AudioStream |
19 | 1 | Adrian Georgescu | from sipsimple.threading.green import run_in_green_thread |
20 | 10 | Adrian Georgescu | |
21 | 10 | Adrian Georgescu | from threading import Event |
22 | 1 | Adrian Georgescu | |
23 | 1 | Adrian Georgescu | class SimpleCallApplication(SIPApplication): |
24 | 11 | Adrian Georgescu | |
25 | 1 | Adrian Georgescu | def __init__(self): |
26 | 1 | Adrian Georgescu | SIPApplication.__init__(self) |
27 | 1 | Adrian Georgescu | self.ended = Event() |
28 | 1 | Adrian Georgescu | self.callee = None |
29 | 1 | Adrian Georgescu | self.session = None |
30 | 1 | Adrian Georgescu | notification_center = NotificationCenter() |
31 | 1 | Adrian Georgescu | notification_center.add_observer(self) |
32 | 1 | Adrian Georgescu | |
33 | 1 | Adrian Georgescu | def call(self, callee): |
34 | 1 | Adrian Georgescu | self.callee = callee |
35 | 7 | Patrick Simmons | self.start(FileStorage('config')) |
36 | 1 | Adrian Georgescu | |
37 | 1 | Adrian Georgescu | @run_in_green_thread |
38 | 1 | Adrian Georgescu | def _NH_SIPApplicationDidStart(self, notification): |
39 | 1 | Adrian Georgescu | self.callee = ToHeader(SIPURI.parse(self.callee)) |
40 | 1 | Adrian Georgescu | try: |
41 | 1 | Adrian Georgescu | routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait() |
42 | 1 | Adrian Georgescu | except DNSLookupError, e: |
43 | 1 | Adrian Georgescu | print 'DNS lookup failed: %s' % str(e) |
44 | 1 | Adrian Georgescu | else: |
45 | 1 | Adrian Georgescu | account = AccountManager().default_account |
46 | 1 | Adrian Georgescu | self.session = Session(account) |
47 | 1 | Adrian Georgescu | self.session.connect(self.callee, routes, [AudioStream(account)]) |
48 | 1 | Adrian Georgescu | |
49 | 1 | Adrian Georgescu | def _NH_SIPSessionGotRingIndication(self, notification): |
50 | 1 | Adrian Georgescu | print 'Ringing!' |
51 | 1 | Adrian Georgescu | |
52 | 1 | Adrian Georgescu | def _NH_SIPSessionDidStart(self, notification): |
53 | 9 | Adrian Georgescu | audio_stream = notification.data.streams[0] |
54 | 9 | Adrian Georgescu | print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate) |
55 | 1 | Adrian Georgescu | |
56 | 1 | Adrian Georgescu | def _NH_SIPSessionDidFail(self, notification): |
57 | 1 | Adrian Georgescu | print 'Failed to connect' |
58 | 1 | Adrian Georgescu | self.stop() |
59 | 1 | Adrian Georgescu | |
60 | 1 | Adrian Georgescu | def _NH_SIPSessionDidEnd(self, notification): |
61 | 1 | Adrian Georgescu | print 'Session ended' |
62 | 1 | Adrian Georgescu | self.stop() |
63 | 1 | Adrian Georgescu | |
64 | 1 | Adrian Georgescu | def _NH_SIPApplicationDidEnd(self, notification): |
65 | 1 | Adrian Georgescu | self.ended.set() |
66 | 1 | Adrian Georgescu | |
67 | 11 | Adrian Georgescu | # place an audio call to the specified SIP URI in user@domain format |
68 | 1 | Adrian Georgescu | target_uri="sip:3333@sip2sip.info" |
69 | 11 | Adrian Georgescu | application = SimpleCallApplication() |
70 | 9 | Adrian Georgescu | application.call(target_uri) |
71 | 1 | Adrian Georgescu | print "Placing call to %s, press Enter to quit the program" % target_uri |
72 | 1 | Adrian Georgescu | raw_input() |
73 | 1 | Adrian Georgescu | application.session.end() |
74 | 1 | Adrian Georgescu | application.ended.wait() |
75 | 12 | Adrian Georgescu | </pre> |