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 : #pragma once 18 : 19 : #include <map> 20 : #include <memory> 21 : #include <mutex> 22 : #include <vector> 23 : #include <string> 24 : #include <utility> 25 : 26 : #include "call.h" 27 : #include "account.h" 28 : 29 : namespace jami { 30 : 31 : class SIPAccountBase; 32 : class SIPCall; 33 : 34 : class CallFactory 35 : { 36 : public: 37 38 : CallFactory(std::mt19937_64& rand) 38 38 : : rand_(rand) 39 38 : {} 40 : 41 : std::string getNewCallID() const; 42 : 43 : /** 44 : * Create a new call instance. 45 : * @param account Account used to create this call 46 : * @param type Set definitely this call as incoming/outgoing 47 : * @param mediaList The list of media to include 48 : * @return A shared pointer to the created call 49 : */ 50 : std::shared_ptr<SIPCall> newSipCall(const std::shared_ptr<SIPAccountBase>& account, 51 : Call::CallType type, 52 : const std::vector<libjami::MediaMap>& mediaList); 53 : 54 : /** 55 : * Forbid creation of new calls. 56 : */ 57 : void forbid(); 58 : 59 : /** 60 : * Remove given call instance from call list. 61 : */ 62 : void removeCall(Call& call); 63 : 64 : /** 65 : * Accessor on removeCall with callID than instance. 66 : */ 67 : void removeCall(const std::string& id); 68 : 69 : /** 70 : * Return call pointer associated to given ID.Type can optionally be specified. 71 : */ 72 : std::shared_ptr<Call> getCall(const std::string& id) const; 73 : std::shared_ptr<Call> getCall(const std::string& id, Call::LinkType link) const; 74 : 75 : template<class C> 76 2 : std::shared_ptr<C> getCall(const std::string& id) 77 : { 78 2 : return std::dynamic_pointer_cast<C>(getCall(id, C::LINK_TYPE)); 79 : } 80 : 81 : /** 82 : * Return if given call exists. Type can optionally be specified. 83 : */ 84 : bool hasCall(const std::string& id) const; 85 : bool hasCall(const std::string& id, Call::LinkType link) const; 86 : 87 : /** 88 : * Return if calls exist. Type can optionally be specified. 89 : */ 90 : bool empty() const; 91 : bool empty(Call::LinkType link) const; 92 : 93 : /** 94 : * Erase all calls. 95 : */ 96 : void clear(); 97 : 98 : /** 99 : * Return all calls. Type can optionally be specified. 100 : */ 101 : std::vector<std::shared_ptr<Call>> getAllCalls() const; 102 : std::vector<std::shared_ptr<Call>> getAllCalls(Call::LinkType link) const; 103 : 104 : /** 105 : * Return all call's IDs. Type can optionally be specified. 106 : */ 107 : std::vector<std::string> getCallIDs() const; 108 : std::vector<std::string> getCallIDs(Call::LinkType link) const; 109 : 110 : /** 111 : * Return number of calls. Type can optionally be specified. 112 : */ 113 : std::size_t callCount() const; 114 : std::size_t callCount(Call::LinkType link) const; 115 : 116 : private: 117 : /** 118 : * @brief Get the calls map 119 : * @param link The call type 120 : * @return A pointer to the calls map instance 121 : * @warning Concurrency protection must done by the caller. 122 : */ 123 35 : const CallMap* getMap_(Call::LinkType link) const 124 : { 125 35 : auto const& itermap = callMaps_.find(link); 126 35 : if (itermap != callMaps_.cend()) 127 2 : return &itermap->second; 128 33 : return nullptr; 129 : } 130 : 131 : std::mt19937_64& rand_; 132 : 133 : mutable std::recursive_mutex callMapsMutex_ {}; 134 : 135 : std::atomic_bool allowNewCall_ {true}; 136 : 137 : std::map<Call::LinkType, CallMap> callMaps_ {}; 138 : }; 139 : 140 : } // namespace jami