LCOV - code coverage report
Current view: top level - src - conference_protocol.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 35 89 39.3 %
Date: 2024-12-21 08:56:24 Functions: 2 5 40.0 %

          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_WARNING("Received conference order from a non-moderator ({})", peerId_);
      88           0 :         return;
      89             :     }
      90           0 :     if (data_.isMember(ProtocolKeys::LAYOUT)) {
      91           0 :         setLayout_(data_[ProtocolKeys::LAYOUT].asInt());
      92             :     }
      93           0 :     if (data_.isMember(ProtocolKeys::ACTIVEPART)) {
      94           0 :         setActiveParticipant_(data_[ProtocolKeys::ACTIVEPART].asString());
      95             :     }
      96           0 :     if (data_.isMember(ProtocolKeys::MUTEPART) && data_.isMember(ProtocolKeys::MUTESTATE)) {
      97           0 :         muteParticipant_(data_[ProtocolKeys::MUTEPART].asString(),
      98           0 :                          data_[ProtocolKeys::MUTESTATE].asString() == TRUE_STR);
      99             :     }
     100           0 :     if (data_.isMember(ProtocolKeys::HANGUPPART)) {
     101           0 :         kickParticipant_(data_[ProtocolKeys::HANGUPPART].asString());
     102             :     }
     103             : }
     104             : 
     105             : void
     106           6 : ConfProtocolParser::parseV1()
     107             : {
     108          12 :     if (!checkAuthorization_ || !setLayout_ || !raiseHand_ || !hangupParticipant_
     109          12 :         || !muteStreamAudio_ || !setActiveStream_) {
     110           0 :         JAMI_ERR() << "Missing methods for ConfProtocolParser";
     111           0 :         return;
     112             :     }
     113             : 
     114           6 :     auto isPeerModerator = checkAuthorization_(peerId_);
     115          18 :     for (Json::Value::const_iterator itr = data_.begin(); itr != data_.end(); itr++) {
     116          12 :         auto key = itr.key();
     117          12 :         if (key == ProtocolKeys::LAYOUT) {
     118             :             // Note: can be removed soon
     119           0 :             if (isPeerModerator)
     120           0 :                 setLayout_(itr->asInt());
     121          12 :         } else if (key == ProtocolKeys::PROTOVERSION) {
     122           6 :             continue;
     123             :         } else {
     124           6 :             auto accValue = *itr;
     125           6 :             if (accValue.isMember(ProtocolKeys::DEVICES)) {
     126           6 :                 auto accountUri = key.asString();
     127           6 :                 for (Json::Value::const_iterator itrd = accValue[ProtocolKeys::DEVICES].begin();
     128          12 :                      itrd != accValue[ProtocolKeys::DEVICES].end();
     129           6 :                      itrd++) {
     130           6 :                     auto deviceId = itrd.key().asString();
     131           6 :                     auto deviceValue = *itrd;
     132           6 :                     if (deviceValue.isMember(ProtocolKeys::RAISEHAND)) {
     133           6 :                         auto newState = deviceValue[ProtocolKeys::RAISEHAND].asBool();
     134           6 :                         if (peerId_ == accountUri || (!newState && isPeerModerator))
     135           5 :                             raiseHand_(deviceId, newState);
     136             :                     }
     137           6 :                     if (isPeerModerator && deviceValue.isMember(ProtocolKeys::HANGUP)) {
     138           0 :                         hangupParticipant_(accountUri, deviceId);
     139             :                     }
     140           6 :                     if (deviceValue.isMember(ProtocolKeys::MEDIAS)) {
     141           0 :                         for (Json::Value::const_iterator itrm = accValue[ProtocolKeys::MEDIAS]
     142           0 :                                                                     .begin();
     143           0 :                              itrm != accValue[ProtocolKeys::MEDIAS].end();
     144           0 :                              itrm++) {
     145           0 :                             auto streamId = itrm.key().asString();
     146           0 :                             auto mediaVal = *itrm;
     147           0 :                             if (mediaVal.isMember(ProtocolKeys::VOICEACTIVITY)) {
     148           0 :                                 voiceActivity_(streamId,
     149           0 :                                                mediaVal[ProtocolKeys::VOICEACTIVITY].asBool());
     150             :                             }
     151           0 :                             if (isPeerModerator) {
     152           0 :                                 if (mediaVal.isMember(ProtocolKeys::MUTEVIDEO)
     153           0 :                                     && !muteStreamVideo_) {
     154             :                                     // Note: For now, it's not implemented so not set
     155           0 :                                     muteStreamVideo_(accountUri,
     156             :                                                      deviceId,
     157             :                                                      streamId,
     158           0 :                                                      mediaVal[ProtocolKeys::MUTEVIDEO].asBool());
     159             :                                 }
     160           0 :                                 if (mediaVal.isMember(ProtocolKeys::MUTEAUDIO)) {
     161           0 :                                     muteStreamAudio_(accountUri,
     162             :                                                      deviceId,
     163             :                                                      streamId,
     164           0 :                                                      mediaVal[ProtocolKeys::MUTEAUDIO].asBool());
     165             :                                 }
     166           0 :                                 if (mediaVal.isMember(ProtocolKeys::ACTIVE)) {
     167           0 :                                     setActiveStream_(streamId,
     168           0 :                                                      mediaVal[ProtocolKeys::ACTIVE].asBool());
     169             :                                 }
     170             :                             }
     171           0 :                         }
     172             :                     }
     173           6 :                 }
     174           6 :             }
     175           6 :         }
     176          12 :     }
     177             : }
     178             : 
     179             : } // namespace jami

Generated by: LCOV version 1.14