Ignore:
Timestamp:
Feb 7, 2007 8:18:35 AM (17 years ago)
Author:
fahris
Message:

py pjsua updated @ 070207

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/py_pjsua/pjsua_app.py

    r928 r934  
    2727# Globals 
    2828# 
     29g_ua_cfg = None 
    2930g_acc_id = py_pjsua.PJSUA_INVALID_ID 
    3031g_current_call = py_pjsua.PJSUA_INVALID_ID 
    31  
     32g_wav_files = [] 
     33g_wav_id = 0 
     34g_wav_port = 0 
     35g_rec_file = "" 
     36g_rec_id = 0 
     37g_rec_port = 0 
    3238 
    3339# Utility to get call info 
     
    5056def on_incoming_call(acc_id, call_id, rdata): 
    5157        global g_current_call 
     58         
    5259        if g_current_call != py_pjsua.PJSUA_INVALID_ID: 
    5360                # There's call in progress - answer Busy 
    5461                py_pjsua.call_answer(call_id, 486, None, None) 
    5562                return 
    56  
     63         
    5764        g_current_call = call_id 
    5865        ci = py_pjsua.call_get_info(call_id) 
    5966        write_log(3, "*** Incoming call: " + call_name(call_id) + "***") 
    60         write_log(3, "*** Press a to answer or h to hangup  ***"); 
    61  
     67        write_log(3, "*** Press a to answer or h to hangup  ***") 
     68         
     69         
    6270         
    6371# Callback when media state has changed (e.g. established or terminated) 
     
    105113 
    106114 
     115# Received typing indication 
     116# 
     117def on_typing(call_id, strfrom, to, contact, is_typing): 
     118        str_t = "" 
     119        if is_typing: 
     120                str_t = "is typing.." 
     121        else: 
     122                str_t = "has stopped typing" 
     123        write_log(3, "IM indication: " + strfrom + " " + str_t) 
     124 
     125# on call transfer status 
     126# 
     127def on_call_transfer_status(call_id,status_code,status_text,final,p_cont): 
     128        strfinal = "" 
     129        if final == 1: 
     130                strfinal = "[final]" 
     131         
     132        write_log(3, "Call " + `call_id` + ": transfer status= " + `status_code` + " " + status_text+ " " + strfinal) 
     133               
     134        if status_code/100 == 2: 
     135                write_log(3, "Call " + `call_id` + " : call transfered successfully, disconnecting call") 
     136                status = py_pjsua.call_hangup(call_id, 410, None, None) 
     137                p_cont = 0 
     138 
     139# on call transfer request 
     140#                
     141def on_call_transfer_request(call_id, dst, code): 
     142        write_log(3, "Call transfer request from " + `call_id` + " to " + dst + " with code " + `code`) 
     143 
    107144# Utility: display PJ error and exit 
    108145# 
     
    126163# 
    127164def app_init(): 
    128         global g_acc_id 
     165        global g_acc_id, g_ua_cfg 
    129166 
    130167        # Create pjsua before anything else 
     
    152189        ua_cfg.cb.on_pager = on_pager 
    153190        ua_cfg.cb.on_pager_status = on_pager_status 
    154          
     191        ua_cfg.cb.on_typing = on_typing 
     192        ua_cfg.cb.on_call_transfer_status = on_call_transfer_status 
     193        ua_cfg.cb.on_call_transfer_request = on_call_transfer_request 
    155194 
    156195        # Create and initialize media config 
     
    193232 
    194233        g_acc_id = acc_id 
     234        g_ua_cfg = ua_cfg 
    195235 
    196236# Add SIP account interractively 
     
    242282                write_log(3, "Account " + acc_cfg.id + " added") 
    243283 
    244  
     284def add_player(): 
     285        global g_wav_files 
     286        global g_wav_id 
     287        global g_wav_port 
     288         
     289        file_name = "" 
     290        status = -1 
     291        wav_id = 0 
     292         
     293        print "Enter the path of the file player(e.g. /tmp/audio.wav): ", 
     294        file_name = sys.stdin.readline() 
     295        if file_name == "\n":  
     296                return 
     297        file_name = file_name.replace("\n", "") 
     298        status, wav_id = py_pjsua.player_create(file_name, 0) 
     299        if status != 0: 
     300                py_pjsua.perror(THIS_FILE, "Error adding file player ", status) 
     301        else: 
     302                g_wav_files.append(file_name) 
     303                if g_wav_id == 0: 
     304                        g_wav_id = wav_id 
     305                        g_wav_port = py_pjsua.player_get_conf_port(wav_id) 
     306                write_log(3, "File player " + file_name + " added") 
     307                 
     308def add_recorder(): 
     309        global g_rec_file 
     310        global g_rec_id 
     311        global g_rec_port 
     312         
     313        file_name = "" 
     314        status = -1 
     315        rec_id = 0 
     316         
     317        print "Enter the path of the file recorder(e.g. /tmp/audio.wav): ", 
     318        file_name = sys.stdin.readline() 
     319        if file_name == "\n":  
     320                return 
     321        file_name = file_name.replace("\n", "") 
     322        status, rec_id = py_pjsua.recorder_create(file_name, 0, None, 0, 0) 
     323        if status != 0: 
     324                py_pjsua.perror(THIS_FILE, "Error adding file recorder ", status) 
     325        else: 
     326                g_rec_file = file_name 
     327                g_rec_id = rec_id 
     328                g_rec_port = py_pjsua.recorder_get_conf_port(rec_id) 
     329                write_log(3, "File recorder " + file_name + " added") 
     330 
     331def conf_list(): 
     332         
     333         
     334        ports = None 
     335 
     336        print "Conference ports : " 
     337 
     338         
     339        ports = py_pjsua.enum_conf_ports() 
     340 
     341        for port in ports: 
     342                info = None 
     343                info = py_pjsua.conf_get_port_info(port) 
     344                txlist = "" 
     345                for i in range(info.listener_cnt): 
     346                        txlist = txlist + "#" + `info.listeners[i]` + " " 
     347                 
     348                print "Port #" + `info.slot_id` + "[" + `(info.clock_rate/1000)` + "KHz/" + `(info.samples_per_frame * 1000 / info.clock_rate)` + "ms] " + info.name + " transmitting to: " + txlist 
     349                 
     350def connect_port(): 
     351        src_port = 0 
     352        dst_port = 0 
     353         
     354        print "Connect src port # (empty to cancel): " 
     355        src_port = sys.stdin.readline() 
     356        if src_port == "\n":  
     357                return 
     358        src_port = src_port.replace("\n", "") 
     359        src_port = int(src_port) 
     360        print "To dst port # (empty to cancel): " 
     361        dst_port = sys.stdin.readline() 
     362        if dst_port == "\n":  
     363                return 
     364        dst_port = dst_port.replace("\n", "") 
     365        dst_port = int(dst_port) 
     366        status = py_pjsua.conf_connect(src_port, dst_port) 
     367        if status != 0: 
     368                py_pjsua.perror(THIS_FILE, "Error connecting port ", status) 
     369        else:            
     370                write_log(3, "Port connected from " + `src_port` + " to " + `dst_port`) 
     371                 
     372def disconnect_port(): 
     373        src_port = 0 
     374        dst_port = 0 
     375         
     376        print "Disconnect src port # (empty to cancel): " 
     377        src_port = sys.stdin.readline() 
     378        if src_port == "\n":  
     379                return 
     380        src_port = src_port.replace("\n", "") 
     381        src_port = int(src_port) 
     382        print "From dst port # (empty to cancel): " 
     383        dst_port = sys.stdin.readline() 
     384        if dst_port == "\n":  
     385                return 
     386        dst_port = dst_port.replace("\n", "") 
     387        dst_port = int(dst_port) 
     388        status = py_pjsua.conf_disconnect(src_port, dst_port) 
     389        if status != 0: 
     390                py_pjsua.perror(THIS_FILE, "Error disconnecting port ", status) 
     391        else:            
     392                write_log(3, "Port disconnected " + `src_port` + " from " + `dst_port`) 
     393 
     394def dump_call_quality(): 
     395        global g_current_call 
     396         
     397        buf = "" 
     398        if g_current_call != -1: 
     399                buf = py_pjsua.call_dump(g_current_call, 1, 1024, "  ") 
     400                write_log(3, "\n" + buf) 
     401        else: 
     402                write_log(3, "No current call") 
     403                 
     404def xfer_call(): 
     405        global g_current_call 
     406         
     407        if g_current_call == -1: 
     408                 
     409                write_log(3, "No current call") 
     410 
     411        else: 
     412                call = g_current_call            
     413                ci = py_pjsua.call_get_info(g_current_call) 
     414                print "Transfering current call ["+ `g_current_call` + "] " + ci.remote_info 
     415                print "Enter sip url : " 
     416                url = sys.stdin.readline() 
     417                if url == "\n":  
     418                        return 
     419                url = url.replace("\n", "") 
     420                if call != g_current_call: 
     421                        print "Call has been disconnected" 
     422                        return 
     423                msg_data = py_pjsua.msg_data_init() 
     424                status = py_pjsua.call_xfer(g_current_call, url, msg_data); 
     425                if status != 0: 
     426                        py_pjsua.perror(THIS_FILE, "Error transfering call ", status) 
     427                else:            
     428                        write_log(3, "Call transfered to " + url) 
     429                 
     430def xfer_call_replaces(): 
     431        if g_current_call == -1: 
     432                write_log(3, "No current call") 
     433        else: 
     434                call = g_current_call 
     435                 
     436                ids = py_pjsua.enum_calls() 
     437                if len(ids) <= 1: 
     438                        print "There are no other calls" 
     439                        return           
     440 
     441                ci = py_pjsua.call_get_info(g_current_call) 
     442                print "Transfer call [" + `g_current_call` + "] " + ci.remote_info + " to one of the following:" 
     443                for i in range(0, len(ids)):                 
     444                        if ids[i] == call: 
     445                                continue 
     446                        call_info = py_pjsua.call_get_info(ids[i]) 
     447                        print `ids[i]` + "  " +  call_info.remote_info + "  [" + call_info.state_text + "]"              
     448 
     449                print "Enter call number to be replaced : " 
     450                buf = sys.stdin.readline() 
     451                buf = buf.replace("\n","") 
     452                if buf == "": 
     453                        return 
     454                dst_call = int(buf) 
     455                 
     456                if call != g_current_call: 
     457                        print "Call has been disconnected" 
     458                        return           
     459                 
     460                if dst_call == call: 
     461                        print "Destination call number must not be the same as the call being transfered" 
     462                        return 
     463                 
     464                if dst_call >= py_pjsua.PJSUA_MAX_CALLS: 
     465                        print "Invalid destination call number" 
     466                        return 
     467         
     468                if py_pjsua.call_is_active(dst_call) == 0: 
     469                        print "Invalid destination call number" 
     470                        return           
     471 
     472                py_pjsua.call_xfer_replaces(call, dst_call, 0, None) 
     473                 
    245474# 
    246475# Worker thread function. 
     
    282511def print_menu(): 
    283512        print """ 
    284 Menu: 
    285   q   Quit application 
    286  +a   Add account 
    287  +b   Add buddy 
    288   m   Make call 
    289   a   Answer current call (if any) 
    290   h   Hangup current call (if any) 
    291   i   Send instant message 
     513+============================================================================+ 
     514|         Call Commands :      |  Buddy, IM & Presence:   |    Account:      | 
     515|                              |                          |                  | 
     516|  m  Make call                | +b  Add buddy            | +a  Add account  | 
     517|  a  Answer current call      | -b  Delete buddy         | -a  Delete accnt | 
     518|  h  Hangup current call      |                          |                  | 
     519|  H  Hold call                |  i  Send instant message | rr  register     | 
     520|  v  re-inVite (release Hold) |  s  Subscribe presence   | ru  Unregister   | 
     521|  #  Send DTMF string         |  u  Unsubscribe presence |                  | 
     522| dq  Dump curr. call quality  |  t  ToGgle Online status |                  | 
     523|                              +--------------------------+------------------+ 
     524|  x  Xfer call                |     Media Commands:      |    Status:       | 
     525|  X  Xfer with Replaces       |                          |                  | 
     526|                              | cl  List ports           |  d  Dump status  | 
     527|                              | cc  Connect port         |                  | 
     528|                              | cd  Disconnect port      |                  | 
     529|                              | +p  Add file player      |                  | 
     530|------------------------------+ +r  Add file recorder    |                  | 
     531|  q  Quit application         |                          |                  | 
     532+============================================================================+ 
    292533        """ 
    293534        print "Choice: ",  
     
    355596                        if status != 0: 
    356597                                py_pjsua.perror(THIS_FILE, "Error adding buddy", status) 
    357  
     598                elif choice[0] == "-" and choice[1] == "b": 
     599                        print "Enter buddy ID to delete : " 
     600                        buf = sys.stdin.readline() 
     601                        buf = buf.replace("\n","") 
     602                        if buf == "": 
     603                                continue 
     604                        i = int(buf) 
     605                        if py_pjsua.buddy_is_valid(i) == 0: 
     606                                print "Invalid buddy id " + `i` 
     607                        else: 
     608                                py_pjsua.buddy_del(i) 
     609                                print "Buddy " + `i` + " deleted"                
    358610                elif choice[0] == "+" and choice[1] == "a": 
    359611                        # Add account 
    360612                        add_account() 
    361  
     613                elif choice[0] == "-" and choice[1] == "a": 
     614                        print "Enter account ID to delete : " 
     615                        buf = sys.stdin.readline() 
     616                        buf = buf.replace("\n","") 
     617                        if buf == "": 
     618                                continue 
     619                        i = int(buf) 
     620 
     621                        if py_pjsua.acc_is_valid(i) == 0: 
     622                                print "Invalid account id " + `i` 
     623                        else: 
     624                                py_pjsua.acc_del(i) 
     625                                print "Account " + `i` + " deleted" 
     626             
     627                elif choice[0] == "+" and choice[1] == "p": 
     628                        add_player() 
     629                elif choice[0] == "+" and choice[1] == "r": 
     630                        add_recorder() 
     631                elif choice[0] == "c" and choice[1] == "l": 
     632                        conf_list() 
     633                elif choice[0] == "c" and choice[1] == "c": 
     634                        connect_port() 
     635                elif choice[0] == "c" and choice[1] == "d": 
     636                        disconnect_port() 
     637                elif choice[0] == "d" and choice[1] == "q": 
     638                        dump_call_quality() 
     639                elif choice[0] == "x": 
     640                        xfer_call() 
     641                elif choice[0] == "X": 
     642                        xfer_call_replaces() 
    362643                elif choice[0] == "h": 
    363644                        if g_current_call != py_pjsua.PJSUA_INVALID_ID: 
     
    365646                        else: 
    366647                                print "No current call" 
    367  
     648                elif choice[0] == "H": 
     649                        if g_current_call != py_pjsua.PJSUA_INVALID_ID: 
     650                                py_pjsua.call_set_hold(g_current_call, None) 
     651                 
     652                        else: 
     653                                print "No current call" 
     654                elif choice[0] == "v": 
     655                        if g_current_call != py_pjsua.PJSUA_INVALID_ID: 
     656                 
     657                                py_pjsua.call_reinvite(g_current_call, 1, None); 
     658 
     659                        else: 
     660                                print "No current call" 
     661                elif choice[0] == "#": 
     662                        if g_current_call == py_pjsua.PJSUA_INVALID_ID:          
     663                                print "No current call" 
     664                        elif py_pjsua.call_has_media(g_current_call) == 0: 
     665                                print "Media is not established yet!" 
     666                        else: 
     667                                call = g_current_call 
     668                                print "DTMF strings to send (0-9*#A-B)" 
     669                                buf = sys.stdin.readline() 
     670                                buf = buf.replace("\n", "") 
     671                                if buf == "": 
     672                                        continue 
     673                                if call != g_current_call: 
     674                                        print "Call has been disconnected" 
     675                                        continue 
     676                                status = py_pjsua.call_dial_dtmf(g_current_call, buf) 
     677                                if status != 0: 
     678                                        py_pjsua.perror(THIS_FILE, "Unable to send DTMF", status); 
     679                                else: 
     680                                        print "DTMF digits enqueued for transmission" 
     681                elif choice[0] == "s": 
     682                        print "Subscribe presence of (buddy id) : " 
     683                        buf = sys.stdin.readline() 
     684                        buf = buf.replace("\n","") 
     685                        if buf == "": 
     686                                continue 
     687                        i = int(buf) 
     688                        py_pjsua.buddy_subscribe_pres(i, 1) 
     689                elif choice[0] == "u": 
     690                        print "Unsubscribe presence of (buddy id) : " 
     691                        buf = sys.stdin.readline() 
     692                        buf = buf.replace("\n","") 
     693                        if buf == "": 
     694                                continue 
     695                        i = int(buf) 
     696                        py_pjsua.buddy_subscribe_pres(i, 0) 
     697                elif choice[0] == "t": 
     698                        acc_info = py_pjsua.acc_get_info(g_acc_id) 
     699                        if acc_info.online_status == 0: 
     700                                acc_info.online_status = 1 
     701                        else: 
     702                                acc_info.online_status = 0 
     703                        py_pjsua.acc_set_online_status(g_acc_id, acc_info.online_status) 
     704                        st = "" 
     705                        if acc_info.online_status == 0: 
     706                                st = "offline" 
     707                        else: 
     708                                st = "online" 
     709                        print "Setting " + acc_info.acc_uri + " online status to " + st 
     710                elif choice[0] == "r": 
     711                        if choice[1] == "r":         
     712                                py_pjsua.acc_set_registration(g_acc_id, 1) 
     713                        elif choice[1] == "u": 
     714                                py_pjsua.acc_set_registration(g_acc_id, 0) 
     715                elif choice[0] == "d": 
     716                         
     717                        write_log(3, "Start dumping application states : ") 
     718                        write_log(3, "Dumping invite sessions : ") 
     719 
     720                        if py_pjsua.call_get_count() == 0: 
     721                                write_log(3, "  - no sessions -") 
     722                        else: 
     723                                for i in range(0, g_ua_cfg.max_calls): 
     724                                        if py_pjsua.call_is_active(i): 
     725                                                buf = py_pjsua.call_dump(i, 1, 1024, "  ") 
     726                                                write_log(3, buf)        
     727                        py_pjsua.pres_dump(1) 
     728                        write_log(3, "Dump complete") 
    368729                elif choice[0] == "a": 
    369                         if g_current_call != py_pjsua.PJSUA_INVALID_ID: 
     730                        if g_current_call != py_pjsua.PJSUA_INVALID_ID:                          
     731                                 
    370732                                py_pjsua.call_answer(g_current_call, 200, None, None) 
    371733                        else: 
Note: See TracChangeset for help on using the changeset viewer.