blob: 4efeb15fc2e7b7c3c2ae440379b7e421a8a3be94 [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
16#include "webrtc/system_wrappers/interface/atomic32.h"
17#include "webrtc/system_wrappers/interface/constructor_magic.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/scoped_ptr.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 {
23namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
niklase@google.com470e71d2011-07-07 08:21:25 +000025class Channel;
26
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000027// Shared-pointer implementation for keeping track of Channels. The underlying
28// shared instance will be dropped when no more ChannelOwners point to it.
29//
30// One common source of ChannelOwner instances are
31// ChannelManager::CreateChannel() and ChannelManager::GetChannel(...).
32// It has a similar use case to shared_ptr in C++11. Should this move to C++11
33// in the future, this class should be replaced by exactly that.
34//
35// To access the underlying Channel, use .channel().
36// IsValid() implements a convenience method as an alternative for checking
37// whether the underlying pointer is NULL or not.
38//
39// Channel channel_owner = channel_manager.GetChannel(channel_id);
40// if (channel_owner.IsValid())
41// channel_owner.channel()->...;
42//
43class ChannelOwner {
44 public:
45 explicit ChannelOwner(Channel* channel);
46 ChannelOwner(const ChannelOwner& channel_owner);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000048 ~ChannelOwner();
niklase@google.com470e71d2011-07-07 08:21:25 +000049
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000050 ChannelOwner& operator=(const ChannelOwner& other);
niklase@google.com470e71d2011-07-07 08:21:25 +000051
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000052 Channel* channel() { return channel_ref_->channel.get(); }
53 bool IsValid() { return channel_ref_->channel.get() != NULL; }
54 private:
55 // Shared instance of a Channel. Copying ChannelOwners increase the reference
56 // count and destroying ChannelOwners decrease references. Channels are
57 // deleted when no references to them are held.
58 struct ChannelRef {
59 ChannelRef(Channel* channel);
60 const scoped_ptr<Channel> channel;
61 Atomic32 ref_count;
62 };
niklase@google.com470e71d2011-07-07 08:21:25 +000063
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000064 ChannelRef* channel_ref_;
niklase@google.com470e71d2011-07-07 08:21:25 +000065};
66
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000067class ChannelManager {
68 public:
69 ChannelManager(uint32_t instance_id);
niklase@google.com470e71d2011-07-07 08:21:25 +000070
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000071 // Upon construction of an Iterator it will grab a copy of the channel list of
72 // the ChannelManager. The iteration will then occur over this state, not the
73 // current one of the ChannelManager. As the Iterator holds its own references
74 // to the Channels, they will remain valid even if they are removed from the
75 // ChannelManager.
76 class Iterator {
77 public:
78 explicit Iterator(ChannelManager* channel_manager);
niklase@google.com470e71d2011-07-07 08:21:25 +000079
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000080 Channel* GetChannel();
81 bool IsValid();
niklase@google.com470e71d2011-07-07 08:21:25 +000082
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000083 void Increment();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000085 private:
86 size_t iterator_pos_;
87 std::vector<ChannelOwner> channels_;
niklase@google.com470e71d2011-07-07 08:21:25 +000088
pbos@webrtc.org676ff1e2013-08-07 17:57:36 +000089 DISALLOW_COPY_AND_ASSIGN(Iterator);
90 };
91
92 // CreateChannel will always return a valid ChannelOwner instance.
93 ChannelOwner CreateChannel();
94
95 // ChannelOwner.channel() will be NULL if channel_id is invalid or no longer
96 // exists. This should be checked with ChannelOwner::IsValid().
97 ChannelOwner GetChannel(int32_t channel_id);
98 void GetAllChannels(std::vector<ChannelOwner>* channels);
99
100 void DestroyChannel(int32_t channel_id);
101 void DestroyAllChannels();
102
103 size_t NumOfChannels() const;
104
105 private:
106 uint32_t instance_id_;
107
108 Atomic32 last_channel_id_;
109
110 scoped_ptr<CriticalSectionWrapper> lock_;
111 std::vector<ChannelOwner> channels_;
112
113 DISALLOW_COPY_AND_ASSIGN(ChannelManager);
niklase@google.com470e71d2011-07-07 08:21:25 +0000114};
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000115} // namespace voe
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000116} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
118#endif // WEBRTC_VOICE_ENGINE_CHANNEL_MANAGER_H