blob: 5f20b2ed162b2bd30752c86b2de04c56d3ab2d96 [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"
ossu5f7cfa52016-05-30 08:11:28 -070014#include "webrtc/modules/audio_coding/codecs/builtin_audio_decoder_factory.h"
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000015#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000016
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000017namespace webrtc {
18namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000019
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000020ChannelOwner::ChannelOwner(class Channel* channel)
21 : channel_ref_(new ChannelRef(channel)) {}
22
23ChannelOwner::ChannelOwner(const ChannelOwner& channel_owner)
24 : channel_ref_(channel_owner.channel_ref_) {
25 ++channel_ref_->ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000026}
27
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000028ChannelOwner::~ChannelOwner() {
29 if (--channel_ref_->ref_count == 0)
30 delete channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000033ChannelOwner& ChannelOwner::operator=(const ChannelOwner& other) {
34 if (other.channel_ref_ == channel_ref_)
35 return *this;
36
37 if (--channel_ref_->ref_count == 0)
38 delete channel_ref_;
39
40 channel_ref_ = other.channel_ref_;
41 ++channel_ref_->ref_count;
42
43 return *this;
niklase@google.com470e71d2011-07-07 08:21:25 +000044}
45
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000046ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
47 : channel(channel), ref_count(1) {}
48
minyue@webrtc.orge509f942013-09-12 17:03:00 +000049ChannelManager::ChannelManager(uint32_t instance_id, const Config& config)
ivoc14d5dbe2016-07-04 07:06:55 -070050 : instance_id_(instance_id), last_channel_id_(-1), config_(config) {}
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000051
52ChannelOwner ChannelManager::CreateChannel() {
ossu5f7cfa52016-05-30 08:11:28 -070053 return CreateChannel(CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org03f33702013-11-13 00:02:48 +000054}
55
56ChannelOwner ChannelManager::CreateChannel(const Config& external_config) {
ossu5f7cfa52016-05-30 08:11:28 -070057 return CreateChannel(external_config, CreateBuiltinAudioDecoderFactory());
turaj@webrtc.org03f33702013-11-13 00:02:48 +000058}
59
ossu5f7cfa52016-05-30 08:11:28 -070060ChannelOwner ChannelManager::CreateChannel(
61 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
62 return CreateChannelInternal(config_, decoder_factory);
63}
64
65ChannelOwner ChannelManager::CreateChannel(
66 const Config& external_config,
67 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
68 return CreateChannelInternal(external_config, decoder_factory);
69}
70
71ChannelOwner ChannelManager::CreateChannelInternal(
72 const Config& config,
73 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000074 Channel* channel;
ivoc14d5dbe2016-07-04 07:06:55 -070075 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config,
76 decoder_factory);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000077 ChannelOwner channel_owner(channel);
78
tommi31fc21f2016-01-21 10:37:37 -080079 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000080
81 channels_.push_back(channel_owner);
82
83 return channel_owner;
84}
85
86ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
tommi31fc21f2016-01-21 10:37:37 -080087 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000088
89 for (size_t i = 0; i < channels_.size(); ++i) {
90 if (channels_[i].channel()->ChannelId() == channel_id)
91 return channels_[i];
92 }
93 return ChannelOwner(NULL);
94}
95
96void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
tommi31fc21f2016-01-21 10:37:37 -080097 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000098
99 *channels = channels_;
100}
101
102void ChannelManager::DestroyChannel(int32_t channel_id) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000103 assert(channel_id >= 0);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000104 // Holds a reference to a channel, this is used so that we never delete
105 // Channels while holding a lock, but rather when the method returns.
106 ChannelOwner reference(NULL);
107 {
tommi31fc21f2016-01-21 10:37:37 -0800108 rtc::CritScope crit(&lock_);
Minyue2013aec2015-05-13 14:14:42 +0200109 std::vector<ChannelOwner>::iterator to_delete = channels_.end();
110 for (auto it = channels_.begin(); it != channels_.end(); ++it) {
111 Channel* channel = it->channel();
112 // For channels associated with the channel to be deleted, disassociate
113 // with that channel.
114 channel->DisassociateSendChannel(channel_id);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000115
Minyue2013aec2015-05-13 14:14:42 +0200116 if (channel->ChannelId() == channel_id) {
117 to_delete = it;
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000118 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 }
Minyue2013aec2015-05-13 14:14:42 +0200120 if (to_delete != channels_.end()) {
121 reference = *to_delete;
122 channels_.erase(to_delete);
123 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000124 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000125}
126
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000127void ChannelManager::DestroyAllChannels() {
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000128 // Holds references so that Channels are not destroyed while holding this
129 // lock, but rather when the method returns.
130 std::vector<ChannelOwner> references;
131 {
tommi31fc21f2016-01-21 10:37:37 -0800132 rtc::CritScope crit(&lock_);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000133 references = channels_;
134 channels_.clear();
135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136}
137
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000138size_t ChannelManager::NumOfChannels() const {
tommi31fc21f2016-01-21 10:37:37 -0800139 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000140 return channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000143ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
144 : iterator_pos_(0) {
145 channel_manager->GetAllChannels(&channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146}
147
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000148Channel* ChannelManager::Iterator::GetChannel() {
149 if (iterator_pos_ < channels_.size())
150 return channels_[iterator_pos_].channel();
151 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000152}
153
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000154bool ChannelManager::Iterator::IsValid() {
155 return iterator_pos_ < channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000156}
157
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000158void ChannelManager::Iterator::Increment() {
159 ++iterator_pos_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160}
161
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000162} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000163} // namespace webrtc