SipSessionExample » History » 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 | 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 | |||
7 | <pre> |
||
8 | 10 | Adrian Georgescu | #/usr/bin/python |
9 | 1 | Adrian Georgescu | |
10 | from application.notification import NotificationCenter |
||
11 | |||
12 | from sipsimple.account import AccountManager |
||
13 | 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 | from sipsimple.streams import AudioStream |
||
19 | from sipsimple.threading.green import run_in_green_thread |
||
20 | 10 | Adrian Georgescu | |
21 | from threading import Event |
||
22 | 1 | Adrian Georgescu | |
23 | class SimpleCallApplication(SIPApplication): |
||
24 | 11 | Adrian Georgescu | |
25 | 1 | Adrian Georgescu | def __init__(self): |
26 | SIPApplication.__init__(self) |
||
27 | self.ended = Event() |
||
28 | self.callee = None |
||
29 | self.session = None |
||
30 | notification_center = NotificationCenter() |
||
31 | notification_center.add_observer(self) |
||
32 | |||
33 | def call(self, callee): |
||
34 | self.callee = callee |
||
35 | 7 | Patrick Simmons | self.start(FileStorage('config')) |
36 | 1 | Adrian Georgescu | |
37 | @run_in_green_thread |
||
38 | def _NH_SIPApplicationDidStart(self, notification): |
||
39 | self.callee = ToHeader(SIPURI.parse(self.callee)) |
||
40 | try: |
||
41 | routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait() |
||
42 | except DNSLookupError, e: |
||
43 | print 'DNS lookup failed: %s' % str(e) |
||
44 | else: |
||
45 | account = AccountManager().default_account |
||
46 | self.session = Session(account) |
||
47 | self.session.connect(self.callee, routes, [AudioStream(account)]) |
||
48 | |||
49 | def _NH_SIPSessionGotRingIndication(self, notification): |
||
50 | print 'Ringing!' |
||
51 | |||
52 | def _NH_SIPSessionDidStart(self, notification): |
||
53 | 9 | Adrian Georgescu | audio_stream = notification.data.streams[0] |
54 | print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate) |
||
55 | 1 | Adrian Georgescu | |
56 | def _NH_SIPSessionDidFail(self, notification): |
||
57 | print 'Failed to connect' |
||
58 | self.stop() |
||
59 | |||
60 | def _NH_SIPSessionDidEnd(self, notification): |
||
61 | print 'Session ended' |
||
62 | self.stop() |
||
63 | |||
64 | def _NH_SIPApplicationDidEnd(self, notification): |
||
65 | self.ended.set() |
||
66 | |||
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 | raw_input() |
||
73 | application.session.end() |
||
74 | application.ended.wait() |
||
75 | 12 | Adrian Georgescu | </pre> |