SipSessionExample » History » Version 5
Adrian Georgescu, 03/30/2010 11:21 PM
1 | 4 | Adrian Georgescu | == Sample Code == |
---|---|---|---|
2 | 1 | Adrian Georgescu | |
3 | 5 | Adrian Georgescu | [[TOC(WikiStart, Sip*, depth=1)]] |
4 | 1 | Adrian Georgescu | |
5 | 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. |
6 | 1 | Adrian Georgescu | |
7 | {{{ |
||
8 | from threading import Event |
||
9 | |||
10 | from application.notification import NotificationCenter |
||
11 | |||
12 | from sipsimple.core import SIPURI, ToHeader |
||
13 | |||
14 | from sipsimple.account import AccountManager |
||
15 | from sipsimple.application import SIPApplication |
||
16 | from sipsimple.configuration.backend.file import FileBackend |
||
17 | from sipsimple.lookup import DNSLookup, DNSLookupError |
||
18 | from sipsimple.session import Session |
||
19 | from sipsimple.streams import AudioStream |
||
20 | from sipsimple.util import Route, run_in_green_thread |
||
21 | |||
22 | |||
23 | class SimpleCallApplication(SIPApplication): |
||
24 | def __init__(self): |
||
25 | SIPApplication.__init__(self) |
||
26 | self.ended = Event() |
||
27 | self.callee = None |
||
28 | self.session = None |
||
29 | notification_center = NotificationCenter() |
||
30 | notification_center.add_observer(self) |
||
31 | |||
32 | |||
33 | def call(self, callee): |
||
34 | self.callee = callee |
||
35 | self.start(FileBackend('config')) |
||
36 | |||
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 | print 'Session started!' |
||
54 | |||
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 | application.call("sip:3333@ag-projects.com") |
||
69 | print "Placing call, press Enter to quit the program" |
||
70 | raw_input() |
||
71 | application.session.end() |
||
72 | application.ended.wait() |
||
73 | }}} |