blob: 6cf935d16b26ab6bb34cccaa924544c8e5baddf7 [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
47ChannelManager::ChannelManager(uint32_t instance_id)
48 : instance_id_(instance_id),
49 last_channel_id_(-1),
50 lock_(CriticalSectionWrapper::CreateCriticalSection()) {}
51
52ChannelOwner 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
64ChannelOwner 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
74void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
75 CriticalSectionScoped crit(lock_.get());
76
77 *channels = channels_;
78}
79
80void 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.orga7b57da2012-10-22 18:19:23 +000090 }
pbos@webrtc.org9277c942013-08-07 17:57:36 +000091 }
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000092}
93
pbos@webrtc.org9277c942013-08-07 17:57:36 +000094void ChannelManager::DestroyAllChannels() {
95 CriticalSectionScoped crit(lock_.get());
96 channels_.clear();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000097}
98
pbos@webrtc.org9277c942013-08-07 17:57:36 +000099size_t ChannelManager::NumOfChannels() const {
100 CriticalSectionScoped crit(lock_.get());
101 return channels_.size();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000102}
103
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000104ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
105 : iterator_pos_(0) {
106 channel_manager->GetAllChannels(&channels_);
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000107}
108
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000109Channel* ChannelManager::Iterator::GetChannel() {
110 if (iterator_pos_ < channels_.size())
111 return channels_[iterator_pos_].channel();
112 return NULL;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000113}
114
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000115bool ChannelManager::Iterator::IsValid() {
116 return iterator_pos_ < channels_.size();
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000117}
118
pbos@webrtc.org9277c942013-08-07 17:57:36 +0000119void ChannelManager::Iterator::Increment() {
120 ++iterator_pos_;
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000121}
122
pbos@webrtc.org5ab7b932013-07-03 15:12:26 +0000123} // namespace voe
pbos@webrtc.org5ab7b932013-07-03 15:12:26 +0000124} // namespace webrtc