niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef VOICE_ENGINE_CHANNEL_MANAGER_H_ |
| 12 | #define VOICE_ENGINE_CHANNEL_MANAGER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 14 | #include <memory> |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 17 | #include "api/refcountedbase.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/constructormagic.h" |
| 19 | #include "rtc_base/criticalsection.h" |
| 20 | #include "rtc_base/random.h" |
| 21 | #include "rtc_base/scoped_ref_ptr.h" |
| 22 | #include "system_wrappers/include/atomic32.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 23 | #include "typedefs.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "voice_engine/include/voe_base.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 26 | namespace webrtc { |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 27 | |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 28 | class AudioDecoderFactory; |
minyue@webrtc.org | e509f94 | 2013-09-12 17:03:00 +0000 | [diff] [blame] | 29 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 30 | namespace voe { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | class Channel; |
| 33 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 34 | // Shared-pointer implementation for keeping track of Channels. The underlying |
| 35 | // shared instance will be dropped when no more ChannelOwners point to it. |
| 36 | // |
| 37 | // One common source of ChannelOwner instances are |
| 38 | // ChannelManager::CreateChannel() and ChannelManager::GetChannel(...). |
| 39 | // It has a similar use case to shared_ptr in C++11. Should this move to C++11 |
| 40 | // in the future, this class should be replaced by exactly that. |
| 41 | // |
| 42 | // To access the underlying Channel, use .channel(). |
| 43 | // IsValid() implements a convenience method as an alternative for checking |
| 44 | // whether the underlying pointer is NULL or not. |
| 45 | // |
| 46 | // Channel channel_owner = channel_manager.GetChannel(channel_id); |
| 47 | // if (channel_owner.IsValid()) |
| 48 | // channel_owner.channel()->...; |
| 49 | // |
| 50 | class ChannelOwner { |
| 51 | public: |
| 52 | explicit ChannelOwner(Channel* channel); |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 53 | ChannelOwner(const ChannelOwner& channel_owner) = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 55 | ~ChannelOwner() = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 57 | ChannelOwner& operator=(const ChannelOwner& other) = default; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 58 | |
Minyue | 2013aec | 2015-05-13 14:14:42 +0200 | [diff] [blame] | 59 | Channel* channel() const { return channel_ref_->channel.get(); } |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 60 | bool IsValid() { return channel_ref_->channel.get() != NULL; } |
| 61 | private: |
| 62 | // Shared instance of a Channel. Copying ChannelOwners increase the reference |
| 63 | // count and destroying ChannelOwners decrease references. Channels are |
| 64 | // deleted when no references to them are held. |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 65 | struct ChannelRef : public rtc::RefCountedBase { |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 66 | ChannelRef(Channel* channel); |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 67 | const std::unique_ptr<Channel> channel; |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 68 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 69 | |
Niels Möller | f92d871 | 2017-10-23 14:26:10 +0200 | [diff] [blame] | 70 | rtc::scoped_refptr<ChannelRef> channel_ref_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 73 | class ChannelManager { |
| 74 | public: |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 75 | ChannelManager(uint32_t instance_id); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 77 | // Upon construction of an Iterator it will grab a copy of the channel list of |
| 78 | // the ChannelManager. The iteration will then occur over this state, not the |
| 79 | // current one of the ChannelManager. As the Iterator holds its own references |
| 80 | // to the Channels, they will remain valid even if they are removed from the |
| 81 | // ChannelManager. |
| 82 | class Iterator { |
| 83 | public: |
| 84 | explicit Iterator(ChannelManager* channel_manager); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 86 | Channel* GetChannel(); |
| 87 | bool IsValid(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 89 | void Increment(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 90 | |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 91 | private: |
| 92 | size_t iterator_pos_; |
| 93 | std::vector<ChannelOwner> channels_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 95 | RTC_DISALLOW_COPY_AND_ASSIGN(Iterator); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 98 | // CreateChannel will always return a valid ChannelOwner instance. |
| 99 | ChannelOwner CreateChannel(const VoEBase::ChannelConfig& config); |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 100 | |
| 101 | // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer |
| 102 | // exists. This should be checked with ChannelOwner::IsValid(). |
| 103 | ChannelOwner GetChannel(int32_t channel_id); |
| 104 | void GetAllChannels(std::vector<ChannelOwner>* channels); |
| 105 | |
| 106 | void DestroyChannel(int32_t channel_id); |
| 107 | void DestroyAllChannels(); |
| 108 | |
| 109 | size_t NumOfChannels() const; |
| 110 | |
| 111 | private: |
| 112 | uint32_t instance_id_; |
| 113 | |
| 114 | Atomic32 last_channel_id_; |
| 115 | |
pbos | d8de115 | 2016-02-01 09:00:51 -0800 | [diff] [blame] | 116 | rtc::CriticalSection lock_; |
pbos@webrtc.org | 676ff1e | 2013-08-07 17:57:36 +0000 | [diff] [blame] | 117 | std::vector<ChannelOwner> channels_; |
| 118 | |
nisse | 7d59f6b | 2017-02-21 03:40:24 -0800 | [diff] [blame] | 119 | // For generation of random ssrc:s. |
| 120 | webrtc::Random random_; |
| 121 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 122 | RTC_DISALLOW_COPY_AND_ASSIGN(ChannelManager); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | }; |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 124 | } // namespace voe |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 125 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 127 | #endif // VOICE_ENGINE_CHANNEL_MANAGER_H_ |