1 /*
2 * Copyright (C) 2013 Stefan Sayer
3 *
4 * This file is part of SEMS, a free SIP media server.
5 *
6 * SEMS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version. This program is released under
10 * the GPL with the additional exemption that compiling, linking,
11 * and/or using OpenSSL is allowed.
12 *
13 * For a license to use the SEMS software under conditions
14 * other than those described here, or to purchase support for this
15 * software, please contact iptel.org by e-mail at the following addresses:
16 * info@iptel.org
17 *
18 * SEMS is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28 #include "SBCDSMInstance.h"
29 #include "SBCCallLeg.h"
30 #include "SBCSimpleRelay.h"
31
32 #include "DSM.h"
33 #include "SBCDSMParams.h"
34
35 #include "AmAdvancedAudio.h"
36 #include "AmRingTone.h"
37
38 #include <algorithm>
39
40 using namespace std;
41
SBCDSMInstance(SBCCallLeg * call,const VarMapT & values)42 SBCDSMInstance::SBCDSMInstance(SBCCallLeg *call, const VarMapT& values)
43 : call(call), local_media_connected(false)
44 {
45 DBG("SBCDSMInstance::SBCDSMInstance()\n");
46 VarMapT::const_iterator it = values.find(DSM_SBC_CCVAR_START_DIAG);
47 if (it != values.end())
48 startDiagName = it->second;
49
50 it = values.find(DSM_SBC_CCVAR_APP_BUNDLE);
51 if (it != values.end())
52 appBundle = it->second;
53
54 if (startDiagName.empty()) {
55 throw string("DSM SBC call control " DSM_SBC_CCVAR_START_DIAG " parameter not set (see call profile)'");
56 }
57
58 map<string,string> config_vars;
59 bool SetParamVariables; // unused
60
61 if (!DSMFactory::instance()->
62 addScriptDiagsToEngine(appBundle,
63 &engine,
64 config_vars,
65 SetParamVariables)) {
66 ERROR("initializing call with DSM app bundle '%s'\n", appBundle.c_str());
67 throw string("initializing call with DSM app bundle '" +appBundle);
68 }
69
70 for (map<string, string>::const_iterator it =
71 config_vars.begin(); it != config_vars.end(); it++)
72 var["config."+it->first] = it->second;
73
74 // overwrite config. variables with cc-instance variables (map::insert doesn't overwrite)
75 for (VarMapT::const_iterator it = values.begin(); it != values.end(); it++) {
76 var[it->first] = it->second;
77 }
78
79 DBG("Running init of SBCDSMInstance...\n");
80 if (!engine.init(call, this, startDiagName, DSMCondition::Start)) {
81 WARN("Initialization failed for SBCDSMInstance\n");
82 // TODO: mark this as not running!
83 return;
84 }
85 }
86
~SBCDSMInstance()87 SBCDSMInstance::~SBCDSMInstance()
88 {
89 DBG("SBCDSMInstance::~SBCDSMInstance()\n");
90 for (std::set<DSMDisposable*>::iterator it=
91 gc_trash.begin(); it != gc_trash.end(); it++)
92 delete *it;
93
94 for (vector<AmAudio*>::iterator it=
95 audiofiles.begin();it!=audiofiles.end();it++)
96 delete *it;
97
98 AmMediaProcessor::instance()->removeSession(call);
99 }
100
101 #define RETURN_CONTINUE_OR_STOP_PROCESSING \
102 if (event_params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE) \
103 return StopProcessing; \
104 return ContinueProcessing;
105
106 /** @return whether to continue processing */
107 /** called from A/B leg when in-dialog request comes in */
onInitialInvite(SBCCallLeg * call,InitialInviteHandlerParams & params)108 CCChainProcessing SBCDSMInstance::onInitialInvite(SBCCallLeg *call, InitialInviteHandlerParams ¶ms)
109 {
110 DBG("SBCDSMInstance::onInitialInvite()\n");
111
112 VarMapT event_params;
113 event_params["remote_party"] = params.remote_party;
114 event_params["remote_uri"] = params.remote_party;
115 event_params["from"] = params.remote_party;
116
117 avar[DSM_AVAR_REQUEST] = AmArg(params.original_invite);
118 avar[DSM_SBC_AVAR_MODIFIED_INVITE] = AmArg(params.modified_invite);
119
120 engine.runEvent(call, this, DSMCondition::Invite, &event_params);
121
122 avar.erase(DSM_SBC_AVAR_MODIFIED_INVITE);
123
124 RETURN_CONTINUE_OR_STOP_PROCESSING;
125 }
126
extractRequestParameters(VarMapT & event_params,AVarMapT & avar,DSMSipRequest * request)127 void extractRequestParameters(VarMapT& event_params, AVarMapT& avar, DSMSipRequest* request) {
128 if (NULL == request)
129 return;
130
131 if (NULL != request) {
132 event_params["method"] = request->req->method;
133 event_params["r_uri"] = request->req->r_uri;
134 event_params["from"] = request->req->from;
135 event_params["to"] = request->req->to;
136 event_params["hdrs"] = request->req->hdrs;
137 event_params["from_tag"] = request->req->from_tag;
138 event_params["to_tag"] = request->req->to_tag;
139 event_params["callid"] = request->req->callid;
140
141 vector<string> hdrs = explode(request->req->hdrs, CRLF);
142 for (vector<string>::iterator it=hdrs.begin(); it!=hdrs.end();it++) {
143 size_t p = it->find(":");
144 if (p==string::npos)
145 continue;
146 size_t p1=p;
147 if (++p>=it->size())
148 continue;
149 while (p<it->size() && ((*it)[p] == ' ' || (*it)[p] == '\t'))
150 p++;
151 event_params["hdr."+it->substr(0,p1)]=it->substr(p);
152 }
153
154 // avar[DSM_AVAR_REQUEST] = AmArg(const_cast<AmSipRequest*>(request));
155 avar[DSM_AVAR_REQUEST] = AmArg(request);
156 }
157 }
158
clearRequestParameters(AVarMapT & avar)159 void clearRequestParameters(AVarMapT& avar) {
160 avar.erase(DSM_AVAR_REQUEST);
161 }
162
extractReplyParameters(VarMapT & event_params,AVarMapT & avar,DSMSipReply * reply)163 void extractReplyParameters(VarMapT& event_params, AVarMapT& avar, DSMSipReply* reply) {
164 if (NULL == reply)
165 return;
166
167 event_params["sip_reason"] = reply->reply->reason;
168 event_params["sip_code"] = int2str(reply->reply->code);
169 event_params["from"] = reply->reply->from;
170 event_params["from_tag"] = reply->reply->from_tag;
171 event_params["to"] = reply->reply->to;
172 event_params["to_tag"] = reply->reply->to_tag;
173 event_params["callid"] = reply->reply->callid;
174 event_params["hdrs"] = reply->reply->hdrs;
175 #ifdef PROPAGATE_UNPARSED_REPLY_HEADERS
176 for (list<AmSipHeader>::const_iterator it = reply->reply->unparsed_headers.begin();
177 it != reply->reply->unparsed_headers.end(); it++) {
178 event_params["hdr."+it->name] = it->value;
179 }
180 #else
181 vector<string> hdrs = explode(reply->reply->hdrs, CRLF);
182 for (vector<string>::iterator it=hdrs.begin(); it!=hdrs.end();it++) {
183 size_t p = it->find(":");
184 if (p==string::npos)
185 continue;
186 size_t p1=p;
187 if (++p>=it->size())
188 continue;
189 while (p<it->size() && ((*it)[p] == ' ' || (*it)[p] == '\t'))
190 p++;
191 event_params["hdr."+it->substr(0,p1)]=it->substr(p);
192 }
193 #endif
194 avar[DSM_AVAR_REPLY] = AmArg(reply);
195 }
196
clearReplyParameters(AVarMapT & avar)197 void clearReplyParameters(AVarMapT& avar) {
198 avar.erase(DSM_AVAR_REPLY);
199 }
200
onStateChange(SBCCallLeg * call,const CallLeg::StatusChangeCause & cause)201 void SBCDSMInstance::onStateChange(SBCCallLeg *call, const CallLeg::StatusChangeCause &cause) {
202 DBG("SBCDSMInstance::onStateChange()\n");
203 VarMapT event_params;
204
205 event_params["SBCCallStatus"] = call->getCallStatusStr();
206 unique_ptr<DSMSipRequest> dsm_request;
207 unique_ptr<DSMSipReply> dsm_reply;
208
209 switch (cause.reason) {
210 case CallLeg::StatusChangeCause::SipReply:
211 event_params["reason"] = "SipReply";
212 dsm_reply.reset(new DSMSipReply(cause.param.reply));
213 extractReplyParameters(event_params, avar, dsm_reply.get());
214 break;
215 case CallLeg::StatusChangeCause::SipRequest:
216 event_params["reason"] = "SipRequest";
217 dsm_request.reset(new DSMSipRequest(cause.param.request));
218 extractRequestParameters(event_params, avar, dsm_request.get());
219 break;
220 case CallLeg::StatusChangeCause::Other:
221 event_params["reason"] = "other";
222 if (NULL != cause.param.desc)
223 event_params["desc"] = string(cause.param.desc);
224 break;
225 case CallLeg::StatusChangeCause::Canceled: event_params["reason"] = "Canceled"; break;
226 case CallLeg::StatusChangeCause::NoAck: event_params["reason"] = "NoAck"; break;
227 case CallLeg::StatusChangeCause::NoPrack: event_params["reason"] = "NoPrack"; break;
228 case CallLeg::StatusChangeCause::RtpTimeout: event_params["reason"] = "RtpTimeout"; break;
229 case CallLeg::StatusChangeCause::SessionTimeout: event_params["reason"] = "SessionTimeout"; break;
230 case CallLeg::StatusChangeCause::InternalError: event_params["reason"] = "InternalError"; break;
231 default: break;
232 };
233
234 engine.runEvent(call, this, DSMCondition::LegStateChange, &event_params);
235
236 switch (cause.reason) {
237 case CallLeg::StatusChangeCause::SipReply: clearReplyParameters(avar); break;
238 case CallLeg::StatusChangeCause::SipRequest: clearRequestParameters(avar); break;
239 default: break;
240 }
241
242 }
243
244 /** called from A/B leg when in-dialog request comes in */
onInDialogRequest(SBCCallLeg * call,const AmSipRequest & req)245 CCChainProcessing SBCDSMInstance::onInDialogRequest(SBCCallLeg* call, const AmSipRequest& req) {
246 DBG("SBCDSMInstance::onInDialogRequest()\n");
247 VarMapT event_params;
248 DSMSipRequest dsm_request(&req);
249
250 extractRequestParameters(event_params, avar, &dsm_request);
251
252 engine.runEvent(call, this, DSMCondition::SipRequest, &event_params);
253
254 clearRequestParameters(avar);
255 RETURN_CONTINUE_OR_STOP_PROCESSING;
256 }
257
onInDialogReply(SBCCallLeg * call,const AmSipReply & reply)258 CCChainProcessing SBCDSMInstance::onInDialogReply(SBCCallLeg* call, const AmSipReply& reply) {
259 DBG("SBCDSMInstance::onInDialogReply()\n");
260 VarMapT event_params;
261 DSMSipReply dsm_reply(&reply);
262 extractReplyParameters(event_params, avar, &dsm_reply);
263
264 engine.runEvent(call, this, DSMCondition::SipReply, &event_params);
265
266 clearReplyParameters(avar);
267 RETURN_CONTINUE_OR_STOP_PROCESSING;
268 }
269
onEvent(SBCCallLeg * call,AmEvent * event)270 CCChainProcessing SBCDSMInstance::onEvent(SBCCallLeg* call, AmEvent* event) {
271 DBG("SBCDSMInstance::onEvent()\n");
272
273 if (event->event_id == DSM_EVENT_ID) {
274 DSMEvent* dsm_event = dynamic_cast<DSMEvent*>(event);
275 if (dsm_event) {
276 DBG("SBCDSMInstance processing DSM event\n");
277
278 engine.runEvent(call, this, DSMCondition::DSMEvent, &dsm_event->params);
279
280 if (dsm_event->params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
281 return StopProcessing;
282 return ContinueProcessing;
283 }
284 }
285
286 B2BEvent* b2b_ev = dynamic_cast<B2BEvent*>(event);
287 if(b2b_ev && b2b_ev->ev_type == B2BEvent::B2BApplication) {
288 engine.runEvent(call, this, DSMCondition::B2BEvent, &b2b_ev->params);
289
290 if (b2b_ev->params[DSM_SBC_PARAM_PROCESSED] == DSM_TRUE) {
291 ReliableB2BEvent* rel_b2b_ev = dynamic_cast<ReliableB2BEvent*>(b2b_ev);
292 if (NULL != rel_b2b_ev) {
293 rel_b2b_ev->markAsProcessed();
294 } else {
295 DBG("possible script writer error: marked #processed on non-reliable B2BEvent");
296 }
297 }
298
299 if (b2b_ev->params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
300 return StopProcessing;
301 return ContinueProcessing;
302 }
303
304 if(b2b_ev && b2b_ev->ev_type == B2BEvent::B2BCore) {
305 B2BSipRequestEvent* b2b_req_ev = dynamic_cast<B2BSipRequestEvent*>(b2b_ev);
306 if (b2b_req_ev) {
307 VarMapT event_params;
308 DSMMutableSipRequest sip_req(&b2b_req_ev->req);
309 extractRequestParameters(event_params, avar, &sip_req);
310 event_params["forward"] = b2b_req_ev->forward?"true":"false";
311 engine.runEvent(call, this, DSMCondition::B2BOtherRequest, &event_params);
312 avar.erase(DSM_AVAR_REQUEST);
313 if (event_params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
314 return StopProcessing;
315 } else {
316 B2BSipReplyEvent* b2b_reply_ev = dynamic_cast<B2BSipReplyEvent*>(b2b_ev);
317 if (b2b_reply_ev) {
318 VarMapT event_params;
319 DSMMutableSipReply dsm_reply(&b2b_reply_ev->reply);
320 extractReplyParameters(event_params, avar, &dsm_reply);
321 event_params["forward"] = b2b_reply_ev->forward?"true":"false";
322 event_params["trans_method"] = b2b_reply_ev->trans_method;
323 engine.runEvent(call, this, DSMCondition::B2BOtherReply, &event_params);
324 if (event_params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
325 return StopProcessing;
326 }
327 }
328
329 }
330
331 AmPluginEvent* plugin_event = dynamic_cast<AmPluginEvent*>(event);
332 if(plugin_event && plugin_event->name == "timer_timeout") {
333 int timer_id = plugin_event->data.get(0).asInt();
334 map<string, string> params;
335 params["id"] = int2str(timer_id);
336 engine.runEvent(call, this, DSMCondition::Timer, ¶ms);
337
338 if (params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
339 return StopProcessing;
340 return ContinueProcessing;
341 }
342
343 AmPlaylistSeparatorEvent* sep_ev = dynamic_cast<AmPlaylistSeparatorEvent*>(event);
344 if (sep_ev) {
345 map<string, string> params;
346 params["id"] = int2str(sep_ev->event_id);
347 engine.runEvent(call, this, DSMCondition::PlaylistSeparator, ¶ms);
348
349 if (params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
350 return StopProcessing;
351 return ContinueProcessing;
352 }
353
354 AmAudioEvent* audio_event = dynamic_cast<AmAudioEvent*>(event);
355 if(audio_event &&
356 ((audio_event->event_id == AmAudioEvent::cleared) ||
357 (audio_event->event_id == AmAudioEvent::noAudio))){
358 map<string, string> params;
359 params["type"] = audio_event->event_id == AmAudioEvent::cleared?"cleared":"noAudio";
360 engine.runEvent(call, this, DSMCondition::NoAudio, ¶ms);
361
362 if (params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
363 return StopProcessing;
364 return ContinueProcessing;
365 }
366
367 // todo: process JsonRPCEvents (? see DSMCall::process)
368
369 return ContinueProcessing;
370 }
371
onDtmf(SBCCallLeg * call,int event,int duration)372 CCChainProcessing SBCDSMInstance::onDtmf(SBCCallLeg *call, int event, int duration) {
373 DBG("* Got DTMF key %d duration %d\n",
374 event, duration);
375
376 map<string, string> params;
377 params["key"] = int2str(event);
378 params["duration"] = int2str(duration);
379
380 engine.runEvent(call, this, DSMCondition::Key, ¶ms);
381
382 if (params[DSM_SBC_PARAM_STOP_PROCESSING]==DSM_TRUE)
383 return StopProcessing;
384 return ContinueProcessing;
385 }
386
387 /** @return whether to continue processing */
onBLegRefused(SBCCallLeg * call,const AmSipReply & reply)388 CCChainProcessing SBCDSMInstance::onBLegRefused(SBCCallLeg* call, const AmSipReply& reply)
389 {
390 DBG("SBCDSMInstance::onBLegRefused()\n");
391 VarMapT event_params;
392 DSMSipReply dsm_reply(&reply);
393 extractReplyParameters(event_params, avar, &dsm_reply);
394
395 engine.runEvent(call, this, DSMCondition::BLegRefused, &event_params);
396
397 clearRequestParameters(avar);
398 RETURN_CONTINUE_OR_STOP_PROCESSING;
399 }
400
401 // --- hold related ------------------------
402
putOnHold(SBCCallLeg * call)403 CCChainProcessing SBCDSMInstance::putOnHold(SBCCallLeg* call) {
404 DBG("SBCDSMInstance::putOnHold()\n");
405 VarMapT event_params;
406 engine.runEvent(call, this, DSMCondition::PutOnHold, &event_params);
407 RETURN_CONTINUE_OR_STOP_PROCESSING;
408 }
409
resumeHeld(SBCCallLeg * call,bool send_reinvite)410 CCChainProcessing SBCDSMInstance::resumeHeld(SBCCallLeg* call, bool send_reinvite) {
411 DBG("SBCDSMInstance::resumeHeld()\n");
412 VarMapT event_params;
413 event_params["send_reinvite"] = send_reinvite?"true":"false";
414 engine.runEvent(call, this, DSMCondition::ResumeHeld, &event_params);
415 RETURN_CONTINUE_OR_STOP_PROCESSING;
416 }
417
createHoldRequest(SBCCallLeg * call,AmSdp & sdp)418 CCChainProcessing SBCDSMInstance::createHoldRequest(SBCCallLeg* call, AmSdp& sdp) {
419 DBG("SBCDSMInstance::createHoldRequest()\n");
420 VarMapT event_params;
421 // TODO: encapsulate SDP so actions can manipulate Hold request (?)
422 engine.runEvent(call, this, DSMCondition::CreateHoldRequest, &event_params);
423 RETURN_CONTINUE_OR_STOP_PROCESSING;
424 }
425
handleHoldReply(SBCCallLeg * call,bool succeeded)426 CCChainProcessing SBCDSMInstance::handleHoldReply(SBCCallLeg* call, bool succeeded) {
427 DBG("SBCDSMInstance::handleHoldReply()\n");
428 VarMapT event_params;
429 event_params["succeeded"] = succeeded?"true":"false";
430 engine.runEvent(call, this, DSMCondition::HandleHoldReply, &event_params);
431 RETURN_CONTINUE_OR_STOP_PROCESSING;
432 }
433
434 // pretty much nonsense, but necessary because DSM is passing around AmSession
435 // everywhere; so we need this for non-call relays
resetDummySession(SimpleRelayDialog * relay)436 void SBCDSMInstance::resetDummySession(SimpleRelayDialog *relay) {
437 if (NULL == dummy_session.get()) {
438 dummy_session.reset(new AmSession());
439 // copy the most important things
440 // TODO: initialize stuff from relay dialog in dummy session to be visible in DSM
441 dummy_session->dlg->setCallid(relay->getCallid());
442 dummy_session->dlg->setLocalTag(relay->getLocalTag());
443 dummy_session->dlg->setRemoteTag(relay->getRemoteTag());
444 dummy_session->dlg->setLocalUri(relay->getLocalUri());
445 dummy_session->dlg->setRemoteUri(relay->getRemoteUri());
446 }
447 }
448
getPlaylist()449 AmPlaylist* SBCDSMInstance::getPlaylist() {
450 if (NULL == playlist.get())
451 playlist.reset(new AmPlaylist(call));
452
453 return playlist.get();
454 }
455
456 // ------------ simple relay interface --------------------------------------- */
init(SBCCallProfile & profile,SimpleRelayDialog * relay)457 bool SBCDSMInstance::init(SBCCallProfile &profile, SimpleRelayDialog *relay) {
458 DBG("SBCDSMInstance::init() - simple relay\n");
459 resetDummySession(relay);
460
461 VarMapT event_params;
462 event_params["relay_event"] = "init";
463 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
464 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayInit, &event_params);
465 avar.erase(DSM_SBC_AVAR_PROFILE);
466
467 return true;
468 }
469
initUAC(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipRequest & req)470 void SBCDSMInstance::initUAC(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipRequest &req) {
471 DBG("SBCDSMInstance::initUAC() - simple relay\n");
472 resetDummySession(relay);
473
474 VarMapT event_params;
475 event_params["relay_event"] = "initUAC";
476 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
477 DSMSipRequest sip_req(&req);
478 extractRequestParameters(event_params, avar, &sip_req);
479 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayInitUAC, &event_params);
480 clearRequestParameters(avar);
481 avar.erase(DSM_SBC_AVAR_PROFILE);
482 }
483
initUAS(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipRequest & req)484 void SBCDSMInstance::initUAS(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipRequest &req) {
485 DBG("SBCDSMInstance::initUAS() - simple relay\n");
486 resetDummySession(relay);
487 VarMapT event_params;
488 event_params["relay_event"] = "initUAS";
489 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
490 DSMSipRequest sip_req(&req);
491 extractRequestParameters(event_params, avar, &sip_req);
492 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayInitUAS, &event_params);
493 clearRequestParameters(avar);
494 avar.erase(DSM_SBC_AVAR_PROFILE);
495 }
496
finalize(SBCCallProfile & profile,SimpleRelayDialog * relay)497 void SBCDSMInstance::finalize(SBCCallProfile &profile, SimpleRelayDialog *relay) {
498 DBG("SBCDSMInstance::finalize() - relay\n");
499 resetDummySession(relay);
500 VarMapT event_params;
501 event_params["relay_event"] = "finalize";
502 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
503 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayFinalize, &event_params);
504 avar.erase(DSM_SBC_AVAR_PROFILE);
505 }
506
onSipRequest(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipRequest & req)507 void SBCDSMInstance::onSipRequest(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipRequest& req) {
508 DBG("SBCDSMInstance::onSipRequest() - simple relay\n");
509 resetDummySession(relay);
510 VarMapT event_params;
511 event_params["relay_event"] = "onSipRequest";
512 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
513 DSMSipRequest sip_req(&req);
514 extractRequestParameters(event_params, avar, &sip_req);
515 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayOnSipRequest, &event_params);
516 clearRequestParameters(avar);
517 avar.erase(DSM_SBC_AVAR_PROFILE);
518 }
519
onSipReply(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipRequest & req,const AmSipReply & reply,AmBasicSipDialog::Status old_dlg_status)520 void SBCDSMInstance::onSipReply(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipRequest& req,
521 const AmSipReply& reply,
522 AmBasicSipDialog::Status old_dlg_status) {
523 DBG("SBCDSMInstance::onSipReply() - simple relay\n");
524 resetDummySession(relay);
525 VarMapT event_params;
526 event_params["relay_event"] = "onSipReply";
527 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
528 DSMSipRequest sip_req(&req);
529 extractRequestParameters(event_params, avar, &sip_req);
530 DSMSipReply dsm_reply(&reply);
531 extractReplyParameters(event_params, avar, &dsm_reply); // TODO: shadows request
532 event_params["old_dlg_status"] = AmBasicSipDialog::getStatusStr(old_dlg_status);
533 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayOnSipReply, &event_params);
534 clearReplyParameters(avar);
535 clearRequestParameters(avar);
536 avar.erase(DSM_SBC_AVAR_PROFILE);
537 }
538
onB2BRequest(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipRequest & req)539 void SBCDSMInstance::onB2BRequest(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipRequest& req) {
540 DBG("SBCDSMInstance::onB2BRequest() - relay\n");
541 resetDummySession(relay);
542 VarMapT event_params;
543 event_params["relay_event"] = "onB2BRequest";
544 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
545 DSMSipRequest sip_req(&req);
546 extractRequestParameters(event_params, avar, &sip_req);
547 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayOnB2BRequest, &event_params);
548 clearRequestParameters(avar);
549 avar.erase(DSM_SBC_AVAR_PROFILE);
550 }
551
onB2BReply(SBCCallProfile & profile,SimpleRelayDialog * relay,const AmSipReply & reply)552 void SBCDSMInstance::onB2BReply(SBCCallProfile &profile, SimpleRelayDialog *relay, const AmSipReply& reply) {
553 DBG("SBCDSMInstance::onB2BReply() - relay\n");
554 resetDummySession(relay);
555 VarMapT event_params;
556 event_params["relay_event"] = "onB2BReply";
557 avar[DSM_SBC_AVAR_PROFILE] = AmArg(&profile);
558 DSMSipReply dsm_reply(&reply);
559 extractReplyParameters(event_params, avar, &dsm_reply);
560 engine.runEvent(dummy_session.get(), this, DSMCondition::RelayOnB2BReply, &event_params);
561 clearReplyParameters(avar);
562 avar.erase(DSM_SBC_AVAR_PROFILE);
563 }
564
565 // --- garbage collector related ------------------------
566
transferOwnership(DSMDisposable * d)567 void SBCDSMInstance::transferOwnership(DSMDisposable* d) {
568 gc_trash.insert(d);
569 }
570
releaseOwnership(DSMDisposable * d)571 void SBCDSMInstance::releaseOwnership(DSMDisposable* d) {
572 gc_trash.erase(d);
573 }
574
575 // --- DSM session API -------------------------------------------
576
577 #define NOT_IMPLEMENTED_UINT(_func) \
578 unsigned int SBCDSMInstance::_func { \
579 throw DSMException("core", "cause", "not implemented in DSM SBC"); \
580 }
581
582 #define NOT_IMPLEMENTED(_func) \
583 void SBCDSMInstance::_func { \
584 throw DSMException("core", "cause", "not implemented in DSM SBC"); \
585 }
586
587 NOT_IMPLEMENTED(playPrompt(const string& name, bool loop, bool front));
588 NOT_IMPLEMENTED(setPromptSet(const string& name));
589
playFile(const string & name,bool loop,bool front)590 void SBCDSMInstance::playFile(const string& name, bool loop, bool front) {
591 AmAudioFile* af = new AmAudioFile();
592 if(af->open(name,AmAudioFile::Read)) {
593 ERROR("audio file '%s' could not be opened for reading.\n",
594 name.c_str());
595 delete af;
596
597 throw DSMException("file", "path", name);
598
599 return;
600 }
601 if (loop)
602 af->loop.set(true);
603
604 if (front)
605 getPlaylist()->addToPlayListFront(new AmPlaylistItem(af, NULL));
606 else
607 getPlaylist()->addToPlaylist(new AmPlaylistItem(af, NULL));
608
609 audiofiles.push_back(af);
610 CLR_ERRNO;
611 }
612
playSilence(unsigned int length,bool front)613 void SBCDSMInstance::playSilence(unsigned int length, bool front) {
614 AmNullAudio* af = new AmNullAudio();
615 af->setReadLength(length);
616 if (front)
617 getPlaylist()->addToPlayListFront(new AmPlaylistItem(af, NULL));
618 else
619 getPlaylist()->addToPlaylist(new AmPlaylistItem(af, NULL));
620
621 audiofiles.push_back(af);
622 CLR_ERRNO;
623 }
624
playRingtone(int length,int on,int off,int f,int f2,bool front)625 void SBCDSMInstance::playRingtone(int length, int on, int off, int f, int f2, bool front) {
626 AmRingTone* af = new AmRingTone(length, on, off, f, f2);
627 if (front)
628 getPlaylist()->addToPlayListFront(new AmPlaylistItem(af, NULL));
629 else
630 getPlaylist()->addToPlaylist(new AmPlaylistItem(af, NULL));
631
632 audiofiles.push_back(af);
633 CLR_ERRNO;
634 }
635
636 NOT_IMPLEMENTED(recordFile(const string& name));
637 NOT_IMPLEMENTED_UINT(getRecordLength());
638 NOT_IMPLEMENTED_UINT(getRecordDataSize());
639 NOT_IMPLEMENTED(stopRecord());
640
641 NOT_IMPLEMENTED(setInOutPlaylist());
642
643 // void SBCDSMInstance::setInOutPlaylist() {
644 // AmB2BMedia *media = call->getMediaSession();
645 // if (NULL == media) {
646 // ERROR("could not set InOutPlaylist - no media session!\n");
647 // return;
648 // }
649 // media->setFirstStreamInOut(call->isALeg(), getPlaylist(), getPlaylist());
650 // }
651
setInputPlaylist()652 void SBCDSMInstance::setInputPlaylist() {
653 AmB2BMedia *media = call->getMediaSession();
654 if (NULL == media) {
655 ERROR("could not setInputPlaylist - no media session!\n");
656 return;
657 }
658
659 media->setFirstStreamInput(call->isALeg(), getPlaylist());
660 }
661
662 NOT_IMPLEMENTED(setOutputPlaylist());
663 // void SBCDSMInstance::setOutputPlaylist() {
664 // AmB2BMedia *media = call->getMediaSession();
665 // if (NULL == media) {
666 // ERROR("could not setOutputPlaylist - no media session!\n");
667 // return;
668 // } media->setFirstStreamOutput(call->isALeg(), getPlaylist());
669 // }
670
addToPlaylist(AmPlaylistItem * item,bool front)671 void SBCDSMInstance::addToPlaylist(AmPlaylistItem* item, bool front) {
672 DBG("add item to playlist\n");
673 if (front)
674 getPlaylist()->addToPlayListFront(item);
675 else
676 getPlaylist()->addToPlaylist(item);
677 }
678
679
flushPlaylist()680 void SBCDSMInstance::flushPlaylist() {
681 DBG("flush playlist\n");
682 getPlaylist()->flush();
683 }
684
addSeparator(const string & name,bool front)685 void SBCDSMInstance::addSeparator(const string& name, bool front) {
686 unsigned int id = 0;
687 if (str2i(name, id)) {
688 SET_ERRNO(DSM_ERRNO_UNKNOWN_ARG);
689 SET_STRERROR("separator id '"+name+"' not a number");
690 return;
691 }
692
693 AmPlaylistSeparator* sep = new AmPlaylistSeparator(call, id);
694 if (front)
695 getPlaylist()->addToPlayListFront(new AmPlaylistItem(sep, sep));
696 else
697 getPlaylist()->addToPlaylist(new AmPlaylistItem(sep, sep));
698 // for garbage collector
699 audiofiles.push_back(sep);
700 CLR_ERRNO;
701 }
702
connectMedia()703 void SBCDSMInstance::connectMedia() {
704 AmB2BMedia *media = call->getMediaSession();
705 if (NULL == media) {
706 DBG("media session was not set, creating new one\n");
707 media = new AmB2BMedia(call->isALeg() ? call : NULL , call->isALeg() ? NULL : call);
708 call->setMediaSession(media);
709 // TODO: media stream initialization here (does changeRtpMode help?)
710 } else {
711 media->pauseRelay();
712 }
713
714 media->addToMediaProcessor();
715
716 local_media_connected = true;
717 }
718
disconnectMedia()719 void SBCDSMInstance::disconnectMedia() {
720 if (!local_media_connected) {
721 DBG("local media not connected, not disconnecting\n");
722 return;
723 }
724 DBG("disconnecting from local media processing, enabling Relay...\n");
725 local_media_connected = false;
726
727 AmB2BMedia *media = call->getMediaSession();
728 if (NULL == media) {
729 DBG("media session not set, not disconnecting\n");
730 return;
731 }
732 AmMediaProcessor::instance()->removeSession(media);
733 media->restartRelay();
734 }
735
736 NOT_IMPLEMENTED(mute());
737 NOT_IMPLEMENTED(unmute());
738
739 /** B2BUA functions */
740 NOT_IMPLEMENTED(B2BconnectCallee(const string& remote_party,
741 const string& remote_uri,
742 bool relayed_invite));
743 NOT_IMPLEMENTED(B2BterminateOtherLeg());
744 NOT_IMPLEMENTED(B2BaddReceivedRequest(const AmSipRequest& req));
745 NOT_IMPLEMENTED(B2BsetRelayEarlyMediaSDP(bool enabled));
746 NOT_IMPLEMENTED(B2BsetHeaders(const string& hdr, bool replaceCRLF));
747 NOT_IMPLEMENTED(B2BclearHeaders());
748 NOT_IMPLEMENTED(B2BaddHeader(const string& hdr));
749 NOT_IMPLEMENTED(B2BremoveHeader(const string& hdr));
750
751 #undef NOT_IMPLEMENTED
752 #undef NOT_IMPLEMENTED_UINT
753