| 5 | #include "absl/strings/match.h" |
| 6 | #include "api/media_types.h" |
| 7 | #include "api/sequence_checker.h" |
| 8 | #include "media/base/media_constants.h" |
| 9 | #include "rtc_base/checks.h" |
| 10 | #include "rtc_base/trace_event.h" |
| 11 | namespace tgcalls { |
| 12 | // static |
| 13 | std::unique_ptr<ChannelManager> ChannelManager::Create( |
| 14 | std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 15 | rtc::Thread* worker_thread, |
| 16 | rtc::Thread* network_thread) { |
| 17 | RTC_DCHECK(network_thread); |
| 18 | RTC_DCHECK(worker_thread); |
| 19 | return absl::WrapUnique(new ChannelManager( |
| 20 | std::move(media_engine), worker_thread, network_thread)); |
| 21 | } |
| 22 | ChannelManager::ChannelManager( |
| 23 | std::unique_ptr<cricket::MediaEngineInterface> media_engine, |
| 24 | rtc::Thread* worker_thread, |
| 25 | rtc::Thread* network_thread) |
| 26 | : media_engine_(std::move(media_engine)), |
| 27 | signaling_thread_(rtc::Thread::Current()), |
| 28 | worker_thread_(worker_thread), |
| 29 | network_thread_(network_thread) { |
| 30 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 31 | RTC_DCHECK(worker_thread_); |
| 32 | RTC_DCHECK(network_thread_); |
| 33 | if (media_engine_) { |
| 34 | // TODO(tommi): Change VoiceEngine to do ctor time initialization so that |
| 35 | // this isn't necessary. |
| 36 | worker_thread_->BlockingCall([&] { media_engine_->Init(); }); |
| 37 | } |
| 38 | } |
| 39 | ChannelManager::~ChannelManager() { |
| 40 | RTC_DCHECK_RUN_ON(signaling_thread_); |
| 41 | worker_thread_->BlockingCall([&] { |
| 42 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 43 | RTC_DCHECK(voice_channels_.empty()); |
| 44 | RTC_DCHECK(video_channels_.empty()); |
| 45 | // While `media_engine_` is const throughout the ChannelManager's lifetime, |
| 46 | // it requires destruction to happen on the worker thread. Instead of |
| 47 | // marking the pointer as non-const, we live with this const_cast<> in the |
| 48 | // destructor. |
| 49 | const_cast<std::unique_ptr<cricket::MediaEngineInterface>&>(media_engine_).reset(); |
| 50 | }); |
| 51 | } |
| 52 | cricket::VoiceChannel* ChannelManager::CreateVoiceChannel( |
| 53 | webrtc::Call* call, |
| 54 | const cricket::MediaConfig& media_config, |
| 55 | const std::string& mid, |
| 56 | bool srtp_required, |
| 57 | const webrtc::CryptoOptions& crypto_options, |
| 58 | const cricket::AudioOptions& options) { |
| 59 | RTC_DCHECK(call); |
| 60 | RTC_DCHECK(media_engine_); |
| 61 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 62 | // PeerConnection and add the expectation that we're already on the right |
| 63 | // thread. |
| 64 | if (!worker_thread_->IsCurrent()) { |
| 65 | cricket::VoiceChannel* temp = nullptr; |
| 66 | worker_thread_->BlockingCall([&] { |
| 67 | temp = CreateVoiceChannel(call, media_config, mid, srtp_required, |
| 68 | crypto_options, options); |
| 69 | }); |
| 70 | return temp; |
| 71 | } |
| 72 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 73 | std::unique_ptr<cricket::VoiceMediaSendChannelInterface> send_media_channel = media_engine_->voice().CreateSendChannel( |
| 74 | call, media_config, options, crypto_options, webrtc::AudioCodecPairId::Create()); |
| 75 | if (!send_media_channel) { |
| 76 | return nullptr; |
| 77 | } |
| 78 | std::unique_ptr<cricket::VoiceMediaReceiveChannelInterface> receive_media_channel = media_engine_->voice().CreateReceiveChannel( |
| 79 | call, media_config, options, crypto_options, webrtc::AudioCodecPairId::Create()); |
| 80 | if (!receive_media_channel) { |
| 81 | return nullptr; |
| 82 | } |
| 83 | auto voice_channel = std::make_unique<cricket::VoiceChannel>( |
| 84 | worker_thread_, network_thread_, signaling_thread_, |
| 85 | std::move(send_media_channel), std::move(receive_media_channel), mid, srtp_required, crypto_options, |
| 86 | &ssrc_generator_); |
| 87 | cricket::VoiceChannel* voice_channel_ptr = voice_channel.get(); |
| 88 | voice_channels_.push_back(std::move(voice_channel)); |
| 89 | return voice_channel_ptr; |
| 90 | } |
| 91 | void ChannelManager::DestroyVoiceChannel(cricket::VoiceChannel* channel) { |
| 92 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVoiceChannel"); |
| 93 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 94 | voice_channels_.erase(absl::c_find_if( |
| 95 | voice_channels_, [&](const auto& p) { return p.get() == channel; })); |
| 96 | } |
| 97 | cricket::VideoChannel* ChannelManager::CreateVideoChannel( |
| 98 | webrtc::Call* call, |
| 99 | const cricket::MediaConfig& media_config, |
| 100 | const std::string& mid, |
| 101 | bool srtp_required, |
| 102 | const webrtc::CryptoOptions& crypto_options, |
| 103 | const cricket::VideoOptions& options, |
| 104 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
| 105 | RTC_DCHECK(call); |
| 106 | RTC_DCHECK(media_engine_); |
| 107 | // TODO(bugs.webrtc.org/11992): Remove this workaround after updates in |
| 108 | // PeerConnection and add the expectation that we're already on the right |
| 109 | // thread. |
| 110 | if (!worker_thread_->IsCurrent()) { |
| 111 | cricket::VideoChannel* temp = nullptr; |
| 112 | worker_thread_->BlockingCall([&] { |
| 113 | temp = CreateVideoChannel(call, media_config, mid, srtp_required, |
| 114 | crypto_options, options, |
| 115 | video_bitrate_allocator_factory); |
| 116 | }); |
| 117 | return temp; |
| 118 | } |
| 119 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 120 | std::unique_ptr<cricket::VideoMediaSendChannelInterface> send_media_channel = media_engine_->video().CreateSendChannel( |
| 121 | call, media_config, options, crypto_options, |
| 122 | video_bitrate_allocator_factory); |
| 123 | if (!send_media_channel) { |
| 124 | return nullptr; |
| 125 | } |
| 126 | std::unique_ptr<cricket::VideoMediaReceiveChannelInterface> receive_media_channel = media_engine_->video().CreateReceiveChannel( |
| 127 | call, media_config, options, crypto_options); |
| 128 | if (!receive_media_channel) { |
| 129 | return nullptr; |
| 130 | } |
| 131 | auto video_channel = std::make_unique<cricket::VideoChannel>( |
| 132 | worker_thread_, network_thread_, signaling_thread_, |
| 133 | std::move(send_media_channel), std::move(receive_media_channel), mid, srtp_required, crypto_options, |
| 134 | &ssrc_generator_); |
| 135 | cricket::VideoChannel* video_channel_ptr = video_channel.get(); |
| 136 | video_channels_.push_back(std::move(video_channel)); |
| 137 | return video_channel_ptr; |
| 138 | } |
| 139 | void ChannelManager::DestroyVideoChannel(cricket::VideoChannel* channel) { |
| 140 | TRACE_EVENT0("webrtc", "ChannelManager::DestroyVideoChannel"); |
| 141 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 142 | video_channels_.erase(absl::c_find_if( |
| 143 | video_channels_, [&](const auto& p) { return p.get() == channel; })); |
| 144 | } |
| 145 | void ChannelManager::DestroyChannel(cricket::ChannelInterface* channel) { |
| 146 | RTC_DCHECK(channel); |
| 147 | if (!worker_thread_->IsCurrent()) { |
| 148 | // TODO(tommi): Do this asynchronously when we have a way to make sure that |
| 149 | // the call to DestroyChannel runs before ~Call() runs, which today happens |
| 150 | // inside an Invoke from the signaling thread in PeerConnectin::Close(). |
| 151 | worker_thread_->BlockingCall([&] { DestroyChannel(channel); }); |
| 152 | return; |
| 153 | } |
| 154 | if (channel->media_type() == cricket::MEDIA_TYPE_AUDIO) { |
| 155 | DestroyVoiceChannel(static_cast<cricket::VoiceChannel*>(channel)); |
| 156 | } else { |
| 157 | RTC_DCHECK_EQ(channel->media_type(), cricket::MEDIA_TYPE_VIDEO); |
| 158 | DestroyVideoChannel(static_cast<cricket::VideoChannel*>(channel)); |
| 159 | } |
| 160 | } |
| 161 | } // namespace tgcalls |
| 162 |