| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "pc/channel_manager.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <utility> |
| 15 |
| 16 | #include "absl/algorithm/container.h" |
| 17 | #include "absl/memory/memory.h" |
| 18 | #include "absl/strings/match.h" |
| 19 | #include "api/sequence_checker.h" |
| 20 | #include "media/base/media_constants.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/location.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/trace_event.h" |
| 25 |
| 26 | namespace cricket { |
| 27 |
| 28 | // static |
| 29 | std::unique_ptr<ChannelManager> ChannelManager::Create( |
| 30 | std::unique_ptr<MediaEngineInterface> media_engine, |
| 31 | bool enable_rtx, |
| 32 | rtc::Thread* worker_thread, |
| 33 | rtc::Thread* network_thread) { |
| 34 | RTC_DCHECK_RUN_ON(worker_thread); |
| 35 | RTC_DCHECK(network_thread); |
| 36 | RTC_DCHECK(worker_thread); |
| 37 |
| 38 | if (media_engine) |
| 39 | media_engine->Init(); |
| 40 |
| 41 | return absl::WrapUnique(new ChannelManager( |
| 42 | std::move(media_engine), enable_rtx, worker_thread, network_thread)); |
| 43 | } |
| 44 |
| 45 | ChannelManager::ChannelManager( |
| 46 | std::unique_ptr<MediaEngineInterface> media_engine, |
| 47 | bool enable_rtx, |
| 48 | rtc::Thread* worker_thread, |
| 49 | rtc::Thread* network_thread) |
| 50 | : media_engine_(std::move(media_engine)), |
| 51 | worker_thread_(worker_thread), |
| 52 | network_thread_(network_thread), |
| 53 | enable_rtx_(enable_rtx) { |
| 54 | RTC_DCHECK(worker_thread_); |
| 55 | RTC_DCHECK(network_thread_); |
| 56 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 57 | } |
| 58 |
| 59 | ChannelManager::~ChannelManager() { |
| 60 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 61 | } |
| 62 |
| 63 | void ChannelManager::GetSupportedAudioSendCodecs( |
| 64 | std::vector<AudioCodec>* codecs) const { |
| 65 | if (!media_engine_) { |
| 66 | return; |
| 67 | } |
| 68 | *codecs = media_engine_->voice().send_codecs(); |
| 69 | } |
| 70 |
| 71 | void ChannelManager::GetSupportedAudioReceiveCodecs( |
| 72 | std::vector<AudioCodec>* codecs) const { |
| 73 | if (!media_engine_) { |
| 74 | return; |
| 75 | } |
| 76 | *codecs = media_engine_->voice().recv_codecs(); |
| 77 | } |
| 78 |
| 79 | void ChannelManager::GetSupportedVideoSendCodecs( |
| 80 | std::vector<VideoCodec>* codecs) const { |
| 81 | if (!media_engine_) { |
| 82 | return; |
| 83 | } |
| 84 | codecs->clear(); |
| 85 |
| 86 | std::vector<VideoCodec> video_codecs = media_engine_->video().send_codecs(); |
| 87 | for (const auto& video_codec : video_codecs) { |
| 88 | if (!enable_rtx_ && |
| 89 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
| 90 | continue; |
| 91 | } |
| 92 | codecs->push_back(video_codec); |
| 93 | } |
| 94 | } |
| 95 |
| 96 | void ChannelManager::GetSupportedVideoReceiveCodecs( |
| 97 | std::vector<VideoCodec>* codecs) const { |
| 98 | if (!media_engine_) { |
| 99 | return; |
| 100 | } |
| 101 | codecs->clear(); |
| 102 |
| 103 | std::vector<VideoCodec> video_codecs = media_engine_->video().recv_codecs(); |
| 104 | for (const auto& video_codec : video_codecs) { |
| 105 | if (!enable_rtx_ && |
| 106 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
| 107 | continue; |
| 108 | } |
| 109 | codecs->push_back(video_codec); |
| 110 | } |
| 111 | } |
| 112 |
| 113 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledAudioRtpHeaderExtensions() |
| 114 | const { |
| 115 | if (!media_engine_) |
| 116 | return {}; |
| 117 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->voice()); |
| 118 | } |
| 119 |
| 120 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 121 | ChannelManager::GetSupportedAudioRtpHeaderExtensions() const { |
| 122 | if (!media_engine_) |
| 123 | return {}; |
| 124 | return media_engine_->voice().GetRtpHeaderExtensions(); |
| 125 | } |
| 126 |
| 127 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledVideoRtpHeaderExtensions() |
| 128 | const { |
| 129 | if (!media_engine_) |
| 130 | return {}; |
| 131 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->video()); |
| 132 | } |
| 133 |
| 134 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 135 | ChannelManager::GetSupportedVideoRtpHeaderExtensions() const { |
| 136 | if (!media_engine_) |
| 137 | return {}; |
| 138 | return media_engine_->video().GetRtpHeaderExtensions(); |
| 139 | } |
| 140 |
| 141 | VoiceChannel* ChannelManager::CreateVoiceChannel( |
| 142 | webrtc::Call* call, |
| 143 | const MediaConfig& media_config, |
| 144 | webrtc::RtpTransportInternal* rtp_transport, |
| 145 | rtc::Thread* signaling_thread, |
| 146 | const std::string& content_name, |
| 147 | bool srtp_required, |
| 148 | const webrtc::CryptoOptions& crypto_options, |
| 149 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
| 150 | const AudioOptions& options) { |
| 151 | RTC_DCHECK(call); |
| 152 | RTC_DCHECK(media_engine_); |
| 153 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 154 | // PeerConnection and add the expectation that we're already on the right |
| 155 | // thread. |
| 156 | if (!worker_thread_->IsCurrent()) { |
| 157 | return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] { |
| 158 | return CreateVoiceChannel(call, media_config, rtp_transport, |
| 159 | signaling_thread, content_name, srtp_required, |
| 160 | crypto_options, ssrc_generator, options); |
| 161 | }); |
| 162 | } |
| 163 |
| 164 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 165 |
| 166 | VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel( |
| 167 | call, media_config, options, crypto_options); |
| 168 | if (!media_channel) { |
| 169 | return nullptr; |
| 170 | } |
| 171 |
| 172 | auto voice_channel = std::make_unique<VoiceChannel>( |
| 173 | worker_thread_, network_thread_, signaling_thread, |
| 174 | absl::WrapUnique(media_channel), content_name, srtp_required, |
| 175 | crypto_options, ssrc_generator); |
| 176 |
| 177 | voice_channel->Init_w(rtp_transport); |
| 178 |
| 179 | VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 180 | voice_channels_.push_back(std::move(voice_channel)); |
| 181 | return voice_channel_ptr; |
| 182 | } |
| 183 |
| 184 | void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) { |
| 185 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
| 186 | RTC_DCHECK(voice_channel); |
| 187 |
| 188 | if (!worker_thread_->IsCurrent()) { |
| 189 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 190 | [&] { DestroyVoiceChannel(voice_channel); }); |
| 191 | return; |
| 192 | } |
| 193 |
| 194 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 195 |
| 196 | voice_channels_.erase(absl::c_find_if( |
| 197 | voice_channels_, [&](const std::unique_ptr<VoiceChannel>& p) { |
| 198 | return p.get() == voice_channel; |
| 199 | })); |
| 200 | } |
| 201 |
| 202 | VideoChannel* ChannelManager::CreateVideoChannel( |
| 203 | webrtc::Call* call, |
| 204 | const MediaConfig& media_config, |
| 205 | webrtc::RtpTransportInternal* rtp_transport, |
| 206 | rtc::Thread* signaling_thread, |
| 207 | const std::string& content_name, |
| 208 | bool srtp_required, |
| 209 | const webrtc::CryptoOptions& crypto_options, |
| 210 | rtc::UniqueRandomIdGenerator* ssrc_generator, |
| 211 | const VideoOptions& options, |
| 212 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
| 213 | RTC_DCHECK(call); |
| 214 | RTC_DCHECK(media_engine_); |
| 215 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 216 | // PeerConnection and add the expectation that we're already on the right |
| 217 | // thread. |
| 218 | if (!worker_thread_->IsCurrent()) { |
| 219 | return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] { |
| 220 | return CreateVideoChannel(call, media_config, rtp_transport, |
| 221 | signaling_thread, content_name, srtp_required, |
| 222 | crypto_options, ssrc_generator, options, |
| 223 | video_bitrate_allocator_factory); |
| 224 | }); |
| 225 | } |
| 226 |
| 227 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 228 |
| 229 | VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel( |
| 230 | call, media_config, options, crypto_options, |
| 231 | video_bitrate_allocator_factory); |
| 232 | if (!media_channel) { |
| 233 | return nullptr; |
| 234 | } |
| 235 |
| 236 | auto video_channel = std::make_unique<VideoChannel>( |
| 237 | worker_thread_, network_thread_, signaling_thread, |
| 238 | absl::WrapUnique(media_channel), content_name, srtp_required, |
| 239 | crypto_options, ssrc_generator); |
| 240 |
| 241 | video_channel->Init_w(rtp_transport); |
| 242 |
| 243 | VideoChannel* video_channel_ptr = video_channel.get(); |
| 244 | video_channels_.push_back(std::move(video_channel)); |
| 245 | return video_channel_ptr; |
| 246 | } |
| 247 |
| 248 | void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) { |
| 249 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
| 250 | RTC_DCHECK(video_channel); |
| 251 |
| 252 | if (!worker_thread_->IsCurrent()) { |
| 253 | worker_thread_->Invoke<void>(RTC_FROM_HERE, |
| 254 | [&] { DestroyVideoChannel(video_channel); }); |
| 255 | return; |
| 256 | } |
| 257 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 258 |
| 259 | video_channels_.erase(absl::c_find_if( |
| 260 | video_channels_, [&](const std::unique_ptr<VideoChannel>& p) { |
| 261 | return p.get() == video_channel; |
| 262 | })); |
| 263 | } |
| 264 |
| 265 | bool ChannelManager::StartAecDump(webrtc::FileWrapper file, |
| 266 | int64_t max_size_bytes) { |
| 267 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 268 | return media_engine_->voice().StartAecDump(std::move(file), max_size_bytes); |
| 269 | } |
| 270 |
| 271 | void ChannelManager::StopAecDump() { |
| 272 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 273 | media_engine_->voice().StopAecDump(); |
| 274 | } |
| 275 |
| 276 | } // namespace cricket |
| 277 |