SipSessionExample » History » Version 6
Adrian Georgescu, 04/09/2010 10:29 AM
| 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 | from sipsimple.configuration.backend.file import FileBackend |
||
| 15 | from sipsimple.lookup import DNSLookup, DNSLookupError |
||
| 16 | from sipsimple.session import Session |
||
| 17 | from sipsimple.streams import AudioStream |
||
| 18 | from sipsimple.util import Route, run_in_green_thread |
||
| 19 | |||
| 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 | self.start(FileBackend('config')) |
||
| 34 | |||
| 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 | print 'Session started!' |
||
| 52 | |||
| 53 | def _NH_SIPSessionDidFail(self, notification): |
||
| 54 | print 'Failed to connect' |
||
| 55 | self.stop() |
||
| 56 | |||
| 57 | def _NH_SIPSessionDidEnd(self, notification): |
||
| 58 | print 'Session ended' |
||
| 59 | self.stop() |
||
| 60 | |||
| 61 | def _NH_SIPApplicationDidEnd(self, notification): |
||
| 62 | self.ended.set() |
||
| 63 | |||
| 64 | # place an audio call to the specified URI |
||
| 65 | application = SimpleCallApplication() |
||
| 66 | application.call("sip:3333@ag-projects.com") |
||
| 67 | print "Placing call, press Enter to quit the program" |
||
| 68 | raw_input() |
||
| 69 | application.session.end() |
||
| 70 | application.ended.wait() |
||
| 71 | }}} |