henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 2 | * Copyright 2004 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | 65c7f67 | 2016-02-12 00:05:01 -0800 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/channel_manager.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame] | 13 | #include <utility> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | |
Steve Anton | 64b626b | 2019-01-28 17:25:26 -0800 | [diff] [blame] | 15 | #include "absl/algorithm/container.h" |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 16 | #include "absl/memory/memory.h" |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 +0000 | [diff] [blame] | 18 | #include "api/media_types.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 19 | #include "api/sequence_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "media/base/media_constants.h" |
Harald Alvestrand | 65685a6 | 2022-04-27 12:15:49 +0000 | [diff] [blame] | 21 | #include "pc/channel.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/location.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "rtc_base/trace_event.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | |
| 26 | namespace cricket { |
| 27 | |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 28 | // static |
| 29 | std::unique_ptr<ChannelManager> ChannelManager::Create( |
| 30 | std::unique_ptr<MediaEngineInterface> media_engine, |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 31 | bool enable_rtx, |
| 32 | rtc::Thread* worker_thread, |
| 33 | rtc::Thread* network_thread) { |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 34 | RTC_DCHECK(network_thread); |
| 35 | RTC_DCHECK(worker_thread); |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 36 | |
Harald Alvestrand | 7af57c6 | 2021-04-16 11:12:14 +0000 | [diff] [blame] | 37 | return absl::WrapUnique(new ChannelManager( |
| 38 | std::move(media_engine), enable_rtx, worker_thread, network_thread)); |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 39 | } |
| 40 | |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 41 | ChannelManager::ChannelManager( |
| 42 | std::unique_ptr<MediaEngineInterface> media_engine, |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 43 | bool enable_rtx, |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 44 | rtc::Thread* worker_thread, |
| 45 | rtc::Thread* network_thread) |
| 46 | : media_engine_(std::move(media_engine)), |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 47 | signaling_thread_(rtc::Thread::Current()), |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 48 | worker_thread_(worker_thread), |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 49 | network_thread_(network_thread), |
| 50 | enable_rtx_(enable_rtx) { |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 51 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Steve Anton | c9e1560 | 2017-11-06 15:40:09 -0800 | [diff] [blame] | 52 | RTC_DCHECK(worker_thread_); |
| 53 | RTC_DCHECK(network_thread_); |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 54 | |
| 55 | if (media_engine_) { |
| 56 | // TODO(tommi): Change VoiceEngine to do ctor time initialization so that |
| 57 | // this isn't necessary. |
| 58 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { media_engine_->Init(); }); |
| 59 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ChannelManager::~ChannelManager() { |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 63 | RTC_DCHECK_RUN_ON(signaling_thread_); |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 64 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 65 | RTC_DCHECK_RUN_ON(worker_thread_); |
Tommi | e8d854e | 2022-01-28 08:53:00 +0100 | [diff] [blame] | 66 | // While `media_engine_` is const throughout the ChannelManager's lifetime, |
| 67 | // it requires destruction to happen on the worker thread. Instead of |
| 68 | // marking the pointer as non-const, we live with this const_cast<> in the |
| 69 | // destructor. |
Tomas Gunnarsson | 16de216 | 2022-01-26 10:21:57 +0100 | [diff] [blame] | 70 | const_cast<std::unique_ptr<MediaEngineInterface>&>(media_engine_).reset(); |
| 71 | }); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 72 | } |
| 73 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 74 | void ChannelManager::GetSupportedAudioSendCodecs( |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 75 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 76 | if (!media_engine_) { |
| 77 | return; |
| 78 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 79 | *codecs = media_engine_->voice().send_codecs(); |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 80 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | |
ossu | dedfd28 | 2016-06-14 07:12:39 -0700 | [diff] [blame] | 82 | void ChannelManager::GetSupportedAudioReceiveCodecs( |
| 83 | std::vector<AudioCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 84 | if (!media_engine_) { |
| 85 | return; |
| 86 | } |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 87 | *codecs = media_engine_->voice().recv_codecs(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 90 | void ChannelManager::GetSupportedVideoSendCodecs( |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 91 | std::vector<VideoCodec>* codecs) const { |
zhihuang | 38ede13 | 2017-06-15 12:52:32 -0700 | [diff] [blame] | 92 | if (!media_engine_) { |
| 93 | return; |
| 94 | } |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 95 | codecs->clear(); |
| 96 | |
Johannes Kron | 3e98368 | 2020-03-29 22:17:00 +0200 | [diff] [blame] | 97 | std::vector<VideoCodec> video_codecs = media_engine_->video().send_codecs(); |
| 98 | for (const auto& video_codec : video_codecs) { |
| 99 | if (!enable_rtx_ && |
| 100 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
| 101 | continue; |
| 102 | } |
| 103 | codecs->push_back(video_codec); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void ChannelManager::GetSupportedVideoReceiveCodecs( |
| 108 | std::vector<VideoCodec>* codecs) const { |
| 109 | if (!media_engine_) { |
| 110 | return; |
| 111 | } |
| 112 | codecs->clear(); |
| 113 | |
| 114 | std::vector<VideoCodec> video_codecs = media_engine_->video().recv_codecs(); |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 115 | for (const auto& video_codec : video_codecs) { |
| 116 | if (!enable_rtx_ && |
Niels Möller | 3c7d599 | 2018-10-19 15:29:54 +0200 | [diff] [blame] | 117 | absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) { |
magjed | 3cf8ece | 2016-11-10 03:36:53 -0800 | [diff] [blame] | 118 | continue; |
| 119 | } |
brandtr | ffc6118 | 2016-11-28 06:02:22 -0800 | [diff] [blame] | 120 | codecs->push_back(video_codec); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Markus Handell | 0357b3e | 2020-03-16 13:40:51 +0100 | [diff] [blame] | 124 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledAudioRtpHeaderExtensions() |
| 125 | const { |
| 126 | if (!media_engine_) |
| 127 | return {}; |
| 128 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->voice()); |
| 129 | } |
| 130 | |
| 131 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 132 | ChannelManager::GetSupportedAudioRtpHeaderExtensions() const { |
| 133 | if (!media_engine_) |
| 134 | return {}; |
| 135 | return media_engine_->voice().GetRtpHeaderExtensions(); |
| 136 | } |
| 137 | |
| 138 | RtpHeaderExtensions ChannelManager::GetDefaultEnabledVideoRtpHeaderExtensions() |
| 139 | const { |
| 140 | if (!media_engine_) |
| 141 | return {}; |
| 142 | return GetDefaultEnabledRtpHeaderExtensions(media_engine_->video()); |
| 143 | } |
| 144 | |
| 145 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 146 | ChannelManager::GetSupportedVideoRtpHeaderExtensions() const { |
| 147 | if (!media_engine_) |
| 148 | return {}; |
| 149 | return media_engine_->video().GetRtpHeaderExtensions(); |
| 150 | } |
| 151 | |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 152 | std::unique_ptr<VoiceChannel> ChannelManager::CreateVoiceChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 153 | webrtc::Call* call, |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 154 | const MediaConfig& media_config, |
Harald Alvestrand | 8f42992 | 2022-05-04 10:32:30 +0000 | [diff] [blame] | 155 | absl::string_view mid, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 156 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 157 | const webrtc::CryptoOptions& crypto_options, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 158 | const AudioOptions& options) { |
Tomas Gunnarsson | 00f4fd9 | 2021-04-08 14:39:47 +0200 | [diff] [blame] | 159 | RTC_DCHECK(call); |
| 160 | RTC_DCHECK(media_engine_); |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 10:39:31 +0200 | [diff] [blame] | 161 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 162 | // PeerConnection and add the expectation that we're already on the right |
| 163 | // thread. |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 164 | if (!worker_thread_->IsCurrent()) { |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 165 | return worker_thread_->Invoke<std::unique_ptr<VoiceChannel>>( |
| 166 | RTC_FROM_HERE, [&] { |
| 167 | return CreateVoiceChannel(call, media_config, mid, srtp_required, |
| 168 | crypto_options, options); |
| 169 | }); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | RTC_DCHECK_RUN_ON(worker_thread_); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 173 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 174 | VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel( |
| 175 | call, media_config, options, crypto_options); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 176 | if (!media_channel) { |
| 177 | return nullptr; |
| 178 | } |
| 179 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 180 | auto voice_channel = std::make_unique<VoiceChannel>( |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 181 | worker_thread_, network_thread_, signaling_thread_, |
| 182 | absl::WrapUnique(media_channel), mid, srtp_required, crypto_options, |
| 183 | &ssrc_generator_); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 184 | |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 185 | return voice_channel; |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 188 | std::unique_ptr<VideoChannel> ChannelManager::CreateVideoChannel( |
nisse | eaabdf6 | 2017-05-05 02:23:02 -0700 | [diff] [blame] | 189 | webrtc::Call* call, |
Tomas Gunnarsson | 0b5ec18 | 2021-04-01 16:49:42 +0200 | [diff] [blame] | 190 | const MediaConfig& media_config, |
Harald Alvestrand | 8f42992 | 2022-05-04 10:32:30 +0000 | [diff] [blame] | 191 | absl::string_view mid, |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 192 | bool srtp_required, |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 193 | const webrtc::CryptoOptions& crypto_options, |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 194 | const VideoOptions& options, |
| 195 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
Tomas Gunnarsson | 00f4fd9 | 2021-04-08 14:39:47 +0200 | [diff] [blame] | 196 | RTC_DCHECK(call); |
| 197 | RTC_DCHECK(media_engine_); |
Tomas Gunnarsson | 1e40a0c | 2020-09-28 10:39:31 +0200 | [diff] [blame] | 198 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 199 | // PeerConnection and add the expectation that we're already on the right |
| 200 | // thread. |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 201 | if (!worker_thread_->IsCurrent()) { |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 202 | return worker_thread_->Invoke<std::unique_ptr<VideoChannel>>( |
| 203 | RTC_FROM_HERE, [&] { |
| 204 | return CreateVideoChannel(call, media_config, mid, srtp_required, |
| 205 | crypto_options, options, |
| 206 | video_bitrate_allocator_factory); |
| 207 | }); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | RTC_DCHECK_RUN_ON(worker_thread_); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 211 | |
Sebastian Jansson | 6eb8a16 | 2018-11-16 11:29:55 +0100 | [diff] [blame] | 212 | VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel( |
Jonas Oreland | a3aa9bd | 2019-04-17 07:38:40 +0200 | [diff] [blame] | 213 | call, media_config, options, crypto_options, |
| 214 | video_bitrate_allocator_factory); |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 215 | if (!media_channel) { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 219 | auto video_channel = std::make_unique<VideoChannel>( |
Tomas Gunnarsson | 5411b17 | 2022-01-24 08:45:26 +0100 | [diff] [blame] | 220 | worker_thread_, network_thread_, signaling_thread_, |
| 221 | absl::WrapUnique(media_channel), mid, srtp_required, crypto_options, |
| 222 | &ssrc_generator_); |
Anton Sukhanov | 98a462c | 2018-10-17 13:15:42 -0700 | [diff] [blame] | 223 | |
Harald Alvestrand | 3af79d1 | 2022-04-29 15:04:58 +0000 | [diff] [blame] | 224 | return video_channel; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Niels Möller | e8e4dc4 | 2019-06-11 14:04:16 +0200 | [diff] [blame] | 227 | bool ChannelManager::StartAecDump(webrtc::FileWrapper file, |
ivoc | d66b44d | 2016-01-15 03:06:36 -0800 | [diff] [blame] | 228 | int64_t max_size_bytes) { |
Tomas Gunnarsson | 95d2f47 | 2021-04-01 19:14:54 +0200 | [diff] [blame] | 229 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 230 | return media_engine_->voice().StartAecDump(std::move(file), max_size_bytes); |
wu@webrtc.org | a989080 | 2013-12-13 00:21:03 +0000 | [diff] [blame] | 231 | } |
| 232 | |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 233 | void ChannelManager::StopAecDump() { |
Tomas Gunnarsson | 95d2f47 | 2021-04-01 19:14:54 +0200 | [diff] [blame] | 234 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 235 | media_engine_->voice().StopAecDump(); |
ivoc | 797ef12 | 2015-10-22 03:25:41 -0700 | [diff] [blame] | 236 | } |
| 237 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 238 | } // namespace cricket |