blob: 76664d4b12ccd9becadd39f81363fc6b72a8e38c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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.org956aa7e2013-05-21 13:52:32 +000011#include "webrtc/voice_engine/channel_manager.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Minyue2013aec2015-05-13 14:14:42 +020013#include "webrtc/common.h"
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000014#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000016namespace webrtc {
17namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000018
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000019ChannelOwner::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;
niklase@google.com470e71d2011-07-07 08:21:25 +000025}
26
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000027ChannelOwner::~ChannelOwner() {
28 if (--channel_ref_->ref_count == 0)
29 delete channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000030}
31
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000032ChannelOwner& 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.com470e71d2011-07-07 08:21:25 +000043}
44
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000045ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
46 : channel(channel), ref_count(1) {}
47
minyue@webrtc.orge509f942013-09-12 17:03:00 +000048ChannelManager::ChannelManager(uint32_t instance_id, const Config& config)
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000049 : instance_id_(instance_id),
50 last_channel_id_(-1),
minyue@webrtc.orge509f942013-09-12 17:03:00 +000051 lock_(CriticalSectionWrapper::CreateCriticalSection()),
52 config_(config) {}
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000053
54ChannelOwner ChannelManager::CreateChannel() {
turaj@webrtc.org03f33702013-11-13 00:02:48 +000055 return CreateChannelInternal(config_);
56}
57
58ChannelOwner ChannelManager::CreateChannel(const Config& external_config) {
59 return CreateChannelInternal(external_config);
60}
61
62ChannelOwner ChannelManager::CreateChannelInternal(const Config& config) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000063 Channel* channel;
turaj@webrtc.org03f33702013-11-13 00:02:48 +000064 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000065 ChannelOwner channel_owner(channel);
66
67 CriticalSectionScoped crit(lock_.get());
68
69 channels_.push_back(channel_owner);
70
71 return channel_owner;
72}
73
74ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
75 CriticalSectionScoped crit(lock_.get());
76
77 for (size_t i = 0; i < channels_.size(); ++i) {
78 if (channels_[i].channel()->ChannelId() == channel_id)
79 return channels_[i];
80 }
81 return ChannelOwner(NULL);
82}
83
84void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
85 CriticalSectionScoped crit(lock_.get());
86
87 *channels = channels_;
88}
89
90void ChannelManager::DestroyChannel(int32_t channel_id) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000091 assert(channel_id >= 0);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000092 // Holds a reference to a channel, this is used so that we never delete
93 // Channels while holding a lock, but rather when the method returns.
94 ChannelOwner reference(NULL);
95 {
96 CriticalSectionScoped crit(lock_.get());
Minyue2013aec2015-05-13 14:14:42 +020097 std::vector<ChannelOwner>::iterator to_delete = channels_.end();
98 for (auto it = channels_.begin(); it != channels_.end(); ++it) {
99 Channel* channel = it->channel();
100 // For channels associated with the channel to be deleted, disassociate
101 // with that channel.
102 channel->DisassociateSendChannel(channel_id);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000103
Minyue2013aec2015-05-13 14:14:42 +0200104 if (channel->ChannelId() == channel_id) {
105 to_delete = it;
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000106 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 }
Minyue2013aec2015-05-13 14:14:42 +0200108 if (to_delete != channels_.end()) {
109 reference = *to_delete;
110 channels_.erase(to_delete);
111 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000112 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000115void ChannelManager::DestroyAllChannels() {
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000116 // Holds references so that Channels are not destroyed while holding this
117 // lock, but rather when the method returns.
118 std::vector<ChannelOwner> references;
119 {
120 CriticalSectionScoped crit(lock_.get());
121 references = channels_;
122 channels_.clear();
123 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000124}
125
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000126size_t ChannelManager::NumOfChannels() const {
127 CriticalSectionScoped crit(lock_.get());
128 return channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000131ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
132 : iterator_pos_(0) {
133 channel_manager->GetAllChannels(&channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134}
135
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000136Channel* ChannelManager::Iterator::GetChannel() {
137 if (iterator_pos_ < channels_.size())
138 return channels_[iterator_pos_].channel();
139 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000140}
141
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000142bool ChannelManager::Iterator::IsValid() {
143 return iterator_pos_ < channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000144}
145
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000146void ChannelManager::Iterator::Increment() {
147 ++iterator_pos_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000148}
149
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000150} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000151} // namespace webrtc