blob: 11e268067a270c8c975fd2250eb8bff4c6885a5d [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org79af7342012-01-31 12:22:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 "voe_network_impl.h"
12
13#include "channel.h"
14#include "critical_section_wrapper.h"
15#include "trace.h"
16#include "voe_errors.h"
17#include "voice_engine_impl.h"
18
19namespace webrtc
20{
21
22VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine)
23{
niklase@google.com470e71d2011-07-07 08:21:25 +000024 if (NULL == voiceEngine)
25 {
26 return NULL;
27 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000028 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000029 s->AddRef();
30 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000031}
32
tommi@webrtc.org851becd2012-04-04 14:57:19 +000033VoENetworkImpl::VoENetworkImpl(voe::SharedData* shared) : _shared(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000034{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000035 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000036 "VoENetworkImpl() - ctor");
37}
38
39VoENetworkImpl::~VoENetworkImpl()
40{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000041 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000042 "~VoENetworkImpl() - dtor");
43}
44
niklase@google.com470e71d2011-07-07 08:21:25 +000045int VoENetworkImpl::RegisterExternalTransport(int channel,
46 Transport& transport)
47{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000048 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000049 "SetExternalTransport(channel=%d, transport=0x%x)",
50 channel, &transport);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000051 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000052 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000053 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000054 return -1;
55 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000056 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000057 voe::Channel* channelPtr = sc.ChannelPtr();
58 if (channelPtr == NULL)
59 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000060 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000061 "SetExternalTransport() failed to locate channel");
62 return -1;
63 }
64 return channelPtr->RegisterExternalTransport(transport);
65}
66
67int VoENetworkImpl::DeRegisterExternalTransport(int channel)
68{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000069 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000070 "DeRegisterExternalTransport(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000071 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000072 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000073 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000074 return -1;
75 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000076 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000077 voe::Channel* channelPtr = sc.ChannelPtr();
78 if (channelPtr == NULL)
79 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000080 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000081 "DeRegisterExternalTransport() failed to locate channel");
82 return -1;
83 }
84 return channelPtr->DeRegisterExternalTransport();
85}
86
87int VoENetworkImpl::ReceivedRTPPacket(int channel,
88 const void* data,
89 unsigned int length)
90{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000091 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000092 "ReceivedRTPPacket(channel=%d, length=%u)", channel, length);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000093 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000094 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000095 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000096 return -1;
97 }
98 if ((length < 12) || (length > 807))
99 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000100 _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 "ReceivedRTPPacket() invalid packet length");
102 return -1;
103 }
104 if (NULL == data)
105 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000106 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 "ReceivedRTPPacket() invalid data vector");
108 return -1;
109 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000110 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000111 voe::Channel* channelPtr = sc.ChannelPtr();
112 if (channelPtr == NULL)
113 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000114 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 "ReceivedRTPPacket() failed to locate channel");
116 return -1;
117 }
118
119 if (!channelPtr->ExternalTransport())
120 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000121 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000122 "ReceivedRTPPacket() external transport is not enabled");
123 return -1;
124 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000125 return channelPtr->ReceivedRTPPacket((const int8_t*) data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126}
127
128int VoENetworkImpl::ReceivedRTCPPacket(int channel, const void* data,
129 unsigned int length)
130{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000131 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 "ReceivedRTCPPacket(channel=%d, length=%u)", channel, length);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000133 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000135 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 return -1;
137 }
138 if (length < 4)
139 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000140 _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 "ReceivedRTCPPacket() invalid packet length");
142 return -1;
143 }
144 if (NULL == data)
145 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000146 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000147 "ReceivedRTCPPacket() invalid data vector");
148 return -1;
149 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000150 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000151 voe::Channel* channelPtr = sc.ChannelPtr();
152 if (channelPtr == NULL)
153 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000154 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 "ReceivedRTCPPacket() failed to locate channel");
156 return -1;
157 }
158 if (!channelPtr->ExternalTransport())
159 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000160 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 "ReceivedRTCPPacket() external transport is not enabled");
162 return -1;
163 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000164 return channelPtr->ReceivedRTCPPacket((const int8_t*) data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000165}
166
niklase@google.com470e71d2011-07-07 08:21:25 +0000167int VoENetworkImpl::SetPacketTimeoutNotification(int channel,
168 bool enable,
169 int timeoutSeconds)
170{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000171 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 "SetPacketTimeoutNotification(channel=%d, enable=%d, "
173 "timeoutSeconds=%d)",
174 channel, (int) enable, timeoutSeconds);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000175 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000177 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 return -1;
179 }
180 if (enable &&
181 ((timeoutSeconds < kVoiceEngineMinPacketTimeoutSec) ||
182 (timeoutSeconds > kVoiceEngineMaxPacketTimeoutSec)))
183 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000184 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000185 "SetPacketTimeoutNotification() invalid timeout size");
186 return -1;
187 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000188 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000189 voe::Channel* channelPtr = sc.ChannelPtr();
190 if (channelPtr == NULL)
191 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000192 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000193 "SetPacketTimeoutNotification() failed to locate channel");
194 return -1;
195 }
196 return channelPtr->SetPacketTimeoutNotification(enable, timeoutSeconds);
197}
198
199int VoENetworkImpl::GetPacketTimeoutNotification(int channel,
200 bool& enabled,
201 int& timeoutSeconds)
202{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000203 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 "GetPacketTimeoutNotification(channel=%d, enabled=?,"
205 " timeoutSeconds=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000206 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000208 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 return -1;
210 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000211 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 voe::Channel* channelPtr = sc.ChannelPtr();
213 if (channelPtr == NULL)
214 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000215 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 "GetPacketTimeoutNotification() failed to locate channel");
217 return -1;
218 }
219 return channelPtr->GetPacketTimeoutNotification(enabled, timeoutSeconds);
220}
221
222int VoENetworkImpl::RegisterDeadOrAliveObserver(int channel,
223 VoEConnectionObserver&
224 observer)
225{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000226 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000227 "RegisterDeadOrAliveObserver(channel=%d, observer=0x%x)",
228 channel, &observer);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000229 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000230 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000231 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 return -1;
233 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000234 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 voe::Channel* channelPtr = sc.ChannelPtr();
236 if (channelPtr == NULL)
237 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000238 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 "RegisterDeadOrAliveObserver() failed to locate channel");
240 return -1;
241 }
242 return channelPtr->RegisterDeadOrAliveObserver(observer);
243}
244
245int VoENetworkImpl::DeRegisterDeadOrAliveObserver(int channel)
246{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000247 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 "DeRegisterDeadOrAliveObserver(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000249 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000251 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 return -1;
253 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000254 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000255 voe::Channel* channelPtr = sc.ChannelPtr();
256 if (channelPtr == NULL)
257 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000258 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000259 "DeRegisterDeadOrAliveObserver() failed to locate channel");
260 return -1;
261 }
262 return channelPtr->DeRegisterDeadOrAliveObserver();
263}
264
265int VoENetworkImpl::SetPeriodicDeadOrAliveStatus(int channel, bool enable,
266 int sampleTimeSeconds)
267{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000268 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 "SetPeriodicDeadOrAliveStatus(channel=%d, enable=%d,"
270 " sampleTimeSeconds=%d)",
271 channel, enable, sampleTimeSeconds);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000272 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000273 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000274 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 return -1;
276 }
277 if (enable &&
278 ((sampleTimeSeconds < kVoiceEngineMinSampleTimeSec) ||
279 (sampleTimeSeconds > kVoiceEngineMaxSampleTimeSec)))
280 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000281 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000282 "SetPeriodicDeadOrAliveStatus() invalid sample time");
283 return -1;
284 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000285 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286 voe::Channel* channelPtr = sc.ChannelPtr();
287 if (channelPtr == NULL)
288 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000289 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000290 "SetPeriodicDeadOrAliveStatus() failed to locate channel");
291 return -1;
292 }
293 return channelPtr->SetPeriodicDeadOrAliveStatus(enable, sampleTimeSeconds);
294}
295
296int VoENetworkImpl::GetPeriodicDeadOrAliveStatus(int channel,
297 bool& enabled,
298 int& sampleTimeSeconds)
299{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000300 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000301 "GetPeriodicDeadOrAliveStatus(channel=%d, enabled=?,"
302 " sampleTimeSeconds=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000303 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000304 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000305 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 return -1;
307 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000308 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309 voe::Channel* channelPtr = sc.ChannelPtr();
310 if (channelPtr == NULL)
311 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000312 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000313 "GetPeriodicDeadOrAliveStatus() failed to locate channel");
314 return -1;
315 }
316 return channelPtr->GetPeriodicDeadOrAliveStatus(enabled,
317 sampleTimeSeconds);
318}
319
niklase@google.com470e71d2011-07-07 08:21:25 +0000320} // namespace webrtc