Project

General

Profile

SipSessionExample » History » 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
{{{
6
from threading import Event
7
8
from application.notification import NotificationCenter
9
10
from sipsimple.core import SIPURI, ToHeader
11
12
from sipsimple.account import AccountManager
13
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
from sipsimple.session import Session
17
from sipsimple.streams import AudioStream
18 8 Adrian Georgescu
from sipsimple.threading.green import run_in_green_thread
19 1 Adrian Georgescu
20
21
class SimpleCallApplication(SIPApplication):
22
    def __init__(self):
23
        SIPApplication.__init__(self)
24
        self.ended = Event()
25
        self.callee = None
26
        self.session = None
27
        notification_center = NotificationCenter()
28
        notification_center.add_observer(self)
29
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
# place an audio call to the specified URI
66
application = SimpleCallApplication()
67 9 Adrian Georgescu
target_uri="sip:3333@sip2sip.info"
68
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
}}}