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