Line data Source code
1 : /* 2 : * Copyright (C) 2004-2024 Savoir-faire Linux Inc. 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 3 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, see <https://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "conference_protocol.h" 19 : 20 : #include "string_utils.h" 21 : 22 : namespace jami { 23 : 24 : namespace ProtocolKeys { 25 : 26 : constexpr static const char* PROTOVERSION = "version"; 27 : constexpr static const char* LAYOUT = "layout"; 28 : // V0 29 : constexpr static const char* HANDRAISED = "handRaised"; 30 : constexpr static const char* HANDSTATE = "handState"; 31 : constexpr static const char* ACTIVEPART = "activeParticipant"; 32 : constexpr static const char* MUTEPART = "muteParticipant"; 33 : constexpr static const char* MUTESTATE = "muteState"; 34 : constexpr static const char* HANGUPPART = "hangupParticipant"; 35 : // V1 36 : constexpr static const char* DEVICES = "devices"; 37 : constexpr static const char* MEDIAS = "medias"; 38 : constexpr static const char* RAISEHAND = "raiseHand"; 39 : constexpr static const char* HANGUP = "hangup"; 40 : constexpr static const char* ACTIVE = "active"; 41 : constexpr static const char* MUTEAUDIO = "muteAudio"; 42 : // Future 43 : constexpr static const char* MUTEVIDEO = "muteVideo"; 44 : constexpr static const char* VOICEACTIVITY = "voiceActivity"; 45 : 46 : } // namespace ProtocolKeys 47 : 48 : void 49 6 : ConfProtocolParser::parse() 50 : { 51 6 : if (data_.isMember(ProtocolKeys::PROTOVERSION)) { 52 6 : uint32_t version = data_[ProtocolKeys::PROTOVERSION].asUInt(); 53 6 : if (version_) 54 6 : version_(version); 55 6 : if (version == 1) { 56 6 : parseV1(); 57 : } else { 58 0 : JAMI_WARN() << "Unsupported protocol version " << version; 59 : } 60 : } else { 61 0 : parseV0(); 62 : } 63 6 : } 64 : 65 : void 66 0 : ConfProtocolParser::parseV0() 67 : { 68 0 : if (!checkAuthorization_ || !raiseHandUri_ || !setLayout_ || !setActiveParticipant_ 69 0 : || !muteParticipant_ || !kickParticipant_) { 70 0 : JAMI_ERR() << "Missing methods for ConfProtocolParser"; 71 0 : return; 72 : } 73 : // Check if all lambdas set 74 0 : auto isPeerModerator = checkAuthorization_(peerId_); 75 0 : if (data_.isMember(ProtocolKeys::HANDRAISED)) { 76 0 : auto state = data_[ProtocolKeys::HANDSTATE].asString() == TRUE_STR; 77 0 : auto uri = data_[ProtocolKeys::HANDRAISED].asString(); 78 0 : if (peerId_ == uri) { 79 : // In this case, the user want to change their state 80 0 : raiseHandUri_(uri, state); 81 0 : } else if (!state && isPeerModerator) { 82 : // In this case a moderator can lower the hand 83 0 : raiseHandUri_(uri, state); 84 : } 85 0 : } 86 0 : if (!isPeerModerator) { 87 0 : JAMI_WARN("Received conference order from a non master (%.*s)", 88 : (int) peerId_.size(), 89 : peerId_.data()); 90 0 : return; 91 : } 92 0 : if (data_.isMember(ProtocolKeys::LAYOUT)) { 93 0 : setLayout_(data_[ProtocolKeys::LAYOUT].asInt()); 94 : } 95 0 : if (data_.isMember(ProtocolKeys::ACTIVEPART)) { 96 0 : setActiveParticipant_(data_[ProtocolKeys::ACTIVEPART].asString()); 97 : } 98 0 : if (data_.isMember(ProtocolKeys::MUTEPART) && data_.isMember(ProtocolKeys::MUTESTATE)) { 99 0 : muteParticipant_(data_[ProtocolKeys::MUTEPART].asString(), 100 0 : data_[ProtocolKeys::MUTESTATE].asString() == TRUE_STR); 101 : } 102 0 : if (data_.isMember(ProtocolKeys::HANGUPPART)) { 103 0 : kickParticipant_(data_[ProtocolKeys::HANGUPPART].asString()); 104 : } 105 : } 106 : 107 : void 108 6 : ConfProtocolParser::parseV1() 109 : { 110 12 : if (!checkAuthorization_ || !setLayout_ || !raiseHand_ || !hangupParticipant_ 111 12 : || !muteStreamAudio_ || !setActiveStream_) { 112 0 : JAMI_ERR() << "Missing methods for ConfProtocolParser"; 113 0 : return; 114 : } 115 : 116 6 : auto isPeerModerator = checkAuthorization_(peerId_); 117 18 : for (Json::Value::const_iterator itr = data_.begin(); itr != data_.end(); itr++) { 118 12 : auto key = itr.key(); 119 12 : if (key == ProtocolKeys::LAYOUT) { 120 : // Note: can be removed soon 121 0 : if (isPeerModerator) 122 0 : setLayout_(itr->asInt()); 123 12 : } else if (key == ProtocolKeys::PROTOVERSION) { 124 6 : continue; 125 : } else { 126 6 : auto accValue = *itr; 127 6 : if (accValue.isMember(ProtocolKeys::DEVICES)) { 128 6 : auto accountUri = key.asString(); 129 6 : for (Json::Value::const_iterator itrd = accValue[ProtocolKeys::DEVICES].begin(); 130 12 : itrd != accValue[ProtocolKeys::DEVICES].end(); 131 6 : itrd++) { 132 6 : auto deviceId = itrd.key().asString(); 133 6 : auto deviceValue = *itrd; 134 6 : if (deviceValue.isMember(ProtocolKeys::RAISEHAND)) { 135 6 : auto newState = deviceValue[ProtocolKeys::RAISEHAND].asBool(); 136 6 : if (peerId_ == accountUri || (!newState && isPeerModerator)) 137 5 : raiseHand_(deviceId, newState); 138 : } 139 6 : if (isPeerModerator && deviceValue.isMember(ProtocolKeys::HANGUP)) { 140 0 : hangupParticipant_(accountUri, deviceId); 141 : } 142 6 : if (deviceValue.isMember(ProtocolKeys::MEDIAS)) { 143 0 : for (Json::Value::const_iterator itrm = accValue[ProtocolKeys::MEDIAS] 144 0 : .begin(); 145 0 : itrm != accValue[ProtocolKeys::MEDIAS].end(); 146 0 : itrm++) { 147 0 : auto streamId = itrm.key().asString(); 148 0 : auto mediaVal = *itrm; 149 0 : if (mediaVal.isMember(ProtocolKeys::VOICEACTIVITY)) { 150 0 : voiceActivity_(streamId, 151 0 : mediaVal[ProtocolKeys::VOICEACTIVITY].asBool()); 152 : } 153 0 : if (isPeerModerator) { 154 0 : if (mediaVal.isMember(ProtocolKeys::MUTEVIDEO) 155 0 : && !muteStreamVideo_) { 156 : // Note: For now, it's not implemented so not set 157 0 : muteStreamVideo_(accountUri, 158 : deviceId, 159 : streamId, 160 0 : mediaVal[ProtocolKeys::MUTEVIDEO].asBool()); 161 : } 162 0 : if (mediaVal.isMember(ProtocolKeys::MUTEAUDIO)) { 163 0 : muteStreamAudio_(accountUri, 164 : deviceId, 165 : streamId, 166 0 : mediaVal[ProtocolKeys::MUTEAUDIO].asBool()); 167 : } 168 0 : if (mediaVal.isMember(ProtocolKeys::ACTIVE)) { 169 0 : setActiveStream_(streamId, 170 0 : mediaVal[ProtocolKeys::ACTIVE].asBool()); 171 : } 172 : } 173 0 : } 174 : } 175 6 : } 176 6 : } 177 6 : } 178 12 : } 179 : } 180 : 181 : } // namespace jami