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