blob: 5adfcbef1b8ce3f47459cae61834207296385678 [file] [log] [blame]
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +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
pbos@webrtc.orgbe72fe42013-05-21 13:52:32 +000011#include "webrtc/voice_engine/channel.h"
12#include "webrtc/voice_engine/channel_manager.h"
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000013
14namespace webrtc
15{
16
17namespace voe
18{
19
pbos@webrtc.org07a1c112013-05-14 08:31:39 +000020ChannelManager::ChannelManager(uint32_t instanceId) :
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000021 ChannelManagerBase(),
22 _instanceId(instanceId)
23{
24}
25
26ChannelManager::~ChannelManager()
27{
28 ChannelManagerBase::DestroyAllItems();
29}
30
pbos@webrtc.org1d46b922013-04-09 10:09:10 +000031bool ChannelManager::CreateChannel(int32_t& channelId)
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000032{
33 return ChannelManagerBase::CreateItem(channelId);
34}
35
pbos@webrtc.org07a1c112013-05-14 08:31:39 +000036int32_t ChannelManager::DestroyChannel(int32_t channelId)
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000037{
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.org1d46b922013-04-09 10:09:10 +000048int32_t ChannelManager::NumOfChannels() const
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000049{
50 return ChannelManagerBase::NumOfItems();
51}
52
pbos@webrtc.org1d46b922013-04-09 10:09:10 +000053int32_t ChannelManager::MaxNumOfChannels() const
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000054{
55 return ChannelManagerBase::MaxNumOfItems();
56}
57
pbos@webrtc.org1d46b922013-04-09 10:09:10 +000058void* ChannelManager::NewItem(int32_t itemID)
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000059{
60 Channel* channel;
61 if (Channel::CreateChannel(channel, itemID, _instanceId) == -1)
62 {
63 return NULL;
64 }
65 return static_cast<void*> (channel);
66}
67
68void ChannelManager::DeleteItem(void* item)
69{
70 Channel* deleteItem = static_cast<Channel*> (item);
71 delete deleteItem;
72}
73
pbos@webrtc.org07a1c112013-05-14 08:31:39 +000074Channel* ChannelManager::GetChannel(int32_t channelId) const
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000075{
76 return static_cast<Channel*> (ChannelManagerBase::GetItem(channelId));
77}
78
79void ChannelManager::ReleaseChannel()
80{
81 ChannelManagerBase::ReleaseItem();
82}
83
pbos@webrtc.org1d46b922013-04-09 10:09:10 +000084void ChannelManager::GetChannelIds(int32_t* channelsArray,
85 int32_t& numOfChannels) const
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +000086{
87 ChannelManagerBase::GetItemIds(channelsArray, numOfChannels);
88}
89
90void ChannelManager::GetChannels(MapWrapper& channels) const
91{
92 ChannelManagerBase::GetChannels(channels);
93}
94
95ScopedChannel::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
106ScopedChannel::ScopedChannel(ChannelManager& chManager,
pbos@webrtc.org1d46b922013-04-09 10:09:10 +0000107 int32_t channelId) :
andrew@webrtc.orga7b57da2012-10-22 18:19:23 +0000108 _chManager(chManager),
109 _channelPtr(NULL)
110{
111 _channelPtr = _chManager.GetChannel(channelId);
112}
113
114ScopedChannel::~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
126Channel* ScopedChannel::ChannelPtr()
127{
128 return _channelPtr;
129}
130
131Channel* 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
142Channel* 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
159} // namespace voe
160
161} // namespace webrtc