blob: 9965f9eddd3af5f491f07c0a96a8cef2732dc64e [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org07c68b92012-02-29 16:09:51 +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
pwestin@webrtc.org82dcc9f2013-04-02 20:37:14 +000011#include "webrtc/video_engine/vie_network_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pwestin@webrtc.org684f0572013-03-13 23:20:57 +000013#include <stdio.h>
14#if (defined(WIN32_) || defined(WIN64_))
15#include <qos.h>
16#endif
17
pwestin@webrtc.org82dcc9f2013-04-02 20:37:14 +000018#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"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000028namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000029
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000030ViENetwork* ViENetwork::GetInterface(VideoEngine* video_engine) {
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000031 if (!video_engine) {
niklase@google.com470e71d2011-07-07 08:21:25 +000032 return NULL;
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000033 }
andrew@webrtc.orgd72262d2013-05-09 02:12:07 +000034 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000035 ViENetworkImpl* vie_networkImpl = vie_impl;
36 // Increase ref count.
37 (*vie_networkImpl)++;
38 return vie_networkImpl;
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000041int ViENetworkImpl::Release() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000042 WEBRTC_TRACE(kTraceApiCall, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000043 "ViENetwork::Release()");
44 // Decrease ref count.
45 (*this)--;
niklase@google.com470e71d2011-07-07 08:21:25 +000046
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000047 int32_t ref_count = GetCount();
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000048 if (ref_count < 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000049 WEBRTC_TRACE(kTraceWarning, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000050 "ViENetwork release too many times");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000051 shared_data_->SetLastError(kViEAPIDoesNotExist);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000052 return -1;
53 }
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000054 WEBRTC_TRACE(kTraceInfo, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000055 "ViENetwork reference count: %d", ref_count);
56 return ref_count;
niklase@google.com470e71d2011-07-07 08:21:25 +000057}
58
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000059void 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");
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000065 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
66 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
67 if (!vie_encoder) {
68 WEBRTC_TRACE(kTraceError, kTraceVideo,
69 ViEId(shared_data_->instance_id(), video_channel),
70 "An encoder doesn't exist for this channel");
71 shared_data_->SetLastError(kViENetworkInvalidChannelId);
72 return;
73 }
74 vie_encoder->SetNetworkTransmissionState(is_transmitting);
75}
76
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000077ViENetworkImpl::ViENetworkImpl(ViESharedData* shared_data)
78 : shared_data_(shared_data) {
79 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000080 "ViENetworkImpl::ViENetworkImpl() Ctor");
niklase@google.com470e71d2011-07-07 08:21:25 +000081}
82
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000083ViENetworkImpl::~ViENetworkImpl() {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000084 WEBRTC_TRACE(kTraceMemory, kTraceVideo, shared_data_->instance_id(),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000085 "ViENetworkImpl::~ViENetworkImpl() Dtor");
niklase@google.com470e71d2011-07-07 08:21:25 +000086}
87
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000088int ViENetworkImpl::RegisterSendTransport(const int video_channel,
89 Transport& transport) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000090 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
91 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000092 "%s(channel: %d)", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000093 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000094 ViEChannel* vie_channel = cs.Channel(video_channel);
95 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000096 WEBRTC_TRACE(kTraceError, kTraceVideo,
97 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +000098 "%s Channel doesn't exist", __FUNCTION__);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +000099 shared_data_->SetLastError(kViENetworkInvalidChannelId);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000100 return -1;
101 }
102 if (vie_channel->Sending()) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000103 WEBRTC_TRACE(kTraceError, kTraceVideo,
104 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000105 "%s Channel already sending.", __FUNCTION__);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000106 shared_data_->SetLastError(kViENetworkAlreadySending);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000107 return -1;
108 }
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000109 if (vie_channel->RegisterSendTransport(&transport) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000110 shared_data_->SetLastError(kViENetworkUnknownError);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000111 return -1;
112 }
113 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000114}
115
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000116int ViENetworkImpl::DeregisterSendTransport(const int video_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000117 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
118 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000119 "%s(channel: %d)", __FUNCTION__, video_channel);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000120 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000121 ViEChannel* vie_channel = cs.Channel(video_channel);
122 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000123 WEBRTC_TRACE(kTraceError, kTraceVideo,
124 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000125 "%s Channel doesn't exist", __FUNCTION__);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000126 shared_data_->SetLastError(kViENetworkInvalidChannelId);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000127 return -1;
128 }
129 if (vie_channel->Sending()) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000130 WEBRTC_TRACE(kTraceError, kTraceVideo,
131 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000132 "%s Channel already sending", __FUNCTION__);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000133 shared_data_->SetLastError(kViENetworkAlreadySending);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000134 return -1;
135 }
136 if (vie_channel->DeregisterSendTransport() != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000137 shared_data_->SetLastError(kViENetworkUnknownError);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000138 return -1;
139 }
140 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000141}
142
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000143int ViENetworkImpl::ReceivedRTPPacket(const int video_channel, const void* data,
wu@webrtc.orga9890802013-12-13 00:21:03 +0000144 const int length,
145 const PacketTime& packet_time) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000146 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
147 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000148 "%s(channel: %d, data: -, length: %d)", __FUNCTION__,
149 video_channel, length);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000150 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000151 ViEChannel* vie_channel = cs.Channel(video_channel);
152 if (!vie_channel) {
153 // The channel doesn't exists
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000154 WEBRTC_TRACE(kTraceError, kTraceVideo,
155 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000156 "Channel doesn't exist");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000157 shared_data_->SetLastError(kViENetworkInvalidChannelId);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000158 return -1;
159 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000160 return vie_channel->ReceivedRTPPacket(data, length, packet_time);
niklase@google.com470e71d2011-07-07 08:21:25 +0000161}
162
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000163int ViENetworkImpl::ReceivedRTCPPacket(const int video_channel,
164 const void* data, const int length) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000165 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
166 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000167 "%s(channel: %d, data: -, length: %d)", __FUNCTION__,
168 video_channel, length);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000169 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000170 ViEChannel* vie_channel = cs.Channel(video_channel);
171 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000172 WEBRTC_TRACE(kTraceError, kTraceVideo,
173 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000174 "Channel doesn't exist");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000175 shared_data_->SetLastError(kViENetworkInvalidChannelId);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000176 return -1;
177 }
178 return vie_channel->ReceivedRTCPPacket(data, length);
niklase@google.com470e71d2011-07-07 08:21:25 +0000179}
180
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000181int ViENetworkImpl::SetMTU(int video_channel, unsigned int mtu) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000182 WEBRTC_TRACE(kTraceApiCall, kTraceVideo,
183 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000184 "%s(channel: %d, mtu: %u)", __FUNCTION__, video_channel, mtu);
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000185 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000186 ViEChannel* vie_channel = cs.Channel(video_channel);
187 if (!vie_channel) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000188 WEBRTC_TRACE(kTraceError, kTraceVideo,
189 ViEId(shared_data_->instance_id(), video_channel),
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000190 "Channel doesn't exist");
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000191 shared_data_->SetLastError(kViENetworkInvalidChannelId);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000192 return -1;
193 }
194 if (vie_channel->SetMTU(mtu) != 0) {
mflodman@webrtc.orgb11424b2012-01-25 13:42:03 +0000195 shared_data_->SetLastError(kViENetworkUnknownError);
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000196 return -1;
197 }
198 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199}
mflodman@webrtc.org1bdf1df2011-12-20 11:57:47 +0000200} // namespace webrtc