blob: ae16aca2b878aa8c6782c72294dd924abb8596e6 [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
kwibergb7f89d62016-02-17 10:04:18 -080014#include <memory>
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000015#include <vector>
16
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000017#include "webrtc/base/constructormagic.h"
tommi31fc21f2016-01-21 10:37:37 -080018#include "webrtc/base/criticalsection.h"
ossu5f7cfa52016-05-30 08:11:28 -070019#include "webrtc/base/scoped_ref_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/atomic32.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000021#include "webrtc/typedefs.h"
solenberg88499ec2016-09-07 07:34:41 -070022#include "webrtc/voice_engine/include/voe_base.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000024namespace webrtc {
minyue@webrtc.orge509f942013-09-12 17:03:00 +000025
ossu5f7cfa52016-05-30 08:11:28 -070026class AudioDecoderFactory;
minyue@webrtc.orge509f942013-09-12 17:03:00 +000027
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000028namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
niklase@google.com470e71d2011-07-07 08:21:25 +000030class Channel;
31
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000032// Shared-pointer implementation for keeping track of Channels. The underlying
33// shared instance will be dropped when no more ChannelOwners point to it.
34//
35// One common source of ChannelOwner instances are
36// ChannelManager::CreateChannel() and ChannelManager::GetChannel(...).
37// It has a similar use case to shared_ptr in C++11. Should this move to C++11
38// in the future, this class should be replaced by exactly that.
39//
40// To access the underlying Channel, use .channel().
41// IsValid() implements a convenience method as an alternative for checking
42// whether the underlying pointer is NULL or not.
43//
44// Channel channel_owner = channel_manager.GetChannel(channel_id);
45// if (channel_owner.IsValid())
46// channel_owner.channel()->...;
47//
48class ChannelOwner {
49 public:
50 explicit ChannelOwner(Channel* channel);
51 ChannelOwner(const ChannelOwner& channel_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +000052
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000053 ~ChannelOwner();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000055 ChannelOwner& operator=(const ChannelOwner& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
Minyue2013aec2015-05-13 14:14:42 +020057 Channel* channel() const { return channel_ref_->channel.get(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000058 bool IsValid() { return channel_ref_->channel.get() != NULL; }
Minyue2013aec2015-05-13 14:14:42 +020059 int use_count() const { return channel_ref_->ref_count.Value(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000060 private:
61 // Shared instance of a Channel. Copying ChannelOwners increase the reference
62 // count and destroying ChannelOwners decrease references. Channels are
63 // deleted when no references to them are held.
64 struct ChannelRef {
65 ChannelRef(Channel* channel);
kwibergb7f89d62016-02-17 10:04:18 -080066 const std::unique_ptr<Channel> channel;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000067 Atomic32 ref_count;
68 };
niklase@google.com470e71d2011-07-07 08:21:25 +000069
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000070 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000071};
72
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000073class ChannelManager {
74 public:
solenberg88499ec2016-09-07 07:34:41 -070075 ChannelManager(uint32_t instance_id);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000077 // 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.com470e71d2011-07-07 08:21:25 +000085
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000086 Channel* GetChannel();
87 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000089 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000090
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000091 private:
92 size_t iterator_pos_;
93 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000094
henrikg3c089d72015-09-16 05:37:44 -070095 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000096 };
97
solenberg88499ec2016-09-07 07:34:41 -070098 // CreateChannel will always return a valid ChannelOwner instance.
99 ChannelOwner CreateChannel(const VoEBase::ChannelConfig& config);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000100
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
pbosd8de1152016-02-01 09:00:51 -0800116 rtc::CriticalSection lock_;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000117 std::vector<ChannelOwner> channels_;
118
henrikg3c089d72015-09-16 05:37:44 -0700119 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000121} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000122} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
124#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H