niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "voice_engine/channel_manager.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/timeutils.h" |
| 14 | #include "voice_engine/channel.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 18 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 19 | ChannelOwner::ChannelOwner(class Channel* channel) |
| 20 | : channel_ref_(new ChannelRef(channel)) {} |
| 21 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 22 | ChannelOwner::ChannelRef::ChannelRef(class Channel* channel) |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 23 | : channel(channel) {} |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 24 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 25 | ChannelManager::ChannelManager(uint32_t instance_id) |
nisse | 7d59f6b | 2017-02-21 03:40:24 -0800 | [diff] [blame] | 26 | : instance_id_(instance_id), |
| 27 | last_channel_id_(-1), |
| 28 | random_(rtc::TimeNanos()) {} |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 29 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 30 | ChannelOwner ChannelManager::CreateChannel( |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 31 | const VoEBase::ChannelConfig& config) { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 32 | Channel* channel; |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 33 | Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config); |
nisse | 7d59f6b | 2017-02-21 03:40:24 -0800 | [diff] [blame] | 34 | // TODO(solenberg): Delete this, users should configure ssrc |
| 35 | // explicitly. |
| 36 | channel->SetLocalSSRC(random_.Rand<uint32_t>()); |
| 37 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 38 | ChannelOwner channel_owner(channel); |
| 39 | |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 40 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 41 | |
| 42 | channels_.push_back(channel_owner); |
| 43 | |
| 44 | return channel_owner; |
| 45 | } |
| 46 | |
| 47 | ChannelOwner ChannelManager::GetChannel(int32_t channel_id) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 48 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 49 | |
| 50 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 51 | if (channels_[i].channel()->ChannelId() == channel_id) |
| 52 | return channels_[i]; |
| 53 | } |
| 54 | return ChannelOwner(NULL); |
| 55 | } |
| 56 | |
| 57 | void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 58 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 59 | |
| 60 | *channels = channels_; |
| 61 | } |
| 62 | |
| 63 | void ChannelManager::DestroyChannel(int32_t channel_id) { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 64 | assert(channel_id >= 0); |
pbos@webrtc.org | 58d76cb | 2013-08-08 17:32:21 +0000 | [diff] [blame] | 65 | // Holds a reference to a channel, this is used so that we never delete |
| 66 | // Channels while holding a lock, but rather when the method returns. |
| 67 | ChannelOwner reference(NULL); |
| 68 | { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 69 | rtc::CritScope crit(&lock_); |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 70 | std::vector<ChannelOwner>::iterator to_delete = channels_.end(); |
| 71 | for (auto it = channels_.begin(); it != channels_.end(); ++it) { |
| 72 | Channel* channel = it->channel(); |
| 73 | // For channels associated with the channel to be deleted, disassociate |
| 74 | // with that channel. |
| 75 | channel->DisassociateSendChannel(channel_id); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 76 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 77 | if (channel->ChannelId() == channel_id) { |
| 78 | to_delete = it; |
pbos@webrtc.org | 58d76cb | 2013-08-08 17:32:21 +0000 | [diff] [blame] | 79 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | } |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 81 | if (to_delete != channels_.end()) { |
| 82 | reference = *to_delete; |
| 83 | channels_.erase(to_delete); |
| 84 | } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 85 | } |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 86 | if (reference.channel()) { |
| 87 | // Ensure the channel is torn down now, on this thread, since a reference |
| 88 | // may still be held on a different thread (e.g. in the audio capture |
| 89 | // thread). |
| 90 | reference.channel()->Terminate(); |
| 91 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | } |
| 93 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 94 | void ChannelManager::DestroyAllChannels() { |
pbos@webrtc.org | 58d76cb | 2013-08-08 17:32:21 +0000 | [diff] [blame] | 95 | // Holds references so that Channels are not destroyed while holding this |
| 96 | // lock, but rather when the method returns. |
| 97 | std::vector<ChannelOwner> references; |
| 98 | { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 99 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | 58d76cb | 2013-08-08 17:32:21 +0000 | [diff] [blame] | 100 | references = channels_; |
| 101 | channels_.clear(); |
| 102 | } |
tommi | 0a2391f | 2017-03-21 02:31:51 -0700 | [diff] [blame] | 103 | for (auto& owner : references) { |
| 104 | if (owner.channel()) |
| 105 | owner.channel()->Terminate(); |
| 106 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | } |
| 108 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 109 | size_t ChannelManager::NumOfChannels() const { |
tommi | 31fc21f | 2016-01-21 10:37:37 -0800 | [diff] [blame] | 110 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 111 | return channels_.size(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
| 113 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 114 | ChannelManager::Iterator::Iterator(ChannelManager* channel_manager) |
| 115 | : iterator_pos_(0) { |
| 116 | channel_manager->GetAllChannels(&channels_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | } |
| 118 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 119 | Channel* ChannelManager::Iterator::GetChannel() { |
| 120 | if (iterator_pos_ < channels_.size()) |
| 121 | return channels_[iterator_pos_].channel(); |
| 122 | return NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | } |
| 124 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 125 | bool ChannelManager::Iterator::IsValid() { |
| 126 | return iterator_pos_ < channels_.size(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 129 | void ChannelManager::Iterator::Increment() { |
| 130 | ++iterator_pos_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | } |
| 132 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 133 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 134 | } // namespace webrtc |