Skip to content

Commit

Permalink
Merge 9710954 into 370ab3c
Browse files Browse the repository at this point in the history
  • Loading branch information
kingster authored Mar 29, 2021
2 parents 370ab3c + 9710954 commit 85fe92f
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 48 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ The softphone exposes the following resources on port `6060`.
<td>UnHold call with specified <code>call_id</code></td>
</tr>
<tr>
<td><code>/calls/{call_id}/conference</code></td>
<td>PUT</td>
<td></td>
<td>Create conference by merging other running calls with given <code>call_id</code></td>
</tr>
<tr>
<td><code>/calls/{call_id}/conference</code></td>
<td>DELETE</td>
<td></td>
<td>Break specified <code>call_id</code> out of conference</td>
</tr>
<tr>
<td><code>/calls/{call_id}/transfer</code></td>
<td>POST</td>
<td>
Expand Down
42 changes: 16 additions & 26 deletions tinyphone/call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace tp {
/* Start ringback for 180 for UAC unless there's SDP in 180 */
if (call_info.role==PJSIP_ROLE_UAC && code == 180 && !hasMedia()) {
account->getPhone()->StartRinging(this, RingBack);
} else {
PJ_LOG(3, (__FILENAME__, "Call [%d] Already has Media In EARLY", ci.id));
}
}
break;
Expand Down Expand Up @@ -66,34 +68,22 @@ namespace tp {
account->getPhone()->StopRinging(this);

try {
AudioMedia *aud_med = NULL;
// Iterate all the call medias
for (unsigned i = 0; i < ci.media.size(); i++) {
if (ci.media[i].type == PJMEDIA_TYPE_AUDIO && getMedia(i)) {
if (ci.media[i].status == PJSUA_CALL_MEDIA_ACTIVE || ci.media[i].status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
PJ_LOG(3, (__FILENAME__, "Found Call [%d] Media Resource [%d]", ci.id, i));
aud_med = (AudioMedia *)getMedia(i);
break;
}
else {
pj_assert(ci.media[i].status <= PJ_ARRAY_SIZE(status_name));
PJ_LOG(3, (__FILENAME__, "Call [%d] OnCallMediaState Media State %s", ci.id, status_name[ci.media[i].status]));
}
}
}

if (aud_med) {
// Connect the call audio media to sound device
AudDevManager& mgr = Endpoint::instance().audDevManager();
PJ_LOG(3, (__FILENAME__, "Connecting Call [%d] to Media Device Input #%d , Output # %d", ci.id, mgr.getCaptureDev(), mgr.getPlaybackDev()));
// This will connect the sound device/mic to the call audio media
mgr.getCaptureDevMedia().startTransmit(*aud_med);
// And this will connect the call audio media to the sound device/speaker
aud_med->startTransmit(mgr.getPlaybackDevMedia());
}
else {
AudioMedia aud_med;
try {
// Get the first audio media // for PJSIP version > 2.8
aud_med = getAudioMedia(-1);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "ERROR: Call [%d] OnCallMediaState Media Not Found", ci.id));
return;
}

// Connect the call audio media to sound device
AudDevManager& mgr = Endpoint::instance().audDevManager();
PJ_LOG(3, (__FILENAME__, "Connecting Call [%d] to Media Device Input #%d , Output # %d", ci.id, mgr.getCaptureDev(), mgr.getPlaybackDev()));
// This will connect the sound device/mic to the call audio media
mgr.getCaptureDevMedia().startTransmit(aud_med);
// And this will connect the call audio media to the sound device/speaker
aud_med.startTransmit(mgr.getPlaybackDevMedia());
}
catch (...) {
PJ_LOG(3, (__FILENAME__, "Call [%d] OnCallMediaState Media Connect Error", ci.id));
Expand Down
54 changes: 54 additions & 0 deletions tinyphone/phone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,4 +471,58 @@ namespace tp {
ringbackTone->stop();
}

bool TinyPhone::Conference(SIPCall* call) {

CallInfo ci = call->getInfo();
AudioMedia aud_med, aud_med2;
try {
aud_med = call->getAudioMedia(-1);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Conference getAudioMedia Error"));
return false;
}

BOOST_FOREACH(SIPCall* c, Calls()) {
if (c->getId() != call->getId()){
try {
c->UnHoldCall();
aud_med2 = call->getAudioMedia(-1);
aud_med.startTransmit(aud_med2);
aud_med2.startTransmit(aud_med);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Conference getAudioMedia2 Error"));
return false;
}
}
}
return true;
}

bool TinyPhone::BreakConference(SIPCall* call) {
CallInfo ci = call->getInfo();
AudioMedia aud_med, aud_med2;
try {
aud_med = call->getAudioMedia(-1);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::BreakConference getAudioMedia Error"));
return false;
}

BOOST_FOREACH(SIPCall* c, Calls()) {
if (c->getId() != call->getId()){
try {
c->HoldCall();
aud_med2 = call->getAudioMedia(-1);
aud_med.stopTransmit(aud_med2);
aud_med2.stopTransmit(aud_med);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::BreakConference getAudioMedia2 Error"));
return false;
}
}
}
return true;
}

}

3 changes: 3 additions & 0 deletions tinyphone/phone.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ namespace tp {
void Answer(SIPCall* call);
void Hangup(SIPCall* call);

bool Conference(SIPCall* call);
bool BreakConference(SIPCall* call);

void HangupAllCalls();

bool Initialize();
Expand Down
67 changes: 51 additions & 16 deletions tinyphone/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,26 +483,61 @@ void TinyPhoneHttpServer::Start() {
json response;
bool status;
switch (req.method) {
case crow::HTTPMethod::Put:
status = call->HoldCall();
response = {
{ "message", "Hold Triggered" },
{ "call_id" , call_id },
{ "status" , status }
};
break;
case crow::HTTPMethod::Delete:
status = call->UnHoldCall();
response = {
{ "message", "UnHold Triggered" },
{ "call_id" , call_id },
{ "status" , status }
};
break;
case crow::HTTPMethod::Put:
status = call->HoldCall();
response = {
{ "message", "Hold Triggered" },
{ "call_id" , call_id },
{ "status" , status }
};
break;
case crow::HTTPMethod::Delete:
status = call->UnHoldCall();
response = {
{ "message", "UnHold Triggered" },
{ "call_id" , call_id },
{ "status" , status }
};
break;
default:
break;
}
return tp::response(202, response);
}
});

CROW_ROUTE(app, "/calls/<int>/conference")
.methods("PUT"_method, "DELETE"_method)
([&phone](const crow::request& req,int call_id) {
pj_thread_auto_register();

SIPCall* call = phone.CallById(call_id);
if (call == nullptr) {
return tp::response(400, {
{ "message", "Call Not Found" },
{ "call_id", call_id}
});
}
else {
json response = {
{ "call_id" , call_id }
};
switch (req.method)
{
case crow::HTTPMethod::Put:
response["message"] = "Conference Triggered";
response["status"] = phone.Conference(call);
break;
case crow::HTTPMethod::Delete:
response["message"] = "BreakConference Triggered";
response["status"] = phone.BreakConference(call);
break;
default:
break;
}
return tp::response(200, response);
}
});

CROW_ROUTE(app, "/calls/<int>/transfer")
.methods("POST"_method)
Expand Down
14 changes: 8 additions & 6 deletions tinyphone/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ namespace tp {
std::string AddTransportSuffix(std::string &str, pjsip_transport_type_e transport)
{
switch (transport) {
case PJSIP_TRANSPORT_TCP:
return str.append(_T(";transport=tcp"));
break;
case PJSIP_TRANSPORT_TLS:
return str.append(_T(";transport=tls"));
break;
case PJSIP_TRANSPORT_TCP:
return str.append(_T(";transport=tcp"));
break;
case PJSIP_TRANSPORT_TLS:
return str.append(_T(";transport=tls"));
break;
default:
break;
}
return str;
}
Expand Down

0 comments on commit 85fe92f

Please # to comment.