blob: e13db54806fee04ddc979c5b331da782ebf7b371 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander65c7f672016-02-12 00:05:01 -08002 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander65c7f672016-02-12 00:05:01 -08004 * 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.org28e20752013-07-10 00:45:36 +00009 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/channel_manager.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
Steve Anton36b29d12017-10-30 09:57:42 -070013#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
Steve Anton64b626b2019-01-28 17:25:26 -080015#include "absl/algorithm/container.h"
Karl Wiberg918f50c2018-07-05 11:40:33 +020016#include "absl/memory/memory.h"
Niels Möller3c7d5992018-10-19 15:29:54 +020017#include "absl/strings/match.h"
Harald Alvestrandc24a2182022-02-23 13:44:59 +000018#include "api/media_types.h"
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "media/base/media_constants.h"
Harald Alvestrand65685a62022-04-27 12:15:49 +000021#include "pc/channel.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/location.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/trace_event.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025
26namespace cricket {
27
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020028// static
29std::unique_ptr<ChannelManager> ChannelManager::Create(
30 std::unique_ptr<MediaEngineInterface> media_engine,
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020031 bool enable_rtx,
32 rtc::Thread* worker_thread,
33 rtc::Thread* network_thread) {
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020034 RTC_DCHECK(network_thread);
35 RTC_DCHECK(worker_thread);
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020036
Harald Alvestrand7af57c62021-04-16 11:12:14 +000037 return absl::WrapUnique(new ChannelManager(
38 std::move(media_engine), enable_rtx, worker_thread, network_thread));
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020039}
40
Steve Antonc9e15602017-11-06 15:40:09 -080041ChannelManager::ChannelManager(
42 std::unique_ptr<MediaEngineInterface> media_engine,
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020043 bool enable_rtx,
Steve Antonc9e15602017-11-06 15:40:09 -080044 rtc::Thread* worker_thread,
45 rtc::Thread* network_thread)
46 : media_engine_(std::move(media_engine)),
Tomas Gunnarsson5411b172022-01-24 08:45:26 +010047 signaling_thread_(rtc::Thread::Current()),
Steve Antonc9e15602017-11-06 15:40:09 -080048 worker_thread_(worker_thread),
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +020049 network_thread_(network_thread),
50 enable_rtx_(enable_rtx) {
Tomas Gunnarsson5411b172022-01-24 08:45:26 +010051 RTC_DCHECK_RUN_ON(signaling_thread_);
Steve Antonc9e15602017-11-06 15:40:09 -080052 RTC_DCHECK(worker_thread_);
53 RTC_DCHECK(network_thread_);
Tomas Gunnarsson5411b172022-01-24 08:45:26 +010054
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.org28e20752013-07-10 00:45:36 +000060}
61
62ChannelManager::~ChannelManager() {
Tomas Gunnarsson5411b172022-01-24 08:45:26 +010063 RTC_DCHECK_RUN_ON(signaling_thread_);
Tomas Gunnarsson16de2162022-01-26 10:21:57 +010064 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
65 RTC_DCHECK_RUN_ON(worker_thread_);
Tommie8d854e2022-01-28 08:53:00 +010066 // 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 Gunnarsson16de2162022-01-26 10:21:57 +010070 const_cast<std::unique_ptr<MediaEngineInterface>&>(media_engine_).reset();
71 });
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072}
73
ossudedfd282016-06-14 07:12:39 -070074void ChannelManager::GetSupportedAudioSendCodecs(
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 std::vector<AudioCodec>* codecs) const {
zhihuang38ede132017-06-15 12:52:32 -070076 if (!media_engine_) {
77 return;
78 }
Sebastian Jansson6eb8a162018-11-16 11:29:55 +010079 *codecs = media_engine_->voice().send_codecs();
ossudedfd282016-06-14 07:12:39 -070080}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
ossudedfd282016-06-14 07:12:39 -070082void ChannelManager::GetSupportedAudioReceiveCodecs(
83 std::vector<AudioCodec>* codecs) const {
zhihuang38ede132017-06-15 12:52:32 -070084 if (!media_engine_) {
85 return;
86 }
Sebastian Jansson6eb8a162018-11-16 11:29:55 +010087 *codecs = media_engine_->voice().recv_codecs();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088}
89
Johannes Kron3e983682020-03-29 22:17:00 +020090void ChannelManager::GetSupportedVideoSendCodecs(
magjed3cf8ece2016-11-10 03:36:53 -080091 std::vector<VideoCodec>* codecs) const {
zhihuang38ede132017-06-15 12:52:32 -070092 if (!media_engine_) {
93 return;
94 }
magjed3cf8ece2016-11-10 03:36:53 -080095 codecs->clear();
96
Johannes Kron3e983682020-03-29 22:17:00 +020097 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
107void 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();
brandtrffc61182016-11-28 06:02:22 -0800115 for (const auto& video_codec : video_codecs) {
116 if (!enable_rtx_ &&
Niels Möller3c7d5992018-10-19 15:29:54 +0200117 absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) {
magjed3cf8ece2016-11-10 03:36:53 -0800118 continue;
119 }
brandtrffc61182016-11-28 06:02:22 -0800120 codecs->push_back(video_codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000121 }
122}
123
Markus Handell0357b3e2020-03-16 13:40:51 +0100124RtpHeaderExtensions ChannelManager::GetDefaultEnabledAudioRtpHeaderExtensions()
125 const {
126 if (!media_engine_)
127 return {};
128 return GetDefaultEnabledRtpHeaderExtensions(media_engine_->voice());
129}
130
131std::vector<webrtc::RtpHeaderExtensionCapability>
132ChannelManager::GetSupportedAudioRtpHeaderExtensions() const {
133 if (!media_engine_)
134 return {};
135 return media_engine_->voice().GetRtpHeaderExtensions();
136}
137
138RtpHeaderExtensions ChannelManager::GetDefaultEnabledVideoRtpHeaderExtensions()
139 const {
140 if (!media_engine_)
141 return {};
142 return GetDefaultEnabledRtpHeaderExtensions(media_engine_->video());
143}
144
145std::vector<webrtc::RtpHeaderExtensionCapability>
146ChannelManager::GetSupportedVideoRtpHeaderExtensions() const {
147 if (!media_engine_)
148 return {};
149 return media_engine_->video().GetRtpHeaderExtensions();
150}
151
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000152std::unique_ptr<VoiceChannel> ChannelManager::CreateVoiceChannel(
nisseeaabdf62017-05-05 02:23:02 -0700153 webrtc::Call* call,
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +0200154 const MediaConfig& media_config,
Harald Alvestrand8f429922022-05-04 10:32:30 +0000155 absl::string_view mid,
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800156 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700157 const webrtc::CryptoOptions& crypto_options,
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800158 const AudioOptions& options) {
Tomas Gunnarsson00f4fd92021-04-08 14:39:47 +0200159 RTC_DCHECK(call);
160 RTC_DCHECK(media_engine_);
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +0200161 // 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 Huang2dfc42d2017-12-04 13:38:48 -0800164 if (!worker_thread_->IsCurrent()) {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000165 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 Huang2dfc42d2017-12-04 13:38:48 -0800170 }
171
172 RTC_DCHECK_RUN_ON(worker_thread_);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800173
Sebastian Jansson6eb8a162018-11-16 11:29:55 +0100174 VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel(
175 call, media_config, options, crypto_options);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800176 if (!media_channel) {
177 return nullptr;
178 }
179
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200180 auto voice_channel = std::make_unique<VoiceChannel>(
Tomas Gunnarsson5411b172022-01-24 08:45:26 +0100181 worker_thread_, network_thread_, signaling_thread_,
182 absl::WrapUnique(media_channel), mid, srtp_required, crypto_options,
183 &ssrc_generator_);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800184
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000185 return voice_channel;
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800186}
187
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000188std::unique_ptr<VideoChannel> ChannelManager::CreateVideoChannel(
nisseeaabdf62017-05-05 02:23:02 -0700189 webrtc::Call* call,
Tomas Gunnarsson0b5ec182021-04-01 16:49:42 +0200190 const MediaConfig& media_config,
Harald Alvestrand8f429922022-05-04 10:32:30 +0000191 absl::string_view mid,
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800192 bool srtp_required,
Benjamin Wrighta54daf12018-10-11 15:33:17 -0700193 const webrtc::CryptoOptions& crypto_options,
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +0200194 const VideoOptions& options,
195 webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) {
Tomas Gunnarsson00f4fd92021-04-08 14:39:47 +0200196 RTC_DCHECK(call);
197 RTC_DCHECK(media_engine_);
Tomas Gunnarsson1e40a0c2020-09-28 10:39:31 +0200198 // 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 Huang2dfc42d2017-12-04 13:38:48 -0800201 if (!worker_thread_->IsCurrent()) {
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000202 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 Huang2dfc42d2017-12-04 13:38:48 -0800208 }
209
210 RTC_DCHECK_RUN_ON(worker_thread_);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800211
Sebastian Jansson6eb8a162018-11-16 11:29:55 +0100212 VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel(
Jonas Orelanda3aa9bd2019-04-17 07:38:40 +0200213 call, media_config, options, crypto_options,
214 video_bitrate_allocator_factory);
Zhi Huang2dfc42d2017-12-04 13:38:48 -0800215 if (!media_channel) {
216 return nullptr;
217 }
218
Mirko Bonadei317a1f02019-09-17 17:06:18 +0200219 auto video_channel = std::make_unique<VideoChannel>(
Tomas Gunnarsson5411b172022-01-24 08:45:26 +0100220 worker_thread_, network_thread_, signaling_thread_,
221 absl::WrapUnique(media_channel), mid, srtp_required, crypto_options,
222 &ssrc_generator_);
Anton Sukhanov98a462c2018-10-17 13:15:42 -0700223
Harald Alvestrand3af79d12022-04-29 15:04:58 +0000224 return video_channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225}
226
Niels Möllere8e4dc42019-06-11 14:04:16 +0200227bool ChannelManager::StartAecDump(webrtc::FileWrapper file,
ivocd66b44d2016-01-15 03:06:36 -0800228 int64_t max_size_bytes) {
Tomas Gunnarsson95d2f472021-04-01 19:14:54 +0200229 RTC_DCHECK_RUN_ON(worker_thread_);
230 return media_engine_->voice().StartAecDump(std::move(file), max_size_bytes);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000231}
232
ivoc797ef122015-10-22 03:25:41 -0700233void ChannelManager::StopAecDump() {
Tomas Gunnarsson95d2f472021-04-01 19:14:54 +0200234 RTC_DCHECK_RUN_ON(worker_thread_);
235 media_engine_->voice().StopAecDump();
ivoc797ef122015-10-22 03:25:41 -0700236}
237
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238} // namespace cricket