SipSessionExample

Version 9 (Adrian Georgescu, 06/12/2011 08:55 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 8 Adrian Georgescu
from sipsimple.threading.green import 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 9 Adrian Georgescu
        audio_stream = notification.data.streams[0]
52 9 Adrian Georgescu
        print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate)
53 1 Adrian Georgescu
54 1 Adrian Georgescu
    def _NH_SIPSessionDidFail(self, notification):
55 1 Adrian Georgescu
        print 'Failed to connect'
56 1 Adrian Georgescu
        self.stop()
57 1 Adrian Georgescu
58 1 Adrian Georgescu
    def _NH_SIPSessionDidEnd(self, notification):
59 1 Adrian Georgescu
        print 'Session ended'
60 1 Adrian Georgescu
        self.stop()
61 1 Adrian Georgescu
62 1 Adrian Georgescu
    def _NH_SIPApplicationDidEnd(self, notification):
63 1 Adrian Georgescu
        self.ended.set()
64 1 Adrian Georgescu
65 1 Adrian Georgescu
# place an audio call to the specified URI
66 1 Adrian Georgescu
application = SimpleCallApplication()
67 9 Adrian Georgescu
target_uri="sip:3333@sip2sip.info"
68 9 Adrian Georgescu
application.call(target_uri)
69 9 Adrian Georgescu
print "Placing call to %s, press Enter to quit the program" % target_uri
70 1 Adrian Georgescu
raw_input()
71 1 Adrian Georgescu
application.session.end()
72 1 Adrian Georgescu
application.ended.wait()
73 1 Adrian Georgescu
}}}