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 "video_engine/vie_network_impl.h" |
| 12 | |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 13 | #include <stdio.h> |
| 14 | #if (defined(WIN32_) || defined(WIN64_)) |
| 15 | #include <qos.h> |
| 16 | #endif |
| 17 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 18 | #include "engine_configurations.h" // NOLINT |
| 19 | #include "system_wrappers/interface/trace.h" |
| 20 | #include "video_engine/include/vie_errors.h" |
| 21 | #include "video_engine/vie_channel.h" |
| 22 | #include "video_engine/vie_channel_manager.h" |
| 23 | #include "video_engine/vie_defines.h" |
| 24 | #include "video_engine/vie_encoder.h" |
| 25 | #include "video_engine/vie_impl.h" |
| 26 | #include "video_engine/vie_shared_data.h" |
| 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | ViENetwork* ViENetwork::GetInterface(VideoEngine* video_engine) { |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 31 | #ifdef WEBRTC_VIDEO_ENGINE_NETWORK_API |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 32 | if (!video_engine) { |
| 33 | return NULL; |
| 34 | } |
| 35 | VideoEngineImpl* vie_impl = reinterpret_cast<VideoEngineImpl*>(video_engine); |
| 36 | ViENetworkImpl* vie_networkImpl = vie_impl; |
| 37 | // Increase ref count. |
| 38 | (*vie_networkImpl)++; |
| 39 | return vie_networkImpl; |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 40 | #else |
| 41 | return NULL; |
| 42 | #endif |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int ViENetworkImpl::Release() { |
| 46 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(), |
| 47 | "ViENetwork::Release()"); |
| 48 | // Decrease ref count. |
| 49 | (*this)--; |
| 50 | |
| 51 | WebRtc_Word32 ref_count = GetCount(); |
| 52 | if (ref_count < 0) { |
| 53 | WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(), |
| 54 | "ViENetwork release too many times"); |
| 55 | shared_data_->SetLastError(kViEAPIDoesNotExist); |
| 56 | return -1; |
| 57 | } |
| 58 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(), |
| 59 | "ViENetwork reference count: %d", ref_count); |
| 60 | return ref_count; |
| 61 | } |
| 62 | |
| 63 | ViENetworkImpl::ViENetworkImpl(ViESharedData* shared_data) |
| 64 | : shared_data_(shared_data) { |
| 65 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 66 | "ViENetworkImpl::ViENetworkImpl() Ctor"); |
| 67 | } |
| 68 | |
| 69 | ViENetworkImpl::~ViENetworkImpl() { |
| 70 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 71 | "ViENetworkImpl::~ViENetworkImpl() Dtor"); |
| 72 | } |
| 73 | |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 74 | int ViENetworkImpl::SetLocalReceiver(const int video_channel, |
| 75 | const uint16_t rtp_port, |
| 76 | const uint16_t rtcp_port, |
| 77 | const char* ip_address) { |
| 78 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 79 | ViEId(shared_data_->instance_id(), video_channel), |
| 80 | "%s(channel: %d, rtp_port: %u, rtcp_port: %u, ip_address: %s)", |
| 81 | __FUNCTION__, video_channel, rtp_port, rtcp_port, ip_address); |
| 82 | if (!shared_data_->Initialized()) { |
| 83 | shared_data_->SetLastError(kViENotInitialized); |
| 84 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 85 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 86 | shared_data_->instance_id()); |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 91 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 92 | if (!vie_channel) { |
| 93 | // The channel doesn't exists. |
| 94 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 95 | ViEId(shared_data_->instance_id(), video_channel), |
| 96 | "Channel doesn't exist"); |
| 97 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | if (vie_channel->Receiving()) { |
| 102 | shared_data_->SetLastError(kViENetworkAlreadyReceiving); |
| 103 | return -1; |
| 104 | } |
| 105 | if (vie_channel->SetLocalReceiver(rtp_port, rtcp_port, ip_address) != 0) { |
| 106 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 107 | return -1; |
| 108 | } |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | int ViENetworkImpl::GetLocalReceiver(const int video_channel, |
| 113 | uint16_t& rtp_port, |
| 114 | uint16_t& rtcp_port, |
| 115 | char* ip_address) { |
| 116 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 117 | ViEId(shared_data_->instance_id(), video_channel), |
| 118 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 119 | |
| 120 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 121 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 122 | if (!vie_channel) { |
| 123 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 124 | ViEId(shared_data_->instance_id(), video_channel), |
| 125 | "Channel doesn't exist"); |
| 126 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 127 | return -1; |
| 128 | } |
| 129 | if (vie_channel->GetLocalReceiver(&rtp_port, &rtcp_port, ip_address) != 0) { |
| 130 | shared_data_->SetLastError(kViENetworkLocalReceiverNotSet); |
| 131 | return -1; |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int ViENetworkImpl::SetSendDestination(const int video_channel, |
| 137 | const char* ip_address, |
| 138 | const uint16_t rtp_port, |
| 139 | const uint16_t rtcp_port, |
| 140 | const uint16_t source_rtp_port, |
| 141 | const uint16_t source_rtcp_port) { |
| 142 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 143 | ViEId(shared_data_->instance_id(), video_channel), |
| 144 | "%s(channel: %d, ip_address: %s, rtp_port: %u, rtcp_port: %u, " |
| 145 | "sourceRtpPort: %u, source_rtcp_port: %u)", |
| 146 | __FUNCTION__, video_channel, ip_address, rtp_port, rtcp_port, |
| 147 | source_rtp_port, source_rtcp_port); |
| 148 | if (!shared_data_->Initialized()) { |
| 149 | shared_data_->SetLastError(kViENotInitialized); |
| 150 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 151 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 152 | shared_data_->instance_id()); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 157 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 158 | if (!vie_channel) { |
| 159 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 160 | ViEId(shared_data_->instance_id(), video_channel), |
| 161 | "%s Channel doesn't exist", __FUNCTION__); |
| 162 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 163 | return -1; |
| 164 | } |
| 165 | if (vie_channel->Sending()) { |
| 166 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 167 | ViEId(shared_data_->instance_id(), video_channel), |
| 168 | "%s Channel already sending.", __FUNCTION__); |
| 169 | shared_data_->SetLastError(kViENetworkAlreadySending); |
| 170 | return -1; |
| 171 | } |
| 172 | if (vie_channel->SetSendDestination(ip_address, rtp_port, rtcp_port, |
| 173 | source_rtp_port, |
| 174 | source_rtcp_port) != 0) { |
| 175 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 176 | return -1; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int ViENetworkImpl::GetSendDestination(const int video_channel, |
| 182 | char* ip_address, |
| 183 | uint16_t& rtp_port, |
| 184 | uint16_t& rtcp_port, |
| 185 | uint16_t& source_rtp_port, |
| 186 | uint16_t& source_rtcp_port) { |
| 187 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 188 | ViEId(shared_data_->instance_id(), video_channel), |
| 189 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 190 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 191 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 192 | if (!vie_channel) { |
| 193 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 194 | ViEId(shared_data_->instance_id(), video_channel), |
| 195 | "Channel doesn't exist"); |
| 196 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 197 | return -1; |
| 198 | } |
| 199 | if (vie_channel->GetSendDestination(ip_address, &rtp_port, &rtcp_port, |
| 200 | &source_rtp_port, |
| 201 | &source_rtcp_port) != 0) { |
| 202 | shared_data_->SetLastError(kViENetworkDestinationNotSet); |
| 203 | return -1; |
| 204 | } |
| 205 | return 0; |
| 206 | } |
| 207 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 208 | int ViENetworkImpl::RegisterSendTransport(const int video_channel, |
| 209 | Transport& transport) { |
| 210 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 211 | ViEId(shared_data_->instance_id(), video_channel), |
| 212 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 213 | if (!shared_data_->Initialized()) { |
| 214 | shared_data_->SetLastError(kViENotInitialized); |
| 215 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 216 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 217 | shared_data_->instance_id()); |
| 218 | return -1; |
| 219 | } |
| 220 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 221 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 222 | if (!vie_channel) { |
| 223 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 224 | ViEId(shared_data_->instance_id(), video_channel), |
| 225 | "%s Channel doesn't exist", __FUNCTION__); |
| 226 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 227 | return -1; |
| 228 | } |
| 229 | if (vie_channel->Sending()) { |
| 230 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 231 | ViEId(shared_data_->instance_id(), video_channel), |
| 232 | "%s Channel already sending.", __FUNCTION__); |
| 233 | shared_data_->SetLastError(kViENetworkAlreadySending); |
| 234 | return -1; |
| 235 | } |
| 236 | if (vie_channel->RegisterSendTransport(&transport) != 0) { |
| 237 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 238 | return -1; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | int ViENetworkImpl::DeregisterSendTransport(const int video_channel) { |
| 244 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 245 | ViEId(shared_data_->instance_id(), video_channel), |
| 246 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 247 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 248 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 249 | if (!vie_channel) { |
| 250 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 251 | ViEId(shared_data_->instance_id(), video_channel), |
| 252 | "%s Channel doesn't exist", __FUNCTION__); |
| 253 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 254 | return -1; |
| 255 | } |
| 256 | if (vie_channel->Sending()) { |
| 257 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 258 | ViEId(shared_data_->instance_id(), video_channel), |
| 259 | "%s Channel already sending", __FUNCTION__); |
| 260 | shared_data_->SetLastError(kViENetworkAlreadySending); |
| 261 | return -1; |
| 262 | } |
| 263 | if (vie_channel->DeregisterSendTransport() != 0) { |
| 264 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 265 | return -1; |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | int ViENetworkImpl::ReceivedRTPPacket(const int video_channel, const void* data, |
| 271 | const int length) { |
| 272 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 273 | ViEId(shared_data_->instance_id(), video_channel), |
| 274 | "%s(channel: %d, data: -, length: %d)", __FUNCTION__, |
| 275 | video_channel, length); |
| 276 | if (!shared_data_->Initialized()) { |
| 277 | shared_data_->SetLastError(kViENotInitialized); |
| 278 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 279 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 280 | shared_data_->instance_id()); |
| 281 | return -1; |
| 282 | } |
| 283 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 284 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 285 | if (!vie_channel) { |
| 286 | // The channel doesn't exists |
| 287 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 288 | ViEId(shared_data_->instance_id(), video_channel), |
| 289 | "Channel doesn't exist"); |
| 290 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 291 | return -1; |
| 292 | } |
| 293 | return vie_channel->ReceivedRTPPacket(data, length); |
| 294 | } |
| 295 | |
| 296 | int ViENetworkImpl::ReceivedRTCPPacket(const int video_channel, |
| 297 | const void* data, const int length) { |
| 298 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 299 | ViEId(shared_data_->instance_id(), video_channel), |
| 300 | "%s(channel: %d, data: -, length: %d)", __FUNCTION__, |
| 301 | video_channel, length); |
| 302 | if (!shared_data_->Initialized()) { |
| 303 | shared_data_->SetLastError(kViENotInitialized); |
| 304 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 305 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 306 | shared_data_->instance_id()); |
| 307 | return -1; |
| 308 | } |
| 309 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 310 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 311 | if (!vie_channel) { |
| 312 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 313 | ViEId(shared_data_->instance_id(), video_channel), |
| 314 | "Channel doesn't exist"); |
| 315 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 316 | return -1; |
| 317 | } |
| 318 | return vie_channel->ReceivedRTCPPacket(data, length); |
| 319 | } |
| 320 | |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 321 | int ViENetworkImpl::GetSourceInfo(const int video_channel, |
| 322 | uint16_t& rtp_port, |
| 323 | uint16_t& rtcp_port, char* ip_address, |
| 324 | unsigned int ip_address_length) { |
| 325 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 326 | ViEId(shared_data_->instance_id(), video_channel), |
| 327 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 328 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 329 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 330 | if (!vie_channel) { |
| 331 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 332 | ViEId(shared_data_->instance_id(), video_channel), |
| 333 | "Channel doesn't exist"); |
| 334 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 335 | return -1; |
| 336 | } |
| 337 | if (vie_channel->GetSourceInfo(&rtp_port, &rtcp_port, ip_address, |
| 338 | ip_address_length) != 0) { |
| 339 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 340 | return -1; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | int ViENetworkImpl::GetLocalIP(char ip_address[64], bool ipv6) { |
| 346 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 347 | "%s( ip_address, ipV6: %d)", __FUNCTION__, ipv6); |
| 348 | |
| 349 | #ifndef WEBRTC_EXTERNAL_TRANSPORT |
| 350 | if (!shared_data_->Initialized()) { |
| 351 | shared_data_->SetLastError(kViENotInitialized); |
| 352 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 353 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 354 | shared_data_->instance_id()); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | if (!ip_address) { |
| 359 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 360 | "%s: No argument", __FUNCTION__); |
| 361 | shared_data_->SetLastError(kViENetworkInvalidArgument); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | WebRtc_UWord8 num_socket_threads = 1; |
| 366 | UdpTransport* socket_transport = UdpTransport::Create( |
| 367 | ViEModuleId(shared_data_->instance_id(), -1), num_socket_threads); |
| 368 | |
| 369 | if (!socket_transport) { |
| 370 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 371 | "%s: Could not create socket module", __FUNCTION__); |
| 372 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | char local_ip_address[64]; |
| 377 | if (ipv6) { |
| 378 | char local_ip[16]; |
| 379 | if (socket_transport->LocalHostAddressIPV6(local_ip) != 0) { |
| 380 | UdpTransport::Destroy(socket_transport); |
| 381 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 382 | "%s: Could not get local IP", __FUNCTION__); |
| 383 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 384 | return -1; |
| 385 | } |
| 386 | // Convert 128-bit address to character string (a:b:c:d:e:f:g:h). |
| 387 | // TODO(mflodman) Change sprintf. |
| 388 | sprintf(local_ip_address, // NOLINT |
| 389 | "%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:" |
| 390 | "%.2x%.2x", |
| 391 | local_ip[0], local_ip[1], local_ip[2], local_ip[3], local_ip[4], |
| 392 | local_ip[5], local_ip[6], local_ip[7], local_ip[8], local_ip[9], |
| 393 | local_ip[10], local_ip[11], local_ip[12], local_ip[13], |
| 394 | local_ip[14], local_ip[15]); |
| 395 | } else { |
| 396 | WebRtc_UWord32 local_ip = 0; |
| 397 | if (socket_transport->LocalHostAddress(local_ip) != 0) { |
| 398 | UdpTransport::Destroy(socket_transport); |
| 399 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 400 | "%s: Could not get local IP", __FUNCTION__); |
| 401 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 402 | return -1; |
| 403 | } |
| 404 | // Convert 32-bit address to character string (x.y.z.w). |
| 405 | // TODO(mflodman) Change sprintf. |
| 406 | sprintf(local_ip_address, "%d.%d.%d.%d", // NOLINT |
| 407 | static_cast<int>((local_ip >> 24) & 0x0ff), |
| 408 | static_cast<int>((local_ip >> 16) & 0x0ff), |
| 409 | static_cast<int>((local_ip >> 8) & 0x0ff), |
| 410 | static_cast<int>(local_ip & 0x0ff)); |
| 411 | } |
| 412 | strncpy(ip_address, local_ip_address, sizeof(local_ip_address)); |
| 413 | UdpTransport::Destroy(socket_transport); |
| 414 | WEBRTC_TRACE(kTraceStateInfo, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 415 | "%s: local ip = %s", __FUNCTION__, local_ip_address); |
| 416 | return 0; |
| 417 | #else |
| 418 | WEBRTC_TRACE(kTraceStateInfo, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 419 | "%s: not available for external transport", __FUNCTION__); |
| 420 | |
| 421 | return -1; |
| 422 | #endif |
| 423 | } |
| 424 | |
| 425 | int ViENetworkImpl::EnableIPv6(int video_channel) { |
| 426 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 427 | ViEId(shared_data_->instance_id(), video_channel), |
| 428 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 429 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 430 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 431 | if (!vie_channel) { |
| 432 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 433 | ViEId(shared_data_->instance_id(), video_channel), |
| 434 | "Channel doesn't exist"); |
| 435 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 436 | return -1; |
| 437 | } |
| 438 | if (vie_channel->EnableIPv6() != 0) { |
| 439 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 440 | return -1; |
| 441 | } |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | bool ViENetworkImpl::IsIPv6Enabled(int video_channel) { |
| 446 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 447 | ViEId(shared_data_->instance_id(), video_channel), |
| 448 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 449 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 450 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 451 | if (!vie_channel) { |
| 452 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 453 | ViEId(shared_data_->instance_id(), video_channel), |
| 454 | "Channel doesn't exist"); |
| 455 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 456 | return false; |
| 457 | } |
| 458 | return vie_channel->IsIPv6Enabled(); |
| 459 | } |
| 460 | |
| 461 | int ViENetworkImpl::SetSourceFilter(const int video_channel, |
| 462 | const uint16_t rtp_port, |
| 463 | const uint16_t rtcp_port, |
| 464 | const char* ip_address) { |
| 465 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 466 | ViEId(shared_data_->instance_id(), video_channel), |
| 467 | "%s(channel: %d, rtp_port: %u, rtcp_port: %u, ip_address: %s)", |
| 468 | __FUNCTION__, video_channel, rtp_port, rtcp_port, ip_address); |
| 469 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 470 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 471 | if (!vie_channel) { |
| 472 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 473 | ViEId(shared_data_->instance_id(), video_channel), |
| 474 | "Channel doesn't exist"); |
| 475 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 476 | return -1; |
| 477 | } |
| 478 | if (vie_channel->SetSourceFilter(rtp_port, rtcp_port, ip_address) != 0) { |
| 479 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 480 | return -1; |
| 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | int ViENetworkImpl::GetSourceFilter(const int video_channel, |
| 486 | uint16_t& rtp_port, |
| 487 | uint16_t& rtcp_port, |
| 488 | char* ip_address) { |
| 489 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 490 | ViEId(shared_data_->instance_id(), video_channel), |
| 491 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 492 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 493 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 494 | if (!vie_channel) { |
| 495 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 496 | ViEId(shared_data_->instance_id(), video_channel), |
| 497 | "Channel doesn't exist"); |
| 498 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 499 | return -1; |
| 500 | } |
| 501 | if (vie_channel->GetSourceFilter(&rtp_port, &rtcp_port, ip_address) != 0) { |
| 502 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 503 | return -1; |
| 504 | } |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | int ViENetworkImpl::SetSendToS(const int video_channel, const int DSCP, |
| 509 | const bool use_set_sockOpt = false) { |
| 510 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 511 | ViEId(shared_data_->instance_id(), video_channel), |
| 512 | "%s(channel: %d, DSCP: %d, use_set_sockOpt: %d)", __FUNCTION__, |
| 513 | video_channel, DSCP, use_set_sockOpt); |
| 514 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 515 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 516 | if (!vie_channel) { |
| 517 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 518 | ViEId(shared_data_->instance_id(), video_channel), |
| 519 | "Channel doesn't exist"); |
| 520 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 521 | return -1; |
| 522 | } |
| 523 | |
| 524 | #if defined(WEBRTC_LINUX) || defined(WEBRTC_MAC) |
| 525 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, |
| 526 | ViEId(shared_data_->instance_id(), video_channel), |
| 527 | " force use_set_sockopt=true since there is no alternative" |
| 528 | " implementation"); |
| 529 | if (vie_channel->SetToS(DSCP, true) != 0) { |
| 530 | #else |
| 531 | if (vie_channel->SetToS(DSCP, use_set_sockOpt) != 0) { |
| 532 | #endif |
| 533 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 534 | return -1; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | int ViENetworkImpl::GetSendToS(const int video_channel, |
| 540 | int& DSCP, // NOLINT |
| 541 | bool& use_set_sockOpt) { // NOLINT |
| 542 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 543 | ViEId(shared_data_->instance_id(), video_channel), |
| 544 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 545 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 546 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 547 | if (!vie_channel) { |
| 548 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 549 | ViEId(shared_data_->instance_id(), video_channel), |
| 550 | "Channel doesn't exist"); |
| 551 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 552 | return -1; |
| 553 | } |
| 554 | if (vie_channel->GetToS(&DSCP, &use_set_sockOpt) != 0) { |
| 555 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 556 | return -1; |
| 557 | } |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | int ViENetworkImpl::SetSendGQoS(const int video_channel, const bool enable, |
| 562 | const int service_type, |
| 563 | const int overrideDSCP) { |
| 564 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 565 | ViEId(shared_data_->instance_id(), video_channel), |
| 566 | "%s(channel: %d, enable: %d, service_type: %d, " |
| 567 | "overrideDSCP: %d)", __FUNCTION__, video_channel, enable, |
| 568 | service_type, overrideDSCP); |
| 569 | if (!shared_data_->Initialized()) { |
| 570 | shared_data_->SetLastError(kViENotInitialized); |
| 571 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 572 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 573 | shared_data_->instance_id()); |
| 574 | return -1; |
| 575 | } |
| 576 | |
| 577 | #if (defined(WIN32_) || defined(WIN64_)) |
| 578 | // Sanity check. We might crash if testing and relying on an OS socket error. |
| 579 | if (enable && |
| 580 | (service_type != SERVICETYPE_BESTEFFORT) && |
| 581 | (service_type != SERVICETYPE_CONTROLLEDLOAD) && |
| 582 | (service_type != SERVICETYPE_GUARANTEED) && |
| 583 | (service_type != SERVICETYPE_QUALITATIVE)) { |
| 584 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 585 | ViEId(shared_data_->instance_id(), video_channel), |
| 586 | "%s: service type %d not supported", __FUNCTION__, |
| 587 | video_channel, service_type); |
| 588 | shared_data_->SetLastError(kViENetworkServiceTypeNotSupported); |
| 589 | return -1; |
| 590 | } |
| 591 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 592 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 593 | if (!vie_channel) { |
| 594 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 595 | ViEId(shared_data_->instance_id(), video_channel), |
| 596 | "Channel doesn't exist"); |
| 597 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 598 | return -1; |
| 599 | } |
| 600 | ViEEncoder* vie_encoder = cs.Encoder(video_channel); |
| 601 | if (!vie_encoder) { |
| 602 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 603 | ViEId(shared_data_->instance_id(), video_channel), |
| 604 | "Channel doesn't exist"); |
| 605 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 606 | return -1; |
| 607 | } |
| 608 | VideoCodec video_codec; |
| 609 | if (vie_encoder->GetEncoder(video_codec) != 0) { |
| 610 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 611 | ViEId(shared_data_->instance_id(), video_channel), |
| 612 | "%s: Could not get max bitrate for the channel", |
| 613 | __FUNCTION__); |
| 614 | shared_data_->SetLastError(kViENetworkSendCodecNotSet); |
| 615 | return -1; |
| 616 | } |
| 617 | if (vie_channel->SetSendGQoS(enable, service_type, video_codec.maxBitrate, |
| 618 | overrideDSCP) != 0) { |
| 619 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 620 | return -1; |
| 621 | } |
| 622 | return 0; |
| 623 | #else |
| 624 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 625 | ViEId(shared_data_->instance_id(), video_channel), |
| 626 | "%s: Not supported", __FUNCTION__); |
| 627 | shared_data_->SetLastError(kViENetworkNotSupported); |
| 628 | return -1; |
| 629 | #endif |
| 630 | } |
| 631 | |
| 632 | int ViENetworkImpl::GetSendGQoS(const int video_channel, |
| 633 | bool& enabled, // NOLINT |
| 634 | int& service_type, // NOLINT |
| 635 | int& overrideDSCP) { // NOLINT |
| 636 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 637 | ViEId(shared_data_->instance_id(), video_channel), |
| 638 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 639 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 640 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 641 | if (!vie_channel) { |
| 642 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 643 | ViEId(shared_data_->instance_id(), video_channel), |
| 644 | "Channel doesn't exist"); |
| 645 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 646 | return -1; |
| 647 | } |
| 648 | if (vie_channel->GetSendGQoS(&enabled, &service_type, &overrideDSCP) != 0) { |
| 649 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 650 | return -1; |
| 651 | } |
| 652 | return 0; |
| 653 | } |
| 654 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 655 | int ViENetworkImpl::SetMTU(int video_channel, unsigned int mtu) { |
| 656 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 657 | ViEId(shared_data_->instance_id(), video_channel), |
| 658 | "%s(channel: %d, mtu: %u)", __FUNCTION__, video_channel, mtu); |
| 659 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 660 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 661 | if (!vie_channel) { |
| 662 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 663 | ViEId(shared_data_->instance_id(), video_channel), |
| 664 | "Channel doesn't exist"); |
| 665 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 666 | return -1; |
| 667 | } |
| 668 | if (vie_channel->SetMTU(mtu) != 0) { |
| 669 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 670 | return -1; |
| 671 | } |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | int ViENetworkImpl::SetPacketTimeoutNotification(const int video_channel, |
| 676 | bool enable, |
| 677 | int timeout_seconds) { |
| 678 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 679 | ViEId(shared_data_->instance_id(), video_channel), |
| 680 | "%s(channel: %d, enable: %d, timeout_seconds: %u)", |
| 681 | __FUNCTION__, video_channel, enable, timeout_seconds); |
| 682 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 683 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 684 | if (!vie_channel) { |
| 685 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 686 | ViEId(shared_data_->instance_id(), video_channel), |
| 687 | "Channel doesn't exist"); |
| 688 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 689 | return -1; |
| 690 | } |
| 691 | if (vie_channel->SetPacketTimeoutNotification(enable, |
| 692 | timeout_seconds) != 0) { |
| 693 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 694 | return -1; |
| 695 | } |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | int ViENetworkImpl::RegisterObserver(const int video_channel, |
| 700 | ViENetworkObserver& observer) { |
| 701 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 702 | ViEId(shared_data_->instance_id(), video_channel), |
| 703 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 704 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 705 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 706 | if (!vie_channel) { |
| 707 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 708 | ViEId(shared_data_->instance_id(), video_channel), |
| 709 | "Channel doesn't exist"); |
| 710 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 711 | return -1; |
| 712 | } |
| 713 | if (vie_channel->RegisterNetworkObserver(&observer) != 0) { |
| 714 | shared_data_->SetLastError(kViENetworkObserverAlreadyRegistered); |
| 715 | return -1; |
| 716 | } |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | int ViENetworkImpl::DeregisterObserver(const int video_channel) { |
| 721 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 722 | ViEId(shared_data_->instance_id(), video_channel), |
| 723 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 724 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 725 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 726 | if (!vie_channel) { |
| 727 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 728 | ViEId(shared_data_->instance_id(), video_channel), |
| 729 | "Channel doesn't exist"); |
| 730 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 731 | return -1; |
| 732 | } |
| 733 | if (!vie_channel->NetworkObserverRegistered()) { |
| 734 | shared_data_->SetLastError(kViENetworkObserverNotRegistered); |
| 735 | return -1; |
| 736 | } |
| 737 | return vie_channel->RegisterNetworkObserver(NULL); |
| 738 | } |
| 739 | |
| 740 | int ViENetworkImpl::SetPeriodicDeadOrAliveStatus( |
| 741 | const int video_channel, |
| 742 | bool enable, |
| 743 | unsigned int sample_time_seconds) { |
| 744 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 745 | ViEId(shared_data_->instance_id(), video_channel), |
| 746 | "%s(channel: %d, enable: %d, sample_time_seconds: %ul)", |
| 747 | __FUNCTION__, video_channel, enable, sample_time_seconds); |
| 748 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 749 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 750 | if (!vie_channel) { |
| 751 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 752 | ViEId(shared_data_->instance_id(), video_channel), |
| 753 | "Channel doesn't exist"); |
| 754 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 755 | return -1; |
| 756 | } |
| 757 | if (!vie_channel->NetworkObserverRegistered()) { |
| 758 | shared_data_->SetLastError(kViENetworkObserverNotRegistered); |
| 759 | return -1; |
| 760 | } |
| 761 | if (vie_channel->SetPeriodicDeadOrAliveStatus(enable, sample_time_seconds) |
| 762 | != 0) { |
| 763 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 764 | return -1; |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
pwestin@webrtc.org | 9a7b9f7 | 2013-03-13 23:20:57 +0000 | [diff] [blame^] | 769 | int ViENetworkImpl::SendUDPPacket(const int video_channel, const void* data, |
| 770 | const unsigned int length, |
| 771 | int& transmitted_bytes, |
| 772 | bool use_rtcp_socket = false) { |
| 773 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 774 | ViEId(shared_data_->instance_id(), video_channel), |
| 775 | "%s(channel: %d, data: -, length: %d, transmitter_bytes: -, " |
| 776 | "useRtcpSocket: %d)", __FUNCTION__, video_channel, length, |
| 777 | use_rtcp_socket); |
| 778 | if (!shared_data_->Initialized()) { |
| 779 | shared_data_->SetLastError(kViENotInitialized); |
| 780 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 781 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 782 | shared_data_->instance_id()); |
| 783 | return -1; |
| 784 | } |
| 785 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 786 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 787 | if (!vie_channel) { |
| 788 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 789 | ViEId(shared_data_->instance_id(), video_channel), |
| 790 | "Channel doesn't exist"); |
| 791 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 792 | return -1; |
| 793 | } |
| 794 | if (vie_channel->SendUDPPacket((const WebRtc_Word8*) data, length, |
| 795 | (WebRtc_Word32&) transmitted_bytes, |
| 796 | use_rtcp_socket) < 0) { |
| 797 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 798 | return -1; |
| 799 | } |
| 800 | return 0; |
| 801 | } |
| 802 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 803 | } // namespace webrtc |