| #include "MediaManager.h" |
| 6 | #include "VideoCaptureInterfaceImpl.h" |
| 7 | #include "VideoCapturerInterface.h" |
| 8 |
| 9 | namespace tgcalls { |
| 10 |
| 11 | namespace { |
| 12 |
| 13 | rtc::Thread *makeManagerThread() { |
| 14 | static std::unique_ptr<rtc::Thread> value = rtc::Thread::Create(); |
| 15 | value->SetName("WebRTC-Manager", nullptr); |
| 16 | value->Start(); |
| 17 | return value.get(); |
| 18 | } |
| 19 |
| 20 |
| 21 | rtc::Thread *getManagerThread() { |
| 22 | static rtc::Thread *value = makeManagerThread(); |
| 23 | return value; |
| 24 | } |
| 25 |
| 26 | } // namespace |
| 27 |
| 28 | InstanceImpl::InstanceImpl(Descriptor &&descriptor) |
| 29 | : _logSink(std::make_unique<LogSinkImpl>(descriptor.config.logPath)) { |
| 30 | rtc::LogMessage::LogToDebug(rtc::LS_INFO); |
| 31 | rtc::LogMessage::SetLogToStderr(false); |
| 32 | rtc::LogMessage::AddLogToStream(_logSink.get(), rtc::LS_INFO); |
| 33 |
| 34 | auto networkType = descriptor.initialNetworkType; |
| 35 |
| 36 | _manager.reset(new ThreadLocalObject<Manager>(getManagerThread(), [descriptor = std::move(descriptor)]() mutable { |
| 37 | return std::make_shared<Manager>(getManagerThread(), std::move(descriptor)); |
| 38 | })); |
| 39 | _manager->perform([](Manager *manager) { |
| 40 | manager->start(); |
| 41 | }); |
| 42 |
| 43 | setNetworkType(networkType); |
| 44 | } |
| 45 |
| 46 | InstanceImpl::~InstanceImpl() { |
| 47 | rtc::LogMessage::RemoveLogToStream(_logSink.get()); |
| 48 | } |
| 49 |
| 50 | void InstanceImpl::receiveSignalingData(const std::vector<uint8_t> &data) { |
| 51 | _manager->perform([data](Manager *manager) { |
| 52 | manager->receiveSignalingData(data); |
| 53 | }); |
| 54 | }; |
| 55 |
| 56 | void InstanceImpl::setVideoCapture(std::shared_ptr<VideoCaptureInterface> videoCapture) { |
| 57 | _manager->perform([videoCapture](Manager *manager) { |
| 58 | manager->setVideoCapture(videoCapture); |
| 59 | }); |
| 60 | } |
| 61 |
| 62 | void InstanceImpl::sendVideoDeviceUpdated() { |
| 63 | _manager->perform([](Manager *manager) { |
| 64 | manager->sendVideoDeviceUpdated(); |
| 65 | }); |
| 66 | } |
| 67 |
| 68 | void InstanceImpl::setRequestedVideoAspect(float aspect) { |
| 69 | _manager->perform([aspect](Manager *manager) { |
| 70 | manager->setRequestedVideoAspect(aspect); |
| 71 | }); |
| 72 | } |
| 73 |
| 74 | void InstanceImpl::setNetworkType(NetworkType networkType) { |
| 75 | bool isLowCostNetwork = false; |
| 76 | switch (networkType) { |
| 77 | case NetworkType::WiFi: |
| 78 | case NetworkType::Ethernet: |
| 79 | isLowCostNetwork = true; |
| 80 | break; |
| 81 | default: |
| 82 | break; |
| 83 | } |
| 84 |
| 85 | _manager->perform([isLowCostNetwork](Manager *manager) { |
| 86 | manager->setIsLocalNetworkLowCost(isLowCostNetwork); |
| 87 | }); |
| 88 | } |
| 89 |
| 90 | void InstanceImpl::setMuteMicrophone(bool muteMicrophone) { |
| 91 | _manager->perform([muteMicrophone](Manager *manager) { |
| 92 | manager->setMuteOutgoingAudio(muteMicrophone); |
| 93 | }); |
| 94 | } |
| 95 |
| 96 | void InstanceImpl::setIncomingVideoOutput(std::weak_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>> sink) { |
| 97 | _manager->perform([sink](Manager *manager) { |
| 98 | manager->setIncomingVideoOutput(sink); |
| 99 | }); |
| 100 | } |
| 101 |
| 102 | void InstanceImpl::setAudioOutputGainControlEnabled(bool enabled) { |
| 103 | } |
| 104 |
| 105 | void InstanceImpl::setEchoCancellationStrength(int strength) { |
| 106 | } |
| 107 |
| 108 | void InstanceImpl::setAudioInputDevice(std::string id) { |
| 109 | _manager->perform([id](Manager *manager) { |
| 110 | manager->setAudioInputDevice(id); |
| 111 | }); |
| 112 | } |
| 113 |
| 114 | void InstanceImpl::setAudioOutputDevice(std::string id) { |
| 115 | _manager->perform([id](Manager *manager) { |
| 116 | manager->setAudioOutputDevice(id); |
| 117 | }); |
| 118 | } |
| 119 |
| 120 | void InstanceImpl::setInputVolume(float level) { |
| 121 | _manager->perform([level](Manager *manager) { |
| 122 | manager->setInputVolume(level); |
| 123 | }); |
| 124 | } |
| 125 |
| 126 | void InstanceImpl::setOutputVolume(float level) { |
| 127 | _manager->perform([level](Manager *manager) { |
| 128 | manager->setOutputVolume(level); |
| 129 | }); |
| 130 | } |
| 131 |
| 132 | void InstanceImpl::setAudioOutputDuckingEnabled(bool enabled) { |
| 133 | // TODO: not implemented |
| 134 | } |
| 135 |
| 136 | void InstanceImpl::addExternalAudioSamples(std::vector<uint8_t> &&samples) { |
| 137 | _manager->perform([samples = std::move(samples)](Manager *manager) mutable { |
| 138 | manager->addExternalAudioSamples(std::move(samples)); |
| 139 | }); |
| 140 | } |
| 141 |
| 142 | void InstanceImpl::setIsLowBatteryLevel(bool isLowBatteryLevel) { |
| 143 | _manager->perform([isLowBatteryLevel](Manager *manager) { |
| 144 | manager->setIsLowBatteryLevel(isLowBatteryLevel); |
| 145 | }); |
| 146 | } |
| 147 |
| 148 | std::string InstanceImpl::getLastError() { |
| 149 | return ""; // TODO: not implemented |
| 150 | } |
| 151 |
| 152 | std::string InstanceImpl::getDebugInfo() { |
| 153 | return ""; // TODO: not implemented |
| 154 | } |
| 155 |
| 156 | int64_t InstanceImpl::getPreferredRelayId() { |
| 157 | return 0; // we don't have endpoint ids |
| 158 | } |
| 159 |
| 160 | TrafficStats InstanceImpl::getTrafficStats() { |
| 161 | return TrafficStats{}; // TODO: not implemented |
| 162 | } |
| 163 |
| 164 | PersistentState InstanceImpl::getPersistentState() { |
| 165 | return PersistentState{}; // we dont't have such information |
| 166 | } |
| 167 |
| 168 | void InstanceImpl::stop(std::function<void(FinalState)> completion) { |
| 169 | RTC_LOG(LS_INFO) << "Stopping InstanceImpl"; |
| 170 |
| 171 | std::string debugLog = _logSink->result(); |
| 172 |
| 173 | _manager->perform([completion, debugLog = std::move(debugLog)](Manager *manager) { |
| 174 | manager->getNetworkStats([completion, debugLog = std::move(debugLog)](TrafficStats stats, CallStats callStats) { |
| 175 | FinalState finalState; |
| 176 | finalState.debugLog = debugLog; |
| 177 | finalState.isRatingSuggested = false; |
| 178 | finalState.trafficStats = stats; |
| 179 | finalState.callStats = callStats; |
| 180 |
| 181 | completion(finalState); |
| 182 | }); |
| 183 | }); |
| 184 | } |
| 185 |
| 186 | int InstanceImpl::GetConnectionMaxLayer() { |
| 187 | return 92; |
| 188 | } |
| 189 |
| 190 | std::vector<std::string> InstanceImpl::GetVersions() { |
| 191 | std::vector<std::string> result; |
| 192 | result.push_back("2.7.7"); |
| 193 | result.push_back("5.0.0"); |
| 194 | return result; |
| 195 | } |
| 196 |
| 197 | template <> |
| 198 | bool Register<InstanceImpl>() { |
| 199 | return Meta::RegisterOne<InstanceImpl>(); |
| 200 | } |
| 201 |
| 202 | } // namespace tgcalls |
| 203 |