blob: 6b4b97734b64d6808228c492b7513a7aa84b4039 [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
andrew@webrtc.org8c845cb2013-05-02 15:28:02 +000011#include "webrtc/voice_engine/voe_network_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
andrew@webrtc.org8c845cb2013-05-02 15:28:02 +000013#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
14#include "webrtc/system_wrappers/interface/logging.h"
15#include "webrtc/system_wrappers/interface/trace.h"
16#include "webrtc/voice_engine/channel.h"
17#include "webrtc/voice_engine/include/voe_errors.h"
18#include "webrtc/voice_engine/voice_engine_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20namespace webrtc
21{
22
23VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine)
24{
niklase@google.com470e71d2011-07-07 08:21:25 +000025 if (NULL == voiceEngine)
26 {
27 return NULL;
28 }
tommi@webrtc.org0989fb72013-02-15 15:07:32 +000029 VoiceEngineImpl* s = static_cast<VoiceEngineImpl*>(voiceEngine);
tommi@webrtc.orga990e122012-04-26 15:28:22 +000030 s->AddRef();
31 return s;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
tommi@webrtc.org851becd2012-04-04 14:57:19 +000034VoENetworkImpl::VoENetworkImpl(voe::SharedData* shared) : _shared(shared)
niklase@google.com470e71d2011-07-07 08:21:25 +000035{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000036 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000037 "VoENetworkImpl() - ctor");
38}
39
40VoENetworkImpl::~VoENetworkImpl()
41{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000042 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000043 "~VoENetworkImpl() - dtor");
44}
45
niklase@google.com470e71d2011-07-07 08:21:25 +000046int VoENetworkImpl::RegisterExternalTransport(int channel,
47 Transport& transport)
48{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000049 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000050 "SetExternalTransport(channel=%d, transport=0x%x)",
51 channel, &transport);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000052 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000053 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000054 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000055 return -1;
56 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000057 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000058 voe::Channel* channelPtr = sc.ChannelPtr();
59 if (channelPtr == NULL)
60 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000061 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000062 "SetExternalTransport() failed to locate channel");
63 return -1;
64 }
65 return channelPtr->RegisterExternalTransport(transport);
66}
67
68int VoENetworkImpl::DeRegisterExternalTransport(int channel)
69{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000070 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000071 "DeRegisterExternalTransport(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000072 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000073 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000074 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000075 return -1;
76 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +000077 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +000078 voe::Channel* channelPtr = sc.ChannelPtr();
79 if (channelPtr == NULL)
80 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000081 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +000082 "DeRegisterExternalTransport() failed to locate channel");
83 return -1;
84 }
85 return channelPtr->DeRegisterExternalTransport();
86}
87
88int VoENetworkImpl::ReceivedRTPPacket(int channel,
89 const void* data,
90 unsigned int length)
91{
tommi@webrtc.org851becd2012-04-04 14:57:19 +000092 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +000093 "ReceivedRTPPacket(channel=%d, length=%u)", channel, length);
tommi@webrtc.org851becd2012-04-04 14:57:19 +000094 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +000095 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +000096 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +000097 return -1;
98 }
andrew@webrtc.org8c845cb2013-05-02 15:28:02 +000099 // L16 at 32 kHz, stereo, 10 ms frames (+12 byte RTP header) -> 1292 bytes
100 if ((length < 12) || (length > 1292))
niklase@google.com470e71d2011-07-07 08:21:25 +0000101 {
andrew@webrtc.org8c845cb2013-05-02 15:28:02 +0000102 _shared->SetLastError(VE_INVALID_PACKET);
103 LOG(LS_ERROR) << "Invalid packet length: " << length;
niklase@google.com470e71d2011-07-07 08:21:25 +0000104 return -1;
105 }
106 if (NULL == data)
107 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000108 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 "ReceivedRTPPacket() invalid data vector");
110 return -1;
111 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000112 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 voe::Channel* channelPtr = sc.ChannelPtr();
114 if (channelPtr == NULL)
115 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000116 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000117 "ReceivedRTPPacket() failed to locate channel");
118 return -1;
119 }
120
121 if (!channelPtr->ExternalTransport())
122 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000123 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 "ReceivedRTPPacket() external transport is not enabled");
125 return -1;
126 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000127 return channelPtr->ReceivedRTPPacket((const int8_t*) data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128}
129
130int VoENetworkImpl::ReceivedRTCPPacket(int channel, const void* data,
131 unsigned int length)
132{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000133 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 "ReceivedRTCPPacket(channel=%d, length=%u)", channel, length);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000135 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000137 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 return -1;
139 }
140 if (length < 4)
141 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000142 _shared->SetLastError(VE_INVALID_PACKET, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000143 "ReceivedRTCPPacket() invalid packet length");
144 return -1;
145 }
146 if (NULL == data)
147 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000148 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000149 "ReceivedRTCPPacket() invalid data vector");
150 return -1;
151 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000152 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000153 voe::Channel* channelPtr = sc.ChannelPtr();
154 if (channelPtr == NULL)
155 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000156 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 "ReceivedRTCPPacket() failed to locate channel");
158 return -1;
159 }
160 if (!channelPtr->ExternalTransport())
161 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000162 _shared->SetLastError(VE_INVALID_OPERATION, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000163 "ReceivedRTCPPacket() external transport is not enabled");
164 return -1;
165 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000166 return channelPtr->ReceivedRTCPPacket((const int8_t*) data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000167}
168
niklase@google.com470e71d2011-07-07 08:21:25 +0000169int VoENetworkImpl::SetPacketTimeoutNotification(int channel,
170 bool enable,
171 int timeoutSeconds)
172{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000173 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000174 "SetPacketTimeoutNotification(channel=%d, enable=%d, "
175 "timeoutSeconds=%d)",
176 channel, (int) enable, timeoutSeconds);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000177 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000179 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 return -1;
181 }
182 if (enable &&
183 ((timeoutSeconds < kVoiceEngineMinPacketTimeoutSec) ||
184 (timeoutSeconds > kVoiceEngineMaxPacketTimeoutSec)))
185 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000186 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 "SetPacketTimeoutNotification() invalid timeout size");
188 return -1;
189 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000190 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000191 voe::Channel* channelPtr = sc.ChannelPtr();
192 if (channelPtr == NULL)
193 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000194 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 "SetPacketTimeoutNotification() failed to locate channel");
196 return -1;
197 }
198 return channelPtr->SetPacketTimeoutNotification(enable, timeoutSeconds);
199}
200
201int VoENetworkImpl::GetPacketTimeoutNotification(int channel,
202 bool& enabled,
203 int& timeoutSeconds)
204{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000205 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000206 "GetPacketTimeoutNotification(channel=%d, enabled=?,"
207 " timeoutSeconds=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000208 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000210 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000211 return -1;
212 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000213 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214 voe::Channel* channelPtr = sc.ChannelPtr();
215 if (channelPtr == NULL)
216 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000217 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 "GetPacketTimeoutNotification() failed to locate channel");
219 return -1;
220 }
221 return channelPtr->GetPacketTimeoutNotification(enabled, timeoutSeconds);
222}
223
224int VoENetworkImpl::RegisterDeadOrAliveObserver(int channel,
225 VoEConnectionObserver&
226 observer)
227{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000228 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 "RegisterDeadOrAliveObserver(channel=%d, observer=0x%x)",
230 channel, &observer);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000231 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000233 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000234 return -1;
235 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000236 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000237 voe::Channel* channelPtr = sc.ChannelPtr();
238 if (channelPtr == NULL)
239 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000240 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 "RegisterDeadOrAliveObserver() failed to locate channel");
242 return -1;
243 }
244 return channelPtr->RegisterDeadOrAliveObserver(observer);
245}
246
247int VoENetworkImpl::DeRegisterDeadOrAliveObserver(int channel)
248{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000249 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000250 "DeRegisterDeadOrAliveObserver(channel=%d)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000251 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000253 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000254 return -1;
255 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000256 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000257 voe::Channel* channelPtr = sc.ChannelPtr();
258 if (channelPtr == NULL)
259 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000260 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000261 "DeRegisterDeadOrAliveObserver() failed to locate channel");
262 return -1;
263 }
264 return channelPtr->DeRegisterDeadOrAliveObserver();
265}
266
267int VoENetworkImpl::SetPeriodicDeadOrAliveStatus(int channel, bool enable,
268 int sampleTimeSeconds)
269{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000270 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000271 "SetPeriodicDeadOrAliveStatus(channel=%d, enable=%d,"
272 " sampleTimeSeconds=%d)",
273 channel, enable, sampleTimeSeconds);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000274 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000276 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000277 return -1;
278 }
279 if (enable &&
280 ((sampleTimeSeconds < kVoiceEngineMinSampleTimeSec) ||
281 (sampleTimeSeconds > kVoiceEngineMaxSampleTimeSec)))
282 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000283 _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000284 "SetPeriodicDeadOrAliveStatus() invalid sample time");
285 return -1;
286 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000287 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288 voe::Channel* channelPtr = sc.ChannelPtr();
289 if (channelPtr == NULL)
290 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000291 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000292 "SetPeriodicDeadOrAliveStatus() failed to locate channel");
293 return -1;
294 }
295 return channelPtr->SetPeriodicDeadOrAliveStatus(enable, sampleTimeSeconds);
296}
297
298int VoENetworkImpl::GetPeriodicDeadOrAliveStatus(int channel,
299 bool& enabled,
300 int& sampleTimeSeconds)
301{
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000302 WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1),
niklase@google.com470e71d2011-07-07 08:21:25 +0000303 "GetPeriodicDeadOrAliveStatus(channel=%d, enabled=?,"
304 " sampleTimeSeconds=?)", channel);
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000305 if (!_shared->statistics().Initialized())
niklase@google.com470e71d2011-07-07 08:21:25 +0000306 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000307 _shared->SetLastError(VE_NOT_INITED, kTraceError);
niklase@google.com470e71d2011-07-07 08:21:25 +0000308 return -1;
309 }
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000310 voe::ScopedChannel sc(_shared->channel_manager(), channel);
niklase@google.com470e71d2011-07-07 08:21:25 +0000311 voe::Channel* channelPtr = sc.ChannelPtr();
312 if (channelPtr == NULL)
313 {
tommi@webrtc.org851becd2012-04-04 14:57:19 +0000314 _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError,
niklase@google.com470e71d2011-07-07 08:21:25 +0000315 "GetPeriodicDeadOrAliveStatus() failed to locate channel");
316 return -1;
317 }
318 return channelPtr->GetPeriodicDeadOrAliveStatus(enabled,
319 sampleTimeSeconds);
320}
321
niklase@google.com470e71d2011-07-07 08:21:25 +0000322} // namespace webrtc