blob: 07cebb0327fb86b90edccf8637c5495c264d2591 [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
11#ifndef WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H
12#define WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H
13
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000014#include <vector>
15
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000016#include "webrtc/base/constructormagic.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000017#include "webrtc/base/scoped_ptr.h"
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000018#include "webrtc/system_wrappers/interface/atomic32.h"
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000019#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000020#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000021
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000022namespace webrtc {
minyue@webrtc.orge509f942013-09-12 17:03:00 +000023
24class Config;
25
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000026namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000027
niklase@google.com470e71d2011-07-07 08:21:25 +000028class Channel;
29
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000030// Shared-pointer implementation for keeping track of Channels. The underlying
31// shared instance will be dropped when no more ChannelOwners point to it.
32//
33// One common source of ChannelOwner instances are
34// ChannelManager::CreateChannel() and ChannelManager::GetChannel(...).
35// It has a similar use case to shared_ptr in C++11. Should this move to C++11
36// in the future, this class should be replaced by exactly that.
37//
38// To access the underlying Channel, use .channel().
39// IsValid() implements a convenience method as an alternative for checking
40// whether the underlying pointer is NULL or not.
41//
42// Channel channel_owner = channel_manager.GetChannel(channel_id);
43// if (channel_owner.IsValid())
44// channel_owner.channel()->...;
45//
46class ChannelOwner {
47 public:
48 explicit ChannelOwner(Channel* channel);
49 ChannelOwner(const ChannelOwner& channel_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +000050
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000051 ~ChannelOwner();
niklase@google.com470e71d2011-07-07 08:21:25 +000052
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000053 ChannelOwner& operator=(const ChannelOwner& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000054
Minyue2013aec2015-05-13 14:14:42 +020055 Channel* channel() const { return channel_ref_->channel.get(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000056 bool IsValid() { return channel_ref_->channel.get() != NULL; }
Minyue2013aec2015-05-13 14:14:42 +020057 int use_count() const { return channel_ref_->ref_count.Value(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000058 private:
59 // Shared instance of a Channel. Copying ChannelOwners increase the reference
60 // count and destroying ChannelOwners decrease references. Channels are
61 // deleted when no references to them are held.
62 struct ChannelRef {
63 ChannelRef(Channel* channel);
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000064 const rtc::scoped_ptr<Channel> channel;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000065 Atomic32 ref_count;
66 };
niklase@google.com470e71d2011-07-07 08:21:25 +000067
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000068 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000069};
70
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000071class ChannelManager {
72 public:
minyue@webrtc.orge509f942013-09-12 17:03:00 +000073 ChannelManager(uint32_t instance_id, const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000075 // Upon construction of an Iterator it will grab a copy of the channel list of
76 // the ChannelManager. The iteration will then occur over this state, not the
77 // current one of the ChannelManager. As the Iterator holds its own references
78 // to the Channels, they will remain valid even if they are removed from the
79 // ChannelManager.
80 class Iterator {
81 public:
82 explicit Iterator(ChannelManager* channel_manager);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000084 Channel* GetChannel();
85 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000087 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000089 private:
90 size_t iterator_pos_;
91 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000092
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000093 DISALLOW_COPY_AND_ASSIGN(Iterator);
94 };
95
turaj@webrtc.org03f33702013-11-13 00:02:48 +000096 // CreateChannel will always return a valid ChannelOwner instance. The channel
97 // is created either based on internal configuration, i.e. |config_|, by
98 // calling CreateChannel(), or using and external configuration
99 // |external_config| if the overloaded method
100 // CreateChannel(const Config& external_config) is called.
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000101 ChannelOwner CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000102 ChannelOwner CreateChannel(const Config& external_config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000103
104 // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer
105 // exists. This should be checked with ChannelOwner::IsValid().
106 ChannelOwner GetChannel(int32_t channel_id);
107 void GetAllChannels(std::vector<ChannelOwner>* channels);
108
109 void DestroyChannel(int32_t channel_id);
110 void DestroyAllChannels();
111
112 size_t NumOfChannels() const;
113
114 private:
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000115 // Create a channel given a configuration, |config|.
116 ChannelOwner CreateChannelInternal(const Config& config);
117
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000118 uint32_t instance_id_;
119
120 Atomic32 last_channel_id_;
121
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000122 rtc::scoped_ptr<CriticalSectionWrapper> lock_;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000123 std::vector<ChannelOwner> channels_;
124
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000125 const Config& config_;
126
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000127 DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000129} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000130} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000131
132#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H