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 11#include "webrtc/voice_engine/channel_manager.h" 12 13#include "webrtc/common.h" 14#include "webrtc/voice_engine/channel.h" 15 16namespace webrtc { 17namespace voe { 18 19ChannelOwner::ChannelOwner(class Channel* channel) 20 : channel_ref_(new ChannelRef(channel)) {} 21 22ChannelOwner::ChannelOwner(const ChannelOwner& channel_owner) 23 : channel_ref_(channel_owner.channel_ref_) { 24 ++channel_ref_->ref_count; 25} 26 27ChannelOwner::~ChannelOwner() { 28 if (--channel_ref_->ref_count == 0) 29 delete channel_ref_; 30} 31 32ChannelOwner& 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; 43} 44 45ChannelOwner::ChannelRef::ChannelRef(class Channel* channel) 46 : channel(channel), ref_count(1) {} 47 48ChannelManager::ChannelManager(uint32_t instance_id, const Config& config) 49 : instance_id_(instance_id), 50 last_channel_id_(-1), 51 lock_(CriticalSectionWrapper::CreateCriticalSection()), 52 config_(config), 53 event_log_(RtcEventLog::Create()) {} 54 55ChannelOwner ChannelManager::CreateChannel() { 56 return CreateChannelInternal(config_); 57} 58 59ChannelOwner ChannelManager::CreateChannel(const Config& external_config) { 60 return CreateChannelInternal(external_config); 61} 62 63ChannelOwner ChannelManager::CreateChannelInternal(const Config& config) { 64 Channel* channel; 65 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, 66 event_log_.get(), config); 67 ChannelOwner channel_owner(channel); 68 69 CriticalSectionScoped crit(lock_.get()); 70 71 channels_.push_back(channel_owner); 72 73 return channel_owner; 74} 75 76ChannelOwner ChannelManager::GetChannel(int32_t channel_id) { 77 CriticalSectionScoped crit(lock_.get()); 78 79 for (size_t i = 0; i < channels_.size(); ++i) { 80 if (channels_[i].channel()->ChannelId() == channel_id) 81 return channels_[i]; 82 } 83 return ChannelOwner(NULL); 84} 85 86void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) { 87 CriticalSectionScoped crit(lock_.get()); 88 89 *channels = channels_; 90} 91 92void ChannelManager::DestroyChannel(int32_t channel_id) { 93 assert(channel_id >= 0); 94 // Holds a reference to a channel, this is used so that we never delete 95 // Channels while holding a lock, but rather when the method returns. 96 ChannelOwner reference(NULL); 97 { 98 CriticalSectionScoped crit(lock_.get()); 99 std::vector<ChannelOwner>::iterator to_delete = channels_.end(); 100 for (auto it = channels_.begin(); it != channels_.end(); ++it) { 101 Channel* channel = it->channel(); 102 // For channels associated with the channel to be deleted, disassociate 103 // with that channel. 104 channel->DisassociateSendChannel(channel_id); 105 106 if (channel->ChannelId() == channel_id) { 107 to_delete = it; 108 } 109 } 110 if (to_delete != channels_.end()) { 111 reference = *to_delete; 112 channels_.erase(to_delete); 113 } 114 } 115} 116 117void ChannelManager::DestroyAllChannels() { 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 { 122 CriticalSectionScoped crit(lock_.get()); 123 references = channels_; 124 channels_.clear(); 125 } 126} 127 128size_t ChannelManager::NumOfChannels() const { 129 CriticalSectionScoped crit(lock_.get()); 130 return channels_.size(); 131} 132 133RtcEventLog* ChannelManager::GetEventLog() const { 134 return event_log_.get(); 135} 136 137ChannelManager::Iterator::Iterator(ChannelManager* channel_manager) 138 : iterator_pos_(0) { 139 channel_manager->GetAllChannels(&channels_); 140} 141 142Channel* ChannelManager::Iterator::GetChannel() { 143 if (iterator_pos_ < channels_.size()) 144 return channels_[iterator_pos_].channel(); 145 return NULL; 146} 147 148bool ChannelManager::Iterator::IsValid() { 149 return iterator_pos_ < channels_.size(); 150} 151 152void ChannelManager::Iterator::Increment() { 153 ++iterator_pos_; 154} 155 156} // namespace voe 157} // namespace webrtc 158