LCOV - code coverage report
Current view: top level - src - conference_protocol.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 32 86 37.2 %
Date: 2024-04-28 07:59:35 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2022-2024 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
       5             :  *
       6             :  *  This program is free software; you can redistribute it and/or modify
       7             :  *  it under the terms of the GNU General Public License as published by
       8             :  *  the Free Software Foundation; either version 3 of the License, or
       9             :  *  (at your option) any later version.
      10             :  *
      11             :  *  This program is distributed in the hope that it will be useful,
      12             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  *  GNU General Public License for more details.
      15             :  *
      16             :  *  You should have received a copy of the GNU General Public License
      17             :  *  along with this program; if not, write to the Free Software
      18             :  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
      19             :  */
      20             : 
      21             : #include "conference_protocol.h"
      22             : 
      23             : #include "string_utils.h"
      24             : 
      25             : namespace jami {
      26             : 
      27             : namespace ProtocolKeys {
      28             : 
      29             : constexpr static const char* PROTOVERSION = "version";
      30             : constexpr static const char* LAYOUT = "layout";
      31             : // V0
      32             : constexpr static const char* HANDRAISED = "handRaised";
      33             : constexpr static const char* HANDSTATE = "handState";
      34             : constexpr static const char* ACTIVEPART = "activeParticipant";
      35             : constexpr static const char* MUTEPART = "muteParticipant";
      36             : constexpr static const char* MUTESTATE = "muteState";
      37             : constexpr static const char* HANGUPPART = "hangupParticipant";
      38             : // V1
      39             : constexpr static const char* DEVICES = "devices";
      40             : constexpr static const char* MEDIAS = "medias";
      41             : constexpr static const char* RAISEHAND = "raiseHand";
      42             : constexpr static const char* HANGUP = "hangup";
      43             : constexpr static const char* ACTIVE = "active";
      44             : constexpr static const char* MUTEAUDIO = "muteAudio";
      45             : // Future
      46             : constexpr static const char* MUTEVIDEO = "muteVideo";
      47             : constexpr static const char* VOICEACTIVITY = "voiceActivity";
      48             : 
      49             : } // namespace ProtocolKeys
      50             : 
      51             : void
      52           6 : ConfProtocolParser::parse()
      53             : {
      54           6 :     if (data_.isMember(ProtocolKeys::PROTOVERSION)) {
      55           6 :         uint32_t version = data_[ProtocolKeys::PROTOVERSION].asUInt();
      56           6 :         if (version_)
      57           6 :             version_(version);
      58           6 :         if (version == 1) {
      59           6 :             parseV1();
      60             :         } else {
      61           0 :             JAMI_WARN() << "Unsupported protocol version " << version;
      62             :         }
      63             :     } else {
      64           0 :         parseV0();
      65             :     }
      66           0 : }
      67             : 
      68             : void
      69           0 : ConfProtocolParser::parseV0()
      70             : {
      71           0 :     if (!checkAuthorization_ || !raiseHandUri_ || !setLayout_ || !setActiveParticipant_
      72           0 :         || !muteParticipant_ || !kickParticipant_) {
      73           0 :         JAMI_ERR() << "Missing methods for ConfProtocolParser";
      74           0 :         return;
      75             :     }
      76             :     // Check if all lambdas set
      77           0 :     auto isPeerModerator = checkAuthorization_(peerId_);
      78           0 :     if (data_.isMember(ProtocolKeys::HANDRAISED)) {
      79           0 :         auto state = data_[ProtocolKeys::HANDSTATE].asString() == TRUE_STR;
      80           0 :         auto uri = data_[ProtocolKeys::HANDRAISED].asString();
      81           0 :         if (peerId_ == uri) {
      82             :             // In this case, the user want to change their state
      83           0 :             raiseHandUri_(uri, state);
      84           0 :         } else if (!state && isPeerModerator) {
      85             :             // In this case a moderator can lower the hand
      86           0 :             raiseHandUri_(uri, state);
      87             :         }
      88           0 :     }
      89           0 :     if (!isPeerModerator) {
      90           0 :         JAMI_WARN("Received conference order from a non master (%.*s)",
      91             :                   (int) peerId_.size(),
      92             :                   peerId_.data());
      93           0 :         return;
      94             :     }
      95           0 :     if (data_.isMember(ProtocolKeys::LAYOUT)) {
      96           0 :         setLayout_(data_[ProtocolKeys::LAYOUT].asInt());
      97             :     }
      98           0 :     if (data_.isMember(ProtocolKeys::ACTIVEPART)) {
      99           0 :         setActiveParticipant_(data_[ProtocolKeys::ACTIVEPART].asString());
     100             :     }
     101           0 :     if (data_.isMember(ProtocolKeys::MUTEPART) && data_.isMember(ProtocolKeys::MUTESTATE)) {
     102           0 :         muteParticipant_(data_[ProtocolKeys::MUTEPART].asString(),
     103           0 :                          data_[ProtocolKeys::MUTESTATE].asString() == TRUE_STR);
     104             :     }
     105           0 :     if (data_.isMember(ProtocolKeys::HANGUPPART)) {
     106           0 :         kickParticipant_(data_[ProtocolKeys::HANGUPPART].asString());
     107             :     }
     108             : }
     109             : 
     110             : void
     111           6 : ConfProtocolParser::parseV1()
     112             : {
     113          12 :     if (!checkAuthorization_ || !setLayout_ || !raiseHand_ || !hangupParticipant_
     114          12 :         || !muteStreamAudio_ || !setActiveStream_) {
     115           0 :         JAMI_ERR() << "Missing methods for ConfProtocolParser";
     116           0 :         return;
     117             :     }
     118             : 
     119           6 :     auto isPeerModerator = checkAuthorization_(peerId_);
     120          12 :     for (Json::Value::const_iterator itr = data_.begin(); itr != data_.end(); itr++) {
     121          12 :         auto key = itr.key();
     122          12 :         if (isPeerModerator && key == ProtocolKeys::LAYOUT) {
     123             :             // Note: can be removed soon
     124           0 :             setLayout_(itr->asInt());
     125             :         } else {
     126          12 :             auto accValue = *itr;
     127          12 :             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          12 :         }
     178          12 :     }
     179             : }
     180             : 
     181             : } // namespace jami

Generated by: LCOV version 1.14