jfk422

PJSUA2 Demo no audio

Jul 23rd, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.92 KB | None | 0 0
  1. /* $Id: pjsua2_demo.cpp 6026 2019-06-12 06:00:35Z nanang $ */
  2. /*
  3.  * Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com)
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19. #include <pjsua2.hpp>
  20. #include <iostream>
  21. #include <pj/file_access.h>
  22.  
  23. #define THIS_FILE   "pjsua2_demo.cpp"
  24.  
  25. #define LOG_LOCATION "sipLog.log"
  26.  
  27.  
  28. //Typedefs
  29. typedef struct {
  30.     pj_lock_t * lock;
  31.  
  32.     void * frame_buffer;
  33.     pj_size_t mem_capture_buffer_size;
  34. } mem_capture_data;
  35.  
  36.  
  37. //Global vars
  38.  
  39.  
  40. //Prototypes
  41.  
  42. using namespace pj;
  43.  
  44. class MyAccount;
  45.  
  46. class MyCall : public Call{
  47.     private:
  48.         MyAccount *myAcc;
  49.         AudioMediaPlayer *wav_player;
  50.  
  51.     public:
  52.         MyCall(Account &acc, int call_id = PJSUA_INVALID_ID):Call(acc, call_id){
  53.             myAcc = (MyAccount *)&acc;
  54.         }
  55.        
  56.         ~MyCall(){
  57.         }
  58.        
  59.         virtual void onCallState(OnCallStateParam &prm);
  60.         virtual void onCallTransferRequest(OnCallTransferRequestParam &prm);
  61.         virtual void onCallReplaced(OnCallReplacedParam &prm);
  62.         virtual void onCallMediaState(OnCallMediaStateParam &prm);
  63. };
  64.  
  65.  
  66. class MyAccount:public Account{
  67. public:
  68.     std::vector<Call *> calls;
  69.  
  70.     MyAccount(){
  71.     }
  72.  
  73.     ~MyAccount(){
  74.         std::cout << "*** Account is being deleted: N° of calls=" << calls.size() << std::endl;
  75.  
  76.         for (std::vector<Call *>::iterator it = calls.begin(); it != calls.end(); ) {
  77.             delete (*it);
  78.             it = calls.erase(it);
  79.         }
  80.     }
  81.    
  82.     void removeCall(Call *call){
  83.         for (std::vector<Call *>::iterator it = calls.begin(); it != calls.end(); ++it){
  84.             if (*it == call) {
  85.                 calls.erase(it);
  86.                 break;
  87.             }
  88.         }
  89.     }
  90.  
  91.     virtual void onRegState(OnRegStateParam &prm){
  92.         AccountInfo ai = getInfo();
  93.         std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=") << prm.code << std::endl;
  94.     }
  95.    
  96.     virtual void onIncomingCall(OnIncomingCallParam &iprm){
  97.         Call *call = new MyCall(*this, iprm.callId);
  98.         CallInfo ci = call->getInfo();
  99.         CallOpParam prm;
  100.        
  101.         std::cout << "*** Incoming Call: " <<  ci.remoteUri << " [" << ci.stateText << "]" << std::endl;
  102.        
  103.         calls.push_back(call);
  104.         prm.statusCode = PJSIP_SC_OK;
  105.         call->answer(prm);
  106.     }
  107. };
  108.  
  109.  
  110. void MyCall::onCallState(OnCallStateParam &prm)
  111. {
  112.     PJ_UNUSED_ARG(prm);
  113.  
  114.     CallInfo ci = getInfo();
  115.     std::cout << "*** Call: " <<  ci.remoteUri << " [" << ci.stateText << "]" << std::endl;
  116.    
  117.     if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
  118.         //myAcc->removeCall(this);
  119.         /* Delete the call */
  120.         //delete this;
  121.     }
  122. }
  123.  
  124. ///////dumping frames///
  125. void dump_incoming_frames(pjmedia_port * port, void * usr_data){
  126.     pj_size_t buf_size = 320; //pjmedia_mem_capture_get_size(port);
  127.     char * data;
  128.     data = (char*) usr_data;
  129.    
  130.     //printf("%p\n", usr_data);
  131.     //printf("%d\n", atoi((char*) usr_data));
  132.  
  133.     FILE * pFile;
  134.     pFile = fopen("myfile.bin", "ab");
  135.     fwrite(usr_data, sizeof(data[0]), buf_size, pFile);
  136.     fclose(pFile);
  137. }
  138.  
  139. void MyCall::onCallMediaState(OnCallMediaStateParam &prm){
  140.     CallInfo ci = getInfo();
  141.     pjsua_call_info ciUA;
  142.     pjsua_call_get_info(ci.id, &ciUA);
  143.     pjsua_conf_port_info cpi;
  144.     pjsua_conf_get_port_info(ciUA.conf_slot, &cpi);
  145.  
  146.     printf("Conference slot: %d\n", ciUA.conf_slot);
  147.  
  148.     pj_pool_t *pool = pjsua_pool_create("POOLNAME", 2000, 2000);
  149.     pjmedia_port *prt;
  150.     uint buf_size = cpi.bits_per_sample*cpi.samples_per_frame/8;
  151.     PJ_LOG(3, (THIS_FILE, "bits_per_sample: %d", cpi.bits_per_sample));
  152.     PJ_LOG(3, (THIS_FILE, "samples_per_frame: %d", cpi.samples_per_frame));
  153.     PJ_LOG(3, (THIS_FILE, "Buf Size: %d", buf_size));
  154.     void *buffer = pj_pool_zalloc(pool, buf_size);
  155.     pjsua_conf_port_id port_id;
  156.  
  157.     pjmedia_mem_capture_create( pool,
  158.                                 buffer,
  159.                                 buf_size,
  160.                                 cpi.clock_rate,
  161.                                 cpi.channel_count,
  162.                                 cpi.samples_per_frame,
  163.                                 cpi.bits_per_sample,
  164.                                 0,
  165.                                 &prt);
  166.     pjmedia_mem_capture_set_eof_cb2(prt, buffer, dump_incoming_frames);
  167.  
  168.     pjsua_conf_add_port(pool, prt, &port_id);
  169.     pjsua_conf_connect(ciUA.conf_slot, port_id); //connect port with conference
  170. }
  171.  
  172. void MyCall::onCallTransferRequest(OnCallTransferRequestParam &prm){
  173.     /* Create new Call for call transfer */
  174.     prm.newCall = new MyCall(*myAcc);
  175. }
  176.  
  177. void MyCall::onCallReplaced(OnCallReplacedParam &prm){
  178.     /* Create new Call for call replace */
  179.     prm.newCall = new MyCall(*myAcc, prm.newCallId);
  180. }
  181.  
  182. static void makeTestCall(Endpoint& enp){
  183.  
  184.     //Transport Config
  185.     AccountConfig acc_cfg;
  186.     acc_cfg.idUri = "sip:25@" SIP_ADDR;
  187.     acc_cfg.regConfig.registrarUri = "sip:" SIP_ADDR;
  188.     acc_cfg.sipConfig.authCreds.push_back(AuthCredInfo("digest", "*", "25", 0, "314159"));
  189.  
  190.     //Create new account
  191.     MyAccount *acc(new MyAccount);
  192.     try {
  193.         acc->create(acc_cfg);
  194.     }
  195.     catch (...) {
  196.         std::cout << "Adding account failed" << std::endl;
  197.     }
  198.  
  199.     PJ_LOG(3, (THIS_FILE, "Wait for incomign call"));
  200.     pj_thread_sleep(1000);
  201.    
  202.     // Make outgoing call
  203.     Call *call = new MyCall(*acc);
  204.     acc->calls.push_back(call);
  205.     CallOpParam prm(true);
  206.     prm.opt.audioCount = 1;
  207.     prm.opt.videoCount = 0;
  208.    
  209.     pj_thread_sleep(2000);
  210.     call->makeCall("sip:12@" SIP_ADDR, prm);
  211.  
  212.     // Hangup all calls
  213.     pj_thread_sleep(20000);
  214.     //enp.hangupAllCalls();
  215.     pj_thread_sleep(1000);
  216.    
  217.     // Destroy library
  218.     PJ_LOG(3, (THIS_FILE, "*** PJSUA2 SHUTTING DOWN ***"));
  219.     delete acc; /* Will delete all calls too */
  220. }
  221.  
  222.  
  223.  
  224. int configureSip(Endpoint& enp){
  225.     //Configure Endpoint
  226.     EpConfig ep_cfg;
  227.     ep_cfg.logConfig.level = 6;
  228.     ep_cfg.logConfig.filename = LOG_LOCATION;
  229.     enp.libInit(ep_cfg);
  230.  
  231.     enp.audDevManager().setNullDev();
  232.     //enp.audDevManager().setNoDev();
  233.  
  234.     // Transport
  235.     TransportConfig tcfg;
  236.     tcfg.port = 5060;
  237.     tcfg.publicAddress = "172.28.178.19";
  238.     enp.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
  239.  
  240.     // Start library
  241.     enp.libStart();
  242.     PJ_LOG(3, (THIS_FILE, "*** PJSUA2 STARTED ***"));
  243.  
  244.     return 0;
  245. }
  246.  
  247. int registerAccount(Endpoint& enp){
  248.     return 0;
  249. }
  250.  
  251. int main(){
  252.     int ret;
  253.     Endpoint enp;
  254.  
  255.     //Run the main prog
  256.     try{
  257.         enp.libCreate();
  258.         Error test;
  259.        
  260.         if(configureSip(enp)) throw;
  261.         if(registerAccount(enp)) throw;
  262.  
  263.         makeTestCall(enp);
  264.         ret = PJ_SUCCESS;
  265.     }
  266.     catch(Error & err){
  267.         std::cout << "Main Programm crashed with exception: " << err.info() << std::endl;
  268.         ret = 1;
  269.     }
  270.  
  271.     //Destroy the endpoint
  272.     try{
  273.         enp.libDestroy();
  274.     }
  275.     catch(Error &err){
  276.         std::cout << "Unable to destroy endpoint. Reason: " << err.info() << std::endl;
  277.         ret = 1;
  278.     }
  279.  
  280.     //Return the status of the program
  281.     if (ret == PJ_SUCCESS) std::cout << "Programm ran successfully!" << std::endl;
  282.     else std::cout << "Program encounterd errors" << std::endl;
  283.  
  284.     return ret;
  285. }
Add Comment
Please, Sign In to add comment