LCOV - code coverage report
Current view: top level - src - call_factory.cpp (source / functions) Coverage Total Hit
Test: jami-coverage-filtered.info Lines: 55.6 % 135 75
Test Date: 2026-07-29 09:02:12 Functions: 61.1 % 18 11

            Line data    Source code
       1              : /*
       2              :  *  Copyright (C) 2004-2026 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 "call_factory.h"
      19              : #include "sip/sipcall.h"
      20              : #include "sip/sipaccountbase.h"
      21              : 
      22              : namespace jami {
      23              : 
      24              : // generate something like 7ea037947eb9fb2f
      25              : std::string
      26          175 : CallFactory::getNewCallID() const
      27              : {
      28          175 :     std::string random_id;
      29              :     do {
      30          175 :         random_id = std::to_string(std::uniform_int_distribution<uint64_t>(1, JAMI_ID_MAX_VAL)(rand_));
      31          175 :     } while (hasCall(random_id));
      32          175 :     return random_id;
      33            0 : }
      34              : 
      35              : std::shared_ptr<SIPCall>
      36          163 : CallFactory::newSipCall(const std::shared_ptr<SIPAccountBase>& account,
      37              :                         Call::CallType type,
      38              :                         const std::vector<libjami::MediaMap>& mediaList)
      39              : {
      40          163 :     if (not allowNewCall_) {
      41            0 :         JAMI_WARNING("Creation of new calls is not allowed");
      42            0 :         return {};
      43              :     }
      44              : 
      45          163 :     std::lock_guard lk(callMapsMutex_);
      46          163 :     auto id = getNewCallID();
      47          163 :     auto call = std::make_shared<SIPCall>(account, id, type, mediaList);
      48          163 :     callMaps_[call->getLinkType()].emplace(id, call);
      49          163 :     account->attach(call);
      50          163 :     return call;
      51          163 : }
      52              : 
      53              : void
      54           39 : CallFactory::forbid()
      55              : {
      56           39 :     allowNewCall_ = false;
      57           39 : }
      58              : 
      59              : void
      60          170 : CallFactory::removeCall(Call& call)
      61              : {
      62          170 :     std::lock_guard lk(callMapsMutex_);
      63              : 
      64          170 :     const auto& id = call.getCallId();
      65          170 :     JAMI_LOG("Removing call {}", id);
      66          170 :     auto& map = callMaps_.at(call.getLinkType());
      67          170 :     map.erase(id);
      68          170 :     JAMI_LOG("Remaining {} call", map.size());
      69          170 : }
      70              : 
      71              : void
      72            0 : CallFactory::removeCall(const std::string& id)
      73              : {
      74            0 :     std::lock_guard lk(callMapsMutex_);
      75              : 
      76            0 :     if (auto call = getCall(id)) {
      77            0 :         removeCall(*call);
      78              :     } else
      79            0 :         JAMI_ERROR("No call with ID {}", id);
      80            0 : }
      81              : 
      82              : bool
      83          175 : CallFactory::hasCall(const std::string& id) const
      84              : {
      85          175 :     std::lock_guard lk(callMapsMutex_);
      86              : 
      87          336 :     for (const auto& item : callMaps_) {
      88          161 :         const auto& map = item.second;
      89          161 :         if (map.find(id) != map.cend())
      90            0 :             return true;
      91              :     }
      92              : 
      93          175 :     return false;
      94          175 : }
      95              : 
      96              : bool
      97            0 : CallFactory::empty() const
      98              : {
      99            0 :     std::lock_guard lk(callMapsMutex_);
     100              : 
     101            0 :     for (const auto& item : callMaps_) {
     102            0 :         if (not item.second.empty())
     103            0 :             return false;
     104              :     }
     105              : 
     106            0 :     return true;
     107            0 : }
     108              : 
     109              : void
     110           41 : CallFactory::clear()
     111              : {
     112           41 :     std::lock_guard lk(callMapsMutex_);
     113           41 :     callMaps_.clear();
     114           41 : }
     115              : 
     116              : std::shared_ptr<Call>
     117          564 : CallFactory::getCall(const std::string& id) const
     118              : {
     119          564 :     std::lock_guard lk(callMapsMutex_);
     120              : 
     121          584 :     for (const auto& item : callMaps_) {
     122          563 :         const auto& map = item.second;
     123          563 :         const auto& iter = map.find(id);
     124          563 :         if (iter != map.cend())
     125          543 :             return iter->second;
     126              :     }
     127              : 
     128           21 :     return nullptr;
     129          564 : }
     130              : 
     131              : std::vector<std::shared_ptr<Call>>
     132          159 : CallFactory::getAllCalls() const
     133              : {
     134          159 :     std::lock_guard lk(callMapsMutex_);
     135          159 :     std::vector<std::shared_ptr<Call>> v;
     136              : 
     137          290 :     for (const auto& itemmap : callMaps_) {
     138          131 :         const auto& map = itemmap.second;
     139          131 :         v.reserve(v.size() + map.size());
     140          555 :         for (const auto& item : map)
     141          424 :             v.push_back(item.second);
     142              :     }
     143              : 
     144          318 :     return v;
     145          159 : }
     146              : 
     147              : std::vector<std::string>
     148            0 : CallFactory::getCallIDs() const
     149              : {
     150            0 :     std::vector<std::string> v;
     151              : 
     152            0 :     for (const auto& item : callMaps_) {
     153            0 :         const auto& map = item.second;
     154            0 :         for (const auto& it : map)
     155            0 :             v.push_back(it.first);
     156              :     }
     157              : 
     158            0 :     v.shrink_to_fit();
     159            0 :     return v;
     160            0 : }
     161              : 
     162              : std::size_t
     163           39 : CallFactory::callCount() const
     164              : {
     165           39 :     std::lock_guard lk(callMapsMutex_);
     166           39 :     std::size_t count = 0;
     167              : 
     168           50 :     for (const auto& itemmap : callMaps_)
     169           11 :         count += itemmap.second.size();
     170              : 
     171           39 :     return count;
     172           39 : }
     173              : 
     174              : bool
     175            0 : CallFactory::hasCall(const std::string& id, Call::LinkType link) const
     176              : {
     177            0 :     std::lock_guard lk(callMapsMutex_);
     178              : 
     179            0 :     const auto* const map = getMap_(link);
     180            0 :     return map and map->find(id) != map->cend();
     181            0 : }
     182              : 
     183              : bool
     184           33 : CallFactory::empty(Call::LinkType link) const
     185              : {
     186           33 :     std::lock_guard lk(callMapsMutex_);
     187              : 
     188           33 :     const auto* const map = getMap_(link);
     189           66 :     return !map or map->empty();
     190           33 : }
     191              : 
     192              : std::shared_ptr<Call>
     193            2 : CallFactory::getCall(const std::string& id, Call::LinkType link) const
     194              : {
     195            2 :     std::lock_guard lk(callMapsMutex_);
     196              : 
     197            2 :     const auto* const map = getMap_(link);
     198            2 :     if (!map)
     199            0 :         return nullptr;
     200              : 
     201            2 :     const auto& it = map->find(id);
     202            2 :     if (it == map->cend())
     203            0 :         return nullptr;
     204              : 
     205            2 :     return it->second;
     206            2 : }
     207              : 
     208              : std::vector<std::shared_ptr<Call>>
     209            0 : CallFactory::getAllCalls(Call::LinkType link) const
     210              : {
     211            0 :     std::lock_guard lk(callMapsMutex_);
     212            0 :     std::vector<std::shared_ptr<Call>> v;
     213              : 
     214            0 :     const auto* const map = getMap_(link);
     215            0 :     if (map) {
     216            0 :         for (const auto& it : *map)
     217            0 :             v.push_back(it.second);
     218              :     }
     219              : 
     220            0 :     v.shrink_to_fit();
     221            0 :     return v;
     222            0 : }
     223              : 
     224              : std::vector<std::string>
     225            0 : CallFactory::getCallIDs(Call::LinkType link) const
     226              : {
     227            0 :     std::lock_guard lk(callMapsMutex_);
     228            0 :     std::vector<std::string> v;
     229              : 
     230            0 :     const auto* const map = getMap_(link);
     231            0 :     if (map) {
     232            0 :         for (const auto& it : *map)
     233            0 :             v.push_back(it.first);
     234              :     }
     235              : 
     236            0 :     v.shrink_to_fit();
     237            0 :     return v;
     238            0 : }
     239              : 
     240              : std::size_t
     241            0 : CallFactory::callCount(Call::LinkType link) const
     242              : {
     243            0 :     std::lock_guard lk(callMapsMutex_);
     244              : 
     245            0 :     const auto* const map = getMap_(link);
     246            0 :     if (!map)
     247            0 :         return 0;
     248              : 
     249            0 :     return map->size();
     250            0 : }
     251              : 
     252              : } // namespace jami
        

Generated by: LCOV version 2.0-1