SipSessionExample

Version 14 (Tijmen de Mes, 04/19/2012 04:08 pm)

1 12 Adrian Georgescu
h2. Sample Code
2 1 Adrian Georgescu
3 12 Adrian Georgescu
4 12 Adrian Georgescu
This sample code implements a minimalist VoIP client. After [[SipInstallation|installing the SDK]], copy and paste this code into your Python interpreter to run it.
5 1 Adrian Georgescu
6 14 Tijmen de Mes
<pre>
7 10 Adrian Georgescu
#/usr/bin/python
8 1 Adrian Georgescu
9 1 Adrian Georgescu
from application.notification import NotificationCenter
10 1 Adrian Georgescu
11 1 Adrian Georgescu
from sipsimple.account import AccountManager
12 1 Adrian Georgescu
from sipsimple.application import SIPApplication
13 10 Adrian Georgescu
from sipsimple.core import SIPURI, ToHeader
14 7 Patrick Simmons
from sipsimple.lookup import DNSLookup, DNSLookupError
15 10 Adrian Georgescu
from sipsimple.storage import FileStorage
16 1 Adrian Georgescu
from sipsimple.session import Session
17 1 Adrian Georgescu
from sipsimple.streams import AudioStream
18 1 Adrian Georgescu
from sipsimple.threading.green import run_in_green_thread
19 10 Adrian Georgescu
20 10 Adrian Georgescu
from threading import Event
21 1 Adrian Georgescu
22 1 Adrian Georgescu
class SimpleCallApplication(SIPApplication):
23 11 Adrian Georgescu
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
    def call(self, callee):
33 1 Adrian Georgescu
        self.callee = callee
34 7 Patrick Simmons
        self.start(FileStorage('config'))
35 1 Adrian Georgescu
36 1 Adrian Georgescu
    @run_in_green_thread
37 1 Adrian Georgescu
    def _NH_SIPApplicationDidStart(self, notification):
38 1 Adrian Georgescu
        self.callee = ToHeader(SIPURI.parse(self.callee))
39 1 Adrian Georgescu
        try:
40 1 Adrian Georgescu
            routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait()
41 1 Adrian Georgescu
        except DNSLookupError, e:
42 1 Adrian Georgescu
            print 'DNS lookup failed: %s' % str(e)
43 1 Adrian Georgescu
        else:
44 1 Adrian Georgescu
            account = AccountManager().default_account
45 1 Adrian Georgescu
            self.session = Session(account)
46 1 Adrian Georgescu
            self.session.connect(self.callee, routes, [AudioStream(account)])
47 1 Adrian Georgescu
48 1 Adrian Georgescu
    def _NH_SIPSessionGotRingIndication(self, notification):
49 1 Adrian Georgescu
        print 'Ringing!'
50 1 Adrian Georgescu
51 1 Adrian Georgescu
    def _NH_SIPSessionDidStart(self, notification):
52 9 Adrian Georgescu
        audio_stream = notification.data.streams[0]
53 9 Adrian Georgescu
        print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate)
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 11 Adrian Georgescu
# place an audio call to the specified SIP URI in user@domain format
67 1 Adrian Georgescu
target_uri="sip:3333@sip2sip.info"
68 11 Adrian Georgescu
application = SimpleCallApplication()
69 9 Adrian Georgescu
application.call(target_uri)
70 1 Adrian Georgescu
print "Placing call to %s, press Enter to quit the program" % target_uri
71 1 Adrian Georgescu
raw_input()
72 1 Adrian Georgescu
application.session.end()
73 1 Adrian Georgescu
application.ended.wait()
74 14 Tijmen de Mes
</pre>