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