blob: 9a82d2445f3fae4bca9b781d066a0d071feaf092 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "voice_engine/channel_manager.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/timeutils.h"
14#include "voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000016namespace webrtc {
17namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000018
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000019ChannelOwner::ChannelOwner(class Channel* channel)
20 : channel_ref_(new ChannelRef(channel)) {}
21
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000022ChannelOwner::ChannelRef::ChannelRef(class Channel* channel)
Niels Möllerf92d8712017-10-23 14:26:10 +020023 : channel(channel) {}
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000024
solenberg88499ec2016-09-07 07:34:41 -070025ChannelManager::ChannelManager(uint32_t instance_id)
nisse7d59f6b2017-02-21 03:40:24 -080026 : instance_id_(instance_id),
27 last_channel_id_(-1),
28 random_(rtc::TimeNanos()) {}
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000029
ossu5f7cfa52016-05-30 08:11:28 -070030ChannelOwner ChannelManager::CreateChannel(
solenberg88499ec2016-09-07 07:34:41 -070031 const VoEBase::ChannelConfig& config) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000032 Channel* channel;
solenberg88499ec2016-09-07 07:34:41 -070033 Channel::CreateChannel(channel, ++last_channel_id_, instance_id_, config);
nisse7d59f6b2017-02-21 03:40:24 -080034 // TODO(solenberg): Delete this, users should configure ssrc
35 // explicitly.
36 channel->SetLocalSSRC(random_.Rand<uint32_t>());
37
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000038 ChannelOwner channel_owner(channel);
39
tommi31fc21f2016-01-21 10:37:37 -080040 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000041
42 channels_.push_back(channel_owner);
43
44 return channel_owner;
45}
46
47ChannelOwner ChannelManager::GetChannel(int32_t channel_id) {
tommi31fc21f2016-01-21 10:37:37 -080048 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000049
50 for (size_t i = 0; i < channels_.size(); ++i) {
51 if (channels_[i].channel()->ChannelId() == channel_id)
52 return channels_[i];
53 }
54 return ChannelOwner(NULL);
55}
56
57void ChannelManager::GetAllChannels(std::vector<ChannelOwner>* channels) {
tommi31fc21f2016-01-21 10:37:37 -080058 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000059
60 *channels = channels_;
61}
62
63void ChannelManager::DestroyChannel(int32_t channel_id) {
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000064 assert(channel_id >= 0);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000065 // Holds a reference to a channel, this is used so that we never delete
66 // Channels while holding a lock, but rather when the method returns.
67 ChannelOwner reference(NULL);
68 {
tommi31fc21f2016-01-21 10:37:37 -080069 rtc::CritScope crit(&lock_);
Minyue2013aec2015-05-13 14:14:42 +020070 std::vector<ChannelOwner>::iterator to_delete = channels_.end();
71 for (auto it = channels_.begin(); it != channels_.end(); ++it) {
72 Channel* channel = it->channel();
73 // For channels associated with the channel to be deleted, disassociate
74 // with that channel.
75 channel->DisassociateSendChannel(channel_id);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000076
Minyue2013aec2015-05-13 14:14:42 +020077 if (channel->ChannelId() == channel_id) {
78 to_delete = it;
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000079 }
niklase@google.com470e71d2011-07-07 08:21:25 +000080 }
Minyue2013aec2015-05-13 14:14:42 +020081 if (to_delete != channels_.end()) {
82 reference = *to_delete;
83 channels_.erase(to_delete);
84 }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000085 }
tommi0a2391f2017-03-21 02:31:51 -070086 if (reference.channel()) {
87 // Ensure the channel is torn down now, on this thread, since a reference
88 // may still be held on a different thread (e.g. in the audio capture
89 // thread).
90 reference.channel()->Terminate();
91 }
niklase@google.com470e71d2011-07-07 08:21:25 +000092}
93
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000094void ChannelManager::DestroyAllChannels() {
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +000095 // Holds references so that Channels are not destroyed while holding this
96 // lock, but rather when the method returns.
97 std::vector<ChannelOwner> references;
98 {
tommi31fc21f2016-01-21 10:37:37 -080099 rtc::CritScope crit(&lock_);
pbos@webrtc.org58d76cb2013-08-08 17:32:21 +0000100 references = channels_;
101 channels_.clear();
102 }
tommi0a2391f2017-03-21 02:31:51 -0700103 for (auto& owner : references) {
104 if (owner.channel())
105 owner.channel()->Terminate();
106 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000107}
108
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000109size_t ChannelManager::NumOfChannels() const {
tommi31fc21f2016-01-21 10:37:37 -0800110 rtc::CritScope crit(&lock_);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000111 return channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000112}
113
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000114ChannelManager::Iterator::Iterator(ChannelManager* channel_manager)
115 : iterator_pos_(0) {
116 channel_manager->GetAllChannels(&channels_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000117}
118
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000119Channel* ChannelManager::Iterator::GetChannel() {
120 if (iterator_pos_ < channels_.size())
121 return channels_[iterator_pos_].channel();
122 return NULL;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123}
124
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000125bool ChannelManager::Iterator::IsValid() {
126 return iterator_pos_ < channels_.size();
niklase@google.com470e71d2011-07-07 08:21:25 +0000127}
128
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000129void ChannelManager::Iterator::Increment() {
130 ++iterator_pos_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000131}
132
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000133} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000134} // namespace webrtc