blob: 4a9d86658563300015af16a1dcccf0d2dd6ebceb [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
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.org065b64d2013-04-02 20:37:14 +000011#include "webrtc/video_engine/vie_network_impl.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
pwestin@webrtc.org912b7f72013-03-13 23:20:57 +000013#include <stdio.h>
14#if (defined(WIN32_) || defined(WIN64_))
15#include <qos.h>
16#endif
17
pwestin@webrtc.org065b64d2013-04-02 20:37:14 +000018#include "webrtc/engine_configurations.h"
pbos@webrtc.org3468f202014-05-14 08:02:22 +000019#include "webrtc/system_wrappers/interface/logging.h"
pwestin@webrtc.org065b64d2013-04-02 20:37:14 +000020#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.orgb015cbe2012-10-22 18:19:23 +000027
28namespace webrtc {
29
30ViENetwork* ViENetwork::GetInterface(VideoEngine* video_engine) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000031 if (!video_engine) {
32 return NULL;
33 }
andrew@webrtc.org7ab72682013-05-09 02:12:07 +000034 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000035 ViENetworkImpl* vie_networkImpl = vie_impl;
36 // Increase ref count.
37 (*vie_networkImpl)++;
38 return vie_networkImpl;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000039}
40
41int ViENetworkImpl::Release() {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000042 // Decrease ref count.
43 (*this)--;
44
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000045 int32_t ref_count = GetCount();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000046 if (ref_count < 0) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +000047 LOG(LS_ERROR) << "ViENetwork release too many times";
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000048 shared_data_->SetLastError(kViEAPIDoesNotExist);
49 return -1;
50 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000051 return ref_count;
52}
53
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000054void ViENetworkImpl::SetNetworkTransmissionState(const int video_channel,
55 const bool is_transmitting) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +000056 LOG_F(LS_INFO) << "channel: " << video_channel
57 << " transmitting: " << (is_transmitting ? "yes" : "no");
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000058 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
59 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
60 if (!vie_encoder) {
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000061 shared_data_->SetLastError(kViENetworkInvalidChannelId);
62 return;
63 }
64 vie_encoder->SetNetworkTransmissionState(is_transmitting);
65}
66
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000067ViENetworkImpl::ViENetworkImpl(ViESharedData* shared_data)
pbos@webrtc.org3468f202014-05-14 08:02:22 +000068 : shared_data_(shared_data) {}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000069
pbos@webrtc.org3468f202014-05-14 08:02:22 +000070ViENetworkImpl::~ViENetworkImpl() {}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000071
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000072int ViENetworkImpl::RegisterSendTransport(const int video_channel,
73 Transport& transport) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +000074 LOG_F(LS_INFO) << "channel: " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000075 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
76 ViEChannel* vie_channel = cs.Channel(video_channel);
77 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000078 shared_data_->SetLastError(kViENetworkInvalidChannelId);
79 return -1;
80 }
81 if (vie_channel->Sending()) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +000082 LOG_F(LS_ERROR) << "Already sending on channel: " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000083 shared_data_->SetLastError(kViENetworkAlreadySending);
84 return -1;
85 }
86 if (vie_channel->RegisterSendTransport(&transport) != 0) {
87 shared_data_->SetLastError(kViENetworkUnknownError);
88 return -1;
89 }
90 return 0;
91}
92
93int ViENetworkImpl::DeregisterSendTransport(const int video_channel) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +000094 LOG_F(LS_INFO) << "channel: " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000095 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
96 ViEChannel* vie_channel = cs.Channel(video_channel);
97 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000098 shared_data_->SetLastError(kViENetworkInvalidChannelId);
99 return -1;
100 }
101 if (vie_channel->Sending()) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +0000102 LOG_F(LS_ERROR) << "Actively sending on channel: " << video_channel;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000103 shared_data_->SetLastError(kViENetworkAlreadySending);
104 return -1;
105 }
106 if (vie_channel->DeregisterSendTransport() != 0) {
107 shared_data_->SetLastError(kViENetworkUnknownError);
108 return -1;
109 }
110 return 0;
111}
112
113int ViENetworkImpl::ReceivedRTPPacket(const int video_channel, const void* data,
wu@webrtc.orgefeb8ce2013-12-13 00:21:03 +0000114 const int length,
115 const PacketTime& packet_time) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000116 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
117 ViEChannel* vie_channel = cs.Channel(video_channel);
118 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000119 shared_data_->SetLastError(kViENetworkInvalidChannelId);
120 return -1;
121 }
wu@webrtc.orgefeb8ce2013-12-13 00:21:03 +0000122 return vie_channel->ReceivedRTPPacket(data, length, packet_time);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000123}
124
125int ViENetworkImpl::ReceivedRTCPPacket(const int video_channel,
126 const void* data, const int length) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000127 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
128 ViEChannel* vie_channel = cs.Channel(video_channel);
129 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000130 shared_data_->SetLastError(kViENetworkInvalidChannelId);
131 return -1;
132 }
133 return vie_channel->ReceivedRTCPPacket(data, length);
134}
135
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000136int ViENetworkImpl::SetMTU(int video_channel, unsigned int mtu) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +0000137 LOG_F(LS_INFO) << "channel: " << video_channel << " mtu: " << mtu;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000138 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
139 ViEChannel* vie_channel = cs.Channel(video_channel);
140 if (!vie_channel) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000141 shared_data_->SetLastError(kViENetworkInvalidChannelId);
142 return -1;
143 }
144 if (vie_channel->SetMTU(mtu) != 0) {
145 shared_data_->SetLastError(kViENetworkUnknownError);
146 return -1;
147 }
148 return 0;
149}
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +0000150
151int ViENetworkImpl::ReceivedBWEPacket(const int video_channel,
152 int64_t arrival_time_ms, int payload_size, const RTPHeader& header) {
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +0000153 ViEChannelManagerScoped cs(*(shared_data_->channel_manager()));
154 ViEChannel* vie_channel = cs.Channel(video_channel);
155 if (!vie_channel) {
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +0000156 shared_data_->SetLastError(kViENetworkInvalidChannelId);
157 return -1;
158 }
159
solenberg@webrtc.org2d3624c2014-03-24 20:28:11 +0000160 vie_channel->ReceivedBWEPacket(arrival_time_ms, payload_size, header);
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +0000161 return 0;
162}
stefan@webrtc.org5d8c9542014-03-25 10:37:31 +0000163
164bool ViENetworkImpl::SetBandwidthEstimationConfig(
165 int video_channel, const webrtc::Config& config) {
pbos@webrtc.org3468f202014-05-14 08:02:22 +0000166 LOG_F(LS_INFO) << "channel: " << video_channel;
stefan@webrtc.org5d8c9542014-03-25 10:37:31 +0000167 return shared_data_->channel_manager()->SetBandwidthEstimationConfig(
168 video_channel, config);
169}
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000170} // namespace webrtc