Debian Codegraph @

Xpjproject/pjsip/src/pjsua2/call.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2012-2013 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pjsua2/call.hpp>
19 #include <pjsua2/endpoint.hpp>
20 #include <pj/ctype.h>
21 #include "util.hpp"
22
23 using namespace pj;
24 using namespace std;
25
26 #include <pjsua-lib/pjsua_internal.h>
27
28 #define THIS_FILE "call.cpp"
29
30 ///////////////////////////////////////////////////////////////////////////////
31
32 /* Avoid conflict with predefined standard macros. */
33 #undef max
34 #undef min
35
36 MathStat::MathStat()
37 : n(0), max(0), min(0), last(0), mean(0)
38 {
39 }
40
41 void MathStat::fromPj(const pj_math_stat &prm)
42 {
43 this->n = prm.n;
44 this->max = prm.max;
45 this->min = prm.min;
46 this->last = prm.last;
47 this->mean = prm.mean;
48 }
49
50 void RtcpStreamStat::fromPj(const pjmedia_rtcp_stream_stat &prm)
51 {
52 this->update.fromPj(prm.update);
53 this->updateCount = prm.update_cnt;
54 this->pkt = (unsigned)prm.pkt;
55 this->bytes = (unsigned)prm.bytes;
56 this->discard = prm.discard;
57 this->loss = prm.loss;
58 this->reorder = prm.loss;
59 this->dup = prm.dup;
60 this->lossPeriodUsec.fromPj(prm.loss_period);
61 this->lossType.burst = prm.loss_type.burst;
62 this->lossType.random = prm.loss_type.random;
63 this->jitterUsec.fromPj(prm.jitter);
64 }
65
66 void RtcpSdes::fromPj(const pjmedia_rtcp_sdes &prm)
67 {
68 this->cname = pj2Str(prm.cname);
69 this->name = pj2Str(prm.name);
70 this->email = pj2Str(prm.email);
71 this->phone = pj2Str(prm.phone);
72 this->loc = pj2Str(prm.loc);
73 this->tool = pj2Str(prm.tool);
74 this->note = pj2Str(prm.note);
75 }
76
77 void RtcpStat::fromPj(const pjmedia_rtcp_stat &prm)
78 {
79 this->start.fromPj(prm.start);
80 this->txStat.fromPj(prm.tx);
81 this->rxStat.fromPj(prm.rx);
82 this->rttUsec.fromPj(prm.rtt);
83 this->rtpTxLastTs = prm.rtp_tx_last_ts;
84 this->rtpTxLastSeq = prm.rtp_tx_last_seq;
85 #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0
86 this->rxIpdvUsec.fromPj(prm.rx_ipdv);
87 #endif
88 #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && \
89 PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0
90 this->rxRawJitterUsec.fromPj(prm.rx_raw_jitter);
91 #endif
92 this->peerSdes.fromPj(prm.peer_sdes);
93 }
94
95 void JbufState::fromPj(const pjmedia_jb_state &prm)
96 {
97 this->frameSize = prm.frame_size;
98 this->minPrefetch = prm.min_prefetch;
99 this->maxPrefetch = prm.max_prefetch;
100 this->burst = prm.burst;
101 this->prefetch = prm.prefetch;
102 this->size = prm.size;
103 this->avgDelayMsec = prm.avg_delay;
104 this->minDelayMsec = prm.min_delay;
105 this->maxDelayMsec = prm.max_delay;
106 this->devDelayMsec = prm.dev_delay;
107 this->avgBurst = prm.avg_burst;
108 this->lost = prm.lost;
109 this->discard = prm.discard;
110 this->empty = prm.empty;
111 }
112
113 void SdpSession::fromPj(const pjmedia_sdp_session &sdp)
114 {
115 #if PJSUA2_MAX_SDP_BUF_LEN
116 char buf[PJSUA2_MAX_SDP_BUF_LEN];
117 int len;
118
119 len = pjmedia_sdp_print(&sdp, buf, sizeof(buf));
120 wholeSdp = (len > -1? string(buf, len): "");
121 #else
122 wholeSdp = "";
123 #endif
124 pjSdpSession = (void *)&sdp;
125 }
126
127 void MediaTransportInfo::fromPj(const pjmedia_transport_info &info)
128 {
129 char straddr[PJ_INET6_ADDRSTRLEN+10];
130
131 localRtpName = localRtcpName = srcRtpName = srcRtcpName = "";
132 if (pj_sockaddr_has_addr(&info.sock_info.rtp_addr_name)) {
133 pj_sockaddr_print(&info.sock_info.rtp_addr_name, straddr,
134 sizeof(straddr), 3);
135 localRtpName = straddr;
136 }
137
138 if (pj_sockaddr_has_addr(&info.sock_info.rtcp_addr_name)) {
139 pj_sockaddr_print(&info.sock_info.rtcp_addr_name, straddr,
140 sizeof(straddr), 3);
141 localRtcpName = straddr;
142 }
143
144 if (pj_sockaddr_has_addr(&info.src_rtp_name)) {
145 pj_sockaddr_print(&info.src_rtp_name, straddr, sizeof(straddr), 3);
146 srcRtpName = straddr;
147 }
148
149 if (pj_sockaddr_has_addr(&info.src_rtcp_name)) {
150 pj_sockaddr_print(&info.src_rtcp_name, straddr, sizeof(straddr), 3);
151 srcRtcpName = straddr;
152 }
153 }
154
155 //////////////////////////////////////////////////////////////////////////////
156
157 CallOpParam::CallOpParam(bool useDefaultCallSetting)
158 : statusCode(pjsip_status_code(0)), reason(""), options(0)
159 {
160 sdp.wholeSdp = "";
161 if (useDefaultCallSetting)
162 opt = CallSetting(true);
163 }
164
165 CallSendRequestParam::CallSendRequestParam()
166 : method("")
167 {
168 }
169
170 CallVidSetStreamParam::CallVidSetStreamParam()
171 {
172 #if PJSUA_HAS_VIDEO
173 pjsua_call_vid_strm_op_param prm;
174
175 pjsua_call_vid_strm_op_param_default(&prm);
176 this->medIdx = prm.med_idx;
177 this->dir = prm.dir;
178 this->capDev = prm.cap_dev;
179 #endif
180 }
181
182 CallSendDtmfParam::CallSendDtmfParam()
183 {
184 pjsua_call_send_dtmf_param param;
185 pjsua_call_send_dtmf_param_default(&param);
186 fromPj(param);
187 }
188
189 pjsua_call_send_dtmf_param CallSendDtmfParam::toPj() const
190 {
191 pjsua_call_send_dtmf_param param;
192 pjsua_call_send_dtmf_param_default(&param);
193 param.method = this->method;
194 param.duration = this->duration;
195 param.digits = str2Pj(this->digits);
196 return param;
197 }
198
199 void CallSendDtmfParam::fromPj(const pjsua_call_send_dtmf_param &param)
200 {
201 this->method = param.method;
202 this->duration = param.duration;
203 this->digits = pj2Str(param.digits);
204 }
205
206 CallSendTextParam::CallSendTextParam()
207 {
208 medIdx = -1;
209 }
210
211 CallSetting::CallSetting(bool useDefaultValues)
212 {
213 if (useDefaultValues) {
214 pjsua_call_setting setting;
215
216 pjsua_call_setting_default(&setting);
217 fromPj(setting);
218 } else {
219 flag = 0;
220 reqKeyframeMethod = 0;
221 audioCount = 0;
222 videoCount = 0;
223 textCount = 0;
224 }
225 }
226
227 bool CallSetting::isEmpty() const
228 {
229 return (flag == 0 && reqKeyframeMethod == 0 && audioCount == 0 &&
230 videoCount == 0 && textCount == 0);
231 }
232
233 void CallSetting::fromPj(const pjsua_call_setting &prm)
234 {
235 int i, mi;
236
237 this->flag = prm.flag;
238 this->reqKeyframeMethod = prm.req_keyframe_method;
239 this->audioCount = prm.aud_cnt;
240 this->videoCount = prm.vid_cnt;
241 this->textCount = prm.txt_cnt;
242 this->mediaDir.clear();
243 this->customCallId = pj2Str(prm.custom_call_id);
244
245 /* Since we don't know the size of media_dir array, we populate
246 * mediaDir vector up to the element with non-default value.
247 */
248 for (mi = PJMEDIA_MAX_SDP_MEDIA - 1; mi >= 0; mi--) {
249 if (prm.media_dir[mi] != PJMEDIA_DIR_ENCODING_DECODING) break;
250 }
251 for (i = 0; i <= mi; i++) {
252 this->mediaDir.push_back(prm.media_dir[i]);
253 }
254 }
255
256 pjsua_call_setting CallSetting::toPj() const
257 {
258 pjsua_call_setting setting;
259 unsigned mi;
260
261 /* This is important to initialize media_dir array. */
262 pjsua_call_setting_default(&setting);
263
264 setting.flag = this->flag;
265 setting.req_keyframe_method = this->reqKeyframeMethod;
266 setting.aud_cnt = this->audioCount;
267 setting.vid_cnt = this->videoCount;
268 setting.txt_cnt = this->textCount;
269 for (mi = 0; mi < this->mediaDir.size(); mi++) {
270 setting.media_dir[mi] = (pjmedia_dir)this->mediaDir[mi];
271 }
272
273 if( ! this->customCallId.empty()) {
274 setting.custom_call_id = str2Pj(this->customCallId);
275 }
276
277 return setting;
278 }
279
280
281 CallMediaInfo::CallMediaInfo()
282 : index(0xFF),
283 type(PJMEDIA_TYPE_NONE),
284 dir(PJMEDIA_DIR_NONE),
285 status(PJSUA_CALL_MEDIA_NONE),
286 audioConfSlot(PJSUA_INVALID_ID),
287 videoIncomingWindowId(PJSUA_INVALID_ID),
288 videoWindow(PJSUA_INVALID_ID),
289 videoCapDev(PJMEDIA_VID_INVALID_DEV)
290 {
291 }
292
293 void CallMediaInfo::fromPj(const pjsua_call_media_info &prm)
294 {
295 this->index = prm.index;
296 this->type = prm.type;
297 this->dir = prm.dir;
298 this->status = prm.status;
299 if (this->type == PJMEDIA_TYPE_AUDIO) {
300 this->audioConfSlot = (int)prm.stream.aud.conf_slot;
301 } else if (this->type == PJMEDIA_TYPE_VIDEO) {
302 this->videoIncomingWindowId = prm.stream.vid.win_in;
303 this->videoWindow = VideoWindow(prm.stream.vid.win_in);
304 this->videoCapDev = prm.stream.vid.cap_dev;
305 }
306 }
307
308 void CallInfo::fromPj(const pjsua_call_info &pci)
309 {
310 unsigned mi;
311
312 id = pci.id;
313 role = pci.role;
314 accId = pci.acc_id;
315 localUri = pj2Str(pci.local_info);
316 localContact = pj2Str(pci.local_contact);
317 remoteUri = pj2Str(pci.remote_info);
318 remoteContact = pj2Str(pci.remote_contact);
319 callIdString = pj2Str(pci.call_id);
320 setting.fromPj(pci.setting);
321 state = pci.state;
322 stateText = pj2Str(pci.state_text);
323 lastStatusCode = pci.last_status;
324 lastReason = pj2Str(pci.last_status_text);
325 connectDuration.fromPj(pci.connect_duration);
326 totalDuration.fromPj(pci.total_duration);
327 remOfferer = PJ2BOOL(pci.rem_offerer);
328 remAudioCount = pci.rem_aud_cnt;
329 remVideoCount = pci.rem_vid_cnt;
330 remTextCount = pci.rem_txt_cnt;
331
332 for (mi = 0; mi < pci.media_cnt; mi++) {
333 CallMediaInfo med;
334
335 med.fromPj(pci.media[mi]);
336 media.push_back(med);
337 }
338 for (mi = 0; mi < pci.prov_media_cnt; mi++) {
339 CallMediaInfo med;
340
341 med.fromPj(pci.prov_media[mi]);
342 provMedia.push_back(med);
343 }
344 }
345
346 void StreamInfo::fromPj(const pjsua_stream_info &info)
347 {
348 char straddr[PJ_INET6_ADDRSTRLEN+10];
349
350 type = info.type;
351 if (type == PJMEDIA_TYPE_AUDIO) {
352 proto = info.info.aud.proto;
353 dir = info.info.aud.dir;
354 pj_sockaddr_print(&info.info.aud.rem_addr, straddr, sizeof(straddr), 3);
355 remoteRtpAddress = straddr;
356 pj_sockaddr_print(&info.info.aud.rem_rtcp, straddr, sizeof(straddr), 3);
357 remoteRtcpAddress = straddr;
358 txPt = info.info.aud.tx_pt;
359 rxPt = info.info.aud.rx_pt;
360 audTxEventPt = info.info.aud.tx_event_pt;
361 audRxEventPt = info.info.aud.rx_event_pt;
362 codecName = pj2Str(info.info.aud.fmt.encoding_name);
363 codecClockRate = info.info.aud.fmt.clock_rate;
364 audCodecParam.fromPj(*info.info.aud.param);
365 jbInit = info.info.aud.jb_init;
366 jbMinPre = info.info.aud.jb_min_pre;
367 jbMaxPre = info.info.aud.jb_max_pre;
368 jbMax = info.info.aud.jb_max;
369 jbDiscardAlgo = info.info.aud.jb_discard_algo;
370 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
371 useKa = PJ2BOOL(info.info.aud.use_ka);
372 startCountKa = info.info.aud.ka_cfg.start_count;
373 startIntervalKa = info.info.aud.ka_cfg.start_interval;
374 intervalKa = info.info.aud.ka_cfg.ka_interval;
375 #endif
376 rtcpSdesByeDisabled = PJ2BOOL(info.info.aud.rtcp_sdes_bye_disabled);
377 } else if (type == PJMEDIA_TYPE_VIDEO) {
378 proto = info.info.vid.proto;
379 dir = info.info.vid.dir;
380 pj_sockaddr_print(&info.info.vid.rem_addr, straddr, sizeof(straddr), 3);
381 remoteRtpAddress = straddr;
382 pj_sockaddr_print(&info.info.vid.rem_rtcp, straddr, sizeof(straddr), 3);
383 remoteRtcpAddress = straddr;
384 txPt = info.info.vid.tx_pt;
385 rxPt = info.info.vid.rx_pt;
386 codecName = pj2Str(info.info.vid.codec_info.encoding_name);
387 codecClockRate = info.info.vid.codec_info.clock_rate;
388 vidCodecParam.fromPj(*info.info.vid.codec_param);
389 jbInit = info.info.vid.jb_init;
390 jbMinPre = info.info.vid.jb_min_pre;
391 jbMaxPre = info.info.vid.jb_max_pre;
392 jbMax = info.info.vid.jb_max;
393 jbDiscardAlgo = PJMEDIA_JB_DISCARD_NONE;
394 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
395 useKa = PJ2BOOL(info.info.vid.use_ka);
396 startCountKa = info.info.vid.ka_cfg.start_count;
397 startIntervalKa = info.info.vid.ka_cfg.start_interval;
398 intervalKa = info.info.vid.ka_cfg.ka_interval;
399 #endif
400 rtcpSdesByeDisabled = PJ2BOOL(info.info.vid.rtcp_sdes_bye_disabled);
401 } else if (type == PJMEDIA_TYPE_TEXT) {
402 proto = info.info.txt.proto;
403 dir = info.info.txt.dir;
404 pj_sockaddr_print(&info.info.txt.rem_addr, straddr, sizeof(straddr), 3);
405 remoteRtpAddress = straddr;
406 pj_sockaddr_print(&info.info.txt.rem_rtcp, straddr, sizeof(straddr), 3);
407 remoteRtcpAddress = straddr;
408 txPt = info.info.txt.tx_pt;
409 rxPt = info.info.txt.rx_pt;
410 codecName = pj2Str(info.info.txt.fmt.encoding_name);
411 codecClockRate = info.info.txt.fmt.clock_rate;
412 jbInit = info.info.txt.jb_init;
413 jbMinPre = info.info.txt.jb_min_pre;
414 jbMaxPre = info.info.txt.jb_max_pre;
415 jbMax = info.info.txt.jb_max;
416 jbDiscardAlgo = PJMEDIA_JB_DISCARD_NONE;
417 #if defined(PJMEDIA_STREAM_ENABLE_KA) && (PJMEDIA_STREAM_ENABLE_KA != 0)
418 useKa = PJ2BOOL(info.info.txt.use_ka);
419 startCountKa = info.info.txt.ka_cfg.start_count;
420 startIntervalKa = info.info.txt.ka_cfg.start_interval;
421 intervalKa = info.info.txt.ka_cfg.ka_interval;
422 #endif
423 rtcpSdesByeDisabled = PJ2BOOL(info.info.txt.rtcp_sdes_bye_disabled);
424 }
425 }
426
427 void StreamStat::fromPj(const pjsua_stream_stat &prm)
428 {
429 rtcp.fromPj(prm.rtcp);
430 jbuf.fromPj(prm.jbuf);
431 }
432
433 void OnCallRxTextParam::fromPj(const pjsua_txt_stream_data &prm)
434 {
435 seq = prm.seq;
436 ts = prm.ts;
437 text = pj2Str(prm.text);
438 }
439
440 ///////////////////////////////////////////////////////////////////////////////
441
442 struct call_param
443 {
444 pjsua_msg_data *p_msg_data;
445 pjsua_call_setting *p_opt;
446 pj_str_t *p_reason;
447 pjmedia_sdp_session *sdp;
448
449 public:
450 /**
451 * Default constructors with specified parameters.
452 */
453 call_param(const SipTxOption &tx_option);
454 call_param(const SipTxOption &tx_option, const CallSetting &setting,
455 const string &reason_str, pj_pool_t *pool = NULL,
456 const string &sdp_str = "");
457
458 private:
459 /* internal storages */
460 pjsua_msg_data msg_data;
461 pjsua_call_setting opt;
462 pj_str_t reason;
463 };
464
465 call_param::call_param(const SipTxOption &tx_option)
466 {
467 pjsua_msg_data_init(&msg_data);
468 if (tx_option.isEmpty()) {
469 p_msg_data = NULL;
470 } else {
471 tx_option.toPj(msg_data);
472 p_msg_data = &msg_data;
473 }
474
475 p_opt = NULL;
476 p_reason = NULL;
477 sdp = NULL;
478 }
479
480 call_param::call_param(const SipTxOption &tx_option, const CallSetting &setting,
481 const string &reason_str, pj_pool_t *pool,
482 const string &sdp_str)
483 {
484 if (tx_option.isEmpty()) {
485 p_msg_data = NULL;
486 } else {
487 tx_option.toPj(msg_data);
488 p_msg_data = &msg_data;
489 }
490
491 if (setting.isEmpty()) {
492 p_opt = NULL;
493 } else {
494 opt = setting.toPj();
495 p_opt = &opt;
496 }
497
498 reason = str2Pj(reason_str);
499 p_reason = (reason.slen == 0? NULL: &reason);
500
501 sdp = NULL;
502 if (pool != NULL && sdp_str != "") {
503 pj_str_t dup_pj_sdp;
504 pj_str_t pj_sdp_str = {(char*)sdp_str.c_str(),
505 (pj_ssize_t)sdp_str.size()};
506 pj_status_t status;
507
508 pj_strdup(pool, &dup_pj_sdp, &pj_sdp_str);
509 status = pjmedia_sdp_parse(pool, dup_pj_sdp.ptr,
510 dup_pj_sdp.slen, &sdp);
511 if (status != PJ_SUCCESS) {
512 PJ_PERROR(4,(THIS_FILE, status,
513 "Failed to parse SDP for call param"));
514 }
515 }
516 }
517
518 Call::Call(Account& account, int call_id)
519 : acc(&account), id(call_id), userData(NULL), child(NULL)
520 {
521 if (call_id != PJSUA_INVALID_ID)
522 pjsua_call_set_user_data(call_id, this);
523 }
524
525 Call::~Call()
526 {
527 /* Remove reference to this instance from PJSUA library */
528 if (id != PJSUA_INVALID_ID)
529 pjsua_call_set_user_data(id, NULL);
530
531 /*
532 * If this instance is deleted, also hangup the corresponding call in
533 * PJSUA library.
534 */
535 if (pjsua_get_state() < PJSUA_STATE_CLOSING && isActive()) {
536 try {
537 CallOpParam prm;
538 hangup(prm);
539 } catch (Error &err) {
540 // Ignore
541 PJ_UNUSED_ARG(err);
542 }
543 }
544 }
545
546 CallInfo Call::getInfo() const PJSUA2_THROW(Error)
547 {
548 pjsua_call_info pj_ci;
549 CallInfo ci;
550
551 PJSUA2_CHECK_EXPR( pjsua_call_get_info(id, &pj_ci) );
552 ci.fromPj(pj_ci);
553 return ci;
554 }
555
556 bool Call::isActive() const
557 {
558 if (id == PJSUA_INVALID_ID)
559 return false;
560
561 return (pjsua_call_is_active(id) != 0);
562 }
563
564 int Call::getId() const
565 {
566 return id;
567 }
568
569 Call *Call::lookup(int call_id)
570 {
571 Call *call = (Call*)pjsua_call_get_user_data(call_id);
572 if (call && call_id != call->id) {
573 if (call->child && call->child->id == PJSUA_INVALID_ID) {
574 /* This must be a new call from call transfer or call replace
575 * which initially shares user_data with its parent (so the
576 * user_data points to its parent's Call instance).
577 * Let's update its user_data to its own Call instance.
578 */
579 call = call->child;
580 pjsua_call_set_user_data(call_id, call);
581 }
582 call->id = call_id;
583 }
584 return call;
585 }
586
587 bool Call::hasMedia() const
588 {
589 return (pjsua_call_has_media(id) != 0);
590 }
591
592 Media *Call::getMedia(unsigned med_idx) const
593 {
594 /* Check if the media index is valid and if the media has a valid port ID */
595 if (med_idx >= medias.size() ||
596 (medias[med_idx] && medias[med_idx]->getType() == PJMEDIA_TYPE_AUDIO &&
597 ((AudioMedia *)medias[med_idx])->getPortId() == PJSUA_INVALID_ID))
598 {
599 return NULL;
600 }
601
602 return medias[med_idx];
603 }
604
605 AudioMedia Call::getAudioMedia(int med_idx) const PJSUA2_THROW(Error)
606 {
607 pjsua_call_info pj_ci;
608 pjsua_call_get_info(id, &pj_ci);
609
610 if (med_idx < 0) {
611 for (unsigned i = 0; i < pj_ci.media_cnt; ++i) {
612 if (pj_ci.media[i].type == PJMEDIA_TYPE_AUDIO &&
613 pj_ci.media[i].stream.aud.conf_slot != PJSUA_INVALID_ID)
614 {
615 med_idx = i;
616 break;
617 }
618 }
619 if (med_idx < 0) {
620 PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "getAudioMedia()",
621 "no active audio media");
622 }
623 }
624
625 if (med_idx >= (int)pj_ci.media_cnt) {
626 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getAudioMedia()",
627 "invalid media index");
628 }
629 if (pj_ci.media[med_idx].type != PJMEDIA_TYPE_AUDIO) {
630 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getAudioMedia()",
631 "media is not audio");
632 }
633 if (pj_ci.media[med_idx].stream.aud.conf_slot == PJSUA_INVALID_ID) {
634 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getAudioMedia()",
635 "no audio slot (inactive?)");
636 }
637
638 AudioMediaHelper am;
639 am.setPortId(pj_ci.media[med_idx].stream.aud.conf_slot);
640 return am;
641 }
642
643 VideoMedia Call::getEncodingVideoMedia(int med_idx) const PJSUA2_THROW(Error)
644 {
645 pjsua_call_info pj_ci;
646 pjsua_call_get_info(id, &pj_ci);
647
648 if (med_idx < 0) {
649 for (unsigned i = 0; i < pj_ci.media_cnt; ++i) {
650 if (pj_ci.media[i].type == PJMEDIA_TYPE_VIDEO &&
651 pj_ci.media[i].stream.vid.enc_slot != PJSUA_INVALID_ID)
652 {
653 med_idx = i;
654 break;
655 }
656 }
657 if (med_idx < 0) {
658 PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "getEncodingVideoMedia()",
659 "no active encoding video media");
660 }
661 }
662
663 if (med_idx >= (int)pj_ci.media_cnt) {
664 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getEncodingVideoMedia()",
665 "invalid media index");
666 }
667 if (pj_ci.media[med_idx].type != PJMEDIA_TYPE_VIDEO) {
668 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getEncodingVideoMedia()",
669 "media is not video");
670 }
671 if (pj_ci.media[med_idx].stream.vid.enc_slot == PJSUA_INVALID_ID) {
672 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getEncodingVideoMedia()",
673 "no encoding slot (recvonly?)");
674 }
675
676 VideoMediaHelper vm;
677 vm.setPortId(pj_ci.media[med_idx].stream.vid.enc_slot);
678 return vm;
679 }
680
681 VideoMedia Call::getDecodingVideoMedia(int med_idx) const PJSUA2_THROW(Error)
682 {
683 pjsua_call_info pj_ci;
684 pjsua_call_get_info(id, &pj_ci);
685
686 if (med_idx < 0) {
687 for (unsigned i = 0; i < pj_ci.media_cnt; ++i) {
688 if (pj_ci.media[i].type == PJMEDIA_TYPE_VIDEO &&
689 pj_ci.media[i].stream.vid.dec_slot != PJSUA_INVALID_ID)
690 {
691 med_idx = i;
692 break;
693 }
694 }
695 if (med_idx < 0) {
696 PJSUA2_RAISE_ERROR3(PJ_ENOTFOUND, "getDecodingVideoMedia()",
697 "no active decoding video media");
698 }
699 }
700
701 if (med_idx >= (int)pj_ci.media_cnt) {
702 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getDecodingVideoMedia()",
703 "invalid media index");
704 }
705 if (pj_ci.media[med_idx].type != PJMEDIA_TYPE_VIDEO) {
706 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getDecodingVideoMedia()",
707 "media is not video");
708 }
709 if (pj_ci.media[med_idx].stream.vid.dec_slot == PJSUA_INVALID_ID) {
710 PJSUA2_RAISE_ERROR3(PJ_EINVAL, "getDecodingVideoMedia()",
711 "no decoding slot (sendonly?)");
712 }
713
714 VideoMediaHelper vm;
715 vm.setPortId(pj_ci.media[med_idx].stream.vid.dec_slot);
716 return vm;
717 }
718
719 pjsip_dialog_cap_status Call::remoteHasCap(int htype,
720 const string &hname,
721 const string &token) const
722 {
723 pj_str_t pj_hname = str2Pj(hname);
724 pj_str_t pj_token = str2Pj(token);
725
726 return pjsua_call_remote_has_cap(id, htype,
727 (htype == PJSIP_H_OTHER)? &pj_hname: NULL,
728 &pj_token);
729 }
730
731 void Call::setUserData(Token user_data)
732 {
733 userData = user_data;
734 }
735
736 Token Call::getUserData() const
737 {
738 return userData;
739 }
740
741 pj_stun_nat_type Call::getRemNatType() PJSUA2_THROW(Error)
742 {
743 pj_stun_nat_type nat;
744
745 PJSUA2_CHECK_EXPR( pjsua_call_get_rem_nat_type(id, &nat) );
746
747 return nat;
748 }
749
750 void Call::makeCall(const string &dst_uri, const CallOpParam &prm)
751 PJSUA2_THROW(Error)
752 {
753 pj_str_t pj_dst_uri = str2Pj(dst_uri);
754 call_param param(prm.txOption, prm.opt, prm.reason);
755
756 PJSUA2_CHECK_EXPR( pjsua_call_make_call(acc->getId(), &pj_dst_uri,
757 param.p_opt, this,
758 param.p_msg_data, &id) );
759 }
760
761 void Call::answer(const CallOpParam &prm) PJSUA2_THROW(Error)
762 {
763 pj_pool_t *tmp_pool = NULL;
764
765 /* Create temporary pool for SDP operations if SDP is provided */
766 if (!prm.sdp.wholeSdp.empty()) {
767 tmp_pool = pjsua_pool_create("tmp-answer", 2048, 512);
768 if (!tmp_pool) {
769 PJSUA2_RAISE_ERROR2(PJ_ENOMEM, "Call::answer()");
770 }
771 }
772
773 call_param param(prm.txOption, prm.opt, prm.reason,
774 tmp_pool, prm.sdp.wholeSdp);
775
776 if (param.sdp) {
777 PJSUA2_CHECK_EXPR( pjsua_call_answer_with_sdp(id, param.sdp,
778 param.p_opt,
779 prm.statusCode,
780 param.p_reason,
781 param.p_msg_data) );
782 } else {
783 PJSUA2_CHECK_EXPR( pjsua_call_answer2(id, param.p_opt, prm.statusCode,
784 param.p_reason,
785 param.p_msg_data) );
786 }
787
788 /* Release temporary pool */
789 if (tmp_pool) {
790 pj_pool_release(tmp_pool);
791 }
792 }
793
794 void Call::hangup(const CallOpParam &prm) PJSUA2_THROW(Error)
795 {
796 call_param param(prm.txOption, prm.opt, prm.reason);
797
798 PJSUA2_CHECK_EXPR( pjsua_call_hangup(id, prm.statusCode, param.p_reason,
799 param.p_msg_data) );
800 }
801
802 void Call::setHold(const CallOpParam &prm) PJSUA2_THROW(Error)
803 {
804 call_param param(prm.txOption, prm.opt, prm.reason);
805
806 PJSUA2_CHECK_EXPR( pjsua_call_set_hold2(id, prm.options,
807 param.p_msg_data) );
808 }
809
810 void Call::reinvite(const CallOpParam &prm) PJSUA2_THROW(Error)
811 {
812 call_param param(prm.txOption, prm.opt, prm.reason);
813
814 PJSUA2_CHECK_EXPR( pjsua_call_reinvite2(id, param.p_opt,
815 param.p_msg_data) );
816 }
817
818 void Call::update(const CallOpParam &prm) PJSUA2_THROW(Error)
819 {
820 call_param param(prm.txOption, prm.opt, prm.reason);
821
822 PJSUA2_CHECK_EXPR( pjsua_call_update2(id, param.p_opt,
823 param.p_msg_data) );
824 }
825
826 void Call::xfer(const string &dest, const CallOpParam &prm)
827 PJSUA2_THROW(Error)
828 {
829 call_param param(prm.txOption);
830 pj_str_t pj_dest = str2Pj(dest);
831
832 PJSUA2_CHECK_EXPR( pjsua_call_xfer(id, &pj_dest, param.p_msg_data) );
833 }
834
835 void Call::xferReplaces(const Call& dest_call,
836 const CallOpParam &prm) PJSUA2_THROW(Error)
837 {
838 call_param param(prm.txOption);
839
840 PJSUA2_CHECK_EXPR(pjsua_call_xfer_replaces(id, dest_call.getId(),
841 prm.options,
842 param.p_msg_data) );
843 }
844
845 void Call::processRedirect(pjsip_redirect_op cmd) PJSUA2_THROW(Error)
846 {
847 PJSUA2_CHECK_EXPR(pjsua_call_process_redirect(id, cmd));
848 }
849
850 void Call::dialDtmf(const string &digits) PJSUA2_THROW(Error)
851 {
852 pj_str_t pj_digits = str2Pj(digits);
853
854 PJSUA2_CHECK_EXPR(pjsua_call_dial_dtmf(id, &pj_digits));
855 }
856
857 void Call::sendDtmf(const CallSendDtmfParam &param) PJSUA2_THROW(Error)
858 {
859 pjsua_call_send_dtmf_param pj_param = param.toPj();
860
861 PJSUA2_CHECK_EXPR(pjsua_call_send_dtmf(id, &pj_param));
862 }
863
864 void Call::sendText(const CallSendTextParam &param) PJSUA2_THROW(Error)
865 {
866 pjsua_call_send_text_param pj_param;
867
868 pjsua_call_send_text_param_default(&pj_param);
869 pj_param.text = str2Pj(param.text);
870
871 PJSUA2_CHECK_EXPR(pjsua_call_send_text(id, &pj_param));
872 }
873
874 void Call::sendInstantMessage(const SendInstantMessageParam& prm)
875 PJSUA2_THROW(Error)
876 {
877 pj_str_t mime_type = str2Pj(prm.contentType);
878 pj_str_t content = str2Pj(prm.content);
879 call_param param(prm.txOption);
880
881 PJSUA2_CHECK_EXPR(pjsua_call_send_im(id, &mime_type, &content,
882 param.p_msg_data, prm.userData) );
883 }
884
885 void Call::sendTypingIndication(const SendTypingIndicationParam &prm)
886 PJSUA2_THROW(Error)
887 {
888 call_param param(prm.txOption);
889
890 PJSUA2_CHECK_EXPR(pjsua_call_send_typing_ind(id,
891 (prm.isTyping?
892 PJ_TRUE: PJ_FALSE),
893 param.p_msg_data) );
894 }
895
896 void Call::sendRequest(const CallSendRequestParam &prm) PJSUA2_THROW(Error)
897 {
898 pj_str_t method = str2Pj(prm.method);
899 call_param param(prm.txOption);
900
901 PJSUA2_CHECK_EXPR(pjsua_call_send_request(id, &method,
902 param.p_msg_data) );
903 }
904
905 string Call::dump(bool with_media, const string indent) PJSUA2_THROW(Error)
906 {
907 #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
908 char buffer[1024 * 10];
909 #else
910 char buffer[1024 * 3];
911 #endif
912
913 PJSUA2_CHECK_EXPR(pjsua_call_dump(id, (with_media? PJ_TRUE: PJ_FALSE),
914 buffer, sizeof(buffer),
915 indent.c_str()));
916
917 return buffer;
918 }
919
920 int Call::vidGetStreamIdx() const
921 {
922 #if PJSUA_HAS_VIDEO
923 return pjsua_call_get_vid_stream_idx(id);
924 #else
925 return PJSUA_INVALID_ID;
926 #endif
927 }
928
929 bool Call::vidStreamIsRunning(int med_idx, pjmedia_dir dir) const
930 {
931 #if PJSUA_HAS_VIDEO
932 return (pjsua_call_vid_stream_is_running(id, med_idx, dir) == PJ_TRUE);
933 #else
934 PJ_UNUSED_ARG(med_idx);
935 PJ_UNUSED_ARG(dir);
936 return false;
937 #endif
938 }
939
940 void Call::vidSetStream(pjsua_call_vid_strm_op op,
941 const CallVidSetStreamParam &param)
942 PJSUA2_THROW(Error)
943 {
944 #if PJSUA_HAS_VIDEO
945 pjsua_call_vid_strm_op_param prm;
946
947 prm.med_idx = param.medIdx;
948 prm.dir = param.dir;
949 prm.cap_dev = param.capDev;
950 PJSUA2_CHECK_EXPR( pjsua_call_set_vid_strm(id, op, &prm) );
951 #else
952 PJ_UNUSED_ARG(op);
953 PJ_UNUSED_ARG(param);
954 PJSUA2_RAISE_ERROR(PJ_EINVALIDOP);
955 #endif
956 }
957
958 void Call::vidStreamModifyCodecParam(int med_idx, const VidCodecParam &param)
959 PJSUA2_THROW(Error)
960 {
961 #if PJSUA_HAS_VIDEO
962 pjmedia_vid_codec_param prm = param.toPj();
963 PJSUA2_CHECK_EXPR( pjsua_call_vid_stream_modify_codec_param(id, med_idx,
964 &prm) );
965 #else
966 PJ_UNUSED_ARG(med_idx);
967 PJ_UNUSED_ARG(param);
968 PJSUA2_RAISE_ERROR(PJ_EINVALIDOP);
969 #endif
970 }
971
972 void Call::audStreamModifyCodecParam(int med_idx, const CodecParam &param)
973 PJSUA2_THROW(Error)
974 {
975 pjmedia_codec_param prm = param.toPj();
976 PJSUA2_CHECK_EXPR( pjsua_call_aud_stream_modify_codec_param(id, med_idx,
977 &prm) );
978 }
979
980 StreamInfo Call::getStreamInfo(unsigned med_idx) const PJSUA2_THROW(Error)
981 {
982 pjsua_stream_info pj_si;
983 StreamInfo si;
984
985 PJSUA2_CHECK_EXPR( pjsua_call_get_stream_info(id, med_idx, &pj_si) );
986 si.fromPj(pj_si);
987 return si;
988 }
989
990 StreamStat Call::getStreamStat(unsigned med_idx) const PJSUA2_THROW(Error)
991 {
992 pjsua_stream_stat pj_ss;
993 StreamStat ss;
994
995 PJSUA2_CHECK_EXPR( pjsua_call_get_stream_stat(id, med_idx, &pj_ss) );
996 ss.fromPj(pj_ss);
997 return ss;
998 }
999
1000 MediaTransportInfo Call::getMedTransportInfo(unsigned med_idx) const
1001 PJSUA2_THROW(Error)
1002 {
1003 pjmedia_transport_info pj_mti;
1004 MediaTransportInfo mti;
1005
1006 PJSUA2_CHECK_EXPR( pjsua_call_get_med_transport_info(id, med_idx,
1007 &pj_mti) );
1008 mti.fromPj(pj_mti);
1009 return mti;
1010 }
1011
1012 void Call::processMediaUpdate(OnCallMediaStateParam &prm)
1013 {
1014 pjsua_call_info pj_ci;
1015 unsigned mi;
1016
1017 if (pjsua_call_get_info(id, &pj_ci) == PJ_SUCCESS) {
1018 if (medias.size()) {
1019 /* Clear medias. */
1020 for (mi = 0; mi < medias.size(); mi++) {
1021 if (medias[mi]) {
1022 Endpoint::instance().mediaRemove((AudioMedia&)*medias[mi]);
1023 delete medias[mi];
1024 }
1025 }
1026 medias.clear();
1027 }
1028
1029 for (mi = 0; mi < pj_ci.media_cnt; mi++) {
1030 if (mi >= medias.size()) {
1031 if (pj_ci.media[mi].type == PJMEDIA_TYPE_AUDIO) {
1032 medias.push_back(new AudioMediaHelper);
1033 } else {
1034 medias.push_back(NULL);
1035 }
1036 }
1037
1038 if (pj_ci.media[mi].type == PJMEDIA_TYPE_AUDIO) {
1039 AudioMediaHelper *aud_med = (AudioMediaHelper*)medias[mi];
1040 aud_med->setPortId(pj_ci.media[mi].stream.aud.conf_slot);
1041
1042 /* Add media if the conference slot ID is valid. */
1043 if (pj_ci.media[mi].stream.aud.conf_slot != PJSUA_INVALID_ID)
1044 {
1045 Endpoint::instance().mediaAdd((AudioMedia &)*aud_med);
1046 } else {
1047 Endpoint::instance().mediaRemove((AudioMedia &)*aud_med);
1048 }
1049 }
1050 }
1051 }
1052
1053 /* Call media state callback. */
1054 onCallMediaState(prm);
1055 }
1056
1057 void Call::processStateChange(OnCallStateParam &prm)
1058 {
1059 pjsua_call_info pj_ci;
1060 unsigned mi;
1061
1062 if (pjsua_call_get_info(id, &pj_ci) == PJ_SUCCESS &&
1063 pj_ci.state == PJSIP_INV_STATE_DISCONNECTED)
1064 {
1065 pjsua_call *call = &pjsua_var.calls[id];
1066
1067 /* We are going to remove the Call object association below,
1068 * so we need to call onStreamDestroyed() callback here.
1069 */
1070 for (mi = 0; mi < call->med_cnt; ++mi) {
1071 pjsua_call_media *call_med = &call->media[mi];
1072 if (call_med->type == PJMEDIA_TYPE_AUDIO &&
1073 call_med->strm.a.stream)
1074 {
1075 OnStreamDestroyedParam strm_prm;
1076 strm_prm.stream = call_med->strm.a.stream;
1077 strm_prm.streamIdx = mi;
1078
1079 onStreamDestroyed(strm_prm);
1080 }
1081 }
1082
1083 /* Clear medias. */
1084 for (mi = 0; mi < medias.size(); mi++) {
1085 if (medias[mi]) {
1086 Endpoint::instance().mediaRemove((AudioMedia &)*medias[mi]);
1087 delete medias[mi];
1088 }
1089 }
1090 medias.clear();
1091
1092 /* Remove this Call object association */
1093 pjsua_call_set_user_data(id, NULL);
1094 }
1095
1096 onCallState(prm);
1097 /* If the state is DISCONNECTED, this call may have already been deleted
1098 * by the application in the callback, so do not access it anymore here.
1099 */
1100 }