Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* $Id: pjsua2_demo.cpp 6026 2019-06-12 06:00:35Z nanang $ */
- /*
- * Copyright (C) 2008-2013 Teluu Inc. (http://www.teluu.com)
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- #include <pjsua2.hpp>
- #include <iostream>
- #include <pj/file_access.h>
- #define THIS_FILE "pjsua2_demo.cpp"
- #define LOG_LOCATION "sipLog.log"
- //Typedefs
- typedef struct {
- pj_lock_t * lock;
- void * frame_buffer;
- pj_size_t mem_capture_buffer_size;
- } mem_capture_data;
- //Global vars
- //Prototypes
- using namespace pj;
- class MyAccount;
- class MyCall : public Call{
- private:
- MyAccount *myAcc;
- AudioMediaPlayer *wav_player;
- public:
- MyCall(Account &acc, int call_id = PJSUA_INVALID_ID):Call(acc, call_id){
- myAcc = (MyAccount *)&acc;
- }
- ~MyCall(){
- }
- virtual void onCallState(OnCallStateParam &prm);
- virtual void onCallTransferRequest(OnCallTransferRequestParam &prm);
- virtual void onCallReplaced(OnCallReplacedParam &prm);
- virtual void onCallMediaState(OnCallMediaStateParam &prm);
- };
- class MyAccount:public Account{
- public:
- std::vector<Call *> calls;
- MyAccount(){
- }
- ~MyAccount(){
- std::cout << "*** Account is being deleted: N° of calls=" << calls.size() << std::endl;
- for (std::vector<Call *>::iterator it = calls.begin(); it != calls.end(); ) {
- delete (*it);
- it = calls.erase(it);
- }
- }
- void removeCall(Call *call){
- for (std::vector<Call *>::iterator it = calls.begin(); it != calls.end(); ++it){
- if (*it == call) {
- calls.erase(it);
- break;
- }
- }
- }
- virtual void onRegState(OnRegStateParam &prm){
- AccountInfo ai = getInfo();
- std::cout << (ai.regIsActive? "*** Register: code=" : "*** Unregister: code=") << prm.code << std::endl;
- }
- virtual void onIncomingCall(OnIncomingCallParam &iprm){
- Call *call = new MyCall(*this, iprm.callId);
- CallInfo ci = call->getInfo();
- CallOpParam prm;
- std::cout << "*** Incoming Call: " << ci.remoteUri << " [" << ci.stateText << "]" << std::endl;
- calls.push_back(call);
- prm.statusCode = PJSIP_SC_OK;
- call->answer(prm);
- }
- };
- void MyCall::onCallState(OnCallStateParam &prm)
- {
- PJ_UNUSED_ARG(prm);
- CallInfo ci = getInfo();
- std::cout << "*** Call: " << ci.remoteUri << " [" << ci.stateText << "]" << std::endl;
- if (ci.state == PJSIP_INV_STATE_DISCONNECTED) {
- //myAcc->removeCall(this);
- /* Delete the call */
- //delete this;
- }
- }
- ///////dumping frames///
- void dump_incoming_frames(pjmedia_port * port, void * usr_data){
- pj_size_t buf_size = 320; //pjmedia_mem_capture_get_size(port);
- char * data;
- data = (char*) usr_data;
- //printf("%p\n", usr_data);
- //printf("%d\n", atoi((char*) usr_data));
- FILE * pFile;
- pFile = fopen("myfile.bin", "ab");
- fwrite(usr_data, sizeof(data[0]), buf_size, pFile);
- fclose(pFile);
- }
- void MyCall::onCallMediaState(OnCallMediaStateParam &prm){
- CallInfo ci = getInfo();
- pjsua_call_info ciUA;
- pjsua_call_get_info(ci.id, &ciUA);
- pjsua_conf_port_info cpi;
- pjsua_conf_get_port_info(ciUA.conf_slot, &cpi);
- printf("Conference slot: %d\n", ciUA.conf_slot);
- pj_pool_t *pool = pjsua_pool_create("POOLNAME", 2000, 2000);
- pjmedia_port *prt;
- uint buf_size = cpi.bits_per_sample*cpi.samples_per_frame/8;
- PJ_LOG(3, (THIS_FILE, "bits_per_sample: %d", cpi.bits_per_sample));
- PJ_LOG(3, (THIS_FILE, "samples_per_frame: %d", cpi.samples_per_frame));
- PJ_LOG(3, (THIS_FILE, "Buf Size: %d", buf_size));
- void *buffer = pj_pool_zalloc(pool, buf_size);
- pjsua_conf_port_id port_id;
- pjmedia_mem_capture_create( pool,
- buffer,
- buf_size,
- cpi.clock_rate,
- cpi.channel_count,
- cpi.samples_per_frame,
- cpi.bits_per_sample,
- 0,
- &prt);
- pjmedia_mem_capture_set_eof_cb2(prt, buffer, dump_incoming_frames);
- pjsua_conf_add_port(pool, prt, &port_id);
- pjsua_conf_connect(ciUA.conf_slot, port_id); //connect port with conference
- }
- void MyCall::onCallTransferRequest(OnCallTransferRequestParam &prm){
- /* Create new Call for call transfer */
- prm.newCall = new MyCall(*myAcc);
- }
- void MyCall::onCallReplaced(OnCallReplacedParam &prm){
- /* Create new Call for call replace */
- prm.newCall = new MyCall(*myAcc, prm.newCallId);
- }
- static void makeTestCall(Endpoint& enp){
- //Transport Config
- AccountConfig acc_cfg;
- acc_cfg.idUri = "sip:25@" SIP_ADDR;
- acc_cfg.regConfig.registrarUri = "sip:" SIP_ADDR;
- acc_cfg.sipConfig.authCreds.push_back(AuthCredInfo("digest", "*", "25", 0, "314159"));
- //Create new account
- MyAccount *acc(new MyAccount);
- try {
- acc->create(acc_cfg);
- }
- catch (...) {
- std::cout << "Adding account failed" << std::endl;
- }
- PJ_LOG(3, (THIS_FILE, "Wait for incomign call"));
- pj_thread_sleep(1000);
- // Make outgoing call
- Call *call = new MyCall(*acc);
- acc->calls.push_back(call);
- CallOpParam prm(true);
- prm.opt.audioCount = 1;
- prm.opt.videoCount = 0;
- pj_thread_sleep(2000);
- call->makeCall("sip:12@" SIP_ADDR, prm);
- // Hangup all calls
- pj_thread_sleep(20000);
- //enp.hangupAllCalls();
- pj_thread_sleep(1000);
- // Destroy library
- PJ_LOG(3, (THIS_FILE, "*** PJSUA2 SHUTTING DOWN ***"));
- delete acc; /* Will delete all calls too */
- }
- int configureSip(Endpoint& enp){
- //Configure Endpoint
- EpConfig ep_cfg;
- ep_cfg.logConfig.level = 6;
- ep_cfg.logConfig.filename = LOG_LOCATION;
- enp.libInit(ep_cfg);
- enp.audDevManager().setNullDev();
- //enp.audDevManager().setNoDev();
- // Transport
- TransportConfig tcfg;
- tcfg.port = 5060;
- tcfg.publicAddress = "172.28.178.19";
- enp.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);
- // Start library
- enp.libStart();
- PJ_LOG(3, (THIS_FILE, "*** PJSUA2 STARTED ***"));
- return 0;
- }
- int registerAccount(Endpoint& enp){
- return 0;
- }
- int main(){
- int ret;
- Endpoint enp;
- //Run the main prog
- try{
- enp.libCreate();
- Error test;
- if(configureSip(enp)) throw;
- if(registerAccount(enp)) throw;
- makeTestCall(enp);
- ret = PJ_SUCCESS;
- }
- catch(Error & err){
- std::cout << "Main Programm crashed with exception: " << err.info() << std::endl;
- ret = 1;
- }
- //Destroy the endpoint
- try{
- enp.libDestroy();
- }
- catch(Error &err){
- std::cout << "Unable to destroy endpoint. Reason: " << err.info() << std::endl;
- ret = 1;
- }
- //Return the status of the program
- if (ret == PJ_SUCCESS) std::cout << "Programm ran successfully!" << std::endl;
- else std::cout << "Program encounterd errors" << std::endl;
- return ret;
- }
Add Comment
Please, Sign In to add comment