blob: eae014ed09c45f96bf60d4be16af9a5e1faa9471 [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
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000055 Channel* channel() { return channel_ref_->channel.get(); }
56 bool IsValid() { return channel_ref_->channel.get() != NULL; }
57 private:
58 // Shared instance of a Channel. Copying ChannelOwners increase the reference
59 // count and destroying ChannelOwners decrease references. Channels are
60 // deleted when no references to them are held.
61 struct ChannelRef {
62 ChannelRef(Channel* channel);
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000063 const rtc::scoped_ptr<Channel> channel;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000064 Atomic32 ref_count;
65 };
niklase@google.com470e71d2011-07-07 08:21:25 +000066
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000067 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000068};
69
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000070class ChannelManager {
71 public:
minyue@webrtc.orge509f942013-09-12 17:03:00 +000072 ChannelManager(uint32_t instance_id, const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000073
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000074 // Upon construction of an Iterator it will grab a copy of the channel list of
75 // the ChannelManager. The iteration will then occur over this state, not the
76 // current one of the ChannelManager. As the Iterator holds its own references
77 // to the Channels, they will remain valid even if they are removed from the
78 // ChannelManager.
79 class Iterator {
80 public:
81 explicit Iterator(ChannelManager* channel_manager);
niklase@google.com470e71d2011-07-07 08:21:25 +000082
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000083 Channel* GetChannel();
84 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000085
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000086 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000088 private:
89 size_t iterator_pos_;
90 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000091
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000092 DISALLOW_COPY_AND_ASSIGN(Iterator);
93 };
94
turaj@webrtc.org03f33702013-11-13 00:02:48 +000095 // CreateChannel will always return a valid ChannelOwner instance. The channel
96 // is created either based on internal configuration, i.e. |config_|, by
97 // calling CreateChannel(), or using and external configuration
98 // |external_config| if the overloaded method
99 // CreateChannel(const Config& external_config) is called.
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000100 ChannelOwner CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000101 ChannelOwner CreateChannel(const Config& external_config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000102
103 // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer
104 // exists. This should be checked with ChannelOwner::IsValid().
105 ChannelOwner GetChannel(int32_t channel_id);
106 void GetAllChannels(std::vector<ChannelOwner>* channels);
107
108 void DestroyChannel(int32_t channel_id);
109 void DestroyAllChannels();
110
111 size_t NumOfChannels() const;
112
113 private:
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000114 // Create a channel given a configuration, |config|.
115 ChannelOwner CreateChannelInternal(const Config& config);
116
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000117 uint32_t instance_id_;
118
119 Atomic32 last_channel_id_;
120
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000121 rtc::scoped_ptr<CriticalSectionWrapper> lock_;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000122 std::vector<ChannelOwner> channels_;
123
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000124 const Config& config_;
125
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000126 DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000127};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000128} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000129} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
131#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H