Line data Source code
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 :
11 : #include "webrtc/voice_engine/channel_manager.h"
12 :
13 : #include "webrtc/voice_engine/channel.h"
14 :
15 : namespace webrtc {
16 : namespace voe {
17 :
18 0 : ChannelOwner::ChannelOwner(class Channel* channel)
19 0 : : channel_ref_(new ChannelRef(channel)) {}
20 :
21 0 : ChannelOwner::ChannelOwner(const ChannelOwner& channel_owner)
22 0 : : channel_ref_(channel_owner.channel_ref_) {
23 0 : ++channel_ref_->ref_count;
24 0 : }
25 :
26 0 : ChannelOwner::~ChannelOwner() {
27 0 : if (--channel_ref_->ref_count == 0)
28 0 : delete channel_ref_;
29 0 : }
30 :
31 0 : ChannelOwner& ChannelOwner::operator=(const ChannelOwner& other) {
32 0 : if (other.channel_ref_ == channel_ref_)
33 0 : return *this;
34 :
35 0 : if (--channel_ref_->ref_count == 0)
36 0 : delete channel_ref_;
37 :
38 0 : channel_ref_ = other.channel_ref_;
39 0 : ++channel_ref_->ref_count;
40 :
41 0 : return *this;
42 : }
43 :
44 0 : ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
45 0 : : channel(channel), ref_count(1) {}
46 :
47 0 : ChannelManager::ChannelManager(uint32_t instance_id)
48 0 : : instance_id_(instance_id), last_channel_id_(-1) {}
49 :
50 0 : ChannelOwner ChannelManager::CreateChannel(
51 : const VoEBase::ChannelConfig& config) {
52 : Channel* channel;
53 0 : Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config);
54 0 : ChannelOwner channel_owner(channel);
55 :
56 0 : rtc::CritScope crit(&lock_);
57 :
58 0 : channels_.push_back(channel_owner);
59 :
60 0 : return channel_owner;
61 : }
62 :
63 0 : ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
64 0 : rtc::CritScope crit(&lock_);
65 :
66 0 : for (size_t i = 0; i < channels_.size(); ++i) {
67 0 : if (channels_[i].channel()->ChannelId() == channel_id)
68 0 : return channels_[i];
69 : }
70 0 : return ChannelOwner(NULL);
71 : }
72 :
73 0 : void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
74 0 : rtc::CritScope crit(&lock_);
75 :
76 0 : *channels = channels_;
77 0 : }
78 :
79 0 : void ChannelManager::DestroyChannel(int32_t channel_id) {
80 0 : assert(channel_id >= 0);
81 : // 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 0 : ChannelOwner reference(NULL);
84 : {
85 0 : rtc::CritScope crit(&lock_);
86 0 : std::vector<ChannelOwner>::iterator to_delete = channels_.end();
87 0 : for (auto it = channels_.begin(); it != channels_.end(); ++it) {
88 0 : Channel* channel = it->channel();
89 : // For channels associated with the channel to be deleted, disassociate
90 : // with that channel.
91 0 : channel->DisassociateSendChannel(channel_id);
92 :
93 0 : if (channel->ChannelId() == channel_id) {
94 0 : to_delete = it;
95 : }
96 : }
97 0 : if (to_delete != channels_.end()) {
98 0 : reference = *to_delete;
99 0 : channels_.erase(to_delete);
100 : }
101 : }
102 0 : }
103 :
104 0 : void ChannelManager::DestroyAllChannels() {
105 : // Holds references so that Channels are not destroyed while holding this
106 : // lock, but rather when the method returns.
107 0 : std::vector<ChannelOwner> references;
108 : {
109 0 : rtc::CritScope crit(&lock_);
110 0 : references = channels_;
111 0 : channels_.clear();
112 : }
113 0 : }
114 :
115 0 : size_t ChannelManager::NumOfChannels() const {
116 0 : rtc::CritScope crit(&lock_);
117 0 : return channels_.size();
118 : }
119 :
120 0 : ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
121 0 : : iterator_pos_(0) {
122 0 : channel_manager->GetAllChannels(&channels_);
123 0 : }
124 :
125 0 : Channel* ChannelManager::Iterator::GetChannel() {
126 0 : if (iterator_pos_ < channels_.size())
127 0 : return channels_[iterator_pos_].channel();
128 0 : return NULL;
129 : }
130 :
131 0 : bool ChannelManager::Iterator::IsValid() {
132 0 : return iterator_pos_ < channels_.size();
133 : }
134 :
135 0 : void ChannelManager::Iterator::Increment() {
136 0 : ++iterator_pos_;
137 0 : }
138 :
139 : } // namespace voe
140 : } // namespace webrtc
|