SipSessionExample
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 | 1 | Adrian Georgescu | {{{ |
8 | 1 | Adrian Georgescu | from threading import Event |
9 | 1 | Adrian Georgescu | |
10 | 1 | Adrian Georgescu | from application.notification import NotificationCenter |
11 | 1 | Adrian Georgescu | |
12 | 1 | Adrian Georgescu | from sipsimple.core import SIPURI, ToHeader |
13 | 1 | Adrian Georgescu | |
14 | 1 | Adrian Georgescu | from sipsimple.account import AccountManager |
15 | 1 | Adrian Georgescu | from sipsimple.application import SIPApplication |
16 | 1 | Adrian Georgescu | from sipsimple.configuration.backend.file import FileBackend |
17 | 1 | Adrian Georgescu | from sipsimple.lookup import DNSLookup, DNSLookupError |
18 | 1 | Adrian Georgescu | from sipsimple.session import Session |
19 | 1 | Adrian Georgescu | from sipsimple.streams import AudioStream |
20 | 1 | Adrian Georgescu | from sipsimple.util import Route, run_in_green_thread |
21 | 1 | Adrian Georgescu | |
22 | 1 | Adrian Georgescu | |
23 | 1 | Adrian Georgescu | class SimpleCallApplication(SIPApplication): |
24 | 1 | Adrian Georgescu | def __init__(self): |
25 | 1 | Adrian Georgescu | SIPApplication.__init__(self) |
26 | 1 | Adrian Georgescu | self.ended = Event() |
27 | 1 | Adrian Georgescu | self.callee = None |
28 | 1 | Adrian Georgescu | self.session = None |
29 | 1 | Adrian Georgescu | notification_center = NotificationCenter() |
30 | 1 | Adrian Georgescu | notification_center.add_observer(self) |
31 | 1 | Adrian Georgescu | |
32 | 1 | Adrian Georgescu | |
33 | 1 | Adrian Georgescu | def call(self, callee): |
34 | 1 | Adrian Georgescu | self.callee = callee |
35 | 1 | Adrian Georgescu | self.start(FileBackend('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 | 1 | Adrian Georgescu | print 'Session started!' |
54 | 1 | Adrian Georgescu | |
55 | 1 | Adrian Georgescu | def _NH_SIPSessionDidFail(self, notification): |
56 | 1 | Adrian Georgescu | print 'Failed to connect' |
57 | 1 | Adrian Georgescu | self.stop() |
58 | 1 | Adrian Georgescu | |
59 | 1 | Adrian Georgescu | def _NH_SIPSessionDidEnd(self, notification): |
60 | 1 | Adrian Georgescu | print 'Session ended' |
61 | 1 | Adrian Georgescu | self.stop() |
62 | 1 | Adrian Georgescu | |
63 | 1 | Adrian Georgescu | def _NH_SIPApplicationDidEnd(self, notification): |
64 | 1 | Adrian Georgescu | self.ended.set() |
65 | 1 | Adrian Georgescu | |
66 | 1 | Adrian Georgescu | # place an audio call to the specified URI |
67 | 1 | Adrian Georgescu | application = SimpleCallApplication() |
68 | 1 | Adrian Georgescu | application.call("sip:3333@ag-projects.com") |
69 | 1 | Adrian Georgescu | print "Placing call, press Enter to quit the program" |
70 | 1 | Adrian Georgescu | raw_input() |
71 | 1 | Adrian Georgescu | application.session.end() |
72 | 1 | Adrian Georgescu | application.ended.wait() |
73 | 1 | Adrian Georgescu | }}} |