You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

373 lines
12 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC project authors. All Rights Reserved.
  3. *
  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.
  9. */
  10. #include "pc/channel_manager.h"
  11. #include <utility>
  12. #include "absl/algorithm/container.h"
  13. #include "absl/memory/memory.h"
  14. #include "absl/strings/match.h"
  15. #include "media/base/media_constants.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/location.h"
  18. #include "rtc_base/logging.h"
  19. #include "rtc_base/thread_checker.h"
  20. #include "rtc_base/trace_event.h"
  21. namespace cricket {
  22. ChannelManager::ChannelManager(
  23. std::unique_ptr<MediaEngineInterface> media_engine,
  24. std::unique_ptr<DataEngineInterface> data_engine,
  25. rtc::Thread* worker_thread,
  26. rtc::Thread* network_thread)
  27. : media_engine_(std::move(media_engine)),
  28. data_engine_(std::move(data_engine)),
  29. main_thread_(rtc::Thread::Current()),
  30. worker_thread_(worker_thread),
  31. network_thread_(network_thread) {
  32. RTC_DCHECK(data_engine_);
  33. RTC_DCHECK(worker_thread_);
  34. RTC_DCHECK(network_thread_);
  35. }
  36. ChannelManager::~ChannelManager() {
  37. if (initialized_) {
  38. Terminate();
  39. }
  40. // The media engine needs to be deleted on the worker thread for thread safe
  41. // destruction,
  42. worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { media_engine_.reset(); });
  43. }
  44. bool ChannelManager::SetVideoRtxEnabled(bool enable) {
  45. // To be safe, this call is only allowed before initialization. Apps like
  46. // Flute only have a singleton ChannelManager and we don't want this flag to
  47. // be toggled between calls or when there's concurrent calls. We expect apps
  48. // to enable this at startup and retain that setting for the lifetime of the
  49. // app.
  50. if (!initialized_) {
  51. enable_rtx_ = enable;
  52. return true;
  53. } else {
  54. RTC_LOG(LS_WARNING) << "Cannot toggle rtx after initialization!";
  55. return false;
  56. }
  57. }
  58. void ChannelManager::GetSupportedAudioSendCodecs(
  59. std::vector<AudioCodec>* codecs) const {
  60. if (!media_engine_) {
  61. return;
  62. }
  63. *codecs = media_engine_->voice().send_codecs();
  64. }
  65. void ChannelManager::GetSupportedAudioReceiveCodecs(
  66. std::vector<AudioCodec>* codecs) const {
  67. if (!media_engine_) {
  68. return;
  69. }
  70. *codecs = media_engine_->voice().recv_codecs();
  71. }
  72. void ChannelManager::GetSupportedAudioRtpHeaderExtensions(
  73. RtpHeaderExtensions* ext) const {
  74. if (!media_engine_) {
  75. return;
  76. }
  77. *ext = media_engine_->voice().GetCapabilities().header_extensions;
  78. }
  79. void ChannelManager::GetSupportedVideoCodecs(
  80. std::vector<VideoCodec>* codecs) const {
  81. if (!media_engine_) {
  82. return;
  83. }
  84. codecs->clear();
  85. std::vector<VideoCodec> video_codecs = media_engine_->video().codecs();
  86. for (const auto& video_codec : video_codecs) {
  87. if (!enable_rtx_ &&
  88. absl::EqualsIgnoreCase(kRtxCodecName, video_codec.name)) {
  89. continue;
  90. }
  91. codecs->push_back(video_codec);
  92. }
  93. }
  94. void ChannelManager::GetSupportedVideoRtpHeaderExtensions(
  95. RtpHeaderExtensions* ext) const {
  96. if (!media_engine_) {
  97. return;
  98. }
  99. *ext = media_engine_->video().GetCapabilities().header_extensions;
  100. }
  101. void ChannelManager::GetSupportedDataCodecs(
  102. std::vector<DataCodec>* codecs) const {
  103. *codecs = data_engine_->data_codecs();
  104. }
  105. bool ChannelManager::Init() {
  106. RTC_DCHECK(!initialized_);
  107. if (initialized_) {
  108. return false;
  109. }
  110. RTC_DCHECK(network_thread_);
  111. RTC_DCHECK(worker_thread_);
  112. if (!network_thread_->IsCurrent()) {
  113. // Do not allow invoking calls to other threads on the network thread.
  114. network_thread_->Invoke<void>(
  115. RTC_FROM_HERE, [&] { network_thread_->DisallowBlockingCalls(); });
  116. }
  117. if (media_engine_) {
  118. initialized_ = worker_thread_->Invoke<bool>(
  119. RTC_FROM_HERE, [&] { return media_engine_->Init(); });
  120. RTC_DCHECK(initialized_);
  121. } else {
  122. initialized_ = true;
  123. }
  124. return initialized_;
  125. }
  126. void ChannelManager::Terminate() {
  127. RTC_DCHECK(initialized_);
  128. if (!initialized_) {
  129. return;
  130. }
  131. // Need to destroy the channels on the worker thread.
  132. worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
  133. video_channels_.clear();
  134. voice_channels_.clear();
  135. data_channels_.clear();
  136. });
  137. initialized_ = false;
  138. }
  139. VoiceChannel* ChannelManager::CreateVoiceChannel(
  140. webrtc::Call* call,
  141. const cricket::MediaConfig& media_config,
  142. webrtc::RtpTransportInternal* rtp_transport,
  143. const webrtc::MediaTransportConfig& media_transport_config,
  144. rtc::Thread* signaling_thread,
  145. const std::string& content_name,
  146. bool srtp_required,
  147. const webrtc::CryptoOptions& crypto_options,
  148. rtc::UniqueRandomIdGenerator* ssrc_generator,
  149. const AudioOptions& options) {
  150. if (!worker_thread_->IsCurrent()) {
  151. return worker_thread_->Invoke<VoiceChannel*>(RTC_FROM_HERE, [&] {
  152. return CreateVoiceChannel(call, media_config, rtp_transport,
  153. media_transport_config, signaling_thread,
  154. content_name, srtp_required, crypto_options,
  155. ssrc_generator, options);
  156. });
  157. }
  158. RTC_DCHECK_RUN_ON(worker_thread_);
  159. RTC_DCHECK(initialized_);
  160. RTC_DCHECK(call);
  161. if (!media_engine_) {
  162. return nullptr;
  163. }
  164. VoiceMediaChannel* media_channel = media_engine_->voice().CreateMediaChannel(
  165. call, media_config, options, crypto_options);
  166. if (!media_channel) {
  167. return nullptr;
  168. }
  169. auto voice_channel = std::make_unique<VoiceChannel>(
  170. worker_thread_, network_thread_, signaling_thread,
  171. absl::WrapUnique(media_channel), content_name, srtp_required,
  172. crypto_options, ssrc_generator);
  173. voice_channel->Init_w(rtp_transport, media_transport_config);
  174. VoiceChannel* voice_channel_ptr = voice_channel.get();
  175. voice_channels_.push_back(std::move(voice_channel));
  176. return voice_channel_ptr;
  177. }
  178. void ChannelManager::DestroyVoiceChannel(VoiceChannel* voice_channel) {
  179. TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel");
  180. if (!voice_channel) {
  181. return;
  182. }
  183. if (!worker_thread_->IsCurrent()) {
  184. worker_thread_->Invoke<void>(RTC_FROM_HERE,
  185. [&] { DestroyVoiceChannel(voice_channel); });
  186. return;
  187. }
  188. RTC_DCHECK(initialized_);
  189. auto it = absl::c_find_if(voice_channels_,
  190. [&](const std::unique_ptr<VoiceChannel>& p) {
  191. return p.get() == voice_channel;
  192. });
  193. RTC_DCHECK(it != voice_channels_.end());
  194. if (it == voice_channels_.end()) {
  195. return;
  196. }
  197. voice_channels_.erase(it);
  198. }
  199. VideoChannel* ChannelManager::CreateVideoChannel(
  200. webrtc::Call* call,
  201. const cricket::MediaConfig& media_config,
  202. webrtc::RtpTransportInternal* rtp_transport,
  203. const webrtc::MediaTransportConfig& media_transport_config,
  204. rtc::Thread* signaling_thread,
  205. const std::string& content_name,
  206. bool srtp_required,
  207. const webrtc::CryptoOptions& crypto_options,
  208. rtc::UniqueRandomIdGenerator* ssrc_generator,
  209. const VideoOptions& options,
  210. webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) {
  211. if (!worker_thread_->IsCurrent()) {
  212. return worker_thread_->Invoke<VideoChannel*>(RTC_FROM_HERE, [&] {
  213. return CreateVideoChannel(
  214. call, media_config, rtp_transport, media_transport_config,
  215. signaling_thread, content_name, srtp_required, crypto_options,
  216. ssrc_generator, options, video_bitrate_allocator_factory);
  217. });
  218. }
  219. RTC_DCHECK_RUN_ON(worker_thread_);
  220. RTC_DCHECK(initialized_);
  221. RTC_DCHECK(call);
  222. if (!media_engine_) {
  223. return nullptr;
  224. }
  225. VideoMediaChannel* media_channel = media_engine_->video().CreateMediaChannel(
  226. call, media_config, options, crypto_options,
  227. video_bitrate_allocator_factory);
  228. if (!media_channel) {
  229. return nullptr;
  230. }
  231. auto video_channel = std::make_unique<VideoChannel>(
  232. worker_thread_, network_thread_, signaling_thread,
  233. absl::WrapUnique(media_channel), content_name, srtp_required,
  234. crypto_options, ssrc_generator);
  235. video_channel->Init_w(rtp_transport, media_transport_config);
  236. VideoChannel* video_channel_ptr = video_channel.get();
  237. video_channels_.push_back(std::move(video_channel));
  238. return video_channel_ptr;
  239. }
  240. void ChannelManager::DestroyVideoChannel(VideoChannel* video_channel) {
  241. TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel");
  242. if (!video_channel) {
  243. return;
  244. }
  245. if (!worker_thread_->IsCurrent()) {
  246. worker_thread_->Invoke<void>(RTC_FROM_HERE,
  247. [&] { DestroyVideoChannel(video_channel); });
  248. return;
  249. }
  250. RTC_DCHECK(initialized_);
  251. auto it = absl::c_find_if(video_channels_,
  252. [&](const std::unique_ptr<VideoChannel>& p) {
  253. return p.get() == video_channel;
  254. });
  255. RTC_DCHECK(it != video_channels_.end());
  256. if (it == video_channels_.end()) {
  257. return;
  258. }
  259. video_channels_.erase(it);
  260. }
  261. RtpDataChannel* ChannelManager::CreateRtpDataChannel(
  262. const cricket::MediaConfig& media_config,
  263. webrtc::RtpTransportInternal* rtp_transport,
  264. rtc::Thread* signaling_thread,
  265. const std::string& content_name,
  266. bool srtp_required,
  267. const webrtc::CryptoOptions& crypto_options,
  268. rtc::UniqueRandomIdGenerator* ssrc_generator) {
  269. if (!worker_thread_->IsCurrent()) {
  270. return worker_thread_->Invoke<RtpDataChannel*>(RTC_FROM_HERE, [&] {
  271. return CreateRtpDataChannel(media_config, rtp_transport, signaling_thread,
  272. content_name, srtp_required, crypto_options,
  273. ssrc_generator);
  274. });
  275. }
  276. // This is ok to alloc from a thread other than the worker thread.
  277. RTC_DCHECK(initialized_);
  278. DataMediaChannel* media_channel = data_engine_->CreateChannel(media_config);
  279. if (!media_channel) {
  280. RTC_LOG(LS_WARNING) << "Failed to create RTP data channel.";
  281. return nullptr;
  282. }
  283. auto data_channel = std::make_unique<RtpDataChannel>(
  284. worker_thread_, network_thread_, signaling_thread,
  285. absl::WrapUnique(media_channel), content_name, srtp_required,
  286. crypto_options, ssrc_generator);
  287. // Media Transports are not supported with Rtp Data Channel.
  288. data_channel->Init_w(rtp_transport, webrtc::MediaTransportConfig());
  289. RtpDataChannel* data_channel_ptr = data_channel.get();
  290. data_channels_.push_back(std::move(data_channel));
  291. return data_channel_ptr;
  292. }
  293. void ChannelManager::DestroyRtpDataChannel(RtpDataChannel* data_channel) {
  294. TRACE_EVENT0("webrtc", "ChannelManager::DestroyRtpDataChannel");
  295. if (!data_channel) {
  296. return;
  297. }
  298. if (!worker_thread_->IsCurrent()) {
  299. worker_thread_->Invoke<void>(
  300. RTC_FROM_HERE, [&] { return DestroyRtpDataChannel(data_channel); });
  301. return;
  302. }
  303. RTC_DCHECK(initialized_);
  304. auto it = absl::c_find_if(data_channels_,
  305. [&](const std::unique_ptr<RtpDataChannel>& p) {
  306. return p.get() == data_channel;
  307. });
  308. RTC_DCHECK(it != data_channels_.end());
  309. if (it == data_channels_.end()) {
  310. return;
  311. }
  312. data_channels_.erase(it);
  313. }
  314. bool ChannelManager::StartAecDump(webrtc::FileWrapper file,
  315. int64_t max_size_bytes) {
  316. return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
  317. return media_engine_->voice().StartAecDump(std::move(file), max_size_bytes);
  318. });
  319. }
  320. void ChannelManager::StopAecDump() {
  321. worker_thread_->Invoke<void>(RTC_FROM_HERE,
  322. [&] { media_engine_->voice().StopAecDump(); });
  323. }
  324. } // namespace cricket