blob: b8ffe6e796c239734e4748fd1e5d87305bdc527f [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
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000013#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000014
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000015namespace webrtc {
16namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000017
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000018ChannelOwner::ChannelOwner(class Channel* channel)
19 : channel_ref_(new ChannelRef(channel)) {}
20
21ChannelOwner::ChannelOwner(const ChannelOwner& channel_owner)
22 : channel_ref_(channel_owner.channel_ref_) {
23 ++channel_ref_->ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000024}
25
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000026ChannelOwner::~ChannelOwner() {
27 if (--channel_ref_->ref_count == 0)
28 delete channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000029}
30
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000031ChannelOwner& 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;
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000044ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
45 : channel(channel), ref_count(1) {}
46
solenberg88499ec2016-09-07 07:34:41 -070047ChannelManager::ChannelManager(uint32_t instance_id)
48 : instance_id_(instance_id), last_channel_id_(-1) {}
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000049
ossu5f7cfa52016-05-30 08:11:28 -070050ChannelOwner ChannelManager::CreateChannel(
solenberg88499ec2016-09-07 07:34:41 -070051 const VoEBase::ChannelConfig& config) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000052 Channel* channel;
solenberg88499ec2016-09-07 07:34:41 -070053 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000054 ChannelOwner channel_owner(channel);
55
tommi31fc21f2016-01-21 10:37:37 -080056 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000057
58 channels_.push_back(channel_owner);
59
60 return channel_owner;
61}
62
63ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
tommi31fc21f2016-01-21 10:37:37 -080064 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000065
66 for (size_t i = 0; i < channels_.size(); ++i) {
67 if (channels_[i].channel()->ChannelId() == channel_id)
68 return channels_[i];
69 }
70 return ChannelOwner(NULL);
71}
72
73void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
tommi31fc21f2016-01-21 10:37:37 -080074 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000075
76 *channels = channels_;
77}
78
79void ChannelManager::DestroyChannel(int32_t channel_id) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000080 assert(channel_id >= 0);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000081 // Holds a reference to a channel, this is used so that we never delete
82 // Channels while holding a lock, but rather when the method returns.
83 ChannelOwner reference(NULL);
84 {
tommi31fc21f2016-01-21 10:37:37 -080085 rtc::CritScope crit(&lock_);
Minyue2013aec2015-05-13 14:14:42 +020086 std::vector<ChannelOwner>::iterator to_delete = channels_.end();
87 for (auto it = channels_.begin(); it != channels_.end(); ++it) {
88 Channel* channel = it->channel();
89 // For channels associated with the channel to be deleted, disassociate
90 // with that channel.
91 channel->DisassociateSendChannel(channel_id);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000092
Minyue2013aec2015-05-13 14:14:42 +020093 if (channel->ChannelId() == channel_id) {
94 to_delete = it;
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000095 }
niklase@google.com470e71d2011-07-07 08:21:25 +000096 }
Minyue2013aec2015-05-13 14:14:42 +020097 if (to_delete != channels_.end()) {
98 reference = *to_delete;
99 channels_.erase(to_delete);
100 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000101 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000102}
103
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000104void ChannelManager::DestroyAllChannels() {
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000105 // Holds references so that Channels are not destroyed while holding this
106 // lock, but rather when the method returns.
107 std::vector<ChannelOwner> references;
108 {
tommi31fc21f2016-01-21 10:37:37 -0800109 rtc::CritScope crit(&lock_);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000110 references = channels_;
111 channels_.clear();
112 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000113}
114
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000115size_t ChannelManager::NumOfChannels() const {
tommi31fc21f2016-01-21 10:37:37 -0800116 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000117 return channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000118}
119
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000120ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
121 : iterator_pos_(0) {
122 channel_manager->GetAllChannels(&channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000125Channel* ChannelManager::Iterator::GetChannel() {
126 if (iterator_pos_ < channels_.size())
127 return channels_[iterator_pos_].channel();
128 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129}
130
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000131bool ChannelManager::Iterator::IsValid() {
132 return iterator_pos_ < channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000133}
134
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000135void ChannelManager::Iterator::Increment() {
136 ++iterator_pos_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000137}
138
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000139} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000140} // namespace webrtc