blob: b56c54a82874b76570651ae6ad7d099dadbc4d84 [file] [log] [blame]
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +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.orgbe72fe42013-05-21 13:52:32 +000011#include "webrtc/voice_engine/channel_manager.h"
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000012
pbos@webrtc.org9277c942013-08-07 17:57:36 +000013#include "webrtc/voice_engine/channel.h"
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000014
pbos@webrtc.org9277c942013-08-07 17:57:36 +000015namespace webrtc {
16namespace voe {
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000017
pbos@webrtc.org9277c942013-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;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000024}
25
pbos@webrtc.org9277c942013-08-07 17:57:36 +000026ChannelOwner::~ChannelOwner() {
27 if (--channel_ref_->ref_count == 0)
28 delete channel_ref_;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000029}
30
pbos@webrtc.org9277c942013-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;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000042}
43
pbos@webrtc.org9277c942013-08-07 17:57:36 +000044ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
45 : channel(channel), ref_count(1) {}
46
minyue@webrtc.org1963a682013-09-12 17:03:00 +000047ChannelManager::ChannelManager(uint32_t instance_id, const Config& config)
pbos@webrtc.org9277c942013-08-07 17:57:36 +000048 : instance_id_(instance_id),
49 last_channel_id_(-1),
minyue@webrtc.org1963a682013-09-12 17:03:00 +000050 lock_(CriticalSectionWrapper::CreateCriticalSection()),
51 config_(config) {}
pbos@webrtc.org9277c942013-08-07 17:57:36 +000052
53ChannelOwner ChannelManager::CreateChannel() {
54 Channel* channel;
minyue@webrtc.org1963a682013-09-12 17:03:00 +000055 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config_);
pbos@webrtc.org9277c942013-08-07 17:57:36 +000056 ChannelOwner channel_owner(channel);
57
58 CriticalSectionScoped crit(lock_.get());
59
60 channels_.push_back(channel_owner);
61
62 return channel_owner;
63}
64
65ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
66 CriticalSectionScoped crit(lock_.get());
67
68 for (size_t i = 0; i < channels_.size(); ++i) {
69 if (channels_[i].channel()->ChannelId() == channel_id)
70 return channels_[i];
71 }
72 return ChannelOwner(NULL);
73}
74
75void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
76 CriticalSectionScoped crit(lock_.get());
77
78 *channels = channels_;
79}
80
81void ChannelManager::DestroyChannel(int32_t channel_id) {
pbos@webrtc.org9277c942013-08-07 17:57:36 +000082 assert(channel_id >= 0);
pbos@webrtc.org54e99552013-08-08 17:32:21 +000083 // Holds a reference to a channel, this is used so that we never delete
84 // Channels while holding a lock, but rather when the method returns.
85 ChannelOwner reference(NULL);
86 {
87 CriticalSectionScoped crit(lock_.get());
pbos@webrtc.org9277c942013-08-07 17:57:36 +000088
pbos@webrtc.org54e99552013-08-08 17:32:21 +000089 for (std::vector<ChannelOwner>::iterator it = channels_.begin();
90 it != channels_.end();
91 ++it) {
92 if (it->channel()->ChannelId() == channel_id) {
93 reference = *it;
94 channels_.erase(it);
95 break;
96 }
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000097 }
pbos@webrtc.org9277c942013-08-07 17:57:36 +000098 }
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000099}
100
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000101void ChannelManager::DestroyAllChannels() {
pbos@webrtc.org54e99552013-08-08 17:32:21 +0000102 // Holds references so that Channels are not destroyed while holding this
103 // lock, but rather when the method returns.
104 std::vector<ChannelOwner> references;
105 {
106 CriticalSectionScoped crit(lock_.get());
107 references = channels_;
108 channels_.clear();
109 }
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000110}
111
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000112size_t ChannelManager::NumOfChannels() const {
113 CriticalSectionScoped crit(lock_.get());
114 return channels_.size();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000115}
116
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000117ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
118 : iterator_pos_(0) {
119 channel_manager->GetAllChannels(&channels_);
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000120}
121
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000122Channel* ChannelManager::Iterator::GetChannel() {
123 if (iterator_pos_ < channels_.size())
124 return channels_[iterator_pos_].channel();
125 return NULL;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000126}
127
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000128bool ChannelManager::Iterator::IsValid() {
129 return iterator_pos_ < channels_.size();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000130}
131
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000132void ChannelManager::Iterator::Increment() {
133 ++iterator_pos_;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000134}
135
pbos@webrtc.org5ab7b932013-07-03 15:12:26 +0000136} // namespace voe
pbos@webrtc.org5ab7b932013-07-03 15:12:26 +0000137} // namespace webrtc