andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
pbos@webrtc.org | be72fe4 | 2013-05-21 13:52:32 +0000 | [diff] [blame] | 11 | #include "webrtc/voice_engine/channel.h" |
| 12 | #include "webrtc/voice_engine/channel_manager.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 13 | |
| 14 | namespace webrtc |
| 15 | { |
| 16 | |
| 17 | namespace voe |
| 18 | { |
| 19 | |
pbos@webrtc.org | 07a1c11 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 20 | ChannelManager::ChannelManager(uint32_t instanceId) : |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 21 | ChannelManagerBase(), |
| 22 | _instanceId(instanceId) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | ChannelManager::~ChannelManager() |
| 27 | { |
| 28 | ChannelManagerBase::DestroyAllItems(); |
| 29 | } |
| 30 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 31 | bool ChannelManager::CreateChannel(int32_t& channelId) |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 32 | { |
| 33 | return ChannelManagerBase::CreateItem(channelId); |
| 34 | } |
| 35 | |
pbos@webrtc.org | 07a1c11 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 36 | int32_t ChannelManager::DestroyChannel(int32_t channelId) |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 37 | { |
| 38 | Channel* deleteChannel = |
| 39 | static_cast<Channel*> (ChannelManagerBase::RemoveItem(channelId)); |
| 40 | if (!deleteChannel) |
| 41 | { |
| 42 | return -1; |
| 43 | } |
| 44 | delete deleteChannel; |
| 45 | return 0; |
| 46 | } |
| 47 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 48 | int32_t ChannelManager::NumOfChannels() const |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 49 | { |
| 50 | return ChannelManagerBase::NumOfItems(); |
| 51 | } |
| 52 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 53 | int32_t ChannelManager::MaxNumOfChannels() const |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 54 | { |
| 55 | return ChannelManagerBase::MaxNumOfItems(); |
| 56 | } |
| 57 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 58 | void* ChannelManager::NewItem(int32_t itemID) |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 59 | { |
| 60 | Channel* channel; |
| 61 | if (Channel::CreateChannel(channel, itemID, _instanceId) == -1) |
| 62 | { |
| 63 | return NULL; |
| 64 | } |
| 65 | return static_cast<void*> (channel); |
| 66 | } |
| 67 | |
| 68 | void ChannelManager::DeleteItem(void* item) |
| 69 | { |
| 70 | Channel* deleteItem = static_cast<Channel*> (item); |
| 71 | delete deleteItem; |
| 72 | } |
| 73 | |
pbos@webrtc.org | 07a1c11 | 2013-05-14 08:31:39 +0000 | [diff] [blame] | 74 | Channel* ChannelManager::GetChannel(int32_t channelId) const |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 75 | { |
| 76 | return static_cast<Channel*> (ChannelManagerBase::GetItem(channelId)); |
| 77 | } |
| 78 | |
| 79 | void ChannelManager::ReleaseChannel() |
| 80 | { |
| 81 | ChannelManagerBase::ReleaseItem(); |
| 82 | } |
| 83 | |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 84 | void ChannelManager::GetChannelIds(int32_t* channelsArray, |
| 85 | int32_t& numOfChannels) const |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 86 | { |
| 87 | ChannelManagerBase::GetItemIds(channelsArray, numOfChannels); |
| 88 | } |
| 89 | |
| 90 | void ChannelManager::GetChannels(MapWrapper& channels) const |
| 91 | { |
| 92 | ChannelManagerBase::GetChannels(channels); |
| 93 | } |
| 94 | |
| 95 | ScopedChannel::ScopedChannel(ChannelManager& chManager) : |
| 96 | _chManager(chManager), |
| 97 | _channelPtr(NULL) |
| 98 | { |
| 99 | // Copy all existing channels to the local map. |
| 100 | // It is not possible to utilize the ChannelPtr() API after |
| 101 | // this constructor. The intention is that this constructor |
| 102 | // is used in combination with the scoped iterator. |
| 103 | _chManager.GetChannels(_channels); |
| 104 | } |
| 105 | |
| 106 | ScopedChannel::ScopedChannel(ChannelManager& chManager, |
pbos@webrtc.org | 1d46b92 | 2013-04-09 10:09:10 +0000 | [diff] [blame] | 107 | int32_t channelId) : |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 108 | _chManager(chManager), |
| 109 | _channelPtr(NULL) |
| 110 | { |
| 111 | _channelPtr = _chManager.GetChannel(channelId); |
| 112 | } |
| 113 | |
| 114 | ScopedChannel::~ScopedChannel() |
| 115 | { |
| 116 | if (_channelPtr != NULL || _channels.Size() != 0) |
| 117 | { |
| 118 | _chManager.ReleaseChannel(); |
| 119 | } |
| 120 | |
| 121 | // Delete the map |
| 122 | while (_channels.Erase(_channels.First()) == 0) |
| 123 | ; |
| 124 | } |
| 125 | |
| 126 | Channel* ScopedChannel::ChannelPtr() |
| 127 | { |
| 128 | return _channelPtr; |
| 129 | } |
| 130 | |
| 131 | Channel* ScopedChannel::GetFirstChannel(void*& iterator) const |
| 132 | { |
| 133 | MapItem* it = _channels.First(); |
| 134 | iterator = (void*) it; |
| 135 | if (!it) |
| 136 | { |
| 137 | return NULL; |
| 138 | } |
| 139 | return static_cast<Channel*> (it->GetItem()); |
| 140 | } |
| 141 | |
| 142 | Channel* ScopedChannel::GetNextChannel(void*& iterator) const |
| 143 | { |
| 144 | MapItem* it = (MapItem*) iterator; |
| 145 | if (!it) |
| 146 | { |
| 147 | iterator = NULL; |
| 148 | return NULL; |
| 149 | } |
| 150 | it = _channels.Next(it); |
| 151 | iterator = (void*) it; |
| 152 | if (!it) |
| 153 | { |
| 154 | return NULL; |
| 155 | } |
| 156 | return static_cast<Channel*> (it->GetItem()); |
| 157 | } |
| 158 | |
pbos@webrtc.org | 5ab7b93 | 2013-07-03 15:12:26 +0000 | [diff] [blame^] | 159 | } // namespace voe |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 160 | |
pbos@webrtc.org | 5ab7b93 | 2013-07-03 15:12:26 +0000 | [diff] [blame^] | 161 | } // namespace webrtc |