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 | |
pwestin@webrtc.org | e561f8c | 2013-04-02 20:37:14 +0000 | [diff] [blame] | 11 | #include "webrtc/video_engine/vie_network_impl.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 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 | |
pwestin@webrtc.org | e561f8c | 2013-04-02 20:37:14 +0000 | [diff] [blame] | 18 | #include "webrtc/engine_configurations.h" |
| 19 | #include "webrtc/system_wrappers/interface/trace.h" |
| 20 | #include "webrtc/video_engine/include/vie_errors.h" |
| 21 | #include "webrtc/video_engine/vie_channel.h" |
| 22 | #include "webrtc/video_engine/vie_channel_manager.h" |
| 23 | #include "webrtc/video_engine/vie_defines.h" |
| 24 | #include "webrtc/video_engine/vie_encoder.h" |
| 25 | #include "webrtc/video_engine/vie_impl.h" |
| 26 | #include "webrtc/video_engine/vie_shared_data.h" |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 27 | |
| 28 | namespace webrtc { |
| 29 | |
| 30 | ViENetwork* ViENetwork::GetInterface(VideoEngine* video_engine) { |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 31 | if (!video_engine) { |
| 32 | return NULL; |
| 33 | } |
| 34 | VideoEngineImpl* vie_impl = reinterpret_cast<VideoEngineImpl*>(video_engine); |
| 35 | ViENetworkImpl* vie_networkImpl = vie_impl; |
| 36 | // Increase ref count. |
| 37 | (*vie_networkImpl)++; |
| 38 | return vie_networkImpl; |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | int ViENetworkImpl::Release() { |
| 42 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(), |
| 43 | "ViENetwork::Release()"); |
| 44 | // Decrease ref count. |
| 45 | (*this)--; |
| 46 | |
pbos@webrtc.org | 2a5d229 | 2013-04-09 13:41:51 +0000 | [diff] [blame] | 47 | int32_t ref_count = GetCount(); |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 48 | if (ref_count < 0) { |
| 49 | WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(), |
| 50 | "ViENetwork release too many times"); |
| 51 | shared_data_->SetLastError(kViEAPIDoesNotExist); |
| 52 | return -1; |
| 53 | } |
| 54 | WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(), |
| 55 | "ViENetwork reference count: %d", ref_count); |
| 56 | return ref_count; |
| 57 | } |
| 58 | |
stefan@webrtc.org | e3eea1b | 2013-03-27 16:36:01 +0000 | [diff] [blame] | 59 | void ViENetworkImpl::SetNetworkTransmissionState(const int video_channel, |
| 60 | const bool is_transmitting) { |
| 61 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 62 | ViEId(shared_data_->instance_id(), video_channel), |
| 63 | "%s(event: Network %s)", __FUNCTION__, |
| 64 | is_transmitting ? "transmitting" : "not transmitting"); |
| 65 | if (!shared_data_->Initialized()) { |
| 66 | shared_data_->SetLastError(kViENotInitialized); |
| 67 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 68 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 69 | shared_data_->instance_id()); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 74 | ViEEncoder* vie_encoder = cs.Encoder(video_channel); |
| 75 | if (!vie_encoder) { |
| 76 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 77 | ViEId(shared_data_->instance_id(), video_channel), |
| 78 | "An encoder doesn't exist for this channel"); |
| 79 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 80 | return; |
| 81 | } |
| 82 | vie_encoder->SetNetworkTransmissionState(is_transmitting); |
| 83 | } |
| 84 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 85 | ViENetworkImpl::ViENetworkImpl(ViESharedData* shared_data) |
| 86 | : shared_data_(shared_data) { |
| 87 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 88 | "ViENetworkImpl::ViENetworkImpl() Ctor"); |
| 89 | } |
| 90 | |
| 91 | ViENetworkImpl::~ViENetworkImpl() { |
| 92 | WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(), |
| 93 | "ViENetworkImpl::~ViENetworkImpl() Dtor"); |
| 94 | } |
| 95 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 96 | int ViENetworkImpl::RegisterSendTransport(const int video_channel, |
| 97 | Transport& transport) { |
| 98 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 99 | ViEId(shared_data_->instance_id(), video_channel), |
| 100 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 101 | if (!shared_data_->Initialized()) { |
| 102 | shared_data_->SetLastError(kViENotInitialized); |
| 103 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 104 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 105 | shared_data_->instance_id()); |
| 106 | return -1; |
| 107 | } |
| 108 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 109 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 110 | if (!vie_channel) { |
| 111 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 112 | ViEId(shared_data_->instance_id(), video_channel), |
| 113 | "%s Channel doesn't exist", __FUNCTION__); |
| 114 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 115 | return -1; |
| 116 | } |
| 117 | if (vie_channel->Sending()) { |
| 118 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 119 | ViEId(shared_data_->instance_id(), video_channel), |
| 120 | "%s Channel already sending.", __FUNCTION__); |
| 121 | shared_data_->SetLastError(kViENetworkAlreadySending); |
| 122 | return -1; |
| 123 | } |
| 124 | if (vie_channel->RegisterSendTransport(&transport) != 0) { |
| 125 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 126 | return -1; |
| 127 | } |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | int ViENetworkImpl::DeregisterSendTransport(const int video_channel) { |
| 132 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 133 | ViEId(shared_data_->instance_id(), video_channel), |
| 134 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 135 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 136 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 137 | if (!vie_channel) { |
| 138 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 139 | ViEId(shared_data_->instance_id(), video_channel), |
| 140 | "%s Channel doesn't exist", __FUNCTION__); |
| 141 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 142 | return -1; |
| 143 | } |
| 144 | if (vie_channel->Sending()) { |
| 145 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 146 | ViEId(shared_data_->instance_id(), video_channel), |
| 147 | "%s Channel already sending", __FUNCTION__); |
| 148 | shared_data_->SetLastError(kViENetworkAlreadySending); |
| 149 | return -1; |
| 150 | } |
| 151 | if (vie_channel->DeregisterSendTransport() != 0) { |
| 152 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 153 | return -1; |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int ViENetworkImpl::ReceivedRTPPacket(const int video_channel, const void* data, |
| 159 | const int length) { |
| 160 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 161 | ViEId(shared_data_->instance_id(), video_channel), |
| 162 | "%s(channel: %d, data: -, length: %d)", __FUNCTION__, |
| 163 | video_channel, length); |
| 164 | if (!shared_data_->Initialized()) { |
| 165 | shared_data_->SetLastError(kViENotInitialized); |
| 166 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 167 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 168 | shared_data_->instance_id()); |
| 169 | return -1; |
| 170 | } |
| 171 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 172 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 173 | if (!vie_channel) { |
| 174 | // The channel doesn't exists |
| 175 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 176 | ViEId(shared_data_->instance_id(), video_channel), |
| 177 | "Channel doesn't exist"); |
| 178 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 179 | return -1; |
| 180 | } |
| 181 | return vie_channel->ReceivedRTPPacket(data, length); |
| 182 | } |
| 183 | |
| 184 | int ViENetworkImpl::ReceivedRTCPPacket(const int video_channel, |
| 185 | const void* data, const int length) { |
| 186 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 187 | ViEId(shared_data_->instance_id(), video_channel), |
| 188 | "%s(channel: %d, data: -, length: %d)", __FUNCTION__, |
| 189 | video_channel, length); |
| 190 | if (!shared_data_->Initialized()) { |
| 191 | shared_data_->SetLastError(kViENotInitialized); |
| 192 | WEBRTC_TRACE(kTraceError, kTraceVideo, ViEId(shared_data_->instance_id()), |
| 193 | "%s - ViE instance %d not initialized", __FUNCTION__, |
| 194 | shared_data_->instance_id()); |
| 195 | return -1; |
| 196 | } |
| 197 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 198 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 199 | if (!vie_channel) { |
| 200 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 201 | ViEId(shared_data_->instance_id(), video_channel), |
| 202 | "Channel doesn't exist"); |
| 203 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 204 | return -1; |
| 205 | } |
| 206 | return vie_channel->ReceivedRTCPPacket(data, length); |
| 207 | } |
| 208 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 209 | int ViENetworkImpl::SetMTU(int video_channel, unsigned int mtu) { |
| 210 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 211 | ViEId(shared_data_->instance_id(), video_channel), |
| 212 | "%s(channel: %d, mtu: %u)", __FUNCTION__, video_channel, mtu); |
| 213 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 214 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 215 | if (!vie_channel) { |
| 216 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 217 | ViEId(shared_data_->instance_id(), video_channel), |
| 218 | "Channel doesn't exist"); |
| 219 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 220 | return -1; |
| 221 | } |
| 222 | if (vie_channel->SetMTU(mtu) != 0) { |
| 223 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 224 | return -1; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int ViENetworkImpl::SetPacketTimeoutNotification(const int video_channel, |
| 230 | bool enable, |
| 231 | int timeout_seconds) { |
| 232 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 233 | ViEId(shared_data_->instance_id(), video_channel), |
| 234 | "%s(channel: %d, enable: %d, timeout_seconds: %u)", |
| 235 | __FUNCTION__, video_channel, enable, timeout_seconds); |
| 236 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 237 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 238 | if (!vie_channel) { |
| 239 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 240 | ViEId(shared_data_->instance_id(), video_channel), |
| 241 | "Channel doesn't exist"); |
| 242 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 243 | return -1; |
| 244 | } |
| 245 | if (vie_channel->SetPacketTimeoutNotification(enable, |
| 246 | timeout_seconds) != 0) { |
| 247 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 248 | return -1; |
| 249 | } |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | int ViENetworkImpl::RegisterObserver(const int video_channel, |
| 254 | ViENetworkObserver& observer) { |
| 255 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 256 | ViEId(shared_data_->instance_id(), video_channel), |
| 257 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 258 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 259 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 260 | if (!vie_channel) { |
| 261 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 262 | ViEId(shared_data_->instance_id(), video_channel), |
| 263 | "Channel doesn't exist"); |
| 264 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 265 | return -1; |
| 266 | } |
| 267 | if (vie_channel->RegisterNetworkObserver(&observer) != 0) { |
| 268 | shared_data_->SetLastError(kViENetworkObserverAlreadyRegistered); |
| 269 | return -1; |
| 270 | } |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | int ViENetworkImpl::DeregisterObserver(const int video_channel) { |
| 275 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 276 | ViEId(shared_data_->instance_id(), video_channel), |
| 277 | "%s(channel: %d)", __FUNCTION__, video_channel); |
| 278 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 279 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 280 | if (!vie_channel) { |
| 281 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 282 | ViEId(shared_data_->instance_id(), video_channel), |
| 283 | "Channel doesn't exist"); |
| 284 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 285 | return -1; |
| 286 | } |
| 287 | if (!vie_channel->NetworkObserverRegistered()) { |
| 288 | shared_data_->SetLastError(kViENetworkObserverNotRegistered); |
| 289 | return -1; |
| 290 | } |
| 291 | return vie_channel->RegisterNetworkObserver(NULL); |
| 292 | } |
| 293 | |
| 294 | int ViENetworkImpl::SetPeriodicDeadOrAliveStatus( |
| 295 | const int video_channel, |
| 296 | bool enable, |
| 297 | unsigned int sample_time_seconds) { |
| 298 | WEBRTC_TRACE(kTraceApiCall, kTraceVideo, |
| 299 | ViEId(shared_data_->instance_id(), video_channel), |
| 300 | "%s(channel: %d, enable: %d, sample_time_seconds: %ul)", |
| 301 | __FUNCTION__, video_channel, enable, sample_time_seconds); |
| 302 | ViEChannelManagerScoped cs(*(shared_data_->channel_manager())); |
| 303 | ViEChannel* vie_channel = cs.Channel(video_channel); |
| 304 | if (!vie_channel) { |
| 305 | WEBRTC_TRACE(kTraceError, kTraceVideo, |
| 306 | ViEId(shared_data_->instance_id(), video_channel), |
| 307 | "Channel doesn't exist"); |
| 308 | shared_data_->SetLastError(kViENetworkInvalidChannelId); |
| 309 | return -1; |
| 310 | } |
| 311 | if (!vie_channel->NetworkObserverRegistered()) { |
| 312 | shared_data_->SetLastError(kViENetworkObserverNotRegistered); |
| 313 | return -1; |
| 314 | } |
| 315 | if (vie_channel->SetPeriodicDeadOrAliveStatus(enable, sample_time_seconds) |
| 316 | != 0) { |
| 317 | shared_data_->SetLastError(kViENetworkUnknownError); |
| 318 | return -1; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
andrew@webrtc.org | a7b57da | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 323 | } // namespace webrtc |