Skip to content
Snippets Groups Projects
voip.cpp 3.38 KiB
Newer Older
  • Learn to ignore specific revisions
  • trilene's avatar
    trilene committed
    #include <cstdlib>
    #include <string>
    
    
    trilene's avatar
    trilene committed
    #include "mtx/events/voip.hpp"
    
    #include <nlohmann/json.hpp>
    
    using json = nlohmann::json;
    
    
    trilene's avatar
    trilene committed
    namespace {
    uint16_t
    version(const json &obj)
    {
            auto v = obj.at("version");
            try {
                    return v.get<uint16_t>();
            } catch (const json::type_error &) {
                    return std::atoi(v.get<std::string>().c_str());
            }
    }
    
    }
    
    
    trilene's avatar
    trilene committed
    namespace mtx::events::msg {
    
    // m.call.invite
    void
    from_json(const json &obj, CallInvite &content)
    {
    
    trilene's avatar
    trilene committed
            content.call_id  = obj.at("call_id").get<std::string>();
            content.sdp      = obj.at("offer").at("sdp").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.version  = version(obj);
    
    trilene's avatar
    trilene committed
            content.lifetime = obj.at("lifetime").get<uint32_t>();
    
    trilene's avatar
    trilene committed
    }
    
    void
    to_json(json &obj, const CallInvite &content)
    {
    
    trilene's avatar
    trilene committed
            obj["call_id"]  = content.call_id;
            obj["offer"]    = {{"sdp", content.sdp}, {"type", "offer"}};
            obj["version"]  = content.version;
    
    trilene's avatar
    trilene committed
            obj["lifetime"] = content.lifetime;
    }
    
    // m.call.candidates
    void
    from_json(const json &obj, CallCandidates::Candidate &content)
    {
    
    trilene's avatar
    trilene committed
            content.sdpMid        = obj.at("sdpMid").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.sdpMLineIndex = obj.at("sdpMLineIndex").get<uint16_t>();
    
    trilene's avatar
    trilene committed
            content.candidate     = obj.at("candidate").get<std::string>();
    
    trilene's avatar
    trilene committed
    }
    
    void
    to_json(json &obj, const CallCandidates::Candidate &content)
    {
    
    trilene's avatar
    trilene committed
            obj["sdpMid"]        = content.sdpMid;
    
    trilene's avatar
    trilene committed
            obj["sdpMLineIndex"] = content.sdpMLineIndex;
    
    trilene's avatar
    trilene committed
            obj["candidate"]     = content.candidate;
    
    trilene's avatar
    trilene committed
    }
    
    void
    from_json(const json &obj, CallCandidates &content)
    {
    
    trilene's avatar
    trilene committed
            content.call_id    = obj.at("call_id").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.candidates = obj.at("candidates").get<std::vector<CallCandidates::Candidate>>();
    
    trilene's avatar
    trilene committed
            content.version    = version(obj);
    
    trilene's avatar
    trilene committed
    }
    
    void
    to_json(json &obj, const CallCandidates &content)
    {
    
    trilene's avatar
    trilene committed
            obj["call_id"]    = content.call_id;
    
    trilene's avatar
    trilene committed
            obj["candidates"] = content.candidates;
    
    trilene's avatar
    trilene committed
            obj["version"]    = content.version;
    
    trilene's avatar
    trilene committed
    }
    
    // m.call.answer
    void
    from_json(const json &obj, CallAnswer &content)
    {
            content.call_id = obj.at("call_id").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.sdp     = obj.at("answer").at("sdp").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.version = version(obj);
    
    trilene's avatar
    trilene committed
    }
    
    void
    to_json(json &obj, const CallAnswer &content)
    {
            obj["call_id"] = content.call_id;
    
    trilene's avatar
    trilene committed
            obj["answer"]  = {{"sdp", content.sdp}, {"type", "answer"}};
    
    trilene's avatar
    trilene committed
            obj["version"] = content.version;
    }
    
    // m.call.hangup
    void
    from_json(const json &obj, CallHangUp &content)
    {
            content.call_id = obj.at("call_id").get<std::string>();
    
    trilene's avatar
    trilene committed
            content.version = version(obj);
    
    trilene's avatar
    trilene committed
            if (obj.count("reason") == 0) {
                    content.reason = CallHangUp::Reason::User;
    
    trilene's avatar
    trilene committed
            } else {
    
    trilene's avatar
    trilene committed
                    if (obj.at("reason").get<std::string>() == "ice_failed")
    
    trilene's avatar
    trilene committed
                            content.reason = CallHangUp::Reason::ICEFailed;
    
    trilene's avatar
    trilene committed
                    else if (obj.at("reason").get<std::string>() == "invite_timeout")
    
    trilene's avatar
    trilene committed
                            content.reason = CallHangUp::Reason::InviteTimeOut;
    
    trilene's avatar
    trilene committed
            }
    }
    
    void
    to_json(json &obj, const CallHangUp &content)
    {
            obj["call_id"] = content.call_id;
            obj["version"] = content.version;
            if (content.reason == CallHangUp::Reason::ICEFailed)
    
    trilene's avatar
    trilene committed
                    obj["reason"] = "ice_failed";
    
    trilene's avatar
    trilene committed
            else if (content.reason == CallHangUp::Reason::InviteTimeOut)
    
    trilene's avatar
    trilene committed
                    obj["reason"] = "invite_timeout";
    
    trilene's avatar
    trilene committed
    }
    
    } // namespace mtx::events::msg