andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [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 | 471ae72 | 2013-05-21 13:52:32 | [diff] [blame] | 11 | #include "webrtc/voice_engine/channel_manager.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 12 | |
Edward Lemur | 76de83e | 2017-07-06 17:44:34 | [diff] [blame] | 13 | #include "webrtc/rtc_base/timeutils.h" |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 14 | #include "webrtc/voice_engine/channel.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 15 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | namespace voe { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 18 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [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; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 25 | } |
| 26 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 27 | ChannelOwner::~ChannelOwner() { |
| 28 | if (--channel_ref_->ref_count == 0) |
| 29 | delete channel_ref_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 30 | } |
| 31 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [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; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 43 | } |
| 44 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 45 | ChannelOwner::ChannelRef::ChannelRef(class Channel* channel) |
| 46 | : channel(channel), ref_count(1) {} |
| 47 | |
solenberg | a686d5e | 2016-09-07 14:34:41 | [diff] [blame] | 48 | ChannelManager::ChannelManager(uint32_t instance_id) |
nisse | 6bbab56 | 2017-02-21 11:40:24 | [diff] [blame] | 49 | : instance_id_(instance_id), |
| 50 | last_channel_id_(-1), |
| 51 | random_(rtc::TimeNanos()) {} |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 52 | |
ossu | 1ac1c3f | 2016-05-30 15:11:28 | [diff] [blame] | 53 | ChannelOwner ChannelManager::CreateChannel( |
solenberg | a686d5e | 2016-09-07 14:34:41 | [diff] [blame] | 54 | const VoEBase::ChannelConfig& config) { |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 55 | Channel* channel; |
solenberg | a686d5e | 2016-09-07 14:34:41 | [diff] [blame] | 56 | Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config); |
nisse | 6bbab56 | 2017-02-21 11:40:24 | [diff] [blame] | 57 | // TODO(solenberg): Delete this, users should configure ssrc |
| 58 | // explicitly. |
| 59 | channel->SetLocalSSRC(random_.Rand<uint32_t>()); |
| 60 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 61 | ChannelOwner channel_owner(channel); |
| 62 | |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 63 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 64 | |
| 65 | channels_.push_back(channel_owner); |
| 66 | |
| 67 | return channel_owner; |
| 68 | } |
| 69 | |
| 70 | ChannelOwner ChannelManager::GetChannel(int32_t channel_id) { |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 71 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 72 | |
| 73 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 74 | if (channels_[i].channel()->ChannelId() == channel_id) |
| 75 | return channels_[i]; |
| 76 | } |
| 77 | return ChannelOwner(NULL); |
| 78 | } |
| 79 | |
| 80 | void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) { |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 81 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 82 | |
| 83 | *channels = channels_; |
| 84 | } |
| 85 | |
| 86 | void ChannelManager::DestroyChannel(int32_t channel_id) { |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 87 | assert(channel_id >= 0); |
pbos@webrtc.org | a4a1afa | 2013-08-08 17:32:21 | [diff] [blame] | 88 | // Holds a reference to a channel, this is used so that we never delete |
| 89 | // Channels while holding a lock, but rather when the method returns. |
| 90 | ChannelOwner reference(NULL); |
| 91 | { |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 92 | rtc::CritScope crit(&lock_); |
Minyue | 1103af5 | 2015-05-13 12:14:42 | [diff] [blame] | 93 | std::vector<ChannelOwner>::iterator to_delete = channels_.end(); |
| 94 | for (auto it = channels_.begin(); it != channels_.end(); ++it) { |
| 95 | Channel* channel = it->channel(); |
| 96 | // For channels associated with the channel to be deleted, disassociate |
| 97 | // with that channel. |
| 98 | channel->DisassociateSendChannel(channel_id); |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 99 | |
Minyue | 1103af5 | 2015-05-13 12:14:42 | [diff] [blame] | 100 | if (channel->ChannelId() == channel_id) { |
| 101 | to_delete = it; |
pbos@webrtc.org | a4a1afa | 2013-08-08 17:32:21 | [diff] [blame] | 102 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 103 | } |
Minyue | 1103af5 | 2015-05-13 12:14:42 | [diff] [blame] | 104 | if (to_delete != channels_.end()) { |
| 105 | reference = *to_delete; |
| 106 | channels_.erase(to_delete); |
| 107 | } |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 108 | } |
tommi | 3d8ed00 | 2017-03-21 09:31:51 | [diff] [blame] | 109 | if (reference.channel()) { |
| 110 | // Ensure the channel is torn down now, on this thread, since a reference |
| 111 | // may still be held on a different thread (e.g. in the audio capture |
| 112 | // thread). |
| 113 | reference.channel()->Terminate(); |
| 114 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 115 | } |
| 116 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 117 | void ChannelManager::DestroyAllChannels() { |
pbos@webrtc.org | a4a1afa | 2013-08-08 17:32:21 | [diff] [blame] | 118 | // Holds references so that Channels are not destroyed while holding this |
| 119 | // lock, but rather when the method returns. |
| 120 | std::vector<ChannelOwner> references; |
| 121 | { |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 122 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | a4a1afa | 2013-08-08 17:32:21 | [diff] [blame] | 123 | references = channels_; |
| 124 | channels_.clear(); |
| 125 | } |
tommi | 3d8ed00 | 2017-03-21 09:31:51 | [diff] [blame] | 126 | for (auto& owner : references) { |
| 127 | if (owner.channel()) |
| 128 | owner.channel()->Terminate(); |
| 129 | } |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 130 | } |
| 131 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 132 | size_t ChannelManager::NumOfChannels() const { |
tommi | c8e8cb0 | 2016-01-21 18:37:37 | [diff] [blame] | 133 | rtc::CritScope crit(&lock_); |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 134 | return channels_.size(); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 135 | } |
| 136 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 137 | ChannelManager::Iterator::Iterator(ChannelManager* channel_manager) |
| 138 | : iterator_pos_(0) { |
| 139 | channel_manager->GetAllChannels(&channels_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 140 | } |
| 141 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 142 | Channel* ChannelManager::Iterator::GetChannel() { |
| 143 | if (iterator_pos_ < channels_.size()) |
| 144 | return channels_[iterator_pos_].channel(); |
| 145 | return NULL; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 146 | } |
| 147 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 148 | bool ChannelManager::Iterator::IsValid() { |
| 149 | return iterator_pos_ < channels_.size(); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 150 | } |
| 151 | |
pbos@webrtc.org | b3ada15 | 2013-08-07 17:57:36 | [diff] [blame] | 152 | void ChannelManager::Iterator::Increment() { |
| 153 | ++iterator_pos_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 154 | } |
| 155 | |
pbos@webrtc.org | 3b89e10 | 2013-07-03 15:12:26 | [diff] [blame] | 156 | } // namespace voe |
pbos@webrtc.org | 3b89e10 | 2013-07-03 15:12:26 | [diff] [blame] | 157 | } // namespace webrtc |