blob: 213a48f769d1bf2290c69fbd0e50139bcb83a22e [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"
ivoc9e03c3b2016-06-30 00:59:43 -070020#include "webrtc/call/rtc_event_log.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010021#include "webrtc/system_wrappers/include/atomic32.h"
pbos@webrtc.org956aa7e2013-05-21 13:52:32 +000022#include "webrtc/typedefs.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
26class Config;
ossu5f7cfa52016-05-30 08:11:28 -070027class AudioDecoderFactory;
minyue@webrtc.orge509f942013-09-12 17:03:00 +000028
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000029namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000030
niklase@google.com470e71d2011-07-07 08:21:25 +000031class Channel;
32
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000033// Shared-pointer implementation for keeping track of Channels. The underlying
34// shared instance will be dropped when no more ChannelOwners point to it.
35//
36// One common source of ChannelOwner instances are
37// ChannelManager::CreateChannel() and ChannelManager::GetChannel(...).
38// It has a similar use case to shared_ptr in C++11. Should this move to C++11
39// in the future, this class should be replaced by exactly that.
40//
41// To access the underlying Channel, use .channel().
42// IsValid() implements a convenience method as an alternative for checking
43// whether the underlying pointer is NULL or not.
44//
45// Channel channel_owner = channel_manager.GetChannel(channel_id);
46// if (channel_owner.IsValid())
47// channel_owner.channel()->...;
48//
49class ChannelOwner {
50 public:
51 explicit ChannelOwner(Channel* channel);
52 ChannelOwner(const ChannelOwner& channel_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +000053
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000054 ~ChannelOwner();
niklase@google.com470e71d2011-07-07 08:21:25 +000055
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000056 ChannelOwner& operator=(const ChannelOwner& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000057
Minyue2013aec2015-05-13 14:14:42 +020058 Channel* channel() const { return channel_ref_->channel.get(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000059 bool IsValid() { return channel_ref_->channel.get() != NULL; }
Minyue2013aec2015-05-13 14:14:42 +020060 int use_count() const { return channel_ref_->ref_count.Value(); }
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000061 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.
65 struct ChannelRef {
66 ChannelRef(Channel* channel);
kwibergb7f89d62016-02-17 10:04:18 -080067 const std::unique_ptr<Channel> channel;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000068 Atomic32 ref_count;
69 };
niklase@google.com470e71d2011-07-07 08:21:25 +000070
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000071 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000072};
73
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000074class ChannelManager {
75 public:
minyue@webrtc.orge509f942013-09-12 17:03:00 +000076 ChannelManager(uint32_t instance_id, const Config& config);
niklase@google.com470e71d2011-07-07 08:21:25 +000077
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000078 // Upon construction of an Iterator it will grab a copy of the channel list of
79 // the ChannelManager. The iteration will then occur over this state, not the
80 // current one of the ChannelManager. As the Iterator holds its own references
81 // to the Channels, they will remain valid even if they are removed from the
82 // ChannelManager.
83 class Iterator {
84 public:
85 explicit Iterator(ChannelManager* channel_manager);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000087 Channel* GetChannel();
88 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000089
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000090 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000091
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000092 private:
93 size_t iterator_pos_;
94 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000095
henrikg3c089d72015-09-16 05:37:44 -070096 RTC_DISALLOW_COPY_AND_ASSIGN(Iterator);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000097 };
98
turaj@webrtc.org03f33702013-11-13 00:02:48 +000099 // CreateChannel will always return a valid ChannelOwner instance. The channel
100 // is created either based on internal configuration, i.e. |config_|, by
101 // calling CreateChannel(), or using and external configuration
102 // |external_config| if the overloaded method
103 // CreateChannel(const Config& external_config) is called.
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000104 ChannelOwner CreateChannel();
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000105 ChannelOwner CreateChannel(const Config& external_config);
ossu5f7cfa52016-05-30 08:11:28 -0700106 ChannelOwner CreateChannel(
107 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory);
108 ChannelOwner CreateChannel(
109 const Config& external_config,
110 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory);
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000111
112 // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer
113 // exists. This should be checked with ChannelOwner::IsValid().
114 ChannelOwner GetChannel(int32_t channel_id);
115 void GetAllChannels(std::vector<ChannelOwner>* channels);
116
117 void DestroyChannel(int32_t channel_id);
118 void DestroyAllChannels();
119
120 size_t NumOfChannels() const;
121
ivoc9e03c3b2016-06-30 00:59:43 -0700122 // Returns a pointer to the event log object stored within the ChannelManager.
123 RtcEventLog* GetEventLog() const;
124
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000125 private:
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000126 // Create a channel given a configuration, |config|.
ossu5f7cfa52016-05-30 08:11:28 -0700127 ChannelOwner CreateChannelInternal(
128 const Config& config,
129 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory);
turaj@webrtc.org03f33702013-11-13 00:02:48 +0000130
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000131 uint32_t instance_id_;
132
133 Atomic32 last_channel_id_;
134
pbosd8de1152016-02-01 09:00:51 -0800135 rtc::CriticalSection lock_;
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +0000136 std::vector<ChannelOwner> channels_;
137
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000138 const Config& config_;
ivoc9e03c3b2016-06-30 00:59:43 -0700139 std::unique_ptr<RtcEventLog> event_log_;
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000140
henrikg3c089d72015-09-16 05:37:44 -0700141 RTC_DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000142};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000143} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000144} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
146#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H