Project

General

Profile

SipSessionExample » History » Version 11

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