SipSessionExample

Version 7 (Patrick Simmons, 05/31/2011 04:07 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 1 Adrian Georgescu
{{{
6 1 Adrian Georgescu
from threading import Event
7 1 Adrian Georgescu
8 1 Adrian Georgescu
from application.notification import NotificationCenter
9 1 Adrian Georgescu
10 1 Adrian Georgescu
from sipsimple.core import SIPURI, ToHeader
11 1 Adrian Georgescu
12 1 Adrian Georgescu
from sipsimple.account import AccountManager
13 1 Adrian Georgescu
from sipsimple.application import SIPApplication
14 7 Patrick Simmons
from sipsimple.storage import FileStorage
15 1 Adrian Georgescu
from sipsimple.lookup import DNSLookup, DNSLookupError
16 1 Adrian Georgescu
from sipsimple.session import Session
17 1 Adrian Georgescu
from sipsimple.streams import AudioStream
18 1 Adrian Georgescu
from sipsimple.util import Route, run_in_green_thread
19 1 Adrian Georgescu
20 1 Adrian Georgescu
21 1 Adrian Georgescu
class SimpleCallApplication(SIPApplication):
22 1 Adrian Georgescu
    def __init__(self):
23 1 Adrian Georgescu
        SIPApplication.__init__(self)
24 1 Adrian Georgescu
        self.ended = Event()
25 1 Adrian Georgescu
        self.callee = None
26 1 Adrian Georgescu
        self.session = None
27 1 Adrian Georgescu
        notification_center = NotificationCenter()
28 1 Adrian Georgescu
        notification_center.add_observer(self)
29 1 Adrian Georgescu
30 1 Adrian Georgescu
31 1 Adrian Georgescu
    def call(self, callee):
32 1 Adrian Georgescu
        self.callee = callee
33 7 Patrick Simmons
        self.start(FileStorage('config'))
34 1 Adrian Georgescu
35 1 Adrian Georgescu
    @run_in_green_thread
36 1 Adrian Georgescu
    def _NH_SIPApplicationDidStart(self, notification):
37 1 Adrian Georgescu
        self.callee = ToHeader(SIPURI.parse(self.callee))
38 1 Adrian Georgescu
        try:
39 1 Adrian Georgescu
            routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait()
40 1 Adrian Georgescu
        except DNSLookupError, e:
41 1 Adrian Georgescu
            print 'DNS lookup failed: %s' % str(e)
42 1 Adrian Georgescu
        else:
43 1 Adrian Georgescu
            account = AccountManager().default_account
44 1 Adrian Georgescu
            self.session = Session(account)
45 1 Adrian Georgescu
            self.session.connect(self.callee, routes, [AudioStream(account)])
46 1 Adrian Georgescu
47 1 Adrian Georgescu
    def _NH_SIPSessionGotRingIndication(self, notification):
48 1 Adrian Georgescu
        print 'Ringing!'
49 1 Adrian Georgescu
50 1 Adrian Georgescu
    def _NH_SIPSessionDidStart(self, notification):
51 1 Adrian Georgescu
        print 'Session started!'
52 1 Adrian Georgescu
53 1 Adrian Georgescu
    def _NH_SIPSessionDidFail(self, notification):
54 1 Adrian Georgescu
        print 'Failed to connect'
55 1 Adrian Georgescu
        self.stop()
56 1 Adrian Georgescu
57 1 Adrian Georgescu
    def _NH_SIPSessionDidEnd(self, notification):
58 1 Adrian Georgescu
        print 'Session ended'
59 1 Adrian Georgescu
        self.stop()
60 1 Adrian Georgescu
61 1 Adrian Georgescu
    def _NH_SIPApplicationDidEnd(self, notification):
62 1 Adrian Georgescu
        self.ended.set()
63 1 Adrian Georgescu
64 1 Adrian Georgescu
# place an audio call to the specified URI
65 1 Adrian Georgescu
application = SimpleCallApplication()
66 1 Adrian Georgescu
application.call("sip:3333@ag-projects.com")
67 1 Adrian Georgescu
print "Placing call, press Enter to quit the program"
68 1 Adrian Georgescu
raw_input()
69 1 Adrian Georgescu
application.session.end()
70 1 Adrian Georgescu
application.ended.wait()
71 1 Adrian Georgescu
}}}