Project

General

Profile

SipSessionExample » History » Revision 10

Revision 9 (Adrian Georgescu, 06/12/2011 08:55 PM) → Revision 10/14 (Adrian Georgescu, 06/16/2011 12:50 PM)

== Sample Code == 

 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. 

 {{{ 
 #/usr/bin/python from threading import Event 

 from application.notification import NotificationCenter 

 from sipsimple.core import SIPURI, ToHeader 

 from sipsimple.account import AccountManager 
 from sipsimple.application import SIPApplication 
 from sipsimple.core sipsimple.storage import SIPURI, ToHeader FileStorage 
 from sipsimple.lookup import DNSLookup, DNSLookupError 
 from sipsimple.storage import FileStorage 
 from sipsimple.session import Session 
 from sipsimple.streams import AudioStream 
 from sipsimple.threading.green import run_in_green_thread 

 from threading import Event 


 class SimpleCallApplication(SIPApplication): 
     def __init__(self): 
         SIPApplication.__init__(self) 
         self.ended = Event() 
         self.callee = None 
         self.session = None 
         notification_center = NotificationCenter() 
         notification_center.add_observer(self) 


     def call(self, callee): 
         self.callee = callee 
         self.start(FileStorage('config')) 

     @run_in_green_thread 
     def _NH_SIPApplicationDidStart(self, notification): 
         self.callee = ToHeader(SIPURI.parse(self.callee)) 
         try: 
             routes = DNSLookup().lookup_sip_proxy(self.callee.uri, ['udp']).wait() 
         except DNSLookupError, e: 
             print 'DNS lookup failed: %s' % str(e) 
         else: 
             account = AccountManager().default_account 
             self.session = Session(account) 
             self.session.connect(self.callee, routes, [AudioStream(account)]) 

     def _NH_SIPSessionGotRingIndication(self, notification): 
         print 'Ringing!' 

     def _NH_SIPSessionDidStart(self, notification): 
         audio_stream = notification.data.streams[0] 
         print 'Audio session established using "%s" codec at %sHz' % (audio_stream.codec, audio_stream.sample_rate) 

     def _NH_SIPSessionDidFail(self, notification): 
         print 'Failed to connect' 
         self.stop() 

     def _NH_SIPSessionDidEnd(self, notification): 
         print 'Session ended' 
         self.stop() 

     def _NH_SIPApplicationDidEnd(self, notification): 
         self.ended.set() 

 # place an audio call to the specified URI 
 application = SimpleCallApplication() 
 target_uri="sip:3333@sip2sip.info" 
 application.call(target_uri) 
 print "Placing call to %s, press Enter to quit the program" % target_uri 
 raw_input() 
 application.session.end() 
 application.ended.wait() 
 }}}