2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #include "webrtc/video_engine/vie_base_impl.h"
17 #include "webrtc/engine_configurations.h"
18 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp.h"
19 #include "webrtc/modules/video_coding/main/interface/video_coding.h"
20 #include "webrtc/modules/video_processing/main/interface/video_processing.h"
21 #include "webrtc/modules/video_render/include/video_render.h"
22 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
23 #include "webrtc/system_wrappers/interface/logging.h"
24 #include "webrtc/video_engine/include/vie_errors.h"
25 #include "webrtc/video_engine/vie_capturer.h"
26 #include "webrtc/video_engine/vie_channel.h"
27 #include "webrtc/video_engine/vie_channel_manager.h"
28 #include "webrtc/video_engine/vie_defines.h"
29 #include "webrtc/video_engine/vie_encoder.h"
30 #include "webrtc/video_engine/vie_impl.h"
31 #include "webrtc/video_engine/vie_input_manager.h"
32 #include "webrtc/video_engine/vie_shared_data.h"
36 ViEBase* ViEBase::GetInterface(VideoEngine* video_engine) {
40 VideoEngineImpl* vie_impl = static_cast<VideoEngineImpl*>(video_engine);
41 ViEBaseImpl* vie_base_impl = vie_impl;
42 (*vie_base_impl)++; // Increase ref count.
47 int ViEBaseImpl::Release() {
48 (*this)--; // Decrease ref count.
50 int32_t ref_count = GetCount();
52 LOG(LS_WARNING) << "ViEBase released too many times.";
58 ViEBaseImpl::ViEBaseImpl(const Config& config)
59 : shared_data_(config) {}
61 ViEBaseImpl::~ViEBaseImpl() {}
63 int ViEBaseImpl::Init() {
67 int ViEBaseImpl::SetVoiceEngine(VoiceEngine* voice_engine) {
68 LOG_F(LS_INFO) << "SetVoiceEngine";
69 if (shared_data_.channel_manager()->SetVoiceEngine(voice_engine) != 0) {
70 shared_data_.SetLastError(kViEBaseVoEFailure);
76 int ViEBaseImpl::RegisterCpuOveruseObserver(int video_channel,
77 CpuOveruseObserver* observer) {
78 LOG_F(LS_INFO) << "RegisterCpuOveruseObserver on channel " << video_channel;
79 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
80 ViEChannel* vie_channel = cs.Channel(video_channel);
82 shared_data_.SetLastError(kViEBaseInvalidChannelId);
85 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
88 ViEInputManagerScoped is(*(shared_data_.input_manager()));
89 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
91 ViECapturer* capturer = is.Capture(provider->Id());
93 capturer->RegisterCpuOveruseObserver(observer);
96 shared_data_.overuse_observers()->insert(
97 std::pair<int, CpuOveruseObserver*>(video_channel, observer));
101 int ViEBaseImpl::SetCpuOveruseOptions(int video_channel,
102 const CpuOveruseOptions& options) {
103 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
104 ViEChannel* vie_channel = cs.Channel(video_channel);
106 shared_data_.SetLastError(kViEBaseInvalidChannelId);
109 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
112 ViEInputManagerScoped is(*(shared_data_.input_manager()));
113 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
115 ViECapturer* capturer = is.Capture(provider->Id());
117 capturer->SetCpuOveruseOptions(options);
124 int ViEBaseImpl::CpuOveruseMeasures(int video_channel,
125 int* capture_jitter_ms,
126 int* avg_encode_time_ms,
127 int* encode_usage_percent,
128 int* capture_queue_delay_ms_per_s) {
129 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
130 ViEChannel* vie_channel = cs.Channel(video_channel);
132 shared_data_.SetLastError(kViEBaseInvalidChannelId);
135 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
138 ViEInputManagerScoped is(*(shared_data_.input_manager()));
139 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
141 ViECapturer* capturer = is.Capture(provider->Id());
143 capturer->CpuOveruseMeasures(capture_jitter_ms,
145 encode_usage_percent,
146 capture_queue_delay_ms_per_s);
153 int ViEBaseImpl::CreateChannel(int& video_channel) { // NOLINT
154 return CreateChannel(video_channel, static_cast<const Config*>(NULL));
157 int ViEBaseImpl::CreateChannel(int& video_channel, // NOLINT
158 const Config* config) {
159 if (shared_data_.channel_manager()->CreateChannel(&video_channel,
162 shared_data_.SetLastError(kViEBaseChannelCreationFailed);
165 LOG(LS_INFO) << "Video channel created: " << video_channel;
169 int ViEBaseImpl::CreateChannel(int& video_channel, // NOLINT
170 int original_channel) {
171 return CreateChannel(video_channel, original_channel, true);
174 int ViEBaseImpl::CreateReceiveChannel(int& video_channel, // NOLINT
175 int original_channel) {
176 return CreateChannel(video_channel, original_channel, false);
179 int ViEBaseImpl::DeleteChannel(const int video_channel) {
181 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
182 ViEChannel* vie_channel = cs.Channel(video_channel);
184 shared_data_.SetLastError(kViEBaseInvalidChannelId);
188 // Deregister the ViEEncoder if no other channel is using it.
189 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
190 if (cs.ChannelUsingViEEncoder(video_channel) == false) {
191 ViEInputManagerScoped is(*(shared_data_.input_manager()));
192 ViEFrameProviderBase* provider = is.FrameProvider(vie_encoder);
194 provider->DeregisterFrameCallback(vie_encoder);
199 if (shared_data_.channel_manager()->DeleteChannel(video_channel) == -1) {
200 shared_data_.SetLastError(kViEBaseUnknownError);
203 LOG(LS_INFO) << "Channel deleted " << video_channel;
207 int ViEBaseImpl::ConnectAudioChannel(const int video_channel,
208 const int audio_channel) {
209 LOG_F(LS_INFO) << "ConnectAudioChannel, video channel " << video_channel
210 << ", audio channel " << audio_channel;
211 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
212 if (!cs.Channel(video_channel)) {
213 shared_data_.SetLastError(kViEBaseInvalidChannelId);
217 if (shared_data_.channel_manager()->ConnectVoiceChannel(video_channel,
218 audio_channel) != 0) {
219 shared_data_.SetLastError(kViEBaseVoEFailure);
225 int ViEBaseImpl::DisconnectAudioChannel(const int video_channel) {
226 LOG_F(LS_INFO) << "DisconnectAudioChannel " << video_channel;
227 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
228 if (!cs.Channel(video_channel)) {
229 shared_data_.SetLastError(kViEBaseInvalidChannelId);
233 if (shared_data_.channel_manager()->DisconnectVoiceChannel(
234 video_channel) != 0) {
235 shared_data_.SetLastError(kViEBaseVoEFailure);
241 int ViEBaseImpl::StartSend(const int video_channel) {
242 LOG_F(LS_INFO) << "StartSend: " << video_channel;
243 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
244 ViEChannel* vie_channel = cs.Channel(video_channel);
246 shared_data_.SetLastError(kViEBaseInvalidChannelId);
250 ViEEncoder* vie_encoder = cs.Encoder(video_channel);
251 assert(vie_encoder != NULL);
252 if (vie_encoder->Owner() != video_channel) {
253 LOG_F(LS_ERROR) << "Can't start send on a receive only channel.";
254 shared_data_.SetLastError(kViEBaseReceiveOnlyChannel);
258 // Pause and trigger a key frame.
259 vie_encoder->Pause();
260 int32_t error = vie_channel->StartSend();
262 vie_encoder->Restart();
263 if (error == kViEBaseAlreadySending) {
264 shared_data_.SetLastError(kViEBaseAlreadySending);
266 LOG_F(LS_ERROR) << "Could not start sending " << video_channel;
267 shared_data_.SetLastError(kViEBaseUnknownError);
270 vie_encoder->SendKeyFrame();
271 vie_encoder->Restart();
275 int ViEBaseImpl::StopSend(const int video_channel) {
276 LOG_F(LS_INFO) << "StopSend " << video_channel;
278 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
279 ViEChannel* vie_channel = cs.Channel(video_channel);
281 shared_data_.SetLastError(kViEBaseInvalidChannelId);
285 int32_t error = vie_channel->StopSend();
287 if (error == kViEBaseNotSending) {
288 shared_data_.SetLastError(kViEBaseNotSending);
290 LOG_F(LS_ERROR) << "Could not stop sending " << video_channel;
291 shared_data_.SetLastError(kViEBaseUnknownError);
298 int ViEBaseImpl::StartReceive(const int video_channel) {
299 LOG_F(LS_INFO) << "StartReceive " << video_channel;
301 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
302 ViEChannel* vie_channel = cs.Channel(video_channel);
304 shared_data_.SetLastError(kViEBaseInvalidChannelId);
307 if (vie_channel->StartReceive() != 0) {
308 shared_data_.SetLastError(kViEBaseUnknownError);
314 int ViEBaseImpl::StopReceive(const int video_channel) {
315 LOG_F(LS_INFO) << "StopReceive " << video_channel;
316 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
317 ViEChannel* vie_channel = cs.Channel(video_channel);
319 shared_data_.SetLastError(kViEBaseInvalidChannelId);
322 if (vie_channel->StopReceive() != 0) {
323 shared_data_.SetLastError(kViEBaseUnknownError);
329 int ViEBaseImpl::GetVersion(char version[1024]) {
330 assert(kViEVersionMaxMessageSize == 1024);
332 shared_data_.SetLastError(kViEBaseInvalidArgument);
336 // Add WebRTC Version.
337 std::stringstream version_stream;
338 version_stream << "VideoEngine 3.53.0" << std::endl;
341 version_stream << "Build: " << BUILDINFO << std::endl;
343 int version_length = version_stream.tellp();
344 assert(version_length < 1024);
345 memcpy(version, version_stream.str().c_str(), version_length);
346 version[version_length] = '\0';
350 int ViEBaseImpl::LastError() {
351 return shared_data_.LastErrorInternal();
354 int ViEBaseImpl::CreateChannel(int& video_channel, // NOLINT
355 int original_channel, bool sender) {
356 ViEChannelManagerScoped cs(*(shared_data_.channel_manager()));
357 if (!cs.Channel(original_channel)) {
358 shared_data_.SetLastError(kViEBaseInvalidChannelId);
362 if (shared_data_.channel_manager()->CreateChannel(&video_channel,
366 shared_data_.SetLastError(kViEBaseChannelCreationFailed);
369 LOG_F(LS_INFO) << "VideoChannel created: " << video_channel
370 << ", base channel " << original_channel
371 << ", is send channel : " << sender;
375 } // namespace webrtc