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