blob: 4c0cede483f29486028ec39e8237908c9168a6cc [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"
Peter Boström5c389d32015-09-25 13:58:30 +020018#include "webrtc/call/rtc_event_log.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010019#include "webrtc/system_wrappers/include/atomic32.h"
20#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000023namespace webrtc {
minyue@webrtc.orge509f942013-09-12 17:03:00 +000024
25class Config;
26
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000027namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000028
niklase@google.com470e71d2011-07-07 08:21:25 +000029class Channel;
30
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000031// Shared-pointer implementation for keeping track of Channels. The underlying
32// shared instance will be dropped when no more ChannelOwners point to it.
33//
34// One common source of ChannelOwner instances are
35// ChannelManager::CreateChannel() and ChannelManager::GetChannel(...).
36// It has a similar use case to shared_ptr in C++11. Should this move to C++11
37// in the future, this class should be replaced by exactly that.
38//
39// To access the underlying Channel, use .channel().
40// IsValid() implements a convenience method as an alternative for checking
41// whether the underlying pointer is NULL or not.
42//
43// Channel channel_owner = channel_manager.GetChannel(channel_id);
44// if (channel_owner.IsValid())
45// channel_owner.channel()->...;
46//
47class ChannelOwner {
48 public:
49 explicit ChannelOwner(Channel* channel);
50 ChannelOwner(const ChannelOwner& channel_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000052 ~ChannelOwner();
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000054 ChannelOwner& operator=(const ChannelOwner& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000055
Minyue2013aec2015-05-13 14:14:42 +020056 Channel* channel() const { return channel_ref_->channel.get(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000057 bool IsValid() { return channel_ref_->channel.get() != NULL; }
Minyue2013aec2015-05-13 14:14:42 +020058 int use_count() const { return channel_ref_->ref_count.Value(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000059 private:
60 // Shared instance of a Channel. Copying ChannelOwners increase the reference
61 // count and destroying ChannelOwners decrease references. Channels are
62 // deleted when no references to them are held.
63 struct ChannelRef {
64 ChannelRef(Channel* channel);
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000065 const rtc::scoped_ptr<Channel> channel;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000066 Atomic32 ref_count;
67 };
niklase@google.com470e71d2011-07-07 08:21:25 +000068
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000069 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000070};
71
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000072class ChannelManager {
73 public:
minyue@webrtc.orge509f942013-09-12 17:03:00 +000074 ChannelManager(uint32_t instance_id, const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000076 // Upon construction of an Iterator it will grab a copy of the channel list of
77 // the ChannelManager. The iteration will then occur over this state, not the
78 // current one of the ChannelManager. As the Iterator holds its own references
79 // to the Channels, they will remain valid even if they are removed from the
80 // ChannelManager.
81 class Iterator {
82 public:
83 explicit Iterator(ChannelManager* channel_manager);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000085 Channel* GetChannel();
86 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000088 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000089
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000090 private:
91 size_t iterator_pos_;
92 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000093
henrikg3c089d72015-09-16 05:37:44 -070094 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000095 };
96
turaj@webrtc.org03f33702013-11-13 00:02:48 +000097 // CreateChannel will always return a valid ChannelOwner instance. The channel
98 // is created either based on internal configuration, i.e. |config_|, by
99 // calling CreateChannel(), or using and external configuration
100 // |external_config| if the overloaded method
101 // CreateChannel(const Config& external_config) is called.
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000102 ChannelOwner CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000103 ChannelOwner CreateChannel(const Config& external_config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000104
105 // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer
106 // exists. This should be checked with ChannelOwner::IsValid().
107 ChannelOwner GetChannel(int32_t channel_id);
108 void GetAllChannels(std::vector<ChannelOwner>* channels);
109
110 void DestroyChannel(int32_t channel_id);
111 void DestroyAllChannels();
112
113 size_t NumOfChannels() const;
114
ivocb04965c2015-09-09 00:09:43 -0700115 // Returns a pointer to the event log object stored within the ChannelManager.
116 RtcEventLog* GetEventLog() const;
117
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000118 private:
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000119 // Create a channel given a configuration, |config|.
120 ChannelOwner CreateChannelInternal(const Config& config);
121
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000122 uint32_t instance_id_;
123
124 Atomic32 last_channel_id_;
125
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000126 rtc::scoped_ptr<CriticalSectionWrapper> lock_;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000127 std::vector<ChannelOwner> channels_;
128
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000129 const Config& config_;
ivocb04965c2015-09-09 00:09:43 -0700130 rtc::scoped_ptr<RtcEventLog> event_log_;
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000131
henrikg3c089d72015-09-16 05:37:44 -0700132 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000134} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000135} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000136
137#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H