andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 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 "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 | |
| 19 | namespace webrtc |
| 20 | { |
| 21 | |
| 22 | VoENetwork* VoENetwork::GetInterface(VoiceEngine* voiceEngine) |
| 23 | { |
| 24 | #ifndef WEBRTC_VOICE_ENGINE_NETWORK_API |
| 25 | return NULL; |
| 26 | #else |
| 27 | if (NULL == voiceEngine) |
| 28 | { |
| 29 | return NULL; |
| 30 | } |
| 31 | VoiceEngineImpl* s = reinterpret_cast<VoiceEngineImpl*>(voiceEngine); |
| 32 | s->AddRef(); |
| 33 | return s; |
| 34 | #endif |
| 35 | } |
| 36 | |
| 37 | #ifdef WEBRTC_VOICE_ENGINE_NETWORK_API |
| 38 | |
| 39 | VoENetworkImpl::VoENetworkImpl(voe::SharedData* shared) : _shared(shared) |
| 40 | { |
| 41 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 42 | "VoENetworkImpl() - ctor"); |
| 43 | } |
| 44 | |
| 45 | VoENetworkImpl::~VoENetworkImpl() |
| 46 | { |
| 47 | WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 48 | "~VoENetworkImpl() - dtor"); |
| 49 | } |
| 50 | |
| 51 | int VoENetworkImpl::RegisterExternalTransport(int channel, |
| 52 | Transport& transport) |
| 53 | { |
| 54 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 55 | "SetExternalTransport(channel=%d, transport=0x%x)", |
| 56 | channel, &transport); |
| 57 | if (!_shared->statistics().Initialized()) |
| 58 | { |
| 59 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 60 | return -1; |
| 61 | } |
| 62 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 63 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 64 | if (channelPtr == NULL) |
| 65 | { |
| 66 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 67 | "SetExternalTransport() failed to locate channel"); |
| 68 | return -1; |
| 69 | } |
| 70 | return channelPtr->RegisterExternalTransport(transport); |
| 71 | } |
| 72 | |
| 73 | int VoENetworkImpl::DeRegisterExternalTransport(int channel) |
| 74 | { |
| 75 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 76 | "DeRegisterExternalTransport(channel=%d)", channel); |
| 77 | if (!_shared->statistics().Initialized()) |
| 78 | { |
| 79 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 80 | return -1; |
| 81 | } |
| 82 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 83 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 84 | if (channelPtr == NULL) |
| 85 | { |
| 86 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 87 | "DeRegisterExternalTransport() failed to locate channel"); |
| 88 | return -1; |
| 89 | } |
| 90 | return channelPtr->DeRegisterExternalTransport(); |
| 91 | } |
| 92 | |
| 93 | int VoENetworkImpl::ReceivedRTPPacket(int channel, |
| 94 | const void* data, |
| 95 | unsigned int length) |
| 96 | { |
| 97 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 98 | "ReceivedRTPPacket(channel=%d, length=%u)", channel, length); |
| 99 | if (!_shared->statistics().Initialized()) |
| 100 | { |
| 101 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 102 | return -1; |
| 103 | } |
| 104 | if ((length < 12) || (length > 807)) |
| 105 | { |
| 106 | _shared->SetLastError(VE_INVALID_PACKET, kTraceError, |
| 107 | "ReceivedRTPPacket() invalid packet length"); |
| 108 | return -1; |
| 109 | } |
| 110 | if (NULL == data) |
| 111 | { |
| 112 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 113 | "ReceivedRTPPacket() invalid data vector"); |
| 114 | return -1; |
| 115 | } |
| 116 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 117 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 118 | if (channelPtr == NULL) |
| 119 | { |
| 120 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 121 | "ReceivedRTPPacket() failed to locate channel"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (!channelPtr->ExternalTransport()) |
| 126 | { |
| 127 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 128 | "ReceivedRTPPacket() external transport is not enabled"); |
| 129 | return -1; |
| 130 | } |
| 131 | return channelPtr->ReceivedRTPPacket((const WebRtc_Word8*) data, length); |
| 132 | } |
| 133 | |
| 134 | int VoENetworkImpl::ReceivedRTCPPacket(int channel, const void* data, |
| 135 | unsigned int length) |
| 136 | { |
| 137 | WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 138 | "ReceivedRTCPPacket(channel=%d, length=%u)", channel, length); |
| 139 | if (!_shared->statistics().Initialized()) |
| 140 | { |
| 141 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 142 | return -1; |
| 143 | } |
| 144 | if (length < 4) |
| 145 | { |
| 146 | _shared->SetLastError(VE_INVALID_PACKET, kTraceError, |
| 147 | "ReceivedRTCPPacket() invalid packet length"); |
| 148 | return -1; |
| 149 | } |
| 150 | if (NULL == data) |
| 151 | { |
| 152 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 153 | "ReceivedRTCPPacket() invalid data vector"); |
| 154 | return -1; |
| 155 | } |
| 156 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 157 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 158 | if (channelPtr == NULL) |
| 159 | { |
| 160 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 161 | "ReceivedRTCPPacket() failed to locate channel"); |
| 162 | return -1; |
| 163 | } |
| 164 | if (!channelPtr->ExternalTransport()) |
| 165 | { |
| 166 | _shared->SetLastError(VE_INVALID_OPERATION, kTraceError, |
| 167 | "ReceivedRTCPPacket() external transport is not enabled"); |
| 168 | return -1; |
| 169 | } |
| 170 | return channelPtr->ReceivedRTCPPacket((const WebRtc_Word8*) data, length); |
| 171 | } |
| 172 | |
| 173 | int VoENetworkImpl::GetSourceInfo(int channel, |
| 174 | int& rtpPort, |
| 175 | int& rtcpPort, |
| 176 | char ipAddr[64]) |
| 177 | { |
| 178 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 179 | "GetSourceInfo(channel=%d, rtpPort=?, rtcpPort=?, ipAddr[]=?)", |
| 180 | channel); |
| 181 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 182 | if (!_shared->statistics().Initialized()) |
| 183 | { |
| 184 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 185 | return -1; |
| 186 | } |
| 187 | if (NULL == ipAddr) |
| 188 | { |
| 189 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 190 | "GetSourceInfo() invalid IP-address buffer"); |
| 191 | return -1; |
| 192 | } |
| 193 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 194 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 195 | if (channelPtr == NULL) |
| 196 | { |
| 197 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 198 | "GetSourceInfo() failed to locate channel"); |
| 199 | return -1; |
| 200 | } |
| 201 | if (channelPtr->ExternalTransport()) |
| 202 | { |
| 203 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 204 | "GetSourceInfo() external transport is enabled"); |
| 205 | return -1; |
| 206 | } |
| 207 | return channelPtr->GetSourceInfo(rtpPort, rtcpPort, ipAddr); |
| 208 | #else |
| 209 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 210 | "GetSourceInfo() VoE is built for external transport"); |
| 211 | return -1; |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | int VoENetworkImpl::GetLocalIP(char ipAddr[64], bool ipv6) |
| 216 | { |
| 217 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 218 | "GetLocalIP(ipAddr[]=?, ipv6=%d)", ipv6); |
| 219 | IPHONE_NOT_SUPPORTED(_shared->statistics()); |
| 220 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 221 | if (!_shared->statistics().Initialized()) |
| 222 | { |
| 223 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 224 | return -1; |
| 225 | } |
| 226 | if (NULL == ipAddr) |
| 227 | { |
| 228 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 229 | "GetLocalIP() invalid IP-address buffer"); |
| 230 | return -1; |
| 231 | } |
| 232 | |
| 233 | // Create a temporary socket module to ensure that this method can be |
| 234 | // called also when no channels are created. |
| 235 | WebRtc_UWord8 numSockThreads(1); |
| 236 | UdpTransport* socketPtr = |
| 237 | UdpTransport::Create( |
| 238 | -1, |
| 239 | numSockThreads); |
| 240 | if (NULL == socketPtr) |
| 241 | { |
| 242 | _shared->SetLastError(VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError, |
| 243 | "GetLocalIP() failed to create socket module"); |
| 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | // Use a buffer big enough for IPv6 addresses and initialize it with zeros. |
| 248 | char localIPAddr[256] = {0}; |
| 249 | |
| 250 | if (ipv6) |
| 251 | { |
| 252 | char localIP[16]; |
| 253 | if (socketPtr->LocalHostAddressIPV6(localIP) != 0) |
| 254 | { |
| 255 | _shared->SetLastError(VE_INVALID_IP_ADDRESS, kTraceError, |
| 256 | "GetLocalIP() failed to retrieve local IP - 1"); |
| 257 | UdpTransport::Destroy(socketPtr); |
| 258 | return -1; |
| 259 | } |
| 260 | // Convert 128-bit address to character string (a:b:c:d:e:f:g:h) |
| 261 | sprintf(localIPAddr, |
| 262 | "%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x" |
| 263 | "%.2x:%.2x%.2x", |
| 264 | localIP[0], localIP[1], localIP[2], localIP[3], localIP[4], |
| 265 | localIP[5], localIP[6], localIP[7], localIP[8], localIP[9], |
| 266 | localIP[10], localIP[11], localIP[12], localIP[13], |
| 267 | localIP[14], localIP[15]); |
| 268 | } |
| 269 | else |
| 270 | { |
| 271 | WebRtc_UWord32 localIP(0); |
| 272 | // Read local IP (as 32-bit address) from the socket module |
| 273 | if (socketPtr->LocalHostAddress(localIP) != 0) |
| 274 | { |
| 275 | _shared->SetLastError(VE_INVALID_IP_ADDRESS, kTraceError, |
| 276 | "GetLocalIP() failed to retrieve local IP - 2"); |
| 277 | UdpTransport::Destroy(socketPtr); |
| 278 | return -1; |
| 279 | } |
| 280 | // Convert 32-bit address to character string (x.y.z.w) |
| 281 | sprintf(localIPAddr, "%d.%d.%d.%d", (int) ((localIP >> 24) & 0x0ff), |
| 282 | (int) ((localIP >> 16) & 0x0ff), |
| 283 | (int) ((localIP >> 8) & 0x0ff), |
| 284 | (int) (localIP & 0x0ff)); |
| 285 | } |
| 286 | |
| 287 | strcpy(ipAddr, localIPAddr); |
| 288 | |
| 289 | UdpTransport::Destroy(socketPtr); |
| 290 | |
| 291 | WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, |
| 292 | VoEId(_shared->instance_id(), -1), |
| 293 | "GetLocalIP() => ipAddr=%s", ipAddr); |
| 294 | return 0; |
| 295 | #else |
| 296 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 297 | "GetLocalIP() VoE is built for external transport"); |
| 298 | return -1; |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | int VoENetworkImpl::EnableIPv6(int channel) |
| 303 | { |
| 304 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 305 | "EnableIPv6(channel=%d)", channel); |
| 306 | ANDROID_NOT_SUPPORTED(_shared->statistics()); |
| 307 | IPHONE_NOT_SUPPORTED(_shared->statistics()); |
| 308 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 309 | if (!_shared->statistics().Initialized()) |
| 310 | { |
| 311 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 312 | return -1; |
| 313 | } |
| 314 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 315 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 316 | if (channelPtr == NULL) |
| 317 | { |
| 318 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 319 | "EnableIPv6() failed to locate channel"); |
| 320 | return -1; |
| 321 | } |
| 322 | if (channelPtr->ExternalTransport()) |
| 323 | { |
| 324 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 325 | "EnableIPv6() external transport is enabled"); |
| 326 | return -1; |
| 327 | } |
| 328 | return channelPtr->EnableIPv6(); |
| 329 | #else |
| 330 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 331 | "EnableIPv6() VoE is built for external transport"); |
| 332 | return -1; |
| 333 | #endif |
| 334 | } |
| 335 | |
| 336 | bool VoENetworkImpl::IPv6IsEnabled(int channel) |
| 337 | { |
| 338 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 339 | "IPv6IsEnabled(channel=%d)", channel); |
| 340 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 341 | if (!_shared->statistics().Initialized()) |
| 342 | { |
| 343 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 344 | return false; |
| 345 | } |
| 346 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 347 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 348 | if (channelPtr == NULL) |
| 349 | { |
| 350 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 351 | "IPv6IsEnabled() failed to locate channel"); |
| 352 | return false; |
| 353 | } |
| 354 | if (channelPtr->ExternalTransport()) |
| 355 | { |
| 356 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 357 | "IPv6IsEnabled() external transport is enabled"); |
| 358 | return false; |
| 359 | } |
| 360 | return channelPtr->IPv6IsEnabled(); |
| 361 | #else |
| 362 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 363 | "IPv6IsEnabled() VoE is built for external transport"); |
| 364 | return false; |
| 365 | #endif |
| 366 | } |
| 367 | |
| 368 | int VoENetworkImpl::SetSourceFilter(int channel, |
| 369 | int rtpPort, |
| 370 | int rtcpPort, |
| 371 | const char ipAddr[64]) |
| 372 | { |
| 373 | (ipAddr == NULL) ? WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 374 | VoEId(_shared->instance_id(), -1), |
| 375 | "SetSourceFilter(channel=%d, rtpPort=%d," |
| 376 | " rtcpPort=%d)", |
| 377 | channel, rtpPort, rtcpPort) |
| 378 | : WEBRTC_TRACE(kTraceApiCall, kTraceVoice, |
| 379 | VoEId(_shared->instance_id(), -1), |
| 380 | "SetSourceFilter(channel=%d, rtpPort=%d," |
| 381 | " rtcpPort=%d, ipAddr=%s)", |
| 382 | channel, rtpPort, rtcpPort, ipAddr); |
| 383 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 384 | if (!_shared->statistics().Initialized()) |
| 385 | { |
| 386 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 387 | return -1; |
| 388 | } |
| 389 | if ((rtpPort < 0) || (rtpPort > 65535)) |
| 390 | { |
| 391 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 392 | "SetSourceFilter() invalid RTP port"); |
| 393 | return -1; |
| 394 | } |
| 395 | if ((rtcpPort < 0) || (rtcpPort > 65535)) |
| 396 | { |
| 397 | _shared->SetLastError(VE_INVALID_PORT_NMBR, kTraceError, |
| 398 | "SetSourceFilter() invalid RTCP port"); |
| 399 | return -1; |
| 400 | } |
| 401 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 402 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 403 | if (channelPtr == NULL) |
| 404 | { |
| 405 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 406 | "SetSourceFilter() failed to locate channel"); |
| 407 | return -1; |
| 408 | } |
| 409 | if (channelPtr->ExternalTransport()) |
| 410 | { |
| 411 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 412 | "SetSourceFilter() external transport is enabled"); |
| 413 | return -1; |
| 414 | } |
| 415 | return channelPtr->SetSourceFilter(rtpPort, rtcpPort, ipAddr); |
| 416 | #else |
| 417 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 418 | "SetSourceFilter() VoE is built for external transport"); |
| 419 | return -1; |
| 420 | #endif |
| 421 | } |
| 422 | |
| 423 | int VoENetworkImpl::GetSourceFilter(int channel, |
| 424 | int& rtpPort, |
| 425 | int& rtcpPort, |
| 426 | char ipAddr[64]) |
| 427 | { |
| 428 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 429 | "GetSourceFilter(channel=%d, rtpPort=?, rtcpPort=?, " |
| 430 | "ipAddr[]=?)", |
| 431 | channel); |
| 432 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 433 | if (!_shared->statistics().Initialized()) |
| 434 | { |
| 435 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 436 | return -1; |
| 437 | } |
| 438 | if (NULL == ipAddr) |
| 439 | { |
| 440 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 441 | "GetSourceFilter() invalid IP-address buffer"); |
| 442 | return -1; |
| 443 | } |
| 444 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 445 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 446 | if (channelPtr == NULL) |
| 447 | { |
| 448 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 449 | "GetSourceFilter() failed to locate channel"); |
| 450 | return -1; |
| 451 | } |
| 452 | if (channelPtr->ExternalTransport()) |
| 453 | { |
| 454 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 455 | "GetSourceFilter() external transport is enabled"); |
| 456 | return -1; |
| 457 | } |
| 458 | return channelPtr->GetSourceFilter(rtpPort, rtcpPort, ipAddr); |
| 459 | #else |
| 460 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 461 | "GetSourceFilter() VoE is built for external transport"); |
| 462 | return -1; |
| 463 | #endif |
| 464 | } |
| 465 | |
| 466 | int VoENetworkImpl::SetSendTOS(int channel, |
| 467 | int DSCP, |
| 468 | int priority, |
| 469 | bool useSetSockopt) |
| 470 | { |
| 471 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 472 | "SetSendTOS(channel=%d, DSCP=%d, useSetSockopt=%d)", |
| 473 | channel, DSCP, useSetSockopt); |
| 474 | |
| 475 | #if !defined(_WIN32) && !defined(WEBRTC_LINUX) && !defined(WEBRTC_MAC) |
| 476 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceWarning, |
| 477 | "SetSendTOS() is not supported on this platform"); |
| 478 | return -1; |
| 479 | #endif |
| 480 | |
| 481 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 482 | if (!_shared->statistics().Initialized()) |
| 483 | { |
| 484 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 485 | return -1; |
| 486 | } |
| 487 | if ((DSCP < 0) || (DSCP > 63)) |
| 488 | { |
| 489 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 490 | "SetSendTOS() Invalid DSCP value"); |
| 491 | return -1; |
| 492 | } |
| 493 | #if defined(_WIN32) || defined(WEBRTC_LINUX) |
| 494 | if ((priority < -1) || (priority > 7)) |
| 495 | { |
| 496 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 497 | "SetSendTOS() Invalid priority value"); |
| 498 | return -1; |
| 499 | } |
| 500 | #else |
| 501 | if (-1 != priority) |
| 502 | { |
| 503 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 504 | "SetSendTOS() priority not supported"); |
| 505 | return -1; |
| 506 | } |
| 507 | #endif |
| 508 | #if defined(_WIN32) |
| 509 | if ((priority >= 0) && useSetSockopt) |
| 510 | { |
| 511 | // On Windows, priority and useSetSockopt cannot be combined |
| 512 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 513 | "SetSendTOS() priority and useSetSockopt conflict"); |
| 514 | return -1; |
| 515 | } |
| 516 | #endif |
| 517 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 518 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 519 | if (channelPtr == NULL) |
| 520 | { |
| 521 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 522 | "SetSendTOS() failed to locate channel"); |
| 523 | return -1; |
| 524 | } |
| 525 | if (channelPtr->ExternalTransport()) |
| 526 | { |
| 527 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 528 | "SetSendTOS() external transport is enabled"); |
| 529 | return -1; |
| 530 | } |
| 531 | #if defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 532 | useSetSockopt = true; |
| 533 | WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 534 | " force useSetSockopt=true since there is no alternative" |
| 535 | " implementation"); |
| 536 | #endif |
| 537 | |
| 538 | return channelPtr->SetSendTOS(DSCP, priority, useSetSockopt); |
| 539 | #else |
| 540 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 541 | "SetSendTOS() VoE is built for external transport"); |
| 542 | return -1; |
| 543 | #endif |
| 544 | } |
| 545 | |
| 546 | int VoENetworkImpl::GetSendTOS(int channel, |
| 547 | int& DSCP, |
| 548 | int& priority, |
| 549 | bool& useSetSockopt) |
| 550 | { |
| 551 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 552 | "GetSendTOS(channel=%d)", channel); |
| 553 | |
| 554 | #if !defined(_WIN32) && !defined(WEBRTC_LINUX) && !defined(WEBRTC_MAC) |
| 555 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceWarning, |
| 556 | "GetSendTOS() is not supported on this platform"); |
| 557 | return -1; |
| 558 | #endif |
| 559 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 560 | if (!_shared->statistics().Initialized()) |
| 561 | { |
| 562 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 563 | return -1; |
| 564 | } |
| 565 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 566 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 567 | if (channelPtr == NULL) |
| 568 | { |
| 569 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 570 | "GetSendTOS() failed to locate channel"); |
| 571 | return -1; |
| 572 | } |
| 573 | if (channelPtr->ExternalTransport()) |
| 574 | { |
| 575 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 576 | "GetSendTOS() external transport is enabled"); |
| 577 | return -1; |
| 578 | } |
| 579 | return channelPtr->GetSendTOS(DSCP, priority, useSetSockopt); |
| 580 | #else |
| 581 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 582 | "GetSendTOS() VoE is built for external transport"); |
| 583 | return -1; |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | int VoENetworkImpl::SetSendGQoS(int channel, |
| 588 | bool enable, |
| 589 | int serviceType, |
| 590 | int overrideDSCP) |
| 591 | { |
| 592 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 593 | "SetSendGQOS(channel=%d, enable=%d, serviceType=%d," |
| 594 | " overrideDSCP=%d)", |
| 595 | channel, (int) enable, serviceType, overrideDSCP); |
| 596 | ANDROID_NOT_SUPPORTED(_shared->statistics()); |
| 597 | IPHONE_NOT_SUPPORTED(_shared->statistics()); |
| 598 | #if !defined(_WIN32) |
| 599 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceWarning, |
| 600 | "SetSendGQOS() is not supported on this platform"); |
| 601 | return -1; |
| 602 | #elif !defined(WEBRTC_EXTERNAL_TRANSPORT) |
| 603 | if (!_shared->statistics().Initialized()) |
| 604 | { |
| 605 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 606 | return -1; |
| 607 | } |
| 608 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 609 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 610 | if (channelPtr == NULL) |
| 611 | { |
| 612 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 613 | "SetSendGQOS() failed to locate channel"); |
| 614 | return -1; |
| 615 | } |
| 616 | if (channelPtr->ExternalTransport()) |
| 617 | { |
| 618 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 619 | "SetSendGQOS() external transport is enabled"); |
| 620 | return -1; |
| 621 | } |
| 622 | return channelPtr->SetSendGQoS(enable, serviceType, overrideDSCP); |
| 623 | #else |
| 624 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 625 | "SetSendGQOS() VoE is built for external transport"); |
| 626 | return -1; |
| 627 | #endif |
| 628 | } |
| 629 | |
| 630 | int VoENetworkImpl::GetSendGQoS(int channel, |
| 631 | bool& enabled, |
| 632 | int& serviceType, |
| 633 | int& overrideDSCP) |
| 634 | { |
| 635 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 636 | "GetSendGQOS(channel=%d)", channel); |
| 637 | ANDROID_NOT_SUPPORTED(_shared->statistics()); |
| 638 | IPHONE_NOT_SUPPORTED(_shared->statistics()); |
| 639 | #if !defined(_WIN32) |
| 640 | _shared->SetLastError(VE_FUNC_NOT_SUPPORTED, kTraceWarning, |
| 641 | "GetSendGQOS() is not supported on this platform"); |
| 642 | return -1; |
| 643 | #elif !defined(WEBRTC_EXTERNAL_TRANSPORT) |
| 644 | if (!_shared->statistics().Initialized()) |
| 645 | { |
| 646 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 647 | return -1; |
| 648 | } |
| 649 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 650 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 651 | if (channelPtr == NULL) |
| 652 | { |
| 653 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 654 | "GetSendGQOS() failed to locate channel"); |
| 655 | return -1; |
| 656 | } |
| 657 | if (channelPtr->ExternalTransport()) |
| 658 | { |
| 659 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError, |
| 660 | "GetSendGQOS() external transport is enabled"); |
| 661 | return -1; |
| 662 | } |
| 663 | return channelPtr->GetSendGQoS(enabled, serviceType, overrideDSCP); |
| 664 | #else |
| 665 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 666 | "GetSendGQOS() VoE is built for external transport"); |
| 667 | return -1; |
| 668 | #endif |
| 669 | } |
| 670 | |
| 671 | int VoENetworkImpl::SetPacketTimeoutNotification(int channel, |
| 672 | bool enable, |
| 673 | int timeoutSeconds) |
| 674 | { |
| 675 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 676 | "SetPacketTimeoutNotification(channel=%d, enable=%d, " |
| 677 | "timeoutSeconds=%d)", |
| 678 | channel, (int) enable, timeoutSeconds); |
| 679 | if (!_shared->statistics().Initialized()) |
| 680 | { |
| 681 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 682 | return -1; |
| 683 | } |
| 684 | if (enable && |
| 685 | ((timeoutSeconds < kVoiceEngineMinPacketTimeoutSec) || |
| 686 | (timeoutSeconds > kVoiceEngineMaxPacketTimeoutSec))) |
| 687 | { |
| 688 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 689 | "SetPacketTimeoutNotification() invalid timeout size"); |
| 690 | return -1; |
| 691 | } |
| 692 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 693 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 694 | if (channelPtr == NULL) |
| 695 | { |
| 696 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 697 | "SetPacketTimeoutNotification() failed to locate channel"); |
| 698 | return -1; |
| 699 | } |
| 700 | return channelPtr->SetPacketTimeoutNotification(enable, timeoutSeconds); |
| 701 | } |
| 702 | |
| 703 | int VoENetworkImpl::GetPacketTimeoutNotification(int channel, |
| 704 | bool& enabled, |
| 705 | int& timeoutSeconds) |
| 706 | { |
| 707 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 708 | "GetPacketTimeoutNotification(channel=%d, enabled=?," |
| 709 | " timeoutSeconds=?)", channel); |
| 710 | if (!_shared->statistics().Initialized()) |
| 711 | { |
| 712 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 713 | return -1; |
| 714 | } |
| 715 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 716 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 717 | if (channelPtr == NULL) |
| 718 | { |
| 719 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 720 | "GetPacketTimeoutNotification() failed to locate channel"); |
| 721 | return -1; |
| 722 | } |
| 723 | return channelPtr->GetPacketTimeoutNotification(enabled, timeoutSeconds); |
| 724 | } |
| 725 | |
| 726 | int VoENetworkImpl::RegisterDeadOrAliveObserver(int channel, |
| 727 | VoEConnectionObserver& |
| 728 | observer) |
| 729 | { |
| 730 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 731 | "RegisterDeadOrAliveObserver(channel=%d, observer=0x%x)", |
| 732 | channel, &observer); |
| 733 | if (!_shared->statistics().Initialized()) |
| 734 | { |
| 735 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 736 | return -1; |
| 737 | } |
| 738 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 739 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 740 | if (channelPtr == NULL) |
| 741 | { |
| 742 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 743 | "RegisterDeadOrAliveObserver() failed to locate channel"); |
| 744 | return -1; |
| 745 | } |
| 746 | return channelPtr->RegisterDeadOrAliveObserver(observer); |
| 747 | } |
| 748 | |
| 749 | int VoENetworkImpl::DeRegisterDeadOrAliveObserver(int channel) |
| 750 | { |
| 751 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 752 | "DeRegisterDeadOrAliveObserver(channel=%d)", channel); |
| 753 | if (!_shared->statistics().Initialized()) |
| 754 | { |
| 755 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 756 | return -1; |
| 757 | } |
| 758 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 759 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 760 | if (channelPtr == NULL) |
| 761 | { |
| 762 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 763 | "DeRegisterDeadOrAliveObserver() failed to locate channel"); |
| 764 | return -1; |
| 765 | } |
| 766 | return channelPtr->DeRegisterDeadOrAliveObserver(); |
| 767 | } |
| 768 | |
| 769 | int VoENetworkImpl::SetPeriodicDeadOrAliveStatus(int channel, bool enable, |
| 770 | int sampleTimeSeconds) |
| 771 | { |
| 772 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 773 | "SetPeriodicDeadOrAliveStatus(channel=%d, enable=%d," |
| 774 | " sampleTimeSeconds=%d)", |
| 775 | channel, enable, sampleTimeSeconds); |
| 776 | if (!_shared->statistics().Initialized()) |
| 777 | { |
| 778 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 779 | return -1; |
| 780 | } |
| 781 | if (enable && |
| 782 | ((sampleTimeSeconds < kVoiceEngineMinSampleTimeSec) || |
| 783 | (sampleTimeSeconds > kVoiceEngineMaxSampleTimeSec))) |
| 784 | { |
| 785 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 786 | "SetPeriodicDeadOrAliveStatus() invalid sample time"); |
| 787 | return -1; |
| 788 | } |
| 789 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 790 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 791 | if (channelPtr == NULL) |
| 792 | { |
| 793 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 794 | "SetPeriodicDeadOrAliveStatus() failed to locate channel"); |
| 795 | return -1; |
| 796 | } |
| 797 | return channelPtr->SetPeriodicDeadOrAliveStatus(enable, sampleTimeSeconds); |
| 798 | } |
| 799 | |
| 800 | int VoENetworkImpl::GetPeriodicDeadOrAliveStatus(int channel, |
| 801 | bool& enabled, |
| 802 | int& sampleTimeSeconds) |
| 803 | { |
| 804 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 805 | "GetPeriodicDeadOrAliveStatus(channel=%d, enabled=?," |
| 806 | " sampleTimeSeconds=?)", channel); |
| 807 | if (!_shared->statistics().Initialized()) |
| 808 | { |
| 809 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 810 | return -1; |
| 811 | } |
| 812 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 813 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 814 | if (channelPtr == NULL) |
| 815 | { |
| 816 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 817 | "GetPeriodicDeadOrAliveStatus() failed to locate channel"); |
| 818 | return -1; |
| 819 | } |
| 820 | return channelPtr->GetPeriodicDeadOrAliveStatus(enabled, |
| 821 | sampleTimeSeconds); |
| 822 | } |
| 823 | |
| 824 | int VoENetworkImpl::SendUDPPacket(int channel, |
| 825 | const void* data, |
| 826 | unsigned int length, |
| 827 | int& transmittedBytes, |
| 828 | bool useRtcpSocket) |
| 829 | { |
| 830 | WEBRTC_TRACE(kTraceApiCall, kTraceVoice, VoEId(_shared->instance_id(), -1), |
| 831 | "SendUDPPacket(channel=%d, data=0x%x, length=%u, useRTCP=%d)", |
| 832 | channel, data, length, useRtcpSocket); |
| 833 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 834 | if (!_shared->statistics().Initialized()) |
| 835 | { |
| 836 | _shared->SetLastError(VE_NOT_INITED, kTraceError); |
| 837 | return -1; |
| 838 | } |
| 839 | if (NULL == data) |
| 840 | { |
| 841 | _shared->SetLastError(VE_INVALID_ARGUMENT, kTraceError, |
| 842 | "SendUDPPacket() invalid data buffer"); |
| 843 | return -1; |
| 844 | } |
| 845 | if (0 == length) |
| 846 | { |
| 847 | _shared->SetLastError(VE_INVALID_PACKET, kTraceError, |
| 848 | "SendUDPPacket() invalid packet size"); |
| 849 | return -1; |
| 850 | } |
| 851 | voe::ScopedChannel sc(_shared->channel_manager(), channel); |
| 852 | voe::Channel* channelPtr = sc.ChannelPtr(); |
| 853 | if (channelPtr == NULL) |
| 854 | { |
| 855 | _shared->SetLastError(VE_CHANNEL_NOT_VALID, kTraceError, |
| 856 | "SendUDPPacket() failed to locate channel"); |
| 857 | return -1; |
| 858 | } |
| 859 | return channelPtr->SendUDPPacket(data, |
| 860 | length, |
| 861 | transmittedBytes, |
| 862 | useRtcpSocket); |
| 863 | #else |
| 864 | _shared->SetLastError(VE_EXTERNAL_TRANSPORT_ENABLED, kTraceWarning, |
| 865 | "SendUDPPacket() VoE is built for external transport"); |
| 866 | return -1; |
| 867 | #endif |
| 868 | } |
| 869 | |
| 870 | #endif // WEBRTC_VOICE_ENGINE_NETWORK_API |
| 871 | |
| 872 | } // namespace webrtc |