Skip to content

Commit

Permalink
Fixed pjsua2 unnecessary object copy (#4248)
Browse files Browse the repository at this point in the history
  • Loading branch information
sauwming authored Jan 14, 2025
1 parent 90293cc commit d8795ba
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
16 changes: 9 additions & 7 deletions pjsip-apps/src/samples/pjsua2_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ void MyCall::onCallMediaState(OnCallMediaStateParam &prm)
AudioMedia& play_dev_med =
MyEndpoint::instance().audDevManager().getPlaybackDevMedia();

std::cout << "Getting playback dev media" << std::endl;

try {
// Get the first audio media
aud_med = getAudioMedia(-1);
Expand Down Expand Up @@ -240,7 +242,7 @@ static void mainProg1(MyEndpoint &ep)

// Transport
TransportConfig tcfg;
tcfg.port = 5060;
tcfg.port = 6000;
ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg);

// Start library
Expand All @@ -249,17 +251,17 @@ static void mainProg1(MyEndpoint &ep)

// Add account
AccountConfig acc_cfg;
acc_cfg.idUri = "sip:test1@pjsip.org";
acc_cfg.idUri = "sip:401@pjsip.org";
acc_cfg.regConfig.registrarUri = "sip:sip.pjsip.org";

#if PJSIP_HAS_DIGEST_AKA_AUTH
AuthCredInfo aci("Digest", "*", "test", PJSIP_CRED_DATA_EXT_AKA | PJSIP_CRED_DATA_PLAIN_PASSWD, "passwd");
aci.akaK = "passwd";
#else
AuthCredInfo aci("digest", "*", "test1", 0, "test1");
AuthCredInfo aci("digest", "*", "401", 0, "pw401");
#endif

acc_cfg.sipConfig.authCreds.push_back(aci);
acc_cfg.sipConfig.authCreds.push_back(PJSUA2_MOVE(aci));
MyAccount *acc(new MyAccount);
try {
acc->create(acc_cfg);
Expand All @@ -275,7 +277,7 @@ static void mainProg1(MyEndpoint &ep)
CallOpParam prm(true);
prm.opt.audioCount = 1;
prm.opt.videoCount = 0;
call->makeCall("sip:test1@pjsip.org", prm);
call->makeCall("sip:402@sip.pjsip.org", prm);

// Hangup all calls
pj_thread_sleep(4000);
Expand Down Expand Up @@ -412,7 +414,7 @@ static void mainProg(MyEndpoint &)
SipHeader h;
h.hName = "X-Header";
h.hValue = "User header";
accCfg.regConfig.headers.push_back(h);
accCfg.regConfig.headers.push_back(PJSUA2_MOVE(h));

accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tcp>");
accCfg.sipConfig.proxies.push_back("<sip:sip.pjsip.org;transport=tls>");
Expand All @@ -431,7 +433,7 @@ static void mainProg(MyEndpoint &)
aci.dataType |= PJSIP_CRED_DATA_EXT_AKA;
aci.akaK = "key";
#endif
accCfg.sipConfig.authCreds.push_back(aci);
accCfg.sipConfig.authCreds.push_back(PJSUA2_MOVE(aci));

jdoc.writeObject(accCfg);
json_str = jdoc.saveString();
Expand Down
10 changes: 10 additions & 0 deletions pjsip/include/pjsua2/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@
# define PJSUA2_THROW(x) throw(x)
#endif

/**
* std::move() is only supported from C++11.
*/
#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L) || \
(__cplusplus >= 201103L)
#define PJSUA2_MOVE(x) std::move(x)
#else
#define PJSUA2_MOVE(x) (x)
#endif

/**
* @} PJSUA2_CFG
*/
Expand Down
3 changes: 2 additions & 1 deletion pjsip/include/pjsua2/presence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ struct BuddyInfo
* Default constructor
*/
BuddyInfo()
: presMonitorEnabled(true),
: accId(PJSUA_INVALID_ID),
presMonitorEnabled(true),
subState(PJSIP_EVSUB_STATE_UNKNOWN),
subTermCode(PJSIP_SC_NULL)
{}
Expand Down
16 changes: 8 additions & 8 deletions pjsip/src/pjsua2/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void RtcpFbConfig::fromPj(const pjmedia_rtcp_fb_setting &prm)
for (unsigned i = 0; i < prm.cap_count; ++i) {
RtcpFbCap cap;
cap.fromPj(prm.caps[i]);
this->caps.push_back(cap);
this->caps.push_back(PJSUA2_MOVE(cap));
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ void RtcpFbConfig::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
NODE_READ_NUM_T (cap_node, pjmedia_rtcp_fb_type, cap.type);
NODE_READ_STRING (cap_node, cap.typeName);
NODE_READ_STRING (cap_node, cap.param);
this->caps.push_back(cap);
this->caps.push_back(PJSUA2_MOVE(cap));
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ void SrtpOpt::fromPj(const pjsua_srtp_opt &prm)
for (unsigned i = 0; i < prm.crypto_count; ++i) {
SrtpCrypto crypto;
crypto.fromPj(prm.crypto[i]);
this->cryptos.push_back(crypto);
this->cryptos.push_back(PJSUA2_MOVE(crypto));
}

this->keyings.clear();
Expand Down Expand Up @@ -196,7 +196,7 @@ void SrtpOpt::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
NODE_READ_STRING (crypto_node, crypto.key);
NODE_READ_STRING (crypto_node, crypto.name);
NODE_READ_UNSIGNED (crypto_node, crypto.flags);
this->cryptos.push_back(crypto);
this->cryptos.push_back(PJSUA2_MOVE(crypto));
}

ContainerNode keying_node = this_node.readArray("keyings");
Expand Down Expand Up @@ -287,7 +287,7 @@ void AccountSipConfig::readObject(const ContainerNode &node)
while (creds_node.hasUnread()) {
AuthCredInfo cred;
cred.readObject(creds_node);
authCreds.push_back(cred);
authCreds.push_back(PJSUA2_MOVE(cred));
}
}

Expand Down Expand Up @@ -762,7 +762,7 @@ void AccountConfig::fromPj(const pjsua_acc_config &prm,
SipHeader new_hdr;
new_hdr.fromPj(hdr);

regConfig.headers.push_back(new_hdr);
regConfig.headers.push_back(PJSUA2_MOVE(new_hdr));

hdr = hdr->next;
}
Expand All @@ -782,7 +782,7 @@ void AccountConfig::fromPj(const pjsua_acc_config &prm,
cred.akaOp = pj2Str(src.ext.aka.op);
cred.akaAmf = pj2Str(src.ext.aka.amf);

sipConfig.authCreds.push_back(cred);
sipConfig.authCreds.push_back(PJSUA2_MOVE(cred));
}
sipConfig.proxies.clear();
for (i=0; i<prm.proxy_cnt; ++i) {
Expand Down Expand Up @@ -810,7 +810,7 @@ void AccountConfig::fromPj(const pjsua_acc_config &prm,
while (hdr != &prm.sub_hdr_list) {
SipHeader new_hdr;
new_hdr.fromPj(hdr);
presConfig.headers.push_back(new_hdr);
presConfig.headers.push_back(PJSUA2_MOVE(new_hdr));
hdr = hdr->next;
}
presConfig.publishEnabled = PJ2BOOL(prm.publish_enabled);
Expand Down
8 changes: 4 additions & 4 deletions pjsip/src/pjsua2/endpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void SslCertInfo::fromPj(const pj_ssl_cert_info &info)
SslCertName cname;
cname.type = info.subj_alt_name.entry[i].type;
cname.name = pj2Str(info.subj_alt_name.entry[i].name);
subjectAltName.push_back(cname);
subjectAltName.push_back(PJSUA2_MOVE(cname));
}
}

Expand Down Expand Up @@ -1843,7 +1843,7 @@ void Endpoint::on_create_media_transport_srtp(pjsua_call_id call_id,
crypto.key = pj2Str(srtp_opt->crypto[i].key);
crypto.name = pj2Str(srtp_opt->crypto[i].name);
crypto.flags = srtp_opt->crypto[i].flags;
prm.cryptos.push_back(crypto);
prm.cryptos.push_back(PJSUA2_MOVE(crypto));
}

call->onCreateMediaTransportSrtp(prm);
Expand Down Expand Up @@ -2480,7 +2480,7 @@ CodecInfoVector2 Endpoint::codecEnum2() const PJSUA2_THROW(Error)
for (unsigned i = 0; i<count; ++i) {
CodecInfo codec_info;
codec_info.fromPj(pj_codec[i]);
civ2.push_back(codec_info);
civ2.push_back(PJSUA2_MOVE(codec_info));
}
return civ2;
}
Expand Down Expand Up @@ -2627,7 +2627,7 @@ CodecInfoVector2 Endpoint::videoCodecEnum2() const PJSUA2_THROW(Error)
for (unsigned i = 0; i<count; ++i) {
CodecInfo codec_info;
codec_info.fromPj(pj_codec[i]);
civ2.push_back(codec_info);
civ2.push_back(PJSUA2_MOVE(codec_info));
}
#endif
return civ2;
Expand Down
12 changes: 6 additions & 6 deletions pjsip/src/pjsua2/media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ ToneDigitMapVector ToneGenerator::getDigitMap() const PJSUA2_THROW(Error)
d.freq1 = pdm->digits[i].freq1;
d.freq2 = pdm->digits[i].freq2;

tdm.push_back(d);
tdm.push_back(PJSUA2_MOVE(d));
}

return tdm;
Expand Down Expand Up @@ -796,7 +796,7 @@ void AudioDevInfo::fromPj(const pjmedia_aud_dev_info &dev_info)
MediaFormatAudio format;
format.fromPj(dev_info.ext_fmt[i]);
if (format.type == PJMEDIA_TYPE_AUDIO)
extFmt.push_back(format);
extFmt.push_back(PJSUA2_MOVE(format));
}
}

Expand Down Expand Up @@ -946,7 +946,7 @@ AudioDevInfoVector2 AudDevManager::enumDev2() const PJSUA2_THROW(Error)
for (unsigned i = 0; i<count ;++i) {
AudioDevInfo di;
di.fromPj(pj_info[i]);
adiv2.push_back(di);
adiv2.push_back(PJSUA2_MOVE(di));
}

return adiv2;
Expand Down Expand Up @@ -1677,7 +1677,7 @@ void VideoDevInfo::fromPj(const pjmedia_vid_dev_info &dev_info)
MediaFormatVideo format;
format.fromPj(dev_info.fmt[i]);
if (format.type == PJMEDIA_TYPE_VIDEO)
fmt.push_back(format);
fmt.push_back(PJSUA2_MOVE(format));
}
#else
PJ_UNUSED_ARG(dev_info);
Expand Down Expand Up @@ -1765,7 +1765,7 @@ VideoDevInfoVector2 VidDevManager::enumDev2() const PJSUA2_THROW(Error)
for (unsigned i = 0; i<count;++i) {
VideoDevInfo vdi;
vdi.fromPj(pj_info[i]);
vdiv2.push_back(vdi);
vdiv2.push_back(PJSUA2_MOVE(vdi));
}
#endif
return vdiv2;
Expand Down Expand Up @@ -1979,7 +1979,7 @@ class CodecFmtpUtil
fmtp.name = pj2Str(in_fmtp.param[i].name);
fmtp.val = pj2Str(in_fmtp.param[i].val);

out_fmtp.push_back(fmtp);
out_fmtp.push_back(PJSUA2_MOVE(fmtp));
}
}

Expand Down
14 changes: 7 additions & 7 deletions pjsip/src/pjsua2/siptypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void readSipHeaders( const ContainerNode &node,
ContainerNode header_node = headers_node.readContainer("header");
hdr.hName = header_node.readString("hname");
hdr.hValue = header_node.readString("hvalue");
headers.push_back(hdr);
headers.push_back(PJSUA2_MOVE(hdr));
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ AuthCredInfo::AuthCredInfo(const string &param_scheme,
const int param_data_type,
const string param_data)
: scheme(param_scheme), realm(param_realm), username(param_user_name),
dataType(param_data_type), data(param_data),
dataType(param_data_type), data(PJSUA2_MOVE(param_data)),
algoType(PJSIP_AUTH_ALGORITHM_NOT_SET)
{
}
Expand Down Expand Up @@ -357,7 +357,7 @@ void SockOptParams::fromPj(const pj_sockopt_params &prm)
if (prm.options[i].optlen == sizeof(int)) {
so.setOptValInt(*((int *)prm.options[i].optval));
}
this->sockOpts.push_back(so);
this->sockOpts.push_back(PJSUA2_MOVE(so));
}
}

Expand All @@ -375,7 +375,7 @@ void SockOptParams::readObject(const ContainerNode &node) PJSUA2_THROW(Error)
int optVal = so_node.readInt("optVal");
so.setOptValInt(optVal);
}
sockOpts.push_back(so);
sockOpts.push_back(PJSUA2_MOVE(so));
}
}

Expand Down Expand Up @@ -609,7 +609,7 @@ void SipMultipartPart::fromPj(const pjsip_multipart_part &prm)
while (pj_hdr != &prm.hdr) {
SipHeader sh;
sh.fromPj(pj_hdr);
headers.push_back(sh);
headers.push_back(PJSUA2_MOVE(sh));
pj_hdr = pj_hdr->next;
}

Expand Down Expand Up @@ -757,7 +757,7 @@ void SipTxOption::fromPj(const pjsua_msg_data &prm) PJSUA2_THROW(Error)
while (pj_hdr != &prm.hdr_list) {
SipHeader sh;
sh.fromPj(pj_hdr);
headers.push_back(sh);
headers.push_back(PJSUA2_MOVE(sh));
pj_hdr = pj_hdr->next;
}

Expand All @@ -770,7 +770,7 @@ void SipTxOption::fromPj(const pjsua_msg_data &prm) PJSUA2_THROW(Error)
while (pj_mp != &prm.multipart_parts) {
SipMultipartPart smp;
smp.fromPj(*pj_mp);
multipartParts.push_back(smp);
multipartParts.push_back(PJSUA2_MOVE(smp));
pj_mp = pj_mp->next;
}
}
Expand Down

0 comments on commit d8795ba

Please sign in to comment.