Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TinyPhone::Join (can be used to join two specific calls together) #81

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ The softphone exposes the following resources on port `6060`.
<td>Break specified <code>call_id</code> out of conference</td>
</tr>
<tr>
<td><code>/calls/{call_id}/join/{call_to_join_id}</code></td>
<td>POST</td>
<td></td>
<td>Merges the current call (<code>call_id</code>) with another running call (<code>call_to_join_id</code>)</td>
</tr>
<tr>
<td><code>/calls/{call_id}/unjoin/{call_to_unjoin_id}</code></td>
<td>POST</td>
<td></td>
<td>Break specified <code>call_id</code> out of conference with <code>call_to_unjoin_id</code></td>
</tr>
<tr>
<td><code>/calls/{call_id}/transfer</code></td>
<td>POST</td>
<td>
Expand Down
56 changes: 55 additions & 1 deletion tinyphone/phone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,59 @@ namespace tp {
return true;
}

}
bool TinyPhone::Join(SIPCall* call, SIPCall* call_to_join) {
try {
call_to_join->UnHoldCall();
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Join UnHoldCall Error"));
return false;
}

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

try {
aud_med.startTransmit(aud_med2);
aud_med2.startTransmit(aud_med);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Join startTransmit Error"));
return false;
}

return true;
}

bool TinyPhone::Unjoin(SIPCall* call, SIPCall* call_to_unjoin) {
try {
call_to_unjoin->HoldCall();
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Unjoin HoldCall Error"));
return false;
}

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

try {
aud_med.stopTransmit(aud_med2);
aud_med2.stopTransmit(aud_med);
} catch(...) {
PJ_LOG(3, (__FILENAME__, "TinyPhone::Unjoin stopTransmit 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 @@ -122,6 +122,9 @@ namespace tp {

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

bool Join(SIPCall* call, SIPCall* call_to_join);
bool Unjoin(SIPCall* call, SIPCall* call_to_unjoin);

void HangupAllCalls();

Expand Down
90 changes: 89 additions & 1 deletion tinyphone/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,95 @@ void TinyPhoneHttpServer::Start() {
return tp::response(200, response);
}
});


CROW_ROUTE(app, "/calls/<int>/join/<int>")
.methods("POST"_method)
([&phone](int call_id, int call_to_join_id) {
pj_thread_auto_register();

SIPCall* call = phone.CallById(call_id);
SIPCall* call_to_join = phone.CallById(call_to_join_id);

if (call == nullptr) {
return tp::response(400, {
{ "message", "Current Call Not Found" },
{ "call_id" , call_id },
{ "call_to_join_id" , call_to_join_id },
});
}
else if (call_to_join == nullptr) {
return tp::response(400, {
{ "message", "Call To Join Not Found" },
{ "call_id" , call_id },
{ "call_to_join_id" , call_to_join_id }
});
}
else if (call->HoldState() == +HoldStatus::LOCAL_HOLD) {
json response = {
{ "message", "Bad Request, CallOnHold Currently" },
{ "call_id" , call_id },
{ "call_to_join_id" , call_to_join_id },
{ "status", "400" }
};

return tp::response(400, response);
}
else {
json response = {
{ "message", "Calls Join Triggered" },
{ "call_id" , call_id },
{ "call_to_join_id" , call_to_join_id },
{ "response", phone.Join(call, call_to_join) }
};

return tp::response(200, response);
}
});

CROW_ROUTE(app, "/calls/<int>/unjoin/<int>")
.methods("POST"_method)
([&phone](int call_id, int call_to_unjoin_id) {
pj_thread_auto_register();

SIPCall* call = phone.CallById(call_id);
SIPCall* call_to_unjoin = phone.CallById(call_to_unjoin_id);

if (call == nullptr) {
return tp::response(400, {
{ "message", "Current Call Not Found" },
{ "call_id" , call_id },
{ "call_to_unjoin_id" , call_to_unjoin_id },
});
}
else if (call_to_unjoin == nullptr) {
return tp::response(400, {
{ "message", "Call To Unjoin Not Found" },
{ "call_id" , call_id },
{ "call_to_unjoin_id" , call_to_unjoin_id }
});
}
else if (call->HoldState() == +HoldStatus::LOCAL_HOLD) {
json response = {
{ "message", "Bad Request, CallOnHold Currently" },
{ "call_id" , call_id },
{ "call_to_unjoin_id" , call_to_unjoin_id },
{ "status", "400" }
};

return tp::response(400, response);
}
else {
json response = {
{ "message", "Calls Unjoin Triggered" },
{ "call_id" , call_id },
{ "call_to_unjoin_id" , call_to_unjoin_id },
{ "response", phone.Unjoin(call, call_to_unjoin) }
};

return tp::response(200, response);
}
});

CROW_ROUTE(app, "/calls/<int>/transfer")
.methods("POST"_method)
([&phone](const crow::request& req, int call_id) {
Expand Down