SipSessionExample » History » Version 13
  Tijmen de Mes, 04/19/2012 04:07 PM 
  
| 1 | 12 | Adrian Georgescu | h2. Sample Code | 
|---|---|---|---|
| 2 | 1 | Adrian Georgescu | |
| 3 | 12 | Adrian Georgescu | |
| 4 | 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 | 13 | Tijmen de Mes | <pre><code class="python"> | 
| 7 | 10 | Adrian Georgescu | #/usr/bin/python | 
| 8 | 1 | Adrian Georgescu | |
| 9 | from application.notification import NotificationCenter | ||
| 10 | |||
| 11 | from sipsimple.account import AccountManager | ||
| 12 | 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 | from sipsimple.streams import AudioStream | ||
| 18 | from sipsimple.threading.green import run_in_green_thread | ||
| 19 | 10 | Adrian Georgescu | |
| 20 | from threading import Event | ||
| 21 | 1 | Adrian Georgescu | |
| 22 | class SimpleCallApplication(SIPApplication): | ||
| 23 | 11 | Adrian Georgescu | |
| 24 | 1 | Adrian Georgescu | def __init__(self): | 
| 25 | SIPApplication.__init__(self) | ||
| 26 | self.ended = Event() | ||
| 27 | self.callee = None | ||
| 28 | self.session = None | ||
| 29 | notification_center = NotificationCenter() | ||
| 30 | notification_center.add_observer(self) | ||
| 31 | |||
| 32 | def call(self, callee): | ||
| 33 | self.callee = callee | ||
| 34 | 7 | Patrick Simmons |         self.start(FileStorage('config')) | 
| 35 | 1 | Adrian Georgescu | |
| 36 | @run_in_green_thread | ||
| 37 | def _NH_SIPApplicationDidStart(self, notification): | ||
| 38 | self.callee = ToHeader(SIPURI.parse(self.callee)) | ||
| 39 | try: | ||
| 40 | routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait() | ||
| 41 | except DNSLookupError, e: | ||
| 42 | print 'DNS lookup failed: %s' % str(e) | ||
| 43 | else: | ||
| 44 | account = AccountManager().default_account | ||
| 45 | self.session = Session(account) | ||
| 46 | self.session.connect(self.callee, routes, [AudioStream(account)]) | ||
| 47 | |||
| 48 | def _NH_SIPSessionGotRingIndication(self, notification): | ||
| 49 | print 'Ringing!' | ||
| 50 | |||
| 51 | def _NH_SIPSessionDidStart(self, notification): | ||
| 52 | 9 | Adrian Georgescu | audio_stream = notification.data.streams[0] | 
| 53 | print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate) | ||
| 54 | 1 | Adrian Georgescu | |
| 55 | def _NH_SIPSessionDidFail(self, notification): | ||
| 56 | print 'Failed to connect' | ||
| 57 | self.stop() | ||
| 58 | |||
| 59 | def _NH_SIPSessionDidEnd(self, notification): | ||
| 60 | print 'Session ended' | ||
| 61 | self.stop() | ||
| 62 | |||
| 63 | def _NH_SIPApplicationDidEnd(self, notification): | ||
| 64 | self.ended.set() | ||
| 65 | |||
| 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 | raw_input() | ||
| 72 | application.session.end() | ||
| 73 | application.ended.wait() | ||
| 74 | 13 | Tijmen de Mes | </code></pre> |