andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +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 | be72fe4 | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/channel_manager.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 13 | #include "webrtc/voice_engine/channel.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 15 | namespace webrtc { |
| 16 | namespace voe { |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 17 | |
pbos@webrtc.org | 9277c94 | 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; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 24 | } |
| 25 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 26 | ChannelOwner::~ChannelOwner() { |
| 27 | if (--channel_ref_->ref_count == 0) |
| 28 | delete channel_ref_; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 29 | } |
| 30 | |
pbos@webrtc.org | 9277c94 | 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; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 42 | } |
| 43 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 44 | ChannelOwner::ChannelRef::ChannelRef(class Channel* channel) |
| 45 | : channel(channel), ref_count(1) {} |
| 46 | |
| 47 | ChannelManager::ChannelManager(uint32_t instance_id) |
| 48 | : instance_id_(instance_id), |
| 49 | last_channel_id_(-1), |
| 50 | lock_(CriticalSectionWrapper::CreateCriticalSection()) {} |
| 51 | |
| 52 | ChannelOwner ChannelManager::CreateChannel() { |
| 53 | Channel* channel; |
| 54 | Channel::CreateChannel(channel, ++last_channel_id_, instance_id_); |
| 55 | ChannelOwner channel_owner(channel); |
| 56 | |
| 57 | CriticalSectionScoped crit(lock_.get()); |
| 58 | |
| 59 | channels_.push_back(channel_owner); |
| 60 | |
| 61 | return channel_owner; |
| 62 | } |
| 63 | |
| 64 | ChannelOwner ChannelManager::GetChannel(int32_t channel_id) { |
| 65 | CriticalSectionScoped crit(lock_.get()); |
| 66 | |
| 67 | for (size_t i = 0; i < channels_.size(); ++i) { |
| 68 | if (channels_[i].channel()->ChannelId() == channel_id) |
| 69 | return channels_[i]; |
| 70 | } |
| 71 | return ChannelOwner(NULL); |
| 72 | } |
| 73 | |
| 74 | void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) { |
| 75 | CriticalSectionScoped crit(lock_.get()); |
| 76 | |
| 77 | *channels = channels_; |
| 78 | } |
| 79 | |
| 80 | void ChannelManager::DestroyChannel(int32_t channel_id) { |
| 81 | CriticalSectionScoped crit(lock_.get()); |
| 82 | assert(channel_id >= 0); |
| 83 | |
| 84 | for (std::vector<ChannelOwner>::iterator it = channels_.begin(); |
| 85 | it != channels_.end(); |
| 86 | ++it) { |
| 87 | if (it->channel()->ChannelId() == channel_id) { |
| 88 | channels_.erase(it); |
| 89 | break; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 90 | } |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 91 | } |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 94 | void ChannelManager::DestroyAllChannels() { |
| 95 | CriticalSectionScoped crit(lock_.get()); |
| 96 | channels_.clear(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 97 | } |
| 98 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 99 | size_t ChannelManager::NumOfChannels() const { |
| 100 | CriticalSectionScoped crit(lock_.get()); |
| 101 | return channels_.size(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 102 | } |
| 103 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 104 | ChannelManager::Iterator::Iterator(ChannelManager* channel_manager) |
| 105 | : iterator_pos_(0) { |
| 106 | channel_manager->GetAllChannels(&channels_); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 107 | } |
| 108 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 109 | Channel* ChannelManager::Iterator::GetChannel() { |
| 110 | if (iterator_pos_ < channels_.size()) |
| 111 | return channels_[iterator_pos_].channel(); |
| 112 | return NULL; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 113 | } |
| 114 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 115 | bool ChannelManager::Iterator::IsValid() { |
| 116 | return iterator_pos_ < channels_.size(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 117 | } |
| 118 | |
pbos@webrtc.org | 9277c94 | 2013-08-07 17:57:36 +0000 | [diff] [blame^] | 119 | void ChannelManager::Iterator::Increment() { |
| 120 | ++iterator_pos_; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 121 | } |
| 122 | |
pbos@webrtc.org | 5ab7b93 | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 123 | } // namespace voe |
pbos@webrtc.org | 5ab7b93 | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 124 | } // namespace webrtc |