channel_manager.cc revision 956aa7e0874f2e08c335a82a2c32f400fac8b031
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
11#include "webrtc/voice_engine/channel.h"
12#include "webrtc/voice_engine/channel_manager.h"
13
14namespace webrtc
15{
16
17namespace voe
18{
19
20ChannelManager::ChannelManager(uint32_t instanceId) :
21    ChannelManagerBase(),
22    _instanceId(instanceId)
23{
24}
25
26ChannelManager::~ChannelManager()
27{
28    ChannelManagerBase::DestroyAllItems();
29}
30
31bool ChannelManager::CreateChannel(int32_t& channelId)
32{
33    return ChannelManagerBase::CreateItem(channelId);
34}
35
36int32_t ChannelManager::DestroyChannel(int32_t channelId)
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
48int32_t ChannelManager::NumOfChannels() const
49{
50    return ChannelManagerBase::NumOfItems();
51}
52
53int32_t ChannelManager::MaxNumOfChannels() const
54{
55    return ChannelManagerBase::MaxNumOfItems();
56}
57
58void* ChannelManager::NewItem(int32_t itemID)
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
68void ChannelManager::DeleteItem(void* item)
69{
70    Channel* deleteItem = static_cast<Channel*> (item);
71    delete deleteItem;
72}
73
74Channel* ChannelManager::GetChannel(int32_t channelId) const
75{
76    return static_cast<Channel*> (ChannelManagerBase::GetItem(channelId));
77}
78
79void ChannelManager::ReleaseChannel()
80{
81    ChannelManagerBase::ReleaseItem();
82}
83
84void ChannelManager::GetChannelIds(int32_t* channelsArray,
85                                   int32_t& numOfChannels) const
86{
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,
107                             int32_t channelId) :
108    _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
162